From nobody Thu May 2 03:29:29 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232716; cv=none; d=zohomail.com; s=zohoarc; b=HdrTGCeFzyEi++uH6Nkb7O0or5/qMWMUFa8TVEqUQ7Gp8iruki8Zcca2XgDj6llXsaaIydhz21wuXwkJaZgVM/x9UeGMboJbHD/MoJjT5oIczXuXxirvTpo8K00dbINu54mMnyX9CQRFTC4FrVdTA29Jlik60O2ipGa375Ch7XY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232716; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=qAej7fTjYsPwR3P1N23YZ01ampgh/4+BoNoo7EonQIQ=; b=TNyEcFxuz4tzYBmAjFpNRsDLs2RcTgxE9Wwz9QgyXt0glb3o9D0O5tiBNZd1miHD9cPP7pdyo5KOv5uZLG1akNT4aggNHSl/1vdWrSxzeYgm9Go4MH4NCvBkvNuBMP3u2xQFSYoctGB9PWcN+4CEzDP/8qO7LrB0OKLiY5bqCUM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232716565113.9208661823302; Thu, 8 Jun 2023 06:58:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8w-0004DH-UK; Thu, 08 Jun 2023 09:57:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8c-0003yO-Nj for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004go-Pj for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:15 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-446-kUVXhLK5NFGeTg5cmhyO4g-1; Thu, 08 Jun 2023 09:56:57 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 40DD685A5BA; Thu, 8 Jun 2023 13:56:57 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E6A1492B00; Thu, 8 Jun 2023 13:56:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232628; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qAej7fTjYsPwR3P1N23YZ01ampgh/4+BoNoo7EonQIQ=; b=gR1hU0V6cr8O0/0XgjJt/lH/+Ll6FfdSUQo/NEipnTOYMX12iaM71h9cbnvRH0vcTg0o4U lpqAF4fqEJNHDSgVI7UJjWjKg++FM9WP8QaxzLO9mfFPVXT/zWqMtqrI4QDl1JS1SR2SEd CrOEhX44A2h9RuNtURZ8VUqagb7a4zs= X-MC-Unique: kUVXhLK5NFGeTg5cmhyO4g-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, "Dr . David Alan Gilbert" Subject: [PATCH v4 01/24] nbd/client: Use smarter assert Date: Thu, 8 Jun 2023 08:56:30 -0500 Message-Id: <20230608135653.2918540-2-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232718397100003 Content-Type: text/plain; charset="utf-8" Assigning strlen() to a uint32_t and then asserting that it isn't too large doesn't catch the case of an input string 4G in length. Thankfully, the incoming strings can never be that large: if the export name or query is reflecting a string the client got from the server, we already guarantee that we dropped the NBD connection if the server sent more than 32M in a single reply to our NBD_OPT_* request; if the export name is coming from qemu, nbd_receive_negotiate() asserted that strlen(info->name) <=3D NBD_MAX_STRING_SIZE; and similarly, a query string via x->dirty_bitmap coming from the user was bounds-checked in either qemu-nbd or by the limitations of QMP. Still, it doesn't hurt to be more explicit in how we write our assertions to not have to analyze whether inadvertent wraparound is possible. Fixes: 93676c88 ("nbd: Don't send oversize strings", v4.2.0) Reported-by: Dr. David Alan Gilbert Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- nbd/client.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 30d5383cb19..ff75722e487 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -650,19 +650,20 @@ static int nbd_send_meta_query(QIOChannel *ioc, uint3= 2_t opt, Error **errp) { int ret; - uint32_t export_len =3D strlen(export); + uint32_t export_len; uint32_t queries =3D !!query; uint32_t query_len =3D 0; uint32_t data_len; char *data; char *p; + assert(strnlen(export, NBD_MAX_STRING_SIZE + 1) <=3D NBD_MAX_STRING_SI= ZE); + export_len =3D strlen(export); data_len =3D sizeof(export_len) + export_len + sizeof(queries); - assert(export_len <=3D NBD_MAX_STRING_SIZE); if (query) { + assert(strnlen(query, NBD_MAX_STRING_SIZE + 1) <=3D NBD_MAX_STRING= _SIZE); query_len =3D strlen(query); data_len +=3D sizeof(query_len) + query_len; - assert(query_len <=3D NBD_MAX_STRING_SIZE); } else { assert(opt =3D=3D NBD_OPT_LIST_META_CONTEXT); } --=20 2.40.1 From nobody Thu May 2 03:29:29 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232800; cv=none; d=zohomail.com; s=zohoarc; b=FQCRRLmBcGdrOu1cQjcCN4YmxcbKozgeNaL0pwVWsLjZwKO//p2ne9r9xyUZKamCMJU2NXc7PockY/Jg47cOHC8acQTzxvH2/O0L/y+XKgvoERkFsDYYWIrg49fmRVz4MwVroC3ziRksz03pR6ifNiIF+N+xsTZ2W/ubfEkX+l8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232800; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Bj4sFgcf1CT0aTygZ2dK8R2P2P3+I8UOLBiEDFaHrc4=; b=TYwwq9SoRUu/oFMDEyDgA9/IaHvUqGx7sa6wr+3FpGQt1tWkrmoFOXGFAkPuK3ZwTN7i60pfErVvf/oFvhhaAe9Pmn+wn9WnbZSeZtPYcPJ2nVlGBTDuHNE2d1ImRHS9j+uErX7RA5KALS2NOfrAwT8nrjD8FlDIIN3AjtgoBrY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232800628885.3055083359436; Thu, 8 Jun 2023 07:00:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8k-000434-IA; Thu, 08 Jun 2023 09:57:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tJ-8S for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8L-0004e7-2k for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:06 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-NyXT2Xe2P7mYv2V9cHsc3A-1; Thu, 08 Jun 2023 09:56:58 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 123F83823A23; Thu, 8 Jun 2023 13:56:58 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7177F48205E; Thu, 8 Jun 2023 13:56:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232620; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Bj4sFgcf1CT0aTygZ2dK8R2P2P3+I8UOLBiEDFaHrc4=; b=ThvrICpbXbfGgL08vmGzZ5UrAZTn4NF2Y6sItFO+C4xGTqzFPAgXxo/mxz2qheLmSqj28B RhMU7+NdkdzDFO3mf3lvCDScVL0A1tEWty79eljMdxsgcJkVNipqPvmebgghUhk6IZsMGj LTYN1Rfg1c+x+fBohhj1JKKFK9oJoMQ= X-MC-Unique: NyXT2Xe2P7mYv2V9cHsc3A-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 02/24] nbd: Consistent typedef usage in header Date: Thu, 8 Jun 2023 08:56:31 -0500 Message-Id: <20230608135653.2918540-3-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232802775100001 Content-Type: text/plain; charset="utf-8" We had a mix of struct declarataions followed by typedefs, and direct struct definitions as part of a typedef. Pick a single style. Also float a couple of opaque typedefs earlier in the file, as a later patch wants to refer NBDExport* in NBDRequest. No semantic impact. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch --- include/block/nbd.h | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index a4c98169c39..9c3ceae5ba5 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. + * Copyright Red Hat * Copyright (C) 2005 Anthony Liguori * * Network Block Device @@ -26,24 +26,25 @@ #include "qapi/error.h" #include "qemu/bswap.h" +typedef struct NBDExport NBDExport; +typedef struct NBDClient NBDClient; + extern const BlockExportDriver blk_exp_nbd; /* Handshake phase structs - this struct is passed on the wire */ -struct NBDOption { +typedef struct NBDOption { uint64_t magic; /* NBD_OPTS_MAGIC */ uint32_t option; /* NBD_OPT_* */ uint32_t length; -} QEMU_PACKED; -typedef struct NBDOption NBDOption; +} QEMU_PACKED NBDOption; -struct NBDOptionReply { +typedef 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 NBDOptionReply NBDOptionReply; +} QEMU_PACKED NBDOptionReply; typedef struct NBDOptionReplyMetaContext { NBDOptionReply h; /* h.type =3D NBD_REP_META_CONTEXT, h.length > 4 */ @@ -56,14 +57,13 @@ typedef struct NBDOptionReplyMetaContext { * Note: these are _NOT_ the same as the network representation of an NBD * request and reply! */ -struct NBDRequest { +typedef struct NBDRequest { uint64_t handle; uint64_t from; uint32_t len; uint16_t flags; /* NBD_CMD_FLAG_* */ uint16_t type; /* NBD_CMD_* */ -}; -typedef struct NBDRequest NBDRequest; +} NBDRequest; typedef struct NBDSimpleReply { uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */ @@ -282,7 +282,7 @@ static inline bool nbd_reply_type_is_error(int type) #define NBD_ESHUTDOWN 108 /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */ -struct NBDExportInfo { +typedef struct NBDExportInfo { /* Set by client before nbd_receive_negotiate() */ bool request_sizes; char *x_dirty_bitmap; @@ -310,8 +310,7 @@ struct NBDExportInfo { char *description; int n_contexts; char **contexts; -}; -typedef struct NBDExportInfo NBDExportInfo; +} NBDExportInfo; int nbd_receive_negotiate(AioContext *aio_context, QIOChannel *ioc, QCryptoTLSCreds *tlscreds, @@ -330,9 +329,6 @@ int nbd_client(int fd); int nbd_disconnect(int fd); int nbd_errno_to_system_errno(int err); -typedef struct NBDExport NBDExport; -typedef struct NBDClient NBDClient; - void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk); AioContext *nbd_export_aio_context(NBDExport *exp); --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232779; cv=none; d=zohomail.com; s=zohoarc; b=CF3hOIdU+tRJ9QPH6ErNmkMhECLU8FfE9NYmnWeFdh42CwK7Hy4ZloGzDFslEnAN/A7moYR5Rjf+5D8+B+noTm9bqOYuOhl0DIHy1dEh/wwI+S7HpKIXqKDLmCsOCW9obqT63H3EwQJmwswkUbJq/riMx/aVFC2QLoHzJ/sL8fY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232779; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=exAPpjNFpQdIDU1RlqkHA2fF0HUrXaD17Puo6xs/MGQ=; b=MACfsWrDB1chIea85V7e1FHQU/i1pRt1H/OLXYT76wIcIWBpu+Ci4wtM5JBARH77SgA0ZJoeTX3fe3rT8G+jqT54F0VMXKcTgOob0xFu6LJmJnjvvcTDBKlFQAuBk07kzFKy0ebufpkQP48PDPASacd8klQBE6ydorO5p3tR8JE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232779274389.3334767423528; Thu, 8 Jun 2023 06:59:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8k-00042i-BD; Thu, 08 Jun 2023 09:57:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tL-8S for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8N-0004eS-Hx for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:06 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-93--5Gn8zZDMrKs2oQVtM3oDg-1; Thu, 08 Jun 2023 09:57:00 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EDB16280D59E; Thu, 8 Jun 2023 13:56:58 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 461A3515546; Thu, 8 Jun 2023 13:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=exAPpjNFpQdIDU1RlqkHA2fF0HUrXaD17Puo6xs/MGQ=; b=RmmrxZYsvR0SjknubbsrSD1OW3GclKVG1twkjR8GV5jEUrEdRyzCM+Dcte9Muidu5LBiMq t254Djx8mQ2jgEL2oscnX66/dB6cyMGhSwvuTcSQJfibW+c8E6+RfLoP3ei2wuuFcT0nNx +qwoEN7s2ibEXmGUVGp52gPy92mdB8E= X-MC-Unique: -5Gn8zZDMrKs2oQVtM3oDg-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 03/24] nbd/server: Prepare for alternate-size headers Date: Thu, 8 Jun 2023 08:56:32 -0500 Message-Id: <20230608135653.2918540-4-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232781146100001 Content-Type: text/plain; charset="utf-8" Upstream NBD now documents[1] an extension that supports 64-bit effect lengths in requests. As part of that extension, the size of the reply headers will change in order to permit a 64-bit length in the reply for symmetry[2]. Additionally, where the reply header is currently 16 bytes for simple reply, and 20 bytes for structured reply; with the extension enabled, there will only be one extended reply header, of 32 bytes, with both structured and extended modes sending identical payloads for chunked replies. Since we are already wired up to use iovecs, it is easiest to allow for this change in header size by splitting each structured reply across multiple iovecs, one for the header (which will become wider in a future patch according to client negotiation), and the other(s) for the chunk payload, and removing the header from the payload struct definitions. Rename the affected functions with s/structured/chunk/ to make it obvious that the code will be reused in extended mode. Interestingly, the client side code never utilized the packed types, so only the server code needs to be updated. [1] https://github.com/NetworkBlockDevice/nbd/blob/extension-ext-header/doc= /proto.md as of NBD commit e6f3b94a934 [2] Note that on the surface, this is because some future server might permit a 4G+ NBD_CMD_READ and need to reply with that much data in one transaction. But even though the extended reply length is widened to 64 bits, for now the NBD spec is clear that servers will not reply with more than a maximum payload bounded by the 32-bit NBD_INFO_BLOCK_SIZE field; allowing a client and server to mutually agree to transactions larger than 4G would require yet another extension. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: hoist earlier in series, drop most changes to nbd_co_send_simple_reply, pass niov to set_be_chunk, rename several functions, drop R-b --- include/block/nbd.h | 8 +-- nbd/server.c | 137 ++++++++++++++++++++++++++------------------ nbd/trace-events | 8 +-- 3 files changed, 88 insertions(+), 65 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 9c3ceae5ba5..e563f1774b0 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -96,28 +96,28 @@ typedef union NBDReply { /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */ typedef struct NBDStructuredReadData { - NBDStructuredReplyChunk h; /* h.length >=3D 9 */ + /* header's .length >=3D 9 */ uint64_t offset; /* At least one byte of data payload follows, calculated from h.length= */ } QEMU_PACKED NBDStructuredReadData; /* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */ typedef struct NBDStructuredReadHole { - NBDStructuredReplyChunk h; /* h.length =3D=3D 12 */ + /* header's length =3D=3D 12 */ uint64_t offset; uint32_t length; } QEMU_PACKED NBDStructuredReadHole; /* Header of all NBD_REPLY_TYPE_ERROR* errors */ typedef struct NBDStructuredError { - NBDStructuredReplyChunk h; /* h.length >=3D 6 */ + /* header's length >=3D 6 */ uint32_t error; uint16_t message_length; } QEMU_PACKED NBDStructuredError; /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */ typedef struct NBDStructuredMeta { - NBDStructuredReplyChunk h; /* h.length >=3D 12 (at least one extent) */ + /* header's length >=3D 12 (at least one extent) */ uint32_t context_id; /* extents follows */ } QEMU_PACKED NBDStructuredMeta; diff --git a/nbd/server.c b/nbd/server.c index febe001a399..6698ab46365 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. + * Copyright Red Hat * Copyright (C) 2005 Anthony Liguori * * Network Block Device Server Side @@ -1906,16 +1906,36 @@ static int coroutine_fn nbd_co_send_simple_reply(NB= DClient *client, {.iov_base =3D data, .iov_len =3D len} }; + assert(!len || !nbd_err); trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err= ), len); set_be_simple_reply(&reply, nbd_err, handle); - return nbd_co_send_iov(client, iov, len ? 2 : 1, errp); + return nbd_co_send_iov(client, iov, 2, errp); } -static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t f= lags, - uint16_t type, uint64_t handle, uint32_t l= ength) +/* + * Prepare the header of a reply chunk for network transmission. + * + * On input, @iov is partially initialized: iov[0].iov_base must point + * to an uninitialized NBDReply, while the remaining @niov elements + * (if any) must be ready for transmission. This function then + * populates iov[0] for transmission. + */ +static inline void set_be_chunk(NBDClient *client, struct iovec *iov, + size_t niov, uint16_t flags, uint16_t type, + uint64_t handle) { + /* TODO - handle structured vs. extended replies */ + NBDStructuredReplyChunk *chunk =3D iov->iov_base; + size_t i, length =3D 0; + + for (i =3D 1; i < niov; i++) { + length +=3D iov[i].iov_len; + } + assert(length <=3D NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData)= ); + + iov[0].iov_len =3D sizeof(*chunk); stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); stw_be_p(&chunk->flags, flags); stw_be_p(&chunk->type, type); @@ -1923,67 +1943,71 @@ static inline void set_be_chunk(NBDStructuredReplyC= hunk *chunk, uint16_t flags, stl_be_p(&chunk->length, length); } -static int coroutine_fn nbd_co_send_structured_done(NBDClient *client, - uint64_t handle, - Error **errp) +static int coroutine_fn nbd_co_send_chunk_done(NBDClient *client, + uint64_t handle, + Error **errp) { - NBDStructuredReplyChunk chunk; + NBDReply hdr; struct iovec iov[] =3D { - {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, + {.iov_base =3D &hdr}, }; - trace_nbd_co_send_structured_done(handle); - set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle,= 0); + trace_nbd_co_send_chunk_done(handle); + set_be_chunk(client, iov, 1, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_NONE, handle); return nbd_co_send_iov(client, iov, 1, errp); } -static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, - uint64_t handle, - uint64_t offset, - void *data, - size_t size, - bool final, - Error **errp) +static int coroutine_fn nbd_co_send_chunk_read(NBDClient *client, + uint64_t handle, + uint64_t offset, + void *data, + size_t size, + bool final, + Error **errp) { + NBDReply hdr; NBDStructuredReadData chunk; struct iovec iov[] =3D { + {.iov_base =3D &hdr}, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, {.iov_base =3D data, .iov_len =3D size} }; assert(size); - trace_nbd_co_send_structured_read(handle, offset, data, size); - set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_OFFSET_DATA, handle, - sizeof(chunk) - sizeof(chunk.h) + size); + trace_nbd_co_send_chunk_read(handle, offset, data, size); + set_be_chunk(client, iov, 3, final ? NBD_REPLY_FLAG_DONE : 0, + NBD_REPLY_TYPE_OFFSET_DATA, handle); stq_be_p(&chunk.offset, offset); - return nbd_co_send_iov(client, iov, 2, errp); + return nbd_co_send_iov(client, iov, 3, errp); } -static int coroutine_fn nbd_co_send_structured_error(NBDClient *client, - uint64_t handle, - uint32_t error, - const char *msg, - Error **errp) +static int coroutine_fn nbd_co_send_chunk_error(NBDClient *client, + uint64_t handle, + uint32_t error, + const char *msg, + Error **errp) { + NBDReply hdr; NBDStructuredError chunk; int nbd_err =3D system_errno_to_nbd_errno(error); struct iovec iov[] =3D { + {.iov_base =3D &hdr}, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, {.iov_base =3D (char *)msg, .iov_len =3D msg ? strlen(msg) : 0}, }; assert(nbd_err); - trace_nbd_co_send_structured_error(handle, nbd_err, - nbd_err_lookup(nbd_err), msg ? msg = : ""); - set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, hand= le, - sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len); + trace_nbd_co_send_chunk_error(handle, nbd_err, + nbd_err_lookup(nbd_err), msg ? msg : ""); + set_be_chunk(client, iov, 3, NBD_REPLY_FLAG_DONE, + NBD_REPLY_TYPE_ERROR, handle); stl_be_p(&chunk.error, nbd_err); - stw_be_p(&chunk.message_length, iov[1].iov_len); + stw_be_p(&chunk.message_length, iov[2].iov_len); - return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp); + return nbd_co_send_iov(client, iov, 3, errp); } /* Do a sparse read and send the structured reply to the client. @@ -2013,27 +2037,27 @@ static int coroutine_fn nbd_co_send_sparse_read(NBD= Client *client, char *msg =3D g_strdup_printf("unable to check for holes: %s", strerror(-status)); - ret =3D nbd_co_send_structured_error(client, handle, -status, = msg, - errp); + ret =3D nbd_co_send_chunk_error(client, handle, -status, msg, = errp); g_free(msg); return ret; } assert(pnum && pnum <=3D size - progress); final =3D progress + pnum =3D=3D size; if (status & BDRV_BLOCK_ZERO) { + NBDReply hdr; NBDStructuredReadHole chunk; struct iovec iov[] =3D { + {.iov_base =3D &hdr}, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, }; - trace_nbd_co_send_structured_read_hole(handle, offset + progre= ss, - pnum); - set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_OFFSET_HOLE, - handle, sizeof(chunk) - sizeof(chunk.h)); + trace_nbd_co_send_chunk_read_hole(handle, offset + progress, p= num); + set_be_chunk(client, iov, 2, + final ? NBD_REPLY_FLAG_DONE : 0, + NBD_REPLY_TYPE_OFFSET_HOLE, handle); stq_be_p(&chunk.offset, offset + progress); stl_be_p(&chunk.length, pnum); - ret =3D nbd_co_send_iov(client, iov, 1, errp); + ret =3D nbd_co_send_iov(client, iov, 2, errp); } else { ret =3D blk_co_pread(exp->common.blk, offset + progress, pnum, data + progress, 0); @@ -2041,9 +2065,8 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDCl= ient *client, error_setg_errno(errp, -ret, "reading from file failed"); break; } - ret =3D nbd_co_send_structured_read(client, handle, offset + p= rogress, - data + progress, pnum, final, - errp); + ret =3D nbd_co_send_chunk_read(client, handle, offset + progre= ss, + data + progress, pnum, final, err= p); } if (ret < 0) { @@ -2199,8 +2222,10 @@ static int coroutine_fn nbd_co_send_extents(NBDClient *client, uint64_t handle, NBDExtentArray *ea, bool last, uint32_t context_id, Error **errp) { + NBDReply hdr; NBDStructuredMeta chunk; struct iovec iov[] =3D { + {.iov_base =3D &hdr}, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, {.iov_base =3D ea->extents, .iov_len =3D ea->count * sizeof(ea->ex= tents[0])} }; @@ -2209,12 +2234,11 @@ nbd_co_send_extents(NBDClient *client, uint64_t han= dle, NBDExtentArray *ea, trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_len= gth, last); - set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_BLOCK_STATUS, - handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len); + set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, + NBD_REPLY_TYPE_BLOCK_STATUS, handle); stl_be_p(&chunk.context_id, context_id); - return nbd_co_send_iov(client, iov, 2, errp); + return nbd_co_send_iov(client, iov, 3, errp); } /* Get block status from the exported device and send it to the client */ @@ -2235,8 +2259,8 @@ coroutine_fn nbd_co_send_block_status(NBDClient *clie= nt, uint64_t handle, ret =3D blockalloc_to_extents(blk, offset, length, ea); } if (ret < 0) { - return nbd_co_send_structured_error( - client, handle, -ret, "can't get block status", errp); + return nbd_co_send_chunk_error(client, handle, -ret, + "can't get block status", errp); } return nbd_co_send_extents(client, handle, ea, last, context_id, errp); @@ -2408,8 +2432,7 @@ static coroutine_fn int nbd_send_generic_reply(NBDCli= ent *client, Error **errp) { if (client->structured_reply && ret < 0) { - return nbd_co_send_structured_error(client, handle, -ret, error_ms= g, - errp); + return nbd_co_send_chunk_error(client, handle, -ret, error_msg, er= rp); } else { return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0, NULL, 0, errp); @@ -2451,11 +2474,11 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *= client, NBDRequest *request, if (client->structured_reply) { if (request->len) { - return nbd_co_send_structured_read(client, request->handle, - request->from, data, - request->len, true, errp); + return nbd_co_send_chunk_read(client, request->handle, + request->from, data, + request->len, true, errp); } else { - return nbd_co_send_structured_done(client, request->handle, er= rp); + return nbd_co_send_chunk_done(client, request->handle, errp); } } else { return nbd_co_send_simple_reply(client, request->handle, 0, diff --git a/nbd/trace-events b/nbd/trace-events index b7032ca2778..50ca05a9e22 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -64,11 +64,11 @@ nbd_receive_request(uint32_t magic, uint16_t flags, uin= t16_t type, uint64_t from nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching cl= ients to AIO context %p" nbd_blk_aio_detach(const char *name, void *ctx) "Export %s: Detaching clie= nts from AIO context %p" nbd_co_send_simple_reply(uint64_t handle, uint32_t error, const char *errn= ame, int len) "Send simple reply: handle =3D %" PRIu64 ", error =3D %" PRIu= 32 " (%s), len =3D %d" -nbd_co_send_structured_done(uint64_t handle) "Send structured reply done: = handle =3D %" PRIu64 -nbd_co_send_structured_read(uint64_t handle, uint64_t offset, void *data, = size_t size) "Send structured read data reply: handle =3D %" PRIu64 ", offs= et =3D %" PRIu64 ", data =3D %p, len =3D %zu" -nbd_co_send_structured_read_hole(uint64_t handle, uint64_t offset, size_t = size) "Send structured read hole reply: handle =3D %" PRIu64 ", offset =3D = %" PRIu64 ", len =3D %zu" +nbd_co_send_chunk_done(uint64_t handle) "Send structured reply done: handl= e =3D %" PRIu64 +nbd_co_send_chunk_read(uint64_t handle, uint64_t offset, void *data, size_= t size) "Send structured read data reply: handle =3D %" PRIu64 ", offset = =3D %" PRIu64 ", data =3D %p, len =3D %zu" +nbd_co_send_chunk_read_hole(uint64_t handle, uint64_t offset, size_t size)= "Send structured read hole reply: handle =3D %" PRIu64 ", offset =3D %" PR= Iu64 ", len =3D %zu" nbd_co_send_extents(uint64_t handle, unsigned int extents, uint32_t id, ui= nt64_t length, int last) "Send block status reply: handle =3D %" PRIu64 ", = extents =3D %u, context =3D %d (extents cover %" PRIu64 " bytes, last chunk= =3D %d)" -nbd_co_send_structured_error(uint64_t handle, int err, const char *errname= , const char *msg) "Send structured error reply: handle =3D %" PRIu64 ", er= ror =3D %d (%s), msg =3D '%s'" +nbd_co_send_chunk_error(uint64_t handle, int err, const char *errname, con= st char *msg) "Send structured error reply: handle =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type, const c= har *name) "Decoding type: handle =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" nbd_co_receive_request_payload_received(uint64_t handle, uint32_t len) "Pa= yload received: handle =3D %" PRIu64 ", len =3D %" PRIu32 nbd_co_receive_align_compliance(const char *op, uint64_t from, uint32_t le= n, uint32_t align) "client sent non-compliant unaligned %s request: from=3D= 0x%" PRIx64 ", len=3D0x%" PRIx32 ", align=3D0x%" PRIx32 --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232691; cv=none; d=zohomail.com; s=zohoarc; b=OYO8aUDqo+JIRp7ZC0VpDHchc4OtZkYRDhoc4mnYoC/NPSx8YWGfZRfqVp/P5hV4ybfEfTu+8p9DY3HUhdLmLJVzpS99C8GZPV028v7/jleA2e3f4DRFUjF7LKH0/eGiKSr7/djcLRIN1Y++fuKc4wGduAN/jKd/FfWXbaLrbcA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232691; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0MRSY00ZzZNFI+oEzrqbs+Q5FGrrhrzhwh3Yd/lHwXY=; b=TsrVuAQcrJ/HH2Fq3LLEogd5W3ycxn0lwkzV/xt7NMnogldOVI1HWvjT9k5ZsY/uqThKR5lDTGsKjT/uKgOO51kZbieqQYOPs1ULTrnAJ6Q0g37Om5JftRgybvTmnJUgJ7frB1r2WilgcYcLlNOktvHjGceoAliHPnN7vDGSwfI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232691875998.9542290350271; Thu, 8 Jun 2023 06:58:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8s-00048k-SX; Thu, 08 Jun 2023 09:57:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tM-9J for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8O-0004fC-1i for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:08 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-210-a4sTQtrnP7uL-BWc2ZOm7w-1; Thu, 08 Jun 2023 09:57:00 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9ECF9280D58D; Thu, 8 Jun 2023 13:56:59 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22D78515540; Thu, 8 Jun 2023 13:56:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0MRSY00ZzZNFI+oEzrqbs+Q5FGrrhrzhwh3Yd/lHwXY=; b=BDcoMkL3DnxosPtJK5M7HDClyNshk67ygYPSeLXnJRUAeBOYHvAY/W2X8x6oSsrTbM6YI4 DKvyw8tzYrTBZxahA67Ub60Pg/WkLAGyAjbTXHhA6DNG/OiXrx01AlSSvQ26WAxUB37Jqf c905txhqa71RmisNwc9ORnxHX/U2LGU= X-MC-Unique: a4sTQtrnP7uL-BWc2ZOm7w-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 04/24] nbd/server: Refactor to pass full request around Date: Thu, 8 Jun 2023 08:56:33 -0500 Message-Id: <20230608135653.2918540-5-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232692360100004 Content-Type: text/plain; charset="utf-8" Part of NBD's 64-bit headers extension involves passing the client's requested offset back as part of the reply header (one reason it stated for this change: converting absolute offsets stored in NBD_REPLY_TYPE_OFFSET_DATA to relative offsets within the buffer is easier if the absolute offset of the buffer is also available). This is a refactoring patch to pass the full request around the reply stack, rather than just the handle, so that later patches can then access request->from when extended headers are active. Meanwhile, this patch enables us to now assert that simple replies are only attempted when appropriate, and otherwise has no semantic change. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: reorder earlier in series, add assertion, keep R-b --- nbd/server.c | 114 ++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 6698ab46365..26b27d69202 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1893,7 +1893,7 @@ static inline void set_be_simple_reply(NBDSimpleReply= *reply, uint64_t error, } static int coroutine_fn nbd_co_send_simple_reply(NBDClient *client, - uint64_t handle, + NBDRequest *request, uint32_t error, void *data, size_t len, @@ -1907,9 +1907,10 @@ static int coroutine_fn nbd_co_send_simple_reply(NBD= Client *client, }; assert(!len || !nbd_err); - trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err= ), - len); - set_be_simple_reply(&reply, nbd_err, handle); + assert(!client->structured_reply || request->type !=3D NBD_CMD_READ); + trace_nbd_co_send_simple_reply(request->handle, nbd_err, + nbd_err_lookup(nbd_err), len); + set_be_simple_reply(&reply, nbd_err, request->handle); return nbd_co_send_iov(client, iov, 2, errp); } @@ -1924,7 +1925,7 @@ static int coroutine_fn nbd_co_send_simple_reply(NBDC= lient *client, */ static inline void set_be_chunk(NBDClient *client, struct iovec *iov, size_t niov, uint16_t flags, uint16_t type, - uint64_t handle) + NBDRequest *request) { /* TODO - handle structured vs. extended replies */ NBDStructuredReplyChunk *chunk =3D iov->iov_base; @@ -1939,12 +1940,12 @@ static inline void set_be_chunk(NBDClient *client, = struct iovec *iov, stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); stw_be_p(&chunk->flags, flags); stw_be_p(&chunk->type, type); - stq_be_p(&chunk->handle, handle); + stq_be_p(&chunk->handle, request->handle); stl_be_p(&chunk->length, length); } static int coroutine_fn nbd_co_send_chunk_done(NBDClient *client, - uint64_t handle, + NBDRequest *request, Error **errp) { NBDReply hdr; @@ -1952,15 +1953,15 @@ static int coroutine_fn nbd_co_send_chunk_done(NBDC= lient *client, {.iov_base =3D &hdr}, }; - trace_nbd_co_send_chunk_done(handle); + trace_nbd_co_send_chunk_done(request->handle); set_be_chunk(client, iov, 1, NBD_REPLY_FLAG_DONE, - NBD_REPLY_TYPE_NONE, handle); + NBD_REPLY_TYPE_NONE, request); return nbd_co_send_iov(client, iov, 1, errp); } static int coroutine_fn nbd_co_send_chunk_read(NBDClient *client, - uint64_t handle, + NBDRequest *request, uint64_t offset, void *data, size_t size, @@ -1976,16 +1977,16 @@ static int coroutine_fn nbd_co_send_chunk_read(NBDC= lient *client, }; assert(size); - trace_nbd_co_send_chunk_read(handle, offset, data, size); + trace_nbd_co_send_chunk_read(request->handle, offset, data, size); set_be_chunk(client, iov, 3, final ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_OFFSET_DATA, handle); + NBD_REPLY_TYPE_OFFSET_DATA, request); stq_be_p(&chunk.offset, offset); return nbd_co_send_iov(client, iov, 3, errp); } - +/*ebb*/ static int coroutine_fn nbd_co_send_chunk_error(NBDClient *client, - uint64_t handle, + NBDRequest *request, uint32_t error, const char *msg, Error **errp) @@ -2000,10 +2001,10 @@ static int coroutine_fn nbd_co_send_chunk_error(NBD= Client *client, }; assert(nbd_err); - trace_nbd_co_send_chunk_error(handle, nbd_err, + trace_nbd_co_send_chunk_error(request->handle, nbd_err, nbd_err_lookup(nbd_err), msg ? msg : ""); set_be_chunk(client, iov, 3, NBD_REPLY_FLAG_DONE, - NBD_REPLY_TYPE_ERROR, handle); + NBD_REPLY_TYPE_ERROR, request); stl_be_p(&chunk.error, nbd_err); stw_be_p(&chunk.message_length, iov[2].iov_len); @@ -2015,7 +2016,7 @@ static int coroutine_fn nbd_co_send_chunk_error(NBDCl= ient *client, * reported to the client, at which point this function succeeds. */ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, - uint64_t handle, + NBDRequest *request, uint64_t offset, uint8_t *data, size_t size, @@ -2037,7 +2038,7 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDCl= ient *client, char *msg =3D g_strdup_printf("unable to check for holes: %s", strerror(-status)); - ret =3D nbd_co_send_chunk_error(client, handle, -status, msg, = errp); + ret =3D nbd_co_send_chunk_error(client, request, -status, msg,= errp); g_free(msg); return ret; } @@ -2051,10 +2052,11 @@ static int coroutine_fn nbd_co_send_sparse_read(NBD= Client *client, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, }; - trace_nbd_co_send_chunk_read_hole(handle, offset + progress, p= num); + trace_nbd_co_send_chunk_read_hole(request->handle, + offset + progress, pnum); set_be_chunk(client, iov, 2, final ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_OFFSET_HOLE, handle); + NBD_REPLY_TYPE_OFFSET_HOLE, request); stq_be_p(&chunk.offset, offset + progress); stl_be_p(&chunk.length, pnum); ret =3D nbd_co_send_iov(client, iov, 2, errp); @@ -2065,7 +2067,7 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDCl= ient *client, error_setg_errno(errp, -ret, "reading from file failed"); break; } - ret =3D nbd_co_send_chunk_read(client, handle, offset + progre= ss, + ret =3D nbd_co_send_chunk_read(client, request, offset + progr= ess, data + progress, pnum, final, err= p); } @@ -2219,7 +2221,7 @@ static int coroutine_fn blockalloc_to_extents(BlockBa= ckend *blk, * @last controls whether NBD_REPLY_FLAG_DONE is sent. */ static int coroutine_fn -nbd_co_send_extents(NBDClient *client, uint64_t handle, NBDExtentArray *ea, +nbd_co_send_extents(NBDClient *client, NBDRequest *request, NBDExtentArray= *ea, bool last, uint32_t context_id, Error **errp) { NBDReply hdr; @@ -2232,10 +2234,10 @@ nbd_co_send_extents(NBDClient *client, uint64_t han= dle, NBDExtentArray *ea, nbd_extent_array_convert_to_be(ea); - trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_len= gth, - last); + trace_nbd_co_send_extents(request->handle, ea->count, context_id, + ea->total_length, last); set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_BLOCK_STATUS, handle); + NBD_REPLY_TYPE_BLOCK_STATUS, request); stl_be_p(&chunk.context_id, context_id); return nbd_co_send_iov(client, iov, 3, errp); @@ -2243,7 +2245,7 @@ nbd_co_send_extents(NBDClient *client, uint64_t handl= e, NBDExtentArray *ea, /* Get block status from the exported device and send it to the client */ static int -coroutine_fn nbd_co_send_block_status(NBDClient *client, uint64_t handle, +coroutine_fn nbd_co_send_block_status(NBDClient *client, NBDRequest *reque= st, BlockBackend *blk, uint64_t offset, uint32_t length, bool dont_fragment, bool last, uint32_t context_id, @@ -2259,11 +2261,11 @@ coroutine_fn nbd_co_send_block_status(NBDClient *cl= ient, uint64_t handle, ret =3D blockalloc_to_extents(blk, offset, length, ea); } if (ret < 0) { - return nbd_co_send_chunk_error(client, handle, -ret, + return nbd_co_send_chunk_error(client, request, -ret, "can't get block status", errp); } - return nbd_co_send_extents(client, handle, ea, last, context_id, errp); + return nbd_co_send_extents(client, request, ea, last, context_id, errp= ); } /* Populate @ea from a dirty bitmap. */ @@ -2298,17 +2300,20 @@ static void bitmap_to_extents(BdrvDirtyBitmap *bitm= ap, bdrv_dirty_bitmap_unlock(bitmap); } -static int coroutine_fn nbd_co_send_bitmap(NBDClient *client, uint64_t han= dle, - BdrvDirtyBitmap *bitmap, uint64= _t offset, - uint32_t length, bool dont_frag= ment, bool last, - uint32_t context_id, Error **er= rp) +static int coroutine_fn nbd_co_send_bitmap(NBDClient *client, + NBDRequest *request, + BdrvDirtyBitmap *bitmap, + uint64_t offset, + uint32_t length, bool dont_frag= ment, + bool last, uint32_t context_id, + Error **errp) { unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); bitmap_to_extents(bitmap, offset, length, ea); - return nbd_co_send_extents(client, handle, ea, last, context_id, errp); + return nbd_co_send_extents(client, request, ea, last, context_id, errp= ); } /* nbd_co_receive_request @@ -2426,15 +2431,15 @@ static int coroutine_fn nbd_co_receive_request(NBDR= equestData *req, NBDRequest * * Returns 0 if connection is still live, -errno on failure to talk to cli= ent */ static coroutine_fn int nbd_send_generic_reply(NBDClient *client, - uint64_t handle, + NBDRequest *request, int ret, const char *error_msg, Error **errp) { if (client->structured_reply && ret < 0) { - return nbd_co_send_chunk_error(client, handle, -ret, error_msg, er= rp); + return nbd_co_send_chunk_error(client, request, -ret, error_msg, e= rrp); } else { - return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0, + return nbd_co_send_simple_reply(client, request, ret < 0 ? -ret : = 0, NULL, 0, errp); } } @@ -2454,7 +2459,7 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *cl= ient, NBDRequest *request, if (request->flags & NBD_CMD_FLAG_FUA) { ret =3D blk_co_flush(exp->common.blk); if (ret < 0) { - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "flush failed", errp); } } @@ -2462,26 +2467,25 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *= client, NBDRequest *request, if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) && request->len) { - return nbd_co_send_sparse_read(client, request->handle, request->f= rom, + return nbd_co_send_sparse_read(client, request, request->from, data, request->len, errp); } ret =3D blk_co_pread(exp->common.blk, request->from, request->len, dat= a, 0); if (ret < 0) { - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "reading from file failed", errp); } if (client->structured_reply) { if (request->len) { - return nbd_co_send_chunk_read(client, request->handle, - request->from, data, + return nbd_co_send_chunk_read(client, request, request->from, = data, request->len, true, errp); } else { - return nbd_co_send_chunk_done(client, request->handle, errp); + return nbd_co_send_chunk_done(client, request, errp); } } else { - return nbd_co_send_simple_reply(client, request->handle, 0, + return nbd_co_send_simple_reply(client, request, 0, data, request->len, errp); } } @@ -2504,7 +2508,7 @@ static coroutine_fn int nbd_do_cmd_cache(NBDClient *c= lient, NBDRequest *request, ret =3D blk_co_preadv(exp->common.blk, request->from, request->len, NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH); - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "caching data failed", errp); } @@ -2535,7 +2539,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } ret =3D blk_co_pwrite(exp->common.blk, request->from, request->len= , data, flags); - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "writing to file failed", errp); case NBD_CMD_WRITE_ZEROES: @@ -2551,7 +2555,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } ret =3D blk_co_pwrite_zeroes(exp->common.blk, request->from, reque= st->len, flags); - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "writing to file failed", errp); case NBD_CMD_DISC: @@ -2560,7 +2564,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, case NBD_CMD_FLUSH: ret =3D blk_co_flush(exp->common.blk); - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "flush failed", errp); case NBD_CMD_TRIM: @@ -2568,12 +2572,12 @@ static coroutine_fn int nbd_handle_request(NBDClien= t *client, if (ret >=3D 0 && request->flags & NBD_CMD_FLAG_FUA) { ret =3D blk_co_flush(exp->common.blk); } - return nbd_send_generic_reply(client, request->handle, ret, + return nbd_send_generic_reply(client, request, ret, "discard failed", errp); case NBD_CMD_BLOCK_STATUS: if (!request->len) { - return nbd_send_generic_reply(client, request->handle, -EINVAL, + return nbd_send_generic_reply(client, request, -EINVAL, "need non-zero length", errp); } if (client->export_meta.count) { @@ -2581,7 +2585,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, int contexts_remaining =3D client->export_meta.count; if (client->export_meta.base_allocation) { - ret =3D nbd_co_send_block_status(client, request->handle, + ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, request->len, dont_fragment, @@ -2594,7 +2598,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } if (client->export_meta.allocation_depth) { - ret =3D nbd_co_send_block_status(client, request->handle, + ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, request->len, dont_fragment, @@ -2610,7 +2614,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, if (!client->export_meta.bitmaps[i]) { continue; } - ret =3D nbd_co_send_bitmap(client, request->handle, + ret =3D nbd_co_send_bitmap(client, request, client->exp->export_bitmaps[i], request->from, request->len, dont_fragment, !--contexts_remain= ing, @@ -2624,7 +2628,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, return 0; } else { - return nbd_send_generic_reply(client, request->handle, -EINVAL, + return nbd_send_generic_reply(client, request, -EINVAL, "CMD_BLOCK_STATUS not negotiated= ", errp); } @@ -2632,7 +2636,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, default: msg =3D g_strdup_printf("invalid request type (%" PRIu32 ") receiv= ed", request->type); - ret =3D nbd_send_generic_reply(client, request->handle, -EINVAL, m= sg, + ret =3D nbd_send_generic_reply(client, request, -EINVAL, msg, errp); g_free(msg); return ret; @@ -2695,7 +2699,7 @@ static coroutine_fn void nbd_trip(void *opaque) Error *export_err =3D local_err; local_err =3D NULL; - ret =3D nbd_send_generic_reply(client, request.handle, -EINVAL, + ret =3D nbd_send_generic_reply(client, &request, -EINVAL, error_get_pretty(export_err), &local_= err); error_free(export_err); } else { --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232705; cv=none; d=zohomail.com; s=zohoarc; b=AwSThnf7RUQkEoaFMU/dvNeLs0otrh+LIJSofQ59CcUcjqfoU69eqsDtxBXfi97G9uAfaS0h/Okw0SwaCIPMKtEwoEKvTsuDHuh/6e54/6y6sfF6Qo+wf5RH8Q/PSQfSRSS8BQQh4YX+Qww4kLulwQC1awc7jZYiy44Z3tL7Qj8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232705; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IgsQCbf4Afh0t8ydIDvREGK4BuwUFiJk6zgi5cfEUZA=; b=ddkhIRwFPtCgr7IgHQ6RUx0TBxZkylhlrtDXaWlsu4LHpIM/KzLfBXSvktVeeQPKaQF74OjF8N+BSWV1c25141s+mrHzKaNTePifz/ncAJbUAna0RhJINWgVZUkdUcqS1Wdg9kCKADulC8mjHO1k4UtL0xKkkO5Ou5/CPdIwPlk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232705142752.308201650486; Thu, 8 Jun 2023 06:58:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8o-00046Y-GF; Thu, 08 Jun 2023 09:57:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tS-PH for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8P-0004fV-Up for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-417-HN7QS0-BMyyU_fwBuzLeCA-1; Thu, 08 Jun 2023 09:57:00 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6ACAF803471; Thu, 8 Jun 2023 13:57:00 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id D45D7515543; Thu, 8 Jun 2023 13:56:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232624; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IgsQCbf4Afh0t8ydIDvREGK4BuwUFiJk6zgi5cfEUZA=; b=AEZVtBgzPyJRSO0/VeQ+fm7qZti5+Q32By2Mx771ESPOGm2ueQa7a+Jfbh0EMMEydqBKX+ YV5Zk6KxdKb9XRP9Z6a2FXEG0trTGN5Jtc68k0u4PDvMfOg+ZmKm1Ha7LDiCHyAZEF9oiL ApLVA4S7QQCGyclaYbPcRtsHlMvM3J4= X-MC-Unique: HN7QS0-BMyyU_fwBuzLeCA-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 05/24] nbd: s/handle/cookie/ to match NBD spec Date: Thu, 8 Jun 2023 08:56:34 -0500 Message-Id: <20230608135653.2918540-6-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232706402100005 Content-Type: text/plain; charset="utf-8" Externally, libnbd exposed the 64-bit opaque marker for each client NBD packet as the "cookie", because it was less confusing when contrasted with 'struct nbd_handle *' holding all libnbd state. It also avoids confusion between the nown 'handle' as a way to identify a packet and the verb 'handle' for reacting to things like signals. Upstream NBD changed their spec to favor the name "cookie" based on libnbd's recommendations[1], so we can do likewise. [1] https://github.com/NetworkBlockDevice/nbd/commit/ca4392eb2b Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch --- include/block/nbd.h | 11 +++--- block/nbd.c | 96 +++++++++++++++++++++++---------------------- nbd/client.c | 14 +++---- nbd/server.c | 29 +++++++------- nbd/trace-events | 22 +++++------ 5 files changed, 87 insertions(+), 85 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index e563f1774b0..59db69bafa5 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -58,7 +58,7 @@ typedef struct NBDOptionReplyMetaContext { * request and reply! */ typedef struct NBDRequest { - uint64_t handle; + uint64_t cookie; uint64_t from; uint32_t len; uint16_t flags; /* NBD_CMD_FLAG_* */ @@ -68,7 +68,7 @@ typedef struct NBDRequest { typedef struct NBDSimpleReply { uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */ uint32_t error; - uint64_t handle; + uint64_t cookie; } QEMU_PACKED NBDSimpleReply; /* Header of all structured replies */ @@ -76,7 +76,7 @@ typedef struct NBDStructuredReplyChunk { uint32_t magic; /* NBD_STRUCTURED_REPLY_MAGIC */ uint16_t flags; /* combination of NBD_REPLY_FLAG_* */ uint16_t type; /* NBD_REPLY_TYPE_* */ - uint64_t handle; /* request handle */ + uint64_t cookie; /* request handle */ uint32_t length; /* length of payload */ } QEMU_PACKED NBDStructuredReplyChunk; @@ -84,13 +84,14 @@ typedef union NBDReply { NBDSimpleReply simple; NBDStructuredReplyChunk structured; struct { - /* @magic and @handle fields have the same offset and size both in + /* + * @magic and @cookie fields have the same offset and size both in * simple reply and structured reply chunk, so let them be accessi= ble * without ".simple." or ".structured." specification */ uint32_t magic; uint32_t _skip; - uint64_t handle; + uint64_t cookie; } QEMU_PACKED; } NBDReply; diff --git a/block/nbd.c b/block/nbd.c index 5aef5cb6bd5..be3c46c6fee 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1,8 +1,8 @@ /* - * QEMU Block driver for NBD + * QEMU Block driver for NBD * * Copyright (c) 2019 Virtuozzo International GmbH. - * Copyright (C) 2016 Red Hat, Inc. + * Copyright Red Hat * Copyright (C) 2008 Bull S.A.S. * Author: Laurent Vivier * @@ -50,8 +50,8 @@ #define EN_OPTSTR ":exportname=3D" #define MAX_NBD_REQUESTS 16 -#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) -#define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) +#define COOKIE_TO_INDEX(bs, cookie) ((cookie) ^ (uint64_t)(intptr_t)(bs)) +#define INDEX_TO_COOKIE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) typedef struct { Coroutine *coroutine; @@ -417,25 +417,25 @@ static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_a= ttempt(BDRVNBDState *s) reconnect_delay_timer_del(s); } -static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t hand= le) +static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cook= ie) { int ret; - uint64_t ind =3D HANDLE_TO_INDEX(s, handle), ind2; + uint64_t ind =3D COOKIE_TO_INDEX(s, cookie), ind2; QEMU_LOCK_GUARD(&s->receive_mutex); while (true) { - if (s->reply.handle =3D=3D handle) { + if (s->reply.cookie =3D=3D cookie) { /* We are done */ return 0; } - if (s->reply.handle !=3D 0) { + if (s->reply.cookie !=3D 0) { /* * Some other request is being handled now. It should already = be - * woken by whoever set s->reply.handle (or never wait in this + * woken by whoever set s->reply.cookie (or never wait in this * yield). So, we should not wake it here. */ - ind2 =3D HANDLE_TO_INDEX(s, s->reply.handle); + ind2 =3D COOKIE_TO_INDEX(s, s->reply.cookie); assert(!s->requests[ind2].receiving); s->requests[ind].receiving =3D true; @@ -445,9 +445,9 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t handle) /* * We may be woken for 2 reasons: * 1. From this function, executing in parallel coroutine, whe= n our - * handle is received. + * cookie is received. * 2. From nbd_co_receive_one_chunk(), when previous request is - * finished and s->reply.handle set to 0. + * finished and s->reply.cookie set to 0. * Anyway, it's OK to lock the mutex and go to the next iterat= ion. */ @@ -456,8 +456,8 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t handle) continue; } - /* We are under mutex and handle is 0. We have to do the dirty wor= k. */ - assert(s->reply.handle =3D=3D 0); + /* We are under mutex and cookie is 0. We have to do the dirty wor= k. */ + assert(s->reply.cookie =3D=3D 0); ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, NULL); if (ret <=3D 0) { ret =3D ret ? ret : -EIO; @@ -468,12 +468,12 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDSt= ate *s, uint64_t handle) nbd_channel_error(s, -EINVAL); return -EINVAL; } - ind2 =3D HANDLE_TO_INDEX(s, s->reply.handle); + ind2 =3D COOKIE_TO_INDEX(s, s->reply.cookie); if (ind2 >=3D MAX_NBD_REQUESTS || !s->requests[ind2].coroutine) { nbd_channel_error(s, -EINVAL); return -EINVAL; } - if (s->reply.handle =3D=3D handle) { + if (s->reply.cookie =3D=3D cookie) { /* We are done */ return 0; } @@ -519,7 +519,7 @@ nbd_co_send_request(BlockDriverState *bs, NBDRequest *r= equest, qemu_mutex_unlock(&s->requests_lock); qemu_co_mutex_lock(&s->send_mutex); - request->handle =3D INDEX_TO_HANDLE(s, i); + request->cookie =3D INDEX_TO_COOKIE(s, i); assert(s->ioc); @@ -828,11 +828,11 @@ static coroutine_fn int nbd_co_receive_structured_pay= load( * corresponding to the server's error reply), and errp is unchanged. */ static coroutine_fn int nbd_co_do_receive_one_chunk( - BDRVNBDState *s, uint64_t handle, bool only_structured, + BDRVNBDState *s, uint64_t cookie, bool only_structured, int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) { int ret; - int i =3D HANDLE_TO_INDEX(s, handle); + int i =3D COOKIE_TO_INDEX(s, cookie); void *local_payload =3D NULL; NBDStructuredReplyChunk *chunk; @@ -841,14 +841,14 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( } *request_ret =3D 0; - ret =3D nbd_receive_replies(s, handle); + ret =3D nbd_receive_replies(s, cookie); if (ret < 0) { error_setg(errp, "Connection closed"); return -EIO; } assert(s->ioc); - assert(s->reply.handle =3D=3D handle); + assert(s->reply.cookie =3D=3D cookie); if (nbd_reply_is_simple(&s->reply)) { if (only_structured) { @@ -918,11 +918,11 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( * Return value is a fatal error code or normal nbd reply error code */ static coroutine_fn int nbd_co_receive_one_chunk( - BDRVNBDState *s, uint64_t handle, bool only_structured, + BDRVNBDState *s, uint64_t cookie, bool only_structured, int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payl= oad, Error **errp) { - int ret =3D nbd_co_do_receive_one_chunk(s, handle, only_structured, + int ret =3D nbd_co_do_receive_one_chunk(s, cookie, only_structured, request_ret, qiov, payload, errp= ); if (ret < 0) { @@ -932,7 +932,7 @@ static coroutine_fn int nbd_co_receive_one_chunk( /* For assert at loop start in nbd_connection_entry */ *reply =3D s->reply; } - s->reply.handle =3D 0; + s->reply.cookie =3D 0; nbd_recv_coroutines_wake(s); @@ -975,10 +975,10 @@ static void nbd_iter_request_error(NBDReplyChunkIter = *iter, int ret) * NBD_FOREACH_REPLY_CHUNK * The pointer stored in @payload requires g_free() to free it. */ -#define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ +#define NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, structured, \ qiov, reply, payload) \ for (iter =3D (NBDReplyChunkIter) { .only_structured =3D structured };= \ - nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, paylo= ad);) + nbd_reply_chunk_iter_receive(s, &iter, cookie, qiov, reply, paylo= ad);) /* * nbd_reply_chunk_iter_receive @@ -986,7 +986,7 @@ static void nbd_iter_request_error(NBDReplyChunkIter *i= ter, int ret) */ static bool coroutine_fn nbd_reply_chunk_iter_receive(BDRVNBDState *s, NBDReplyChunkIter *i= ter, - uint64_t handle, + uint64_t cookie, QEMUIOVector *qiov, NBDReply *reply, void **payload) @@ -1005,7 +1005,7 @@ static bool coroutine_fn nbd_reply_chunk_iter_receive= (BDRVNBDState *s, reply =3D &local_reply; } - ret =3D nbd_co_receive_one_chunk(s, handle, iter->only_structured, + ret =3D nbd_co_receive_one_chunk(s, cookie, iter->only_structured, &request_ret, qiov, reply, payload, &local_err); if (ret < 0) { @@ -1038,7 +1038,7 @@ static bool coroutine_fn nbd_reply_chunk_iter_receive= (BDRVNBDState *s, break_loop: qemu_mutex_lock(&s->requests_lock); - s->requests[HANDLE_TO_INDEX(s, handle)].coroutine =3D NULL; + s->requests[COOKIE_TO_INDEX(s, cookie)].coroutine =3D NULL; s->in_flight--; qemu_co_queue_next(&s->free_sema); qemu_mutex_unlock(&s->requests_lock); @@ -1046,12 +1046,13 @@ break_loop: return false; } -static int coroutine_fn nbd_co_receive_return_code(BDRVNBDState *s, uint64= _t handle, - int *request_ret, Error= **errp) +static int coroutine_fn +nbd_co_receive_return_code(BDRVNBDState *s, uint64_t cookie, + int *request_ret, Error **errp) { NBDReplyChunkIter iter; - NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { + NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, NULL, NULL) { /* nbd_reply_chunk_iter_receive does all the work */ } @@ -1060,16 +1061,17 @@ static int coroutine_fn nbd_co_receive_return_code(= BDRVNBDState *s, uint64_t han return iter.ret; } -static int coroutine_fn nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint= 64_t handle, - uint64_t offset, QEMU= IOVector *qiov, - int *request_ret, Err= or **errp) +static int coroutine_fn +nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t cookie, + uint64_t offset, QEMUIOVector *qiov, + int *request_ret, Error **errp) { NBDReplyChunkIter iter; NBDReply reply; void *payload =3D NULL; Error *local_err =3D NULL; - NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, + NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, s->info.structured_reply, qiov, &reply, &payload) { int ret; @@ -1112,10 +1114,10 @@ static int coroutine_fn nbd_co_receive_cmdread_repl= y(BDRVNBDState *s, uint64_t h return iter.ret; } -static int coroutine_fn nbd_co_receive_blockstatus_reply(BDRVNBDState *s, - uint64_t handle, = uint64_t length, - NBDExtent *extent, - int *request_ret,= Error **errp) +static int coroutine_fn +nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uint64_t cookie, + uint64_t length, NBDExtent *extent, + int *request_ret, Error **errp) { NBDReplyChunkIter iter; NBDReply reply; @@ -1124,7 +1126,7 @@ static int coroutine_fn nbd_co_receive_blockstatus_re= ply(BDRVNBDState *s, bool received =3D false; assert(!extent->length); - NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload= ) { + NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, &reply, &payload= ) { int ret; NBDStructuredReplyChunk *chunk =3D &reply.structured; @@ -1194,11 +1196,11 @@ nbd_co_request(BlockDriverState *bs, NBDRequest *re= quest, continue; } - ret =3D nbd_co_receive_return_code(s, request->handle, + ret =3D nbd_co_receive_return_code(s, request->cookie, &request_ret, &local_err); if (local_err) { trace_nbd_co_request_fail(request->from, request->len, - request->handle, request->flags, + request->cookie, request->flags, request->type, nbd_cmd_lookup(request->type), ret, error_get_pretty(local_err)); @@ -1253,10 +1255,10 @@ nbd_client_co_preadv(BlockDriverState *bs, int64_t = offset, int64_t bytes, continue; } - ret =3D nbd_co_receive_cmdread_reply(s, request.handle, offset, qi= ov, + ret =3D nbd_co_receive_cmdread_reply(s, request.cookie, offset, qi= ov, &request_ret, &local_err); if (local_err) { - trace_nbd_co_request_fail(request.from, request.len, request.h= andle, + trace_nbd_co_request_fail(request.from, request.len, request.c= ookie, request.flags, request.type, nbd_cmd_lookup(request.type), ret, error_get_pretty(local_err)); @@ -1411,11 +1413,11 @@ static int coroutine_fn GRAPH_RDLOCK nbd_client_co_= block_status( continue; } - ret =3D nbd_co_receive_blockstatus_reply(s, request.handle, bytes, + ret =3D nbd_co_receive_blockstatus_reply(s, request.cookie, bytes, &extent, &request_ret, &local_err); if (local_err) { - trace_nbd_co_request_fail(request.from, request.len, request.h= andle, + trace_nbd_co_request_fail(request.from, request.len, request.c= ookie, request.flags, request.type, nbd_cmd_lookup(request.type), ret, error_get_pretty(local_err)); diff --git a/nbd/client.c b/nbd/client.c index ff75722e487..ea3590ca3d0 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2019 Red Hat, Inc. + * Copyright Red Hat * Copyright (C) 2005 Anthony Liguori * * Network Block Device Client Side @@ -1350,14 +1350,14 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *r= equest) { uint8_t buf[NBD_REQUEST_SIZE]; - trace_nbd_send_request(request->from, request->len, request->handle, + trace_nbd_send_request(request->from, request->len, request->cookie, request->flags, request->type, nbd_cmd_lookup(request->type)); stl_be_p(buf, NBD_REQUEST_MAGIC); stw_be_p(buf + 4, request->flags); stw_be_p(buf + 6, request->type); - stq_be_p(buf + 8, request->handle); + stq_be_p(buf + 8, request->cookie); stq_be_p(buf + 16, request->from); stl_be_p(buf + 24, request->len); @@ -1383,7 +1383,7 @@ static int nbd_receive_simple_reply(QIOChannel *ioc, = NBDSimpleReply *reply, } reply->error =3D be32_to_cpu(reply->error); - reply->handle =3D be64_to_cpu(reply->handle); + reply->cookie =3D be64_to_cpu(reply->cookie); return 0; } @@ -1410,7 +1410,7 @@ static int nbd_receive_structured_reply_chunk(QIOChan= nel *ioc, chunk->flags =3D be16_to_cpu(chunk->flags); chunk->type =3D be16_to_cpu(chunk->type); - chunk->handle =3D be64_to_cpu(chunk->handle); + chunk->cookie =3D be64_to_cpu(chunk->cookie); chunk->length =3D be32_to_cpu(chunk->length); return 0; @@ -1487,7 +1487,7 @@ int coroutine_fn nbd_receive_reply(BlockDriverState *= bs, QIOChannel *ioc, } trace_nbd_receive_simple_reply(reply->simple.error, nbd_err_lookup(reply->simple.error), - reply->handle); + reply->cookie); break; case NBD_STRUCTURED_REPLY_MAGIC: ret =3D nbd_receive_structured_reply_chunk(ioc, &reply->structured= , errp); @@ -1497,7 +1497,7 @@ int coroutine_fn nbd_receive_reply(BlockDriverState *= bs, QIOChannel *ioc, type =3D nbd_reply_type_lookup(reply->structured.type); trace_nbd_receive_structured_reply_chunk(reply->structured.flags, reply->structured.type, t= ype, - reply->structured.handle, + reply->structured.cookie, reply->structured.length); break; default: diff --git a/nbd/server.c b/nbd/server.c index 26b27d69202..8486b64b15d 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1428,7 +1428,7 @@ static int coroutine_fn nbd_receive_request(NBDClient= *client, NBDRequest *reque [ 0 .. 3] magic (NBD_REQUEST_MAGIC) [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) [ 6 .. 7] type (NBD_CMD_READ, ...) - [ 8 .. 15] handle + [ 8 .. 15] cookie [16 .. 23] from [24 .. 27] len */ @@ -1436,7 +1436,7 @@ static int coroutine_fn nbd_receive_request(NBDClient= *client, NBDRequest *reque magic =3D ldl_be_p(buf); request->flags =3D lduw_be_p(buf + 4); request->type =3D lduw_be_p(buf + 6); - request->handle =3D ldq_be_p(buf + 8); + request->cookie =3D ldq_be_p(buf + 8); request->from =3D ldq_be_p(buf + 16); request->len =3D ldl_be_p(buf + 24); @@ -1885,11 +1885,11 @@ static int coroutine_fn nbd_co_send_iov(NBDClient *= client, struct iovec *iov, } static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t err= or, - uint64_t handle) + uint64_t cookie) { stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC); stl_be_p(&reply->error, error); - stq_be_p(&reply->handle, handle); + stq_be_p(&reply->cookie, cookie); } static int coroutine_fn nbd_co_send_simple_reply(NBDClient *client, @@ -1908,9 +1908,9 @@ static int coroutine_fn nbd_co_send_simple_reply(NBDC= lient *client, assert(!len || !nbd_err); assert(!client->structured_reply || request->type !=3D NBD_CMD_READ); - trace_nbd_co_send_simple_reply(request->handle, nbd_err, + trace_nbd_co_send_simple_reply(request->cookie, nbd_err, nbd_err_lookup(nbd_err), len); - set_be_simple_reply(&reply, nbd_err, request->handle); + set_be_simple_reply(&reply, nbd_err, request->cookie); return nbd_co_send_iov(client, iov, 2, errp); } @@ -1940,7 +1940,7 @@ static inline void set_be_chunk(NBDClient *client, st= ruct iovec *iov, stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); stw_be_p(&chunk->flags, flags); stw_be_p(&chunk->type, type); - stq_be_p(&chunk->handle, request->handle); + stq_be_p(&chunk->cookie, request->cookie); stl_be_p(&chunk->length, length); } @@ -1953,10 +1953,9 @@ static int coroutine_fn nbd_co_send_chunk_done(NBDCl= ient *client, {.iov_base =3D &hdr}, }; - trace_nbd_co_send_chunk_done(request->handle); + trace_nbd_co_send_chunk_done(request->cookie); set_be_chunk(client, iov, 1, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, request); - return nbd_co_send_iov(client, iov, 1, errp); } @@ -1977,7 +1976,7 @@ static int coroutine_fn nbd_co_send_chunk_read(NBDCli= ent *client, }; assert(size); - trace_nbd_co_send_chunk_read(request->handle, offset, data, size); + trace_nbd_co_send_chunk_read(request->cookie, offset, data, size); set_be_chunk(client, iov, 3, final ? NBD_REPLY_FLAG_DONE : 0, NBD_REPLY_TYPE_OFFSET_DATA, request); stq_be_p(&chunk.offset, offset); @@ -2001,7 +2000,7 @@ static int coroutine_fn nbd_co_send_chunk_error(NBDCl= ient *client, }; assert(nbd_err); - trace_nbd_co_send_chunk_error(request->handle, nbd_err, + trace_nbd_co_send_chunk_error(request->cookie, nbd_err, nbd_err_lookup(nbd_err), msg ? msg : ""); set_be_chunk(client, iov, 3, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, request); @@ -2052,7 +2051,7 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDCl= ient *client, {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, }; - trace_nbd_co_send_chunk_read_hole(request->handle, + trace_nbd_co_send_chunk_read_hole(request->cookie, offset + progress, pnum); set_be_chunk(client, iov, 2, final ? NBD_REPLY_FLAG_DONE : 0, @@ -2234,7 +2233,7 @@ nbd_co_send_extents(NBDClient *client, NBDRequest *re= quest, NBDExtentArray *ea, nbd_extent_array_convert_to_be(ea); - trace_nbd_co_send_extents(request->handle, ea->count, context_id, + trace_nbd_co_send_extents(request->cookie, ea->count, context_id, ea->total_length, last); set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, NBD_REPLY_TYPE_BLOCK_STATUS, request); @@ -2337,7 +2336,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * return ret; } - trace_nbd_co_receive_request_decode_type(request->handle, request->typ= e, + trace_nbd_co_receive_request_decode_type(request->cookie, request->typ= e, nbd_cmd_lookup(request->type)= ); if (request->type !=3D NBD_CMD_WRITE) { @@ -2378,7 +2377,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * } req->complete =3D true; - trace_nbd_co_receive_request_payload_received(request->handle, + trace_nbd_co_receive_request_payload_received(request->cookie, request->len); } diff --git a/nbd/trace-events b/nbd/trace-events index 50ca05a9e22..f19a4d0db39 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -31,9 +31,9 @@ nbd_client_loop(void) "Doing NBD loop" nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" -nbd_send_request(uint64_t from, uint32_t len, uint64_t handle, uint16_t fl= ags, uint16_t type, const char *name) "Sending request to server: { .from = =3D %" PRIu64", .len =3D %" PRIu32 ", .handle =3D %" PRIu64 ", .flags =3D 0= x%" PRIx16 ", .type =3D %" PRIu16 " (%s) }" -nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t hand= le) "Got simple reply: { .error =3D %" PRId32 " (%s), handle =3D %" PRIu64"= }" -nbd_receive_structured_reply_chunk(uint16_t flags, uint16_t type, const ch= ar *name, uint64_t handle, uint32_t length) "Got structured reply chunk: { = flags =3D 0x%" PRIx16 ", type =3D %d (%s), handle =3D %" PRIu64 ", length = =3D %" PRIu32 " }" +nbd_send_request(uint64_t from, uint32_t len, uint64_t cookie, uint16_t fl= ags, uint16_t type, const char *name) "Sending request to server: { .from = =3D %" PRIu64", .len =3D %" PRIu32 ", .cookie =3D %" PRIu64 ", .flags =3D 0= x%" PRIx16 ", .type =3D %" PRIu16 " (%s) }" +nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t cook= ie) "Got simple reply: { .error =3D %" PRId32 " (%s), cookie =3D %" PRIu64"= }" +nbd_receive_structured_reply_chunk(uint16_t flags, uint16_t type, const ch= ar *name, uint64_t cookie, uint32_t length) "Got structured reply chunk: { = flags =3D 0x%" PRIx16 ", type =3D %d (%s), cookie =3D %" PRIu64 ", length = =3D %" PRIu32 " }" # common.c nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" @@ -63,14 +63,14 @@ nbd_negotiate_success(void) "Negotiation succeeded" nbd_receive_request(uint32_t magic, uint16_t flags, uint16_t type, uint64_= t from, uint32_t len) "Got request: { magic =3D 0x%" PRIx32 ", .flags =3D 0= x%" PRIx16 ", .type =3D 0x%" PRIx16 ", from =3D %" PRIu64 ", len =3D %" PRI= u32 " }" nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching cl= ients to AIO context %p" nbd_blk_aio_detach(const char *name, void *ctx) "Export %s: Detaching clie= nts from AIO context %p" -nbd_co_send_simple_reply(uint64_t handle, uint32_t error, const char *errn= ame, int len) "Send simple reply: handle =3D %" PRIu64 ", error =3D %" PRIu= 32 " (%s), len =3D %d" -nbd_co_send_chunk_done(uint64_t handle) "Send structured reply done: handl= e =3D %" PRIu64 -nbd_co_send_chunk_read(uint64_t handle, uint64_t offset, void *data, size_= t size) "Send structured read data reply: handle =3D %" PRIu64 ", offset = =3D %" PRIu64 ", data =3D %p, len =3D %zu" -nbd_co_send_chunk_read_hole(uint64_t handle, uint64_t offset, size_t size)= "Send structured read hole reply: handle =3D %" PRIu64 ", offset =3D %" PR= Iu64 ", len =3D %zu" -nbd_co_send_extents(uint64_t handle, unsigned int extents, uint32_t id, ui= nt64_t length, int last) "Send block status reply: handle =3D %" PRIu64 ", = extents =3D %u, context =3D %d (extents cover %" PRIu64 " bytes, last chunk= =3D %d)" -nbd_co_send_chunk_error(uint64_t handle, int err, const char *errname, con= st char *msg) "Send structured error reply: handle =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" -nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type, const c= har *name) "Decoding type: handle =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" -nbd_co_receive_request_payload_received(uint64_t handle, uint32_t len) "Pa= yload received: handle =3D %" PRIu64 ", len =3D %" PRIu32 +nbd_co_send_simple_reply(uint64_t cookie, uint32_t error, const char *errn= ame, int len) "Send simple reply: cookie =3D %" PRIu64 ", error =3D %" PRIu= 32 " (%s), len =3D %d" +nbd_co_send_chunk_done(uint64_t cookie) "Send structured reply done: cooki= e =3D %" PRIu64 +nbd_co_send_chunk_read(uint64_t cookie, uint64_t offset, void *data, size_= t size) "Send structured read data reply: cookie =3D %" PRIu64 ", offset = =3D %" PRIu64 ", data =3D %p, len =3D %zu" +nbd_co_send_chunk_read_hole(uint64_t cookie, uint64_t offset, size_t size)= "Send structured read hole reply: cookie =3D %" PRIu64 ", offset =3D %" PR= Iu64 ", len =3D %zu" +nbd_co_send_extents(uint64_t cookie, unsigned int extents, uint32_t id, ui= nt64_t length, int last) "Send block status reply: cookie =3D %" PRIu64 ", = extents =3D %u, context =3D %d (extents cover %" PRIu64 " bytes, last chunk= =3D %d)" +nbd_co_send_chunk_error(uint64_t cookie, int err, const char *errname, con= st char *msg) "Send structured error reply: cookie =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" +nbd_co_receive_request_decode_type(uint64_t cookie, uint16_t type, const c= har *name) "Decoding type: cookie =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" +nbd_co_receive_request_payload_received(uint64_t cookie, uint32_t len) "Pa= yload received: cookie =3D %" PRIu64 ", len =3D %" PRIu32 nbd_co_receive_align_compliance(const char *op, uint64_t from, uint32_t le= n, uint32_t align) "client sent non-compliant unaligned %s request: from=3D= 0x%" PRIx64 ", len=3D0x%" PRIx32 ", align=3D0x%" PRIx32 nbd_trip(void) "Reading request" --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232874; cv=none; d=zohomail.com; s=zohoarc; b=Sp8NPAI+ObA6+Pk3HwJFE7aiQTXfl3XDyUJ3KsrItMzWmC6EvLLNgotl9iaeSVspvxua/kTF3pRf7jr6msHwaAJs/eI4ojb1VSJZwm5yIMsPmoOcnDYai1yz8pal6SbiOQGAqrkSftWB8ImWdEKz4MXN2wOoR9nl/4yjEW3L+DA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232874; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=JokQggT6bH+kDhFJOhw/lO+F4z8+gvpOIodpQcQA6KI=; b=gD8BbWdXa9XRzYdWJ4StltBcBLJQ7iRXQ3/jLIhmIx7tqtp98kc9pF5XnPSHoQuz258/Bwc2641lnARfbvIPXyKZhzw4j38gt1ezN6Mjf2SJVxFfoXjUzaGmWEWjttUAKbrQobWVPbzUNzeLf/kL6ZTyOforKihap63Z/5GjyJQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232874519483.1333684376149; Thu, 8 Jun 2023 07:01:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8n-00044v-4g; Thu, 08 Jun 2023 09:57:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tG-44 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8P-0004fi-VJ for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:10 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-489-iRCxf-1yNAWyzMvso5ujiw-1; Thu, 08 Jun 2023 09:57:01 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2C8E080A0C5; Thu, 8 Jun 2023 13:57:01 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 95E4A492B00; Thu, 8 Jun 2023 13:57:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JokQggT6bH+kDhFJOhw/lO+F4z8+gvpOIodpQcQA6KI=; b=Untah1lUElRAuOpVfbx1v4/1um4KEmYJH9Re6GGmVLrzHrkkm7k69lwT+ud4WVNmOAmcnM vwoGkHQapUp3pdnx6P40RWuSqKC7wvvNitoDtwW8Ie9qjPvEsZtR/WSXRcAN/MY66lAGG6 fxU/rGRbL5uNcRHPzOLZPvvkrS2kX2o= X-MC-Unique: iRCxf-1yNAWyzMvso5ujiw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 06/24] nbd/client: Simplify cookie vs. index computation Date: Thu, 8 Jun 2023 08:56:35 -0500 Message-Id: <20230608135653.2918540-7-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232883577100001 Content-Type: text/plain; charset="utf-8" Our code relies on a sentinel cookie value of zero for deciding when a packet has been handled, as well as relying on array indices between 0 and MAX_NBD_REQUESTS-1 for dereferencing purposes. As long as we can symmetrically convert between two forms, there is no reason to go with the odd choice of using XOR with a random pointer, when we can instead simplify the mappings with a mere offset of 1. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch --- block/nbd.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index be3c46c6fee..5322e66166c 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -50,8 +50,8 @@ #define EN_OPTSTR ":exportname=3D" #define MAX_NBD_REQUESTS 16 -#define COOKIE_TO_INDEX(bs, cookie) ((cookie) ^ (uint64_t)(intptr_t)(bs)) -#define INDEX_TO_COOKIE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) +#define COOKIE_TO_INDEX(cookie) ((cookie) - 1) +#define INDEX_TO_COOKIE(index) ((index) + 1) typedef struct { Coroutine *coroutine; @@ -420,7 +420,7 @@ static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_att= empt(BDRVNBDState *s) static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cook= ie) { int ret; - uint64_t ind =3D COOKIE_TO_INDEX(s, cookie), ind2; + uint64_t ind =3D COOKIE_TO_INDEX(cookie), ind2; QEMU_LOCK_GUARD(&s->receive_mutex); while (true) { @@ -435,7 +435,7 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t cookie) * woken by whoever set s->reply.cookie (or never wait in this * yield). So, we should not wake it here. */ - ind2 =3D COOKIE_TO_INDEX(s, s->reply.cookie); + ind2 =3D COOKIE_TO_INDEX(s->reply.cookie); assert(!s->requests[ind2].receiving); s->requests[ind].receiving =3D true; @@ -468,7 +468,7 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t cookie) nbd_channel_error(s, -EINVAL); return -EINVAL; } - ind2 =3D COOKIE_TO_INDEX(s, s->reply.cookie); + ind2 =3D COOKIE_TO_INDEX(s->reply.cookie); if (ind2 >=3D MAX_NBD_REQUESTS || !s->requests[ind2].coroutine) { nbd_channel_error(s, -EINVAL); return -EINVAL; @@ -519,7 +519,7 @@ nbd_co_send_request(BlockDriverState *bs, NBDRequest *r= equest, qemu_mutex_unlock(&s->requests_lock); qemu_co_mutex_lock(&s->send_mutex); - request->cookie =3D INDEX_TO_COOKIE(s, i); + request->cookie =3D INDEX_TO_COOKIE(i); assert(s->ioc); @@ -832,7 +832,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) { int ret; - int i =3D COOKIE_TO_INDEX(s, cookie); + int i =3D COOKIE_TO_INDEX(cookie); void *local_payload =3D NULL; NBDStructuredReplyChunk *chunk; @@ -1038,7 +1038,7 @@ static bool coroutine_fn nbd_reply_chunk_iter_receive= (BDRVNBDState *s, break_loop: qemu_mutex_lock(&s->requests_lock); - s->requests[COOKIE_TO_INDEX(s, cookie)].coroutine =3D NULL; + s->requests[COOKIE_TO_INDEX(cookie)].coroutine =3D NULL; s->in_flight--; qemu_co_queue_next(&s->free_sema); qemu_mutex_unlock(&s->requests_lock); --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686234207; cv=none; d=zohomail.com; s=zohoarc; b=BB8lAeak9y8evf9bsnKN+v310hnLzjOdZRkpbx5zddOzOqwreYkHjPIQS6eYD2ANpvkxw2luOVBq+gKABP8qcgJWSLYc0BWfYo0Fkf+ch43oh8Zw54SYTGR9BdUHIDo1teEQo7r0uL8X1CxO5zkmkWvwnypRBQUbvbCfV2JDzCk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686234207; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Vs/EcSyKhwS5b6JW7qXWd4CTfHIOTKwEEoggRqe1IYk=; b=KOo+jkW5QdkNppFvD3QGBmBWjTYuJNZGj0MTMdCcLXe4clSv2xuA+cHEt4jwU4bxC9gv1DKSA+RsN9pMIqo7azxwodJ/REjlz8a+iwn+reVBD+iphCyQ7bHH1SW6qsZ9yYwN0Mvv0qhYmhKoDp+mbtm5ZWqh3GSVMivwCHvG+vw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686234207409625.2049924088618; Thu, 8 Jun 2023 07:23:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8t-00049G-KL; Thu, 08 Jun 2023 09:57:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tR-EO for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8Q-0004fs-7C for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-GJJuzvmWNJq7EG5Dqk2Q-Q-1; Thu, 08 Jun 2023 09:57:02 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C50433825BA4; Thu, 8 Jun 2023 13:57:01 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5CD2A492B00; Thu, 8 Jun 2023 13:57:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vs/EcSyKhwS5b6JW7qXWd4CTfHIOTKwEEoggRqe1IYk=; b=QyFVsw8+/5MBu0pF8BN48Wz8Q5r0pmVt5WRqZwxor6c6Lx3bm6i8d7VDFSfzs1deQp12OL XQVaYdZDH1gSeV5BGkMsOcxkd4BjYsUSfUuEJITfZgCgTJB4Z7UDwI99KJzEPkjTb7gPA3 PExo/WY1Lpo90SiTJLYrFP0CQJaPlvs= X-MC-Unique: GJJuzvmWNJq7EG5Dqk2Q-Q-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 07/24] nbd/client: Add safety check on chunk payload length Date: Thu, 8 Jun 2023 08:56:36 -0500 Message-Id: <20230608135653.2918540-8-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686234208200100002 Content-Type: text/plain; charset="utf-8" Our existing use of structured replies either reads into a qiov capped at 32M (NBD_CMD_READ) or caps allocation to 1000 bytes (see NBD_MAX_MALLOC_PAYLOAD in block/nbd.c). But the existing length checks are rather late; if we encounter a buggy (or malicious) server that sends a super-large payload length, we should drop the connection right then rather than assuming the layer on top will be careful. This becomes more important when we permit 64-bit lengths which are even more likely to have the potential for attempted denial of service abuse. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: sink this later in series [Vladimir] --- nbd/client.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/nbd/client.c b/nbd/client.c index ea3590ca3d0..1b5569556fe 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1413,6 +1413,18 @@ static int nbd_receive_structured_reply_chunk(QIOCha= nnel *ioc, chunk->cookie =3D be64_to_cpu(chunk->cookie); chunk->length =3D be32_to_cpu(chunk->length); + /* + * Because we use BLOCK_STATUS with REQ_ONE, and cap READ requests + * at 32M, no valid server should send us payload larger than + * this. Even if we stopped using REQ_ONE, sane servers will cap + * the number of extents they return for block status. + */ + if (chunk->length > NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData= )) { + error_setg(errp, "server chunk %" PRIu32 " (%s) payload is too lon= g", + chunk->type, nbd_rep_lookup(chunk->type)); + return -EINVAL; + } + return 0; } --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232826; cv=none; d=zohomail.com; s=zohoarc; b=EkWG/PbpCKiSzHubigUQvADx/LsUbCUSrvX5Wn6pTnYmUjPeN8uMjCuwd0GBoEeLEx0xRSodivIlMOcTEeHAVa3vIEFasVHJNjvZEuIgV71M1NXRVLro8UmxVf5l8RnydFSf7W4zskPNHA87w+Wgq+8ZlcdGdkKElR5bM+Gts0o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232826; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=I6kHRRRVILrBtqoe6YBjXUIhCNSDqwZE081sGPDI5qg=; b=K/K77c9y/oudYKsGwTbY4Q1nRXqHdRSBsdwbFOjpKs6fE+/olj9O6OtdD4Z1Ow/xTnbENSWB7Z9sNWgIkjQjeV5Wi3/0bpAbqbOHwwTm3CIpaA+c/bQ+34WZddTyLSWB0tNE8qZvreCHH/uckhUSePO77zHlZXc8GscH5w7vjNo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232826663439.75729374053947; Thu, 8 Jun 2023 07:00:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8t-000490-9m; Thu, 08 Jun 2023 09:57:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0003tK-7m for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8P-0004fm-VM for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:10 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-569-vo-uB0jVP2W6y1lHpu1P7Q-1; Thu, 08 Jun 2023 09:57:03 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 996E98030CD; Thu, 8 Jun 2023 13:57:02 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 049DE48205E; Thu, 8 Jun 2023 13:57:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I6kHRRRVILrBtqoe6YBjXUIhCNSDqwZE081sGPDI5qg=; b=AGObAx9lFKJWQwHecnV/6nCUoD33RsBKa8qO0o8FqjhC26/mpYyjvmNbkUVy1bCk73RDj1 gKLl8m+9mB/myQE0mHpu/Njo/STRVp5RRio9hAKxrlh+Cc80tgmOnZ8/O0cxefAgSjtecQ 2a19aBLDGFnghRwdc7Y4BmPNpPzI9l8= X-MC-Unique: vo-uB0jVP2W6y1lHpu1P7Q-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 08/24] nbd: Use enum for various negotiation modes Date: Thu, 8 Jun 2023 08:56:37 -0500 Message-Id: <20230608135653.2918540-9-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232833706100001 Content-Type: text/plain; charset="utf-8" Deciphering the hard-coded list of integer return values from nbd_start_negotiate() will only get more confusing when adding support for 64-bit extended headers. Better is to name things in an enum. Although the function in question is private to client.c, putting the enum in a public header and including an enum-to-string conversion will allow its use in more places in upcoming patches. The enum is intentionally laid out so that operators like <=3D can be used to group multiple modes with similar characteristics, and where the least powerful mode has value 0, even though this patch does not exploit that. No semantic change intended. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch, expanding enum idea from v3 4/14 --- include/block/nbd.h | 11 +++++++++++ nbd/client.c | 46 ++++++++++++++++++++++++--------------------- nbd/common.c | 17 +++++++++++++++++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 59db69bafa5..aba4279b56c 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -52,6 +52,16 @@ typedef struct NBDOptionReplyMetaContext { /* metadata context name follows */ } QEMU_PACKED NBDOptionReplyMetaContext; +/* Track results of negotiation */ +typedef enum NBDMode { + /* Keep this list in a continuum of increasing features. */ + NBD_MODE_OLDSTYLE, /* server lacks newstyle negotiation */ + NBD_MODE_EXPORT_NAME, /* newstyle but only OPT_EXPORT_NAME safe */ + NBD_MODE_SIMPLE, /* newstyle but only simple replies */ + NBD_MODE_STRUCTURED, /* newstyle, structured replies enabled */ + /* TODO add NBD_MODE_EXTENDED */ +} NBDMode; + /* Transmission phase structs * * Note: these are _NOT_ the same as the network representation of an NBD @@ -404,6 +414,7 @@ const char *nbd_rep_lookup(uint32_t rep); const char *nbd_info_lookup(uint16_t info); const char *nbd_cmd_lookup(uint16_t info); const char *nbd_err_lookup(int err); +const char *nbd_mode_lookup(NBDMode mode); /* nbd/client-connection.c */ typedef struct NBDClientConnection NBDClientConnection; diff --git a/nbd/client.c b/nbd/client.c index 1b5569556fe..479208d5d9d 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -875,10 +875,7 @@ static int nbd_list_meta_contexts(QIOChannel *ioc, * Start the handshake to the server. After a positive return, the server * is ready to accept additional NBD_OPT requests. * Returns: negative errno: failure talking to server - * 0: server is oldstyle, must call nbd_negotiate_finish_oldstyle - * 1: server is newstyle, but can only accept EXPORT_NAME - * 2: server is newstyle, but lacks structured replies - * 3: server is newstyle and set up for structured replies + * non-negative: enum NBDMode describing server abilities */ static int nbd_start_negotiate(AioContext *aio_context, QIOChannel *ioc, QCryptoTLSCreds *tlscreds, @@ -969,16 +966,16 @@ static int nbd_start_negotiate(AioContext *aio_contex= t, QIOChannel *ioc, return -EINVAL; } } - return 2 + result; + return result ? NBD_MODE_STRUCTURED : NBD_MODE_SIMPLE; } else { - return 1; + return NBD_MODE_EXPORT_NAME; } } else if (magic =3D=3D NBD_CLIENT_MAGIC) { if (tlscreds) { error_setg(errp, "Server does not support STARTTLS"); return -EINVAL; } - return 0; + return NBD_MODE_OLDSTYLE; } else { error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic); return -EINVAL; @@ -1032,6 +1029,9 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, result =3D nbd_start_negotiate(aio_context, ioc, tlscreds, hostname, o= utioc, info->structured_reply, &zeroes, errp); + if (result < 0) { + return result; + } info->structured_reply =3D false; info->base_allocation =3D false; @@ -1039,8 +1039,8 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, ioc =3D *outioc; } - switch (result) { - case 3: /* newstyle, with structured replies */ + switch ((NBDMode)result) { + case NBD_MODE_STRUCTURED: info->structured_reply =3D true; if (base_allocation) { result =3D nbd_negotiate_simple_meta_context(ioc, info, errp); @@ -1050,7 +1050,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, info->base_allocation =3D result =3D=3D 1; } /* fall through */ - case 2: /* newstyle, try OPT_GO */ + case NBD_MODE_SIMPLE: /* Try NBD_OPT_GO first - if it works, we are done (it * also gives us a good message if the server requires * TLS). If it is not available, fall back to @@ -1073,7 +1073,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, return -EINVAL; } /* fall through */ - case 1: /* newstyle, but limited to EXPORT_NAME */ + case NBD_MODE_EXPORT_NAME: /* write the export name request */ if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, info->na= me, errp) < 0) { @@ -1089,7 +1089,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, return -EINVAL; } break; - case 0: /* oldstyle, parse length and flags */ + case NBD_MODE_OLDSTYLE: if (*info->name) { error_setg(errp, "Server does not support non-empty export nam= es"); return -EINVAL; @@ -1099,7 +1099,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, } break; default: - return result; + g_assert_not_reached(); } trace_nbd_receive_negotiate_size_flags(info->size, info->flags); @@ -1155,10 +1155,13 @@ int nbd_receive_export_list(QIOChannel *ioc, QCrypt= oTLSCreds *tlscreds, if (tlscreds && sioc) { ioc =3D sioc; } + if (result < 0) { + goto out; + } - switch (result) { - case 2: - case 3: + switch ((NBDMode)result) { + case NBD_MODE_SIMPLE: + case NBD_MODE_STRUCTURED: /* newstyle - use NBD_OPT_LIST to populate array, then try * NBD_OPT_INFO on each array member. If structured replies * are enabled, also try NBD_OPT_LIST_META_CONTEXT. */ @@ -1179,7 +1182,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, memset(&array[count - 1], 0, sizeof(*array)); array[count - 1].name =3D name; array[count - 1].description =3D desc; - array[count - 1].structured_reply =3D result =3D=3D 3; + array[count - 1].structured_reply =3D result =3D=3D NBD_MODE_S= TRUCTURED; } for (i =3D 0; i < count; i++) { @@ -1195,7 +1198,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, break; } - if (result =3D=3D 3 && + if (result =3D=3D NBD_MODE_STRUCTURED && nbd_list_meta_contexts(ioc, &array[i], errp) < 0) { goto out; } @@ -1204,11 +1207,12 @@ int nbd_receive_export_list(QIOChannel *ioc, QCrypt= oTLSCreds *tlscreds, /* Send NBD_OPT_ABORT as a courtesy before hanging up */ nbd_send_opt_abort(ioc); break; - case 1: /* newstyle, but limited to EXPORT_NAME */ + case NBD_MODE_EXPORT_NAME: error_setg(errp, "Server does not support export lists"); /* We can't even send NBD_OPT_ABORT, so merely hang up */ goto out; - case 0: /* oldstyle, parse length and flags */ + case NBD_MODE_OLDSTYLE: + /* Lone export name is implied, but we can parse length and flags = */ array =3D g_new0(NBDExportInfo, 1); array->name =3D g_strdup(""); count =3D 1; @@ -1226,7 +1230,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, } break; default: - goto out; + g_assert_not_reached(); } *info =3D array; diff --git a/nbd/common.c b/nbd/common.c index ddfe7d11837..989fbe54a19 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -248,3 +248,20 @@ int nbd_errno_to_system_errno(int err) } return ret; } + + +const char *nbd_mode_lookup(NBDMode mode) +{ + switch (mode) { + case NBD_MODE_OLDSTYLE: + return "oldstyle"; + case NBD_MODE_EXPORT_NAME: + return "export name only"; + case NBD_MODE_SIMPLE: + return "simple headers"; + case NBD_MODE_STRUCTURED: + return "structured replies"; + default: + return ""; + } +} --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232877; cv=none; d=zohomail.com; s=zohoarc; b=P0ZDyZTMnYXTrdi5hHkIrviuCRp4XzqyY4Yuu5CtI/rn/8rFe2tef/a+ROEFPrqFJMje8v1hBIG/HacpiZbJ0+1sfEIb/uDjUtPV6gY+Z1k8RGKqrLNAMMUTQvKQtYlw1VO1jmxhwlUVdRr9KVKIE3N5W9vPlDlNv10yG4GvZAY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232877; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kmsZP6VkAAu/E7gm3FDvsqZsrxjZmoWP71ce9dPZ/DU=; b=fRbhsxmK6AzEAuDfXXI/OoQl/Fy114jPSprmeQs4KcQvdfSsjFiy0krnBmeSHKc4B377tRpk68ugf8HlPpVstdyes/8emDPru4i4gooALn5YjK2GNunYqeWDROzhBTfUkwmKy24m/jDUNSxIhEydL1Wy1Fzu51iztGAe8D51W68= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232877545179.1790756164644; Thu, 8 Jun 2023 07:01:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8s-00048p-Vh; Thu, 08 Jun 2023 09:57:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8X-0003vg-1O for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004gQ-Kd for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:12 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-599-KQs8U6LxPLmoXCmDPz4hzA-1; Thu, 08 Jun 2023 09:57:03 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 646933C1C4DD; Thu, 8 Jun 2023 13:57:03 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id D04BB515540; Thu, 8 Jun 2023 13:57:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232627; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kmsZP6VkAAu/E7gm3FDvsqZsrxjZmoWP71ce9dPZ/DU=; b=axEnLviUSStqeYjIHYA9ci6iGUX2cpQ7oVW60lHUZX9zq03V+uNKSyNykZhotneRhwpjH/ cgCn5LNA8/Q3zm7rct46wUQpbOCd35dax3Y7L+/dwN/29SxsB4y5a7i8aozul5k8ARMWG1 KwFow2XpL+iYTum1ejKAJSYOaFvLcoI= X-MC-Unique: KQs8U6LxPLmoXCmDPz4hzA-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 09/24] nbd: Replace bool structured_reply with mode enum Date: Thu, 8 Jun 2023 08:56:38 -0500 Message-Id: <20230608135653.2918540-10-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232892519100001 Content-Type: text/plain; charset="utf-8" The upcoming patches for 64-bit extensions requires various points in the protocol to make decisions based on what was negotiated. While we could easily add a 'bool extended_headers' alongside the existing 'bool structured_reply', this does not scale well if more modes are added in the future. Better is to expose the mode enum added in the previous patch out to a wider use in the code base. Where the code previously checked for structured_reply being set or clear, it now prefers checking for an inequality; this works because the nodes are in a continuum of increasing abilities, and allows us to touch fewer places if we ever insert other modes in the middle of the enum. There should be no semantic change in this patch. Signed-off-by: Eric Blake --- v4: new patch, expanding enum idea from v3 4/14 --- include/block/nbd.h | 2 +- block/nbd.c | 8 +++++--- nbd/client-connection.c | 4 ++-- nbd/client.c | 18 +++++++++--------- nbd/server.c | 27 +++++++++++++++------------ qemu-nbd.c | 4 +++- 6 files changed, 35 insertions(+), 28 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index aba4279b56c..fea69ac24bb 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -304,7 +304,7 @@ typedef struct NBDExportInfo { /* In-out fields, set by client before nbd_receive_negotiate() and * updated by server results during nbd_receive_negotiate() */ - bool structured_reply; + NBDMode mode; /* input maximum mode tolerated; output actual mode chos= en */ bool base_allocation; /* base:allocation context for NBD_CMD_BLOCK_STA= TUS */ /* Set by server results during nbd_receive_negotiate() and diff --git a/block/nbd.c b/block/nbd.c index 5322e66166c..5f88f7a819b 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -464,7 +464,8 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t cookie) nbd_channel_error(s, ret); return ret; } - if (nbd_reply_is_structured(&s->reply) && !s->info.structured_repl= y) { + if (nbd_reply_is_structured(&s->reply) && + s->info.mode < NBD_MODE_STRUCTURED) { nbd_channel_error(s, -EINVAL); return -EINVAL; } @@ -867,7 +868,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( } /* handle structured reply chunk */ - assert(s->info.structured_reply); + assert(s->info.mode >=3D NBD_MODE_STRUCTURED); chunk =3D &s->reply.structured; if (chunk->type =3D=3D NBD_REPLY_TYPE_NONE) { @@ -1071,7 +1072,8 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, void *payload =3D NULL; Error *local_err =3D NULL; - NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, s->info.structured_reply, + NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, + s->info.mode >=3D NBD_MODE_STRUCTURED, qiov, &reply, &payload) { int ret; diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 3d14296c042..13e4cb6684b 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -1,5 +1,5 @@ /* - * QEMU Block driver for NBD + * QEMU Block driver for NBD * * Copyright (c) 2021 Virtuozzo International GmbH. * @@ -93,7 +93,7 @@ NBDClientConnection *nbd_client_connection_new(const Sock= etAddress *saddr, .do_negotiation =3D do_negotiation, .initial_info.request_sizes =3D true, - .initial_info.structured_reply =3D true, + .initial_info.mode =3D NBD_MODE_STRUCTURED, .initial_info.base_allocation =3D true, .initial_info.x_dirty_bitmap =3D g_strdup(x_dirty_bitmap), .initial_info.name =3D g_strdup(export_name ?: "") diff --git a/nbd/client.c b/nbd/client.c index 479208d5d9d..faa054c4527 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -880,7 +880,7 @@ static int nbd_list_meta_contexts(QIOChannel *ioc, static int nbd_start_negotiate(AioContext *aio_context, QIOChannel *ioc, QCryptoTLSCreds *tlscreds, const char *hostname, QIOChannel **outioc, - bool structured_reply, bool *zeroes, + NBDMode max_mode, bool *zeroes, Error **errp) { ERRP_GUARD(); @@ -958,7 +958,7 @@ static int nbd_start_negotiate(AioContext *aio_context,= QIOChannel *ioc, if (fixedNewStyle) { int result =3D 0; - if (structured_reply) { + if (max_mode >=3D NBD_MODE_STRUCTURED) { result =3D nbd_request_simple_option(ioc, NBD_OPT_STRUCTURED_REPL= Y, false, errp); @@ -1028,20 +1028,19 @@ int nbd_receive_negotiate(AioContext *aio_context, = QIOChannel *ioc, trace_nbd_receive_negotiate_name(info->name); result =3D nbd_start_negotiate(aio_context, ioc, tlscreds, hostname, o= utioc, - info->structured_reply, &zeroes, errp); + info->mode, &zeroes, errp); if (result < 0) { return result; } - info->structured_reply =3D false; + info->mode =3D result; info->base_allocation =3D false; if (tlscreds && *outioc) { ioc =3D *outioc; } - switch ((NBDMode)result) { + switch (info->mode) { case NBD_MODE_STRUCTURED: - info->structured_reply =3D true; if (base_allocation) { result =3D nbd_negotiate_simple_meta_context(ioc, info, errp); if (result < 0) { @@ -1150,8 +1149,8 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, QIOChannel *sioc =3D NULL; *info =3D NULL; - result =3D nbd_start_negotiate(NULL, ioc, tlscreds, hostname, &sioc, t= rue, - NULL, errp); + result =3D nbd_start_negotiate(NULL, ioc, tlscreds, hostname, &sioc, + NBD_MODE_STRUCTURED, NULL, errp); if (tlscreds && sioc) { ioc =3D sioc; } @@ -1182,7 +1181,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, memset(&array[count - 1], 0, sizeof(*array)); array[count - 1].name =3D name; array[count - 1].description =3D desc; - array[count - 1].structured_reply =3D result =3D=3D NBD_MODE_S= TRUCTURED; + array[count - 1].mode =3D result; } for (i =3D 0; i < count; i++) { @@ -1215,6 +1214,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, /* Lone export name is implied, but we can parse length and flags = */ array =3D g_new0(NBDExportInfo, 1); array->name =3D g_strdup(""); + array->mode =3D NBD_MODE_OLDSTYLE; count =3D 1; if (nbd_negotiate_finish_oldstyle(ioc, array, errp) < 0) { diff --git a/nbd/server.c b/nbd/server.c index 8486b64b15d..bade4f7990c 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -143,7 +143,7 @@ struct NBDClient { uint32_t check_align; /* If non-zero, check for aligned client request= s */ - bool structured_reply; + NBDMode mode; NBDExportMetaContexts export_meta; uint32_t opt; /* Current option being negotiated */ @@ -502,7 +502,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, bool no_zeroes, } myflags =3D client->exp->nbdflags; - if (client->structured_reply) { + if (client->mode >=3D NBD_MODE_STRUCTURED) { myflags |=3D NBD_FLAG_SEND_DF; } trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags); @@ -687,7 +687,7 @@ static int nbd_negotiate_handle_info(NBDClient *client,= Error **errp) /* Send NBD_INFO_EXPORT always */ myflags =3D exp->nbdflags; - if (client->structured_reply) { + if (client->mode >=3D NBD_MODE_STRUCTURED) { myflags |=3D NBD_FLAG_SEND_DF; } trace_nbd_negotiate_new_style_size_flags(exp->size, myflags); @@ -985,7 +985,8 @@ static int nbd_negotiate_meta_queries(NBDClient *client, size_t i; size_t count =3D 0; - if (client->opt =3D=3D NBD_OPT_SET_META_CONTEXT && !client->structured= _reply) { + if (client->opt =3D=3D NBD_OPT_SET_META_CONTEXT && + client->mode < NBD_MODE_STRUCTURED) { return nbd_opt_invalid(client, errp, "request option '%s' when structured reply " "is not negotiated", @@ -1261,13 +1262,13 @@ static int nbd_negotiate_options(NBDClient *client,= Error **errp) case NBD_OPT_STRUCTURED_REPLY: if (length) { ret =3D nbd_reject_length(client, false, errp); - } else if (client->structured_reply) { + } else if (client->mode >=3D NBD_MODE_STRUCTURED) { ret =3D nbd_negotiate_send_rep_err( client, NBD_REP_ERR_INVALID, errp, "structured reply already negotiated"); } else { ret =3D nbd_negotiate_send_rep(client, NBD_REP_ACK, er= rp); - client->structured_reply =3D true; + client->mode =3D NBD_MODE_STRUCTURED; } break; @@ -1907,7 +1908,9 @@ static int coroutine_fn nbd_co_send_simple_reply(NBDC= lient *client, }; assert(!len || !nbd_err); - assert(!client->structured_reply || request->type !=3D NBD_CMD_READ); + assert(client->mode < NBD_MODE_STRUCTURED || + (client->mode =3D=3D NBD_MODE_STRUCTURED && + request->type !=3D NBD_CMD_READ)); trace_nbd_co_send_simple_reply(request->cookie, nbd_err, nbd_err_lookup(nbd_err), len); set_be_simple_reply(&reply, nbd_err, request->cookie); @@ -2409,7 +2412,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * client->check_align); } valid_flags =3D NBD_CMD_FLAG_FUA; - if (request->type =3D=3D NBD_CMD_READ && client->structured_reply) { + if (request->type =3D=3D NBD_CMD_READ && client->mode >=3D NBD_MODE_ST= RUCTURED) { valid_flags |=3D NBD_CMD_FLAG_DF; } else if (request->type =3D=3D NBD_CMD_WRITE_ZEROES) { valid_flags |=3D NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO; @@ -2435,7 +2438,7 @@ static coroutine_fn int nbd_send_generic_reply(NBDCli= ent *client, const char *error_msg, Error **errp) { - if (client->structured_reply && ret < 0) { + if (client->mode >=3D NBD_MODE_STRUCTURED && ret < 0) { return nbd_co_send_chunk_error(client, request, -ret, error_msg, e= rrp); } else { return nbd_co_send_simple_reply(client, request, ret < 0 ? -ret : = 0, @@ -2463,8 +2466,8 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *cl= ient, NBDRequest *request, } } - if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) && - request->len) + if (client->mode >=3D NBD_MODE_STRUCTURED && + !(request->flags & NBD_CMD_FLAG_DF) && request->len) { return nbd_co_send_sparse_read(client, request, request->from, data, request->len, errp); @@ -2476,7 +2479,7 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *cl= ient, NBDRequest *request, "reading from file failed", errp); } - if (client->structured_reply) { + if (client->mode >=3D NBD_MODE_STRUCTURED) { if (request->len) { return nbd_co_send_chunk_read(client, request, request->from, = data, request->len, true, errp); diff --git a/qemu-nbd.c b/qemu-nbd.c index 4276163564b..3ddd0bf02b4 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -274,8 +274,10 @@ static void *show_parts(void *arg) static void *nbd_client_thread(void *arg) { + /* TODO: Revisit this if nbd.ko ever gains support for structured repl= y */ char *device =3D arg; - NBDExportInfo info =3D { .request_sizes =3D false, .name =3D g_strdup(= "") }; + NBDExportInfo info =3D { .request_sizes =3D false, .name =3D g_strdup(= ""), + .mode =3D NBD_MODE_SIMPLE }; QIOChannelSocket *sioc; int fd =3D -1; int ret =3D EXIT_FAILURE; --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232704; cv=none; d=zohomail.com; s=zohoarc; b=J4sASsTbCVLy+fx0CAA2QrDosLEIlxS6q6bl5i410NwtKbs4hFcX7UrtXcYfk6yWoMr9A3yXyYspvDt32oTgBH3sFHXrYq725xzwyJlxNWj/TMcvfoLOjhty2E3HXkuc/uatSQP8MK9CajhUoi/1tqqLwTBXtDUolz0ugxJB/2g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232704; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+SsbyLfqXyvCooYWPk3Z0zrcq/9hiGYmqtPURA2YbwQ=; b=gw4xRyxmkADMIGWSd0xIB2VyN2yXOk3e4SPVpgbLc5GONzYFj3cfM5mZSPSwvBlQRJETOSiKAhiWQceTi3yah/bJMdbIqB36QProB6nbMz9RwrS7XA52J9ZqJ1FaCnI7tkEX5yYszlNLCP4Y7nDcOiKKq4bYTks090Ab3+8I9e8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232704953363.0964549203636; Thu, 8 Jun 2023 06:58:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8y-0004Fv-Cq; Thu, 08 Jun 2023 09:57:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8W-0003vd-FA for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004g1-Gd for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:12 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-578-j05coLN3OjaPXH6HIQTB3A-1; Thu, 08 Jun 2023 09:57:04 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 26EAC8582B4; Thu, 8 Jun 2023 13:57:04 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9168F48205E; Thu, 8 Jun 2023 13:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+SsbyLfqXyvCooYWPk3Z0zrcq/9hiGYmqtPURA2YbwQ=; b=d78cRzYbs+B+WOoR7SYMC3bqwLsI3ohqgSTTqLb+vJl//6IuSImwU9g4+i5PPKXwE8Ca2y yPOP7F7Au8K98Oj/9k//QWt0KjoXItyFzljviXvsRRaVXiYJGjgNTYCzbh2YTXxUi7t68n fQVm/qElNUvBK/U3jrZD0JfCEvj7I3Q= X-MC-Unique: j05coLN3OjaPXH6HIQTB3A-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 10/24] nbd/client: Pass mode through to nbd_send_request Date: Thu, 8 Jun 2023 08:56:39 -0500 Message-Id: <20230608135653.2918540-11-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232706379100004 Content-Type: text/plain; charset="utf-8" Once the 64-bit headers extension is enabled, the data layout we send over the wire for a client request depends on the mode negotiated with the server. Rather than adding a parameter to nbd_send_request, we can add a member to struct NBDRequest, since it already does not reflect on-wire format. Some callers initialize it directly; many others rely on a common initialization point during nbd_co_send_request(). At this point, there is no semantic change. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch, based on ideas in v3 4/14, but by modifying NBDRequest instead of adding a parameter --- include/block/nbd.h | 12 +++++++----- block/nbd.c | 5 +++-- nbd/client.c | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index fea69ac24bb..52420660a65 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -62,17 +62,19 @@ typedef enum NBDMode { /* TODO add NBD_MODE_EXTENDED */ } NBDMode; -/* Transmission phase structs - * - * Note: these are _NOT_ the same as the network representation of an NBD - * request and reply! +/* Transmission phase structs */ + +/* + * Note: NBDRequest is _NOT_ the same as the network representation of an = NBD + * request! */ typedef struct NBDRequest { uint64_t cookie; uint64_t from; uint32_t len; uint16_t flags; /* NBD_CMD_FLAG_* */ - uint16_t type; /* NBD_CMD_* */ + uint16_t type; /* NBD_CMD_* */ + NBDMode mode; /* Determines which network representation to use */ } NBDRequest; typedef struct NBDSimpleReply { diff --git a/block/nbd.c b/block/nbd.c index 5f88f7a819b..ca5991f868a 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -339,7 +339,7 @@ int coroutine_fn nbd_co_do_establish_connection(BlockDr= iverState *bs, * We have connected, but must fail for other reasons. * Send NBD_CMD_DISC as a courtesy to the server. */ - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; + NBDRequest request =3D { .type =3D NBD_CMD_DISC, .mode =3D s->info= .mode }; nbd_send_request(s->ioc, &request); @@ -521,6 +521,7 @@ nbd_co_send_request(BlockDriverState *bs, NBDRequest *r= equest, qemu_co_mutex_lock(&s->send_mutex); request->cookie =3D INDEX_TO_COOKIE(i); + request->mode =3D s->info.mode; assert(s->ioc); @@ -1466,7 +1467,7 @@ static void nbd_yank(void *opaque) static void nbd_client_close(BlockDriverState *bs) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; + NBDRequest request =3D { .type =3D NBD_CMD_DISC, .mode =3D s->info.mod= e }; if (s->ioc) { nbd_send_request(s->ioc, &request); diff --git a/nbd/client.c b/nbd/client.c index faa054c4527..40a1eb72346 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1224,7 +1224,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, /* Send NBD_CMD_DISC as a courtesy to the server, but ignore all * errors now that we have the information we wanted. */ if (nbd_drop(ioc, 124, NULL) =3D=3D 0) { - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; + NBDRequest request =3D { .type =3D NBD_CMD_DISC, .mode =3D res= ult }; nbd_send_request(ioc, &request); } @@ -1354,6 +1354,7 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *req= uest) { uint8_t buf[NBD_REQUEST_SIZE]; + assert(request->mode <=3D NBD_MODE_STRUCTURED); /* TODO handle extende= d */ trace_nbd_send_request(request->from, request->len, request->cookie, request->flags, request->type, nbd_cmd_lookup(request->type)); --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686234407; cv=none; d=zohomail.com; s=zohoarc; b=PLvKhgQ2WKmK5hDiv7fK579x78hZn/Mb5JPYher9RZOTW88EXr3cD1KsjUMO3AgZBW+/kUE4kvimNzZzTOEluI5+ckyfkkMb8+fvE/U7FScCSTiEd+Z8FYwOAc4VUSB4WcQDI+Ohyjb3g2Cszh1Bmdl2ZgT7VeC4eJM1lwHNX+c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686234407; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2dYBhlfkH7hYfuaXOhkTsYPit9D637+pdt6XFLwODPQ=; b=fbyeW7hAuclsCTL1+ocG2Y/Wdz0cr60KPx+nqbPZwRNOOIo1o7TsQILRUjjPbezXwLDr+f7qZSWTMbXCTuLR6MdmhC8s+IM2XtAjPX5wWLe5BjWPYRefxWhKr6Lq6i2RU2ms82ZCasLXZowD2DV2eYnzJp9tTTcfLLTyF9FLv/w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686234407797325.6189950761783; Thu, 8 Jun 2023 07:26:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8u-0004AY-UI; Thu, 08 Jun 2023 09:57:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0003yF-Tr for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004gK-Nl for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:15 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-412-TjdZ2y46Pp2BLJP-g7oksw-1; Thu, 08 Jun 2023 09:57:05 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EE5401C0942B; Thu, 8 Jun 2023 13:57:04 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 640AC492B00; Thu, 8 Jun 2023 13:57:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2dYBhlfkH7hYfuaXOhkTsYPit9D637+pdt6XFLwODPQ=; b=JiW+kLOEDyu3TiG286nYxpDz6F8euYM1iz60P8hnXfJLC8MfSp3uLMSRlrLlqe/fkw/p0o UYSXVNCP92WuVrkO+jKMkfqHB0zIyG7QJaggvyW9BRnAZwSAg02dqdCEXMTMGg8JIdw6eX Ii9mV+tYz+L4GqD+m8wwAHLRgKQfXYo= X-MC-Unique: TjdZ2y46Pp2BLJP-g7oksw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 11/24] nbd: Add types for extended headers Date: Thu, 8 Jun 2023 08:56:40 -0500 Message-Id: <20230608135653.2918540-12-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686234409294100003 Content-Type: text/plain; charset="utf-8" Add the constants and structs necessary for later patches to start implementing the NBD_OPT_EXTENDED_HEADERS extension in both the client and server, matching recent upstream nbd.git (through commit e6f3b94a934). This patch does not change any existing behavior, but merely sets the stage for upcoming patches. This patch does not change the status quo that neither the client nor server use a packed-struct representation for the request header. While most of the patch adds new types, there is also some churn for renaming the existing NBDExtent to NBDExtent32 to contrast it with NBDExtent64, which I thought was a nicer name than NBDExtentExt. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: Hoist earlier in series, tweak a few comments, defer docs/interop change to when feature is actually turned on, NBDExtent rename, add QEMU_BUG_BUILD_ON for sanity sake, hoist in block status payload bits from v3 14/14; R-b dropped --- include/block/nbd.h | 124 +++++++++++++++++++++++++++++++------------- nbd/nbd-internal.h | 3 +- block/nbd.c | 6 +-- nbd/common.c | 12 ++++- nbd/server.c | 6 +-- 5 files changed, 106 insertions(+), 45 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 52420660a65..f706e38dc72 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -59,7 +59,7 @@ typedef enum NBDMode { NBD_MODE_EXPORT_NAME, /* newstyle but only OPT_EXPORT_NAME safe */ NBD_MODE_SIMPLE, /* newstyle but only simple replies */ NBD_MODE_STRUCTURED, /* newstyle, structured replies enabled */ - /* TODO add NBD_MODE_EXTENDED */ + NBD_MODE_EXTENDED, /* newstyle, extended headers enabled */ } NBDMode; /* Transmission phase structs */ @@ -92,20 +92,36 @@ typedef struct NBDStructuredReplyChunk { uint32_t length; /* length of payload */ } QEMU_PACKED NBDStructuredReplyChunk; +typedef struct NBDExtendedReplyChunk { + uint32_t magic; /* NBD_EXTENDED_REPLY_MAGIC */ + uint16_t flags; /* combination of NBD_REPLY_FLAG_* */ + uint16_t type; /* NBD_REPLY_TYPE_* */ + uint64_t cookie; /* request handle */ + uint64_t offset; /* request offset */ + uint64_t length; /* length of payload */ +} QEMU_PACKED NBDExtendedReplyChunk; + typedef union NBDReply { NBDSimpleReply simple; NBDStructuredReplyChunk structured; + NBDExtendedReplyChunk extended; struct { /* - * @magic and @cookie fields have the same offset and size both in - * simple reply and structured reply chunk, so let them be accessi= ble - * without ".simple." or ".structured." specification + * @magic and @cookie fields have the same offset and size in all + * forms of replies, so let them be accessible without ".simple.", + * ".structured.", or ".extended." specifications. */ uint32_t magic; uint32_t _skip; uint64_t cookie; - } QEMU_PACKED; + }; } NBDReply; +QEMU_BUILD_BUG_ON(offsetof(NBDReply, simple.cookie) !=3D + offsetof(NBDReply, cookie)); +QEMU_BUILD_BUG_ON(offsetof(NBDReply, structured.cookie) !=3D + offsetof(NBDReply, cookie)); +QEMU_BUILD_BUG_ON(offsetof(NBDReply, extended.cookie) !=3D + offsetof(NBDReply, cookie)); /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */ typedef struct NBDStructuredReadData { @@ -132,14 +148,34 @@ typedef struct NBDStructuredError { typedef struct NBDStructuredMeta { /* header's length >=3D 12 (at least one extent) */ uint32_t context_id; - /* extents follows */ + /* NBDExtent32 extents[] follows, array length implied by header */ } QEMU_PACKED NBDStructuredMeta; -/* Extent chunk for NBD_REPLY_TYPE_BLOCK_STATUS */ -typedef struct NBDExtent { +/* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS */ +typedef struct NBDExtent32 { uint32_t length; uint32_t flags; /* NBD_STATE_* */ -} QEMU_PACKED NBDExtent; +} QEMU_PACKED NBDExtent32; + +/* Header of NBD_REPLY_TYPE_BLOCK_STATUS_EXT */ +typedef struct NBDExtendedMeta { + /* header's length >=3D 24 (at least one extent) */ + uint32_t context_id; + uint32_t count; /* header length must be count * 16 + 8 */ + /* NBDExtent64 extents[count] follows */ +} QEMU_PACKED NBDExtendedMeta; + +/* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS_EXT */ +typedef struct NBDExtent64 { + uint64_t length; + uint64_t flags; /* NBD_STATE_* */ +} QEMU_PACKED NBDExtent64; + +/* Client payload for limiting NBD_CMD_BLOCK_STATUS reply */ +typedef struct NBDBlockStatusPayload { + uint64_t effect_length; + /* uint32_t ids[] follows, array length implied by header */ +} QEMU_PACKED NBDBlockStatusPayload; /* Transmission (export) flags: sent from server to client during handshak= e, but describe what will happen during transmission */ @@ -157,20 +193,22 @@ enum { NBD_FLAG_SEND_RESIZE_BIT =3D 9, /* Send resize */ NBD_FLAG_SEND_CACHE_BIT =3D 10, /* Send CACHE (prefetch) */ NBD_FLAG_SEND_FAST_ZERO_BIT =3D 11, /* FAST_ZERO flag for WRITE_ZE= ROES */ + NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT =3D 12, /* PAYLOAD flag for BLOCK_STAT= US */ }; -#define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT) -#define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT) -#define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT) -#define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT) -#define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT) -#define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT) -#define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT) -#define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT) -#define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT) -#define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT) -#define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT) -#define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT) +#define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT) +#define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT) +#define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT) +#define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT) +#define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT) +#define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT) +#define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT) +#define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT) +#define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT) +#define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT) +#define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT) +#define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT) +#define NBD_FLAG_BLOCK_STAT_PAYLOAD (1 << NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT) /* New-style handshake (global) flags, sent from server to client, and control what will happen during handshake phase. */ @@ -193,6 +231,7 @@ enum { #define NBD_OPT_STRUCTURED_REPLY (8) #define NBD_OPT_LIST_META_CONTEXT (9) #define NBD_OPT_SET_META_CONTEXT (10) +#define NBD_OPT_EXTENDED_HEADERS (11) /* Option reply types. */ #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value)) @@ -210,6 +249,8 @@ enum { #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */ #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting dow= n */ #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZ= E */ +#define NBD_REP_ERR_TOO_BIG NBD_REP_ERR(9) /* Payload size overfl= ow */ +#define NBD_REP_ERR_EXT_HEADER_REQD NBD_REP_ERR(10) /* Need extended heade= rs */ /* Info types, used during NBD_REP_INFO */ #define NBD_INFO_EXPORT 0 @@ -218,12 +259,14 @@ enum { #define NBD_INFO_BLOCK_SIZE 3 /* Request flags, sent from client to server during transmission phase */ -#define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during wri= te */ -#define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */ -#define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read= */ -#define NBD_CMD_FLAG_REQ_ONE (1 << 3) /* only one extent in BLOCK_STATUS - * reply chunk */ -#define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not fa= st */ +#define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during wr= ite */ +#define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run = */ +#define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured rea= d */ +#define NBD_CMD_FLAG_REQ_ONE (1 << 3) \ + /* only one extent in BLOCK_STATUS reply chunk */ +#define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not f= ast */ +#define NBD_CMD_FLAG_PAYLOAD_LEN (1 << 5) \ + /* length describes payload, not effect; only with ext header */ /* Supported request types */ enum { @@ -249,22 +292,31 @@ enum { */ #define NBD_MAX_STRING_SIZE 4096 -/* Two types of reply structures */ +/* Two types of request structures, a given client will only use 1 */ +#define NBD_REQUEST_MAGIC 0x25609513 +#define NBD_EXTENDED_REQUEST_MAGIC 0x21e41c71 + +/* + * Three types of reply structures, but what a client expects depends + * on NBD_OPT_STRUCTURED_REPLY and NBD_OPT_EXTENDED_HEADERS. + */ #define NBD_SIMPLE_REPLY_MAGIC 0x67446698 #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef +#define NBD_EXTENDED_REPLY_MAGIC 0x6e8a278c -/* Structured reply flags */ +/* Chunk reply flags (for structured and extended replies) */ #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last = */ -/* Structured reply types */ +/* Chunk reply types */ #define NBD_REPLY_ERR(value) ((1 << 15) | (value)) -#define NBD_REPLY_TYPE_NONE 0 -#define NBD_REPLY_TYPE_OFFSET_DATA 1 -#define NBD_REPLY_TYPE_OFFSET_HOLE 2 -#define NBD_REPLY_TYPE_BLOCK_STATUS 5 -#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1) -#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) +#define NBD_REPLY_TYPE_NONE 0 +#define NBD_REPLY_TYPE_OFFSET_DATA 1 +#define NBD_REPLY_TYPE_OFFSET_HOLE 2 +#define NBD_REPLY_TYPE_BLOCK_STATUS 5 +#define NBD_REPLY_TYPE_BLOCK_STATUS_EXT 6 +#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1) +#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */ #define NBD_STATE_HOLE (1 << 0) diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index df42fef7066..133b1d94b50 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -1,7 +1,7 @@ /* * NBD Internal Declarations * - * Copyright (C) 2016 Red Hat, Inc. + * Copyright Red Hat * * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. @@ -44,7 +44,6 @@ #define NBD_OLDSTYLE_NEGOTIATE_SIZE (8 + 8 + 8 + 4 + 124) #define NBD_INIT_MAGIC 0x4e42444d41474943LL /* ASCII "NBDMAGI= C" */ -#define NBD_REQUEST_MAGIC 0x25609513 #define NBD_OPTS_MAGIC 0x49484156454F5054LL /* ASCII "IHAVEOP= T" */ #define NBD_CLIENT_MAGIC 0x0000420281861253LL #define NBD_REP_MAGIC 0x0003e889045565a9LL diff --git a/block/nbd.c b/block/nbd.c index ca5991f868a..c7581794873 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -611,7 +611,7 @@ static int nbd_parse_offset_hole_payload(BDRVNBDState *= s, static int nbd_parse_blockstatus_payload(BDRVNBDState *s, NBDStructuredReplyChunk *chunk, uint8_t *payload, uint64_t orig_l= ength, - NBDExtent *extent, Error **errp) + NBDExtent32 *extent, Error **errp) { uint32_t context_id; @@ -1119,7 +1119,7 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, static int coroutine_fn nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uint64_t cookie, - uint64_t length, NBDExtent *extent, + uint64_t length, NBDExtent32 *extent, int *request_ret, Error **errp) { NBDReplyChunkIter iter; @@ -1374,7 +1374,7 @@ static int coroutine_fn GRAPH_RDLOCK nbd_client_co_bl= ock_status( int64_t *pnum, int64_t *map, BlockDriverState **file) { int ret, request_ret; - NBDExtent extent =3D { 0 }; + NBDExtent32 extent =3D { 0 }; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; Error *local_err =3D NULL; diff --git a/nbd/common.c b/nbd/common.c index 989fbe54a19..3247c1d618a 100644 --- a/nbd/common.c +++ b/nbd/common.c @@ -79,6 +79,8 @@ const char *nbd_opt_lookup(uint32_t opt) return "list meta context"; case NBD_OPT_SET_META_CONTEXT: return "set meta context"; + case NBD_OPT_EXTENDED_HEADERS: + return "extended headers"; default: return ""; } @@ -112,6 +114,10 @@ const char *nbd_rep_lookup(uint32_t rep) return "server shutting down"; case NBD_REP_ERR_BLOCK_SIZE_REQD: return "block size required"; + case NBD_REP_ERR_TOO_BIG: + return "option payload too big"; + case NBD_REP_ERR_EXT_HEADER_REQD: + return "extended headers required"; default: return ""; } @@ -170,7 +176,9 @@ const char *nbd_reply_type_lookup(uint16_t type) case NBD_REPLY_TYPE_OFFSET_HOLE: return "hole"; case NBD_REPLY_TYPE_BLOCK_STATUS: - return "block status"; + return "block status (32-bit)"; + case NBD_REPLY_TYPE_BLOCK_STATUS_EXT: + return "block status (64-bit)"; case NBD_REPLY_TYPE_ERROR: return "generic error"; case NBD_REPLY_TYPE_ERROR_OFFSET: @@ -261,6 +269,8 @@ const char *nbd_mode_lookup(NBDMode mode) return "simple headers"; case NBD_MODE_STRUCTURED: return "structured replies"; + case NBD_MODE_EXTENDED: + return "extended headers"; default: return ""; } diff --git a/nbd/server.c b/nbd/server.c index bade4f7990c..9b16f7e5405 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2082,7 +2082,7 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDCl= ient *client, } typedef struct NBDExtentArray { - NBDExtent *extents; + NBDExtent32 *extents; unsigned int nb_alloc; unsigned int count; uint64_t total_length; @@ -2095,7 +2095,7 @@ static NBDExtentArray *nbd_extent_array_new(unsigned = int nb_alloc) NBDExtentArray *ea =3D g_new0(NBDExtentArray, 1); ea->nb_alloc =3D nb_alloc; - ea->extents =3D g_new(NBDExtent, nb_alloc); + ea->extents =3D g_new(NBDExtent32, nb_alloc); ea->can_add =3D true; return ea; @@ -2158,7 +2158,7 @@ static int nbd_extent_array_add(NBDExtentArray *ea, } ea->total_length +=3D length; - ea->extents[ea->count] =3D (NBDExtent) {.length =3D length, .flags =3D= flags}; + ea->extents[ea->count] =3D (NBDExtent32) {.length =3D length, .flags = =3D flags}; ea->count++; return 0; --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686234403; cv=none; d=zohomail.com; s=zohoarc; b=Pm2wyBSDm0YqqvRRRtqGUk9iEMLe9jLCsxu5AJn0rQJ3o9aRHZd2PJR1w9sPuk19mVY9kCezExJV/u2GUDu5RXJFALxPyjsq2ZDMcVr+o2lhHou+5mLdNg2UQIbE4+p/5ml9/ulCVxeW8CQbxon1/bfcTh4RccZrmr2pGV6OtIE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686234403; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ouAK2rrG+bO3VXTAkB9pZtVe89P2rzIF7QS9UG5vc7c=; b=HHUI7MYjpCiJGLFlr38dQ8XUmXgrrI3tjiOPJMVcHP2CLPWKUyPPx3ZYq/06HyJOu43ZIgRyXKPrM3A8DJY1AOY9fGVcLORWc0BjCPaa9IO6R/ALPwpZDBVq50zeLWUK3ZlYUTlKHS75FvW5uwOHgAACa1j60GmPo/vn2h1ZQcw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686234403308448.3548051716639; Thu, 8 Jun 2023 07:26:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8u-00049M-5z; Thu, 08 Jun 2023 09:57:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0003yI-Vb for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004hC-MJ for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:14 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-316-okVij2mgNz28zEDbivb8vw-1; Thu, 08 Jun 2023 09:57:06 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B91FD1C09421; Thu, 8 Jun 2023 13:57:05 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E987492B00; Thu, 8 Jun 2023 13:57:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232629; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ouAK2rrG+bO3VXTAkB9pZtVe89P2rzIF7QS9UG5vc7c=; b=C4avZ1QFI6vnFNjOVaEIJnJK+wCH8C5/4McQmWaKu2PUFJiaLth5PnAJKmQUbCUxam4Er9 xq1Wcl5GumpnSf5+5dPMmGJ4YQg/m5oGgCRDFEOm+ED4D0hGbTt5GZny3C0jtbOLBCdEWU Vq/0YJ+UqHS1d02oAU/L7WKeNAvVQm8= X-MC-Unique: okVij2mgNz28zEDbivb8vw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 12/24] nbd: Prepare for 64-bit request effect lengths Date: Thu, 8 Jun 2023 08:56:41 -0500 Message-Id: <20230608135653.2918540-13-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686234405316100003 Content-Type: text/plain; charset="utf-8" Widen the length field of NBDRequest to 64-bits, although we can assert that all current uses are still under 32 bits, because nothing ever puts us into NBD_MODE_EXTENDED yet. Thus no semantic change. No semantic change yet. Signed-off-by: Eric Blake --- v4: split off enum changes to earlier patches [Vladimir] --- include/block/nbd.h | 4 ++-- block/nbd.c | 25 +++++++++++++++++++------ nbd/client.c | 1 + nbd/server.c | 11 ++++++++--- nbd/trace-events | 8 ++++---- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index f706e38dc72..dc05f5981fb 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -70,8 +70,8 @@ typedef enum NBDMode { */ typedef struct NBDRequest { uint64_t cookie; - uint64_t from; - uint32_t len; + uint64_t from; /* Offset touched by the command */ + uint64_t len; /* Effect length; 32 bit limit without extended header= s */ uint16_t flags; /* NBD_CMD_FLAG_* */ uint16_t type; /* NBD_CMD_* */ NBDMode mode; /* Determines which network representation to use */ diff --git a/block/nbd.c b/block/nbd.c index c7581794873..57123c17f94 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1306,10 +1306,11 @@ nbd_client_co_pwrite_zeroes(BlockDriverState *bs, i= nt64_t offset, int64_t bytes, NBDRequest request =3D { .type =3D NBD_CMD_WRITE_ZEROES, .from =3D offset, - .len =3D bytes, /* .len is uint32_t actually */ + .len =3D bytes, }; - assert(bytes <=3D UINT32_MAX); /* rely on max_pwrite_zeroes */ + /* rely on max_pwrite_zeroes */ + assert(bytes <=3D UINT32_MAX || s->info.mode >=3D NBD_MODE_EXTENDED); assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { @@ -1356,10 +1357,11 @@ nbd_client_co_pdiscard(BlockDriverState *bs, int64_= t offset, int64_t bytes) NBDRequest request =3D { .type =3D NBD_CMD_TRIM, .from =3D offset, - .len =3D bytes, /* len is uint32_t */ + .len =3D bytes, }; - assert(bytes <=3D UINT32_MAX); /* rely on max_pdiscard */ + /* rely on max_pdiscard */ + assert(bytes <=3D UINT32_MAX || s->info.mode >=3D NBD_MODE_EXTENDED); assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { @@ -1381,8 +1383,7 @@ static int coroutine_fn GRAPH_RDLOCK nbd_client_co_bl= ock_status( NBDRequest request =3D { .type =3D NBD_CMD_BLOCK_STATUS, .from =3D offset, - .len =3D MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment), - MIN(bytes, s->info.size - offset)), + .len =3D MIN(bytes, s->info.size - offset), .flags =3D NBD_CMD_FLAG_REQ_ONE, }; @@ -1392,6 +1393,10 @@ static int coroutine_fn GRAPH_RDLOCK nbd_client_co_b= lock_status( *file =3D bs; return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; } + if (s->info.mode < NBD_MODE_EXTENDED) { + request.len =3D MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignm= ent), + request.len); + } /* * Work around the fact that the block layer doesn't do @@ -1956,6 +1961,14 @@ static void nbd_refresh_limits(BlockDriverState *bs,= Error **errp) bs->bl.max_pwrite_zeroes =3D max; bs->bl.max_transfer =3D max; + /* + * Assume that if the server supports extended headers, it also + * supports unlimited size zero and trim commands. + */ + if (s->info.mode >=3D NBD_MODE_EXTENDED) { + bs->bl.max_pdiscard =3D bs->bl.max_pwrite_zeroes =3D 0; + } + if (s->info.opt_block && s->info.opt_block > bs->bl.opt_transfer) { bs->bl.opt_transfer =3D s->info.opt_block; diff --git a/nbd/client.c b/nbd/client.c index 40a1eb72346..1495a9b0ab1 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1355,6 +1355,7 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *req= uest) uint8_t buf[NBD_REQUEST_SIZE]; assert(request->mode <=3D NBD_MODE_STRUCTURED); /* TODO handle extende= d */ + assert(request->len <=3D UINT32_MAX); trace_nbd_send_request(request->from, request->len, request->cookie, request->flags, request->type, nbd_cmd_lookup(request->type)); diff --git a/nbd/server.c b/nbd/server.c index 9b16f7e5405..4ac05d0cd7b 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1439,7 +1439,7 @@ static int coroutine_fn nbd_receive_request(NBDClient= *client, NBDRequest *reque request->type =3D lduw_be_p(buf + 6); request->cookie =3D ldq_be_p(buf + 8); request->from =3D ldq_be_p(buf + 16); - request->len =3D ldl_be_p(buf + 24); + request->len =3D ldl_be_p(buf + 24); /* widen 32 to 64 bits */ trace_nbd_receive_request(magic, request->flags, request->type, request->from, request->len); @@ -2357,7 +2357,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * request->type =3D=3D NBD_CMD_CACHE) { if (request->len > NBD_MAX_BUFFER_SIZE) { - error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u= )", + error_setg(errp, "len (%" PRIu64" ) is larger than max len (%u= )", request->len, NBD_MAX_BUFFER_SIZE); return -EINVAL; } @@ -2373,6 +2373,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * } if (request->type =3D=3D NBD_CMD_WRITE) { + assert(request->len <=3D NBD_MAX_BUFFER_SIZE); if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data= ", errp) < 0) { @@ -2394,7 +2395,7 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * } if (request->from > client->exp->size || request->len > client->exp->size - request->from) { - error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" P= RIu32 + error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" P= RIu64 ", Size: %" PRIu64, request->from, request->len, client->exp->size); return (request->type =3D=3D NBD_CMD_WRITE || @@ -2456,6 +2457,7 @@ static coroutine_fn int nbd_do_cmd_read(NBDClient *cl= ient, NBDRequest *request, NBDExport *exp =3D client->exp; assert(request->type =3D=3D NBD_CMD_READ); + assert(request->len <=3D NBD_MAX_BUFFER_SIZE); /* XXX: NBD Protocol only documents use of FUA with WRITE */ if (request->flags & NBD_CMD_FLAG_FUA) { @@ -2506,6 +2508,7 @@ static coroutine_fn int nbd_do_cmd_cache(NBDClient *c= lient, NBDRequest *request, NBDExport *exp =3D client->exp; assert(request->type =3D=3D NBD_CMD_CACHE); + assert(request->len <=3D NBD_MAX_BUFFER_SIZE); ret =3D blk_co_preadv(exp->common.blk, request->from, request->len, NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH); @@ -2539,6 +2542,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, if (request->flags & NBD_CMD_FLAG_FUA) { flags |=3D BDRV_REQ_FUA; } + assert(request->len <=3D NBD_MAX_BUFFER_SIZE); ret =3D blk_co_pwrite(exp->common.blk, request->from, request->len= , data, flags); return nbd_send_generic_reply(client, request, ret, @@ -2582,6 +2586,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, return nbd_send_generic_reply(client, request, -EINVAL, "need non-zero length", errp); } + assert(request->len <=3D UINT32_MAX); if (client->export_meta.count) { bool dont_fragment =3D request->flags & NBD_CMD_FLAG_REQ_ONE; int contexts_remaining =3D client->export_meta.count; diff --git a/nbd/trace-events b/nbd/trace-events index f19a4d0db39..3338da2be2a 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -31,7 +31,7 @@ nbd_client_loop(void) "Doing NBD loop" nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" -nbd_send_request(uint64_t from, uint32_t len, uint64_t cookie, uint16_t fl= ags, uint16_t type, const char *name) "Sending request to server: { .from = =3D %" PRIu64", .len =3D %" PRIu32 ", .cookie =3D %" PRIu64 ", .flags =3D 0= x%" PRIx16 ", .type =3D %" PRIu16 " (%s) }" +nbd_send_request(uint64_t from, uint64_t len, uint64_t cookie, uint16_t fl= ags, uint16_t type, const char *name) "Sending request to server: { .from = =3D %" PRIu64", .len =3D %" PRIu64 ", .cookie =3D %" PRIu64 ", .flags =3D 0= x%" PRIx16 ", .type =3D %" PRIu16 " (%s) }" nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t cook= ie) "Got simple reply: { .error =3D %" PRId32 " (%s), cookie =3D %" PRIu64"= }" nbd_receive_structured_reply_chunk(uint16_t flags, uint16_t type, const ch= ar *name, uint64_t cookie, uint32_t length) "Got structured reply chunk: { = flags =3D 0x%" PRIx16 ", type =3D %d (%s), cookie =3D %" PRIu64 ", length = =3D %" PRIu32 " }" @@ -60,7 +60,7 @@ nbd_negotiate_options_check_option(uint32_t option, const= char *name) "Checking nbd_negotiate_begin(void) "Beginning negotiation" nbd_negotiate_new_style_size_flags(uint64_t size, unsigned flags) "adverti= sing size %" PRIu64 " and flags 0x%x" nbd_negotiate_success(void) "Negotiation succeeded" -nbd_receive_request(uint32_t magic, uint16_t flags, uint16_t type, uint64_= t from, uint32_t len) "Got request: { magic =3D 0x%" PRIx32 ", .flags =3D 0= x%" PRIx16 ", .type =3D 0x%" PRIx16 ", from =3D %" PRIu64 ", len =3D %" PRI= u32 " }" +nbd_receive_request(uint32_t magic, uint16_t flags, uint16_t type, uint64_= t from, uint64_t len) "Got request: { magic =3D 0x%" PRIx32 ", .flags =3D 0= x%" PRIx16 ", .type =3D 0x%" PRIx16 ", from =3D %" PRIu64 ", len =3D %" PRI= u64 " }" nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching cl= ients to AIO context %p" nbd_blk_aio_detach(const char *name, void *ctx) "Export %s: Detaching clie= nts from AIO context %p" nbd_co_send_simple_reply(uint64_t cookie, uint32_t error, const char *errn= ame, int len) "Send simple reply: cookie =3D %" PRIu64 ", error =3D %" PRIu= 32 " (%s), len =3D %d" @@ -70,8 +70,8 @@ nbd_co_send_chunk_read_hole(uint64_t cookie, uint64_t off= set, size_t size) "Send nbd_co_send_extents(uint64_t cookie, unsigned int extents, uint32_t id, ui= nt64_t length, int last) "Send block status reply: cookie =3D %" PRIu64 ", = extents =3D %u, context =3D %d (extents cover %" PRIu64 " bytes, last chunk= =3D %d)" nbd_co_send_chunk_error(uint64_t cookie, int err, const char *errname, con= st char *msg) "Send structured error reply: cookie =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" nbd_co_receive_request_decode_type(uint64_t cookie, uint16_t type, const c= har *name) "Decoding type: cookie =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" -nbd_co_receive_request_payload_received(uint64_t cookie, uint32_t len) "Pa= yload received: cookie =3D %" PRIu64 ", len =3D %" PRIu32 -nbd_co_receive_align_compliance(const char *op, uint64_t from, uint32_t le= n, uint32_t align) "client sent non-compliant unaligned %s request: from=3D= 0x%" PRIx64 ", len=3D0x%" PRIx32 ", align=3D0x%" PRIx32 +nbd_co_receive_request_payload_received(uint64_t cookie, uint64_t len) "Pa= yload received: cookie =3D %" PRIu64 ", len =3D %" PRIu64 +nbd_co_receive_align_compliance(const char *op, uint64_t from, uint64_t le= n, uint32_t align) "client sent non-compliant unaligned %s request: from=3D= 0x%" PRIx64 ", len=3D0x%" PRIx64 ", align=3D0x%" PRIx32 nbd_trip(void) "Reading request" # client-connection.c --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232690; cv=none; d=zohomail.com; s=zohoarc; b=Ozhmq+ruvBHnuzycN2i4+jHp2hCho9ePngvkarTrigxLiXkt3kGoVoaK7v1F6VM56ia34o4bgYtyR0NSwKs1aic0DNpxeIWDA9QteLFU490xz6IBjsowXzYsNmMT/ECJge2xFyrzVoz+6MZ2AYA1lh1o/BBJpeUBAq6QpeZ74wA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232690; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=97QsBZzgnr607Y5/HyhuSK8H15MqjCKg3VC/padbjqI=; b=jG5NSVrF+BhzSOa3vWxZ/ArvGVGZ0/NbvW8Xlktb9P/JZLa3a/LXpSyCa5ovPANTKsa6VPCGVpOkeuIJrs5gkEWV7mXO7BVpNtzT2jkAyvWrTmIKdlRvxOpM5HSsuUbU1JjNBlvuhVVwj3QVGpa6X+7dpt0tgaAygiI71ftf6X8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232690942702.9155679668523; Thu, 8 Jun 2023 06:58:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8z-0004JX-QS; Thu, 08 Jun 2023 09:57:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0003z5-1R for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8V-0004hc-2m for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:17 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-394-32mYy-o2OHCdO-BCTo2izw-1; Thu, 08 Jun 2023 09:57:06 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 646683C1C4CF; Thu, 8 Jun 2023 13:57:06 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id E713F492B00; Thu, 8 Jun 2023 13:57:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232630; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=97QsBZzgnr607Y5/HyhuSK8H15MqjCKg3VC/padbjqI=; b=LAFr3nynoWl4TMM+WuReX1aZ4U4LkHHism0qJiADjx1/ZPR7o90LcFzGRpm5iHRn+byaDK lrcbvKAxt45mUh6I7OYPLTatLKwbGfbIC04lOwI3qpncFUm1kdDuiDaXNMA3owOy6rrEdX Pr0HrztKgm019WoUadLCcpBzNwR5BSI= X-MC-Unique: 32mYy-o2OHCdO-BCTo2izw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 13/24] nbd/server: Refactor handling of request payload Date: Thu, 8 Jun 2023 08:56:42 -0500 Message-Id: <20230608135653.2918540-14-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232692747100015 Content-Type: text/plain; charset="utf-8" Upcoming additions to support NBD 64-bit effect lengths allow for the possibility to distinguish between payload length (capped at 32M) and effect length (up to 63 bits). Without that extension, only the NBD_CMD_WRITE request has a payload; but with the extension, it makes sense to allow at least NBD_CMD_BLOCK_STATUS to have both a payload and effect length (where the payload is a limited-size struct that in turns gives the real effect length as well as a subset of known ids for which status is requested). Other future NBD commands may also have a request payload, so the 64-bit extension introduces a new NBD_CMD_FLAG_PAYLOAD_LEN that distinguishes between whether the header length is a payload length or an effect length, rather than hard-coding the decision based on the command. Note that we do not support the payload version of BLOCK_STATUS yet. For this patch, no semantic change is intended for a compliant client. For a non-compliant client, it is possible that the error behavior changes (a different message, a change on whether the connection is killed or remains alive for the next command, or so forth), in part because req->complete is set later on some paths, but all errors should still be handled gracefully. Signed-off-by: Eric Blake --- v4: less indentation on several 'if's [Vladimir] --- nbd/server.c | 76 ++++++++++++++++++++++++++++++------------------ nbd/trace-events | 1 + 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 4ac05d0cd7b..d7dc29f0445 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2329,6 +2329,8 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * Error **errp) { NBDClient *client =3D req->client; + bool extended_with_payload; + unsigned payload_len =3D 0; int valid_flags; int ret; @@ -2342,48 +2344,63 @@ static int coroutine_fn nbd_co_receive_request(NBDR= equestData *req, NBDRequest * trace_nbd_co_receive_request_decode_type(request->cookie, request->typ= e, nbd_cmd_lookup(request->type)= ); - if (request->type !=3D NBD_CMD_WRITE) { - /* No payload, we are ready to read the next request. */ - req->complete =3D true; - } - if (request->type =3D=3D NBD_CMD_DISC) { /* Special case: we're going to disconnect without a reply, * whether or not flags, from, or len are bogus */ + req->complete =3D true; return -EIO; } - if (request->type =3D=3D NBD_CMD_READ || request->type =3D=3D NBD_CMD_= WRITE || - request->type =3D=3D NBD_CMD_CACHE) - { - if (request->len > NBD_MAX_BUFFER_SIZE) { - error_setg(errp, "len (%" PRIu64" ) is larger than max len (%u= )", - request->len, NBD_MAX_BUFFER_SIZE); - return -EINVAL; + /* Payload and buffer handling. */ + extended_with_payload =3D client->mode >=3D NBD_MODE_EXTENDED && + (request->flags & NBD_CMD_FLAG_PAYLOAD_LEN); + if ((request->type =3D=3D NBD_CMD_READ || request->type =3D=3D NBD_CMD= _WRITE || + request->type =3D=3D NBD_CMD_CACHE || extended_with_payload) && + request->len > NBD_MAX_BUFFER_SIZE) { + error_setg(errp, "len (%" PRIu64" ) is larger than max len (%u)", + request->len, NBD_MAX_BUFFER_SIZE); + return -EINVAL; + } + + if (request->type =3D=3D NBD_CMD_WRITE || extended_with_payload) { + payload_len =3D request->len; + if (request->type !=3D NBD_CMD_WRITE) { + /* + * For now, we don't support payloads on other + * commands; but we can keep the connection alive. + */ + request->len =3D 0; + } else if (client->mode >=3D NBD_MODE_EXTENDED && + !extended_with_payload) { + /* The client is noncompliant. Trace it, but proceed. */ + trace_nbd_co_receive_ext_payload_compliance(request->from, + request->len); } + } - if (request->type !=3D NBD_CMD_CACHE) { - req->data =3D blk_try_blockalign(client->exp->common.blk, - request->len); - if (req->data =3D=3D NULL) { - error_setg(errp, "No memory"); - return -ENOMEM; - } + if (request->type =3D=3D NBD_CMD_WRITE || request->type =3D=3D NBD_CMD= _READ) { + req->data =3D blk_try_blockalign(client->exp->common.blk, + request->len); + if (req->data =3D=3D NULL) { + error_setg(errp, "No memory"); + return -ENOMEM; } } - if (request->type =3D=3D NBD_CMD_WRITE) { - assert(request->len <=3D NBD_MAX_BUFFER_SIZE); - if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data= ", - errp) < 0) - { + if (payload_len) { + if (req->data) { + ret =3D nbd_read(client->ioc, req->data, payload_len, + "CMD_WRITE data", errp); + } else { + ret =3D nbd_drop(client->ioc, payload_len, errp); + } + if (ret < 0) { return -EIO; } - req->complete =3D true; - trace_nbd_co_receive_request_payload_received(request->cookie, - request->len); + payload_len); } + req->complete =3D true; /* Sanity checks. */ if (client->exp->nbdflags & NBD_FLAG_READ_ONLY && @@ -2413,7 +2430,10 @@ static int coroutine_fn nbd_co_receive_request(NBDRe= questData *req, NBDRequest * client->check_align); } valid_flags =3D NBD_CMD_FLAG_FUA; - if (request->type =3D=3D NBD_CMD_READ && client->mode >=3D NBD_MODE_ST= RUCTURED) { + if (request->type =3D=3D NBD_CMD_WRITE && client->mode >=3D NBD_MODE_E= XTENDED) { + valid_flags |=3D NBD_CMD_FLAG_PAYLOAD_LEN; + } else if (request->type =3D=3D NBD_CMD_READ && + client->mode >=3D NBD_MODE_STRUCTURED) { valid_flags |=3D NBD_CMD_FLAG_DF; } else if (request->type =3D=3D NBD_CMD_WRITE_ZEROES) { valid_flags |=3D NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO; diff --git a/nbd/trace-events b/nbd/trace-events index 3338da2be2a..6a34d7f027a 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -71,6 +71,7 @@ nbd_co_send_extents(uint64_t cookie, unsigned int extents= , uint32_t id, uint64_t nbd_co_send_chunk_error(uint64_t cookie, int err, const char *errname, con= st char *msg) "Send structured error reply: cookie =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" nbd_co_receive_request_decode_type(uint64_t cookie, uint16_t type, const c= har *name) "Decoding type: cookie =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" nbd_co_receive_request_payload_received(uint64_t cookie, uint64_t len) "Pa= yload received: cookie =3D %" PRIu64 ", len =3D %" PRIu64 +nbd_co_receive_ext_payload_compliance(uint64_t from, uint64_t len) "client= sent non-compliant write without payload flag: from=3D0x%" PRIx64 ", len= =3D0x%" PRIx64 nbd_co_receive_align_compliance(const char *op, uint64_t from, uint64_t le= n, uint32_t align) "client sent non-compliant unaligned %s request: from=3D= 0x%" PRIx64 ", len=3D0x%" PRIx64 ", align=3D0x%" PRIx32 nbd_trip(void) "Reading request" --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232737; cv=none; d=zohomail.com; s=zohoarc; b=YNV2LKqfgbVjnBTM44zBZMrbL7b4O6AttmYUYdy60GE0YznlIGMXX/NFXwOlLMRaKY19pl2HlK+JM9tUEVQvgp3xkCAN7CoY9BMJzwJFohSkr9eemtoH3tHXLqYoXcOPM+KyBoJ4IMtjTOTNyTGMmo8oYN4j7kiSgDJfwihK6NM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232737; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/8oYDvlvxL4c16KJ349yDzHzGMX4E0KXWBVSNuiMaZY=; b=BVkBVx0gT/arrGCdQST9ZDX6ydSLw9gOSr5Ea6SQZbE25lbek8viiRqJkPYtH5MhCe7TKanpMQEsNCGKt4LZ46D5tdmQU/No1SoX6kkOqn5lfAn378ObvZyB1pWTjSp8HRuYGsxbO1U/QmHPN6JZCJYzu08ijZeYLxfrwQ85wS8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232737801488.6465504843919; Thu, 8 Jun 2023 06:58:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8o-00046X-CP; Thu, 08 Jun 2023 09:57:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8g-0003yz-O4 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8U-0004h5-PU for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:15 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-ITeThgD5PNmrgIYMaz1Ksg-1; Thu, 08 Jun 2023 09:57:07 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0B568101B040; Thu, 8 Jun 2023 13:57:07 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97C91492B00; Thu, 8 Jun 2023 13:57:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232628; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/8oYDvlvxL4c16KJ349yDzHzGMX4E0KXWBVSNuiMaZY=; b=i7fcT+q+BqKLwingdCjPxFRQcucA2hZwxOLlBjzZzTVVla6VEW/FsKmJwi+T4zDXruHfYm mGxSJ1vvhHKh4jDoU1JGqr3mpwEPfoIFVhFh0e54qwJHohXLGDFh7v7L/yGu/XxXYnqBAK spjTVRB8P8X60r7pEiBJZ8shm1d1Gto= X-MC-Unique: ITeThgD5PNmrgIYMaz1Ksg-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 14/24] nbd/server: Prepare to receive extended header requests Date: Thu, 8 Jun 2023 08:56:43 -0500 Message-Id: <20230608135653.2918540-15-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232738883100007 Content-Type: text/plain; charset="utf-8" Although extended mode is not yet enabled, once we do turn it on, we need to accept extended requests for all messages. Previous patches have already taken care of supporting 64-bit lengths, now we just need to read it off the wire. Note that this implementation will block indefinitely on a buggy client that sends a non-extended payload (that is, we try to read a full packet before we ever check the magic number, but a client that mistakenly sends a simple request after negotiating extended headers doesn't send us enough bytes), but it's no different from any other client that stops talking to us partway through a packet and thus not worth coding around. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch, split out from v3 9/14 --- nbd/nbd-internal.h | 5 ++++- nbd/server.c | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index 133b1d94b50..dfa02f77ee4 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -34,8 +34,11 @@ * https://github.com/yoe/nbd/blob/master/doc/proto.md */ -/* Size of all NBD_OPT_*, without payload */ +/* Size of all compact NBD_CMD_*, without payload */ #define NBD_REQUEST_SIZE (4 + 2 + 2 + 8 + 8 + 4) +/* Size of all extended NBD_CMD_*, without payload */ +#define NBD_EXTENDED_REQUEST_SIZE (4 + 2 + 2 + 8 + 8 + 8) + /* Size of all NBD_REP_* sent in answer to most NBD_OPT_*, without payload= */ #define NBD_REPLY_SIZE (4 + 4 + 8) /* Size of reply to NBD_OPT_EXPORT_NAME */ diff --git a/nbd/server.c b/nbd/server.c index d7dc29f0445..119ac765f09 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1413,11 +1413,13 @@ nbd_read_eof(NBDClient *client, void *buffer, size_= t size, Error **errp) static int coroutine_fn nbd_receive_request(NBDClient *client, NBDRequest = *request, Error **errp) { - uint8_t buf[NBD_REQUEST_SIZE]; - uint32_t magic; + uint8_t buf[NBD_EXTENDED_REQUEST_SIZE]; + uint32_t magic, expect; int ret; + size_t size =3D client->mode >=3D NBD_MODE_EXTENDED ? + NBD_EXTENDED_REQUEST_SIZE : NBD_REQUEST_SIZE; - ret =3D nbd_read_eof(client, buf, sizeof(buf), errp); + ret =3D nbd_read_eof(client, buf, size, errp); if (ret < 0) { return ret; } @@ -1425,13 +1427,21 @@ static int coroutine_fn nbd_receive_request(NBDClie= nt *client, NBDRequest *reque return -EIO; } - /* Request - [ 0 .. 3] magic (NBD_REQUEST_MAGIC) - [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) - [ 6 .. 7] type (NBD_CMD_READ, ...) - [ 8 .. 15] cookie - [16 .. 23] from - [24 .. 27] len + /* + * Compact request + * [ 0 .. 3] magic (NBD_REQUEST_MAGIC) + * [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...) + * [ 6 .. 7] type (NBD_CMD_READ, ...) + * [ 8 .. 15] cookie + * [16 .. 23] from + * [24 .. 27] len + * Extended request + * [ 0 .. 3] magic (NBD_EXTENDED_REQUEST_MAGIC) + * [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, NBD_CMD_FLAG_PAYLOAD_LEN, = ...) + * [ 6 .. 7] type (NBD_CMD_READ, ...) + * [ 8 .. 15] cookie + * [16 .. 23] from + * [24 .. 31] len */ magic =3D ldl_be_p(buf); @@ -1439,13 +1449,20 @@ static int coroutine_fn nbd_receive_request(NBDClie= nt *client, NBDRequest *reque request->type =3D lduw_be_p(buf + 6); request->cookie =3D ldq_be_p(buf + 8); request->from =3D ldq_be_p(buf + 16); - request->len =3D ldl_be_p(buf + 24); /* widen 32 to 64 bits */ + if (client->mode >=3D NBD_MODE_EXTENDED) { + request->len =3D ldq_be_p(buf + 24); + expect =3D NBD_EXTENDED_REQUEST_MAGIC; + } else { + request->len =3D ldl_be_p(buf + 24); /* widen 32 to 64 bits */ + expect =3D NBD_REQUEST_MAGIC; + } trace_nbd_receive_request(magic, request->flags, request->type, request->from, request->len); - if (magic !=3D NBD_REQUEST_MAGIC) { - error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic); + if (magic !=3D expect) { + error_setg(errp, "invalid magic (got 0x%" PRIx32 ", expected 0x%" + PRIx32 ")", magic, expect); return -EINVAL; } return 0; --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686235101; cv=none; d=zohomail.com; s=zohoarc; b=hn7oc7h3zwupgMd5rus5+LX7zEqB09dvd00c3fl5d0q0A8PHKrjMdNidjsYESE6kSIltRzs+G7tbxzy1BXVpxqo/yugpzrsgwbmE2Tm7fPktWbjitHooPQeyg0Vg2oRzTjrrmHXBDkBvf0iTDOx/1UjjiCHj/hpmCyhnZK2WcPk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686235101; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3DYKItSwWR3iyEC+tg0viVd+KWSPt5/xGn1VBX3nNAk=; b=Qoot9eAob4yOY3k1hY2iv6lVuSEw8K7LlumYqZ0HjJEkQuBdlrm5gPNFo6QDrF9uFZ8a77rlcNzjyN7MI94IFpvvmEOW/mxykIIq1WczVAPwk18f1Vc4mOS8OIzsFyJQBXdgxq1PkGZL3t3Gry9zdLKiOo1zhNdLRgRNRNnKrYo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686235101110662.7289904256959; Thu, 8 Jun 2023 07:38:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8v-0004Al-2K; Thu, 08 Jun 2023 09:57:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0003zD-30 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8W-0004ij-3j for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:18 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-141-M1TeW60WNMGZFsCPIhZM-w-1; Thu, 08 Jun 2023 09:57:08 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9CCA43C1C4DF; Thu, 8 Jun 2023 13:57:07 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 350A7492B00; Thu, 8 Jun 2023 13:57:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3DYKItSwWR3iyEC+tg0viVd+KWSPt5/xGn1VBX3nNAk=; b=AjPwdGaclFCJ9SA2lYVfCPdhLGeIU1F9DHBr3J7Xo2BP0919LCNHchPkPK0JbUdMtuUr33 mDmeMd/TN2vRn0emqg0HH5INBQH+yVbj3OwmJ/2Qld+HvtyxpnmXwSK9mLWl3fcb79npUa pefCeuhWYmh+G/YmoLPsX9LnfOQbaGk= X-MC-Unique: M1TeW60WNMGZFsCPIhZM-w-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 15/24] nbd/server: Prepare to send extended header replies Date: Thu, 8 Jun 2023 08:56:44 -0500 Message-Id: <20230608135653.2918540-16-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686235106274100001 Content-Type: text/plain; charset="utf-8" Although extended mode is not yet enabled, once we do turn it on, we need to reply with extended headers to all messages. Update the low level entry points necessary so that all other callers automatically get the right header based on the current mode. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch, split out from v3 9/14 --- nbd/server.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 119ac765f09..84c848a31d3 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1947,8 +1947,6 @@ static inline void set_be_chunk(NBDClient *client, st= ruct iovec *iov, size_t niov, uint16_t flags, uint16_t type, NBDRequest *request) { - /* TODO - handle structured vs. extended replies */ - NBDStructuredReplyChunk *chunk =3D iov->iov_base; size_t i, length =3D 0; for (i =3D 1; i < niov; i++) { @@ -1956,12 +1954,26 @@ static inline void set_be_chunk(NBDClient *client, = struct iovec *iov, } assert(length <=3D NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData)= ); - iov[0].iov_len =3D sizeof(*chunk); - stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); - stw_be_p(&chunk->flags, flags); - stw_be_p(&chunk->type, type); - stq_be_p(&chunk->cookie, request->cookie); - stl_be_p(&chunk->length, length); + if (client->mode >=3D NBD_MODE_EXTENDED) { + NBDExtendedReplyChunk *chunk =3D iov->iov_base; + + iov->iov_len =3D sizeof(*chunk); + stl_be_p(&chunk->magic, NBD_EXTENDED_REPLY_MAGIC); + stw_be_p(&chunk->flags, flags); + stw_be_p(&chunk->type, type); + stq_be_p(&chunk->cookie, request->cookie); + stq_be_p(&chunk->offset, request->from); + stq_be_p(&chunk->length, length); + } else { + NBDStructuredReplyChunk *chunk =3D iov->iov_base; + + iov->iov_len =3D sizeof(*chunk); + stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC); + stw_be_p(&chunk->flags, flags); + stw_be_p(&chunk->type, type); + stq_be_p(&chunk->cookie, request->cookie); + stl_be_p(&chunk->length, length); + } } static int coroutine_fn nbd_co_send_chunk_done(NBDClient *client, @@ -2478,6 +2490,8 @@ static coroutine_fn int nbd_send_generic_reply(NBDCli= ent *client, { if (client->mode >=3D NBD_MODE_STRUCTURED && ret < 0) { return nbd_co_send_chunk_error(client, request, -ret, error_msg, e= rrp); + } else if (client->mode >=3D NBD_MODE_EXTENDED) { + return nbd_co_send_chunk_done(client, request, errp); } else { return nbd_co_send_simple_reply(client, request, ret < 0 ? -ret : = 0, NULL, 0, errp); --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232736; cv=none; d=zohomail.com; s=zohoarc; b=bYw+B/VdbSX12U/pFj5JpQwtYxeI2094rHyB8qEN/+sWhthtu/MDgIRmwhR7NN3lVVBFuL35x9rgY2DLCQFep5eDwRkvFKyic+C4f0As0IERnFcbEV092Mv6XN7gwqIqYRJKouHEZjk5YO166DfSEQHY92svoeM8lZA1Mg+rzVE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232736; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=b4KTHIdASJFojj3Hj/Jrck+pACsUu7CtkXPfpgmPlzc=; b=LumD5+MwNdqCu7BhCdDLxvpqc08Jsqqg9/FIyBhYTc5h41HgtwR6XhS32h4RinnFZdMnSlj5C0IIJ6JrVzPr/orHX9D83g/r3uLgjHo5geN/42ZiH6iE5KtU0JTy7yByYmDO1tVMIgRT38+7OdmputVFM+WynVsTwjZVEU8ioAc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232736202879.6022962256063; Thu, 8 Jun 2023 06:58:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G93-0004MC-Qh; Thu, 08 Jun 2023 09:57:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0003z7-2n for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0004jl-Dw for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:20 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-171-Y4hJ3F6vNLy4N-ZFzohTuA-1; Thu, 08 Jun 2023 09:57:08 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 459BF1C0942A; Thu, 8 Jun 2023 13:57:08 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF364492B00; Thu, 8 Jun 2023 13:57:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b4KTHIdASJFojj3Hj/Jrck+pACsUu7CtkXPfpgmPlzc=; b=dDRmHiswdYQxr61tDMDHoLaRD4Bc2aoL7sUAc3+683N+KcOhRwRONsOpCgZsb4HzEuo5pK RMfKAHzuDyswsi8IZ5ujszjTv+1Xi5ktA1PAwMyTygMS9EvO8V2+afH5vwyBB7NWwNod5W pXuSkVW87utF1f77vLcSUXdh9tUQBfM= X-MC-Unique: Y4hJ3F6vNLy4N-ZFzohTuA-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 16/24] nbd/server: Support 64-bit block status Date: Thu, 8 Jun 2023 08:56:45 -0500 Message-Id: <20230608135653.2918540-17-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232736789100001 Content-Type: text/plain; charset="utf-8" The NBD spec states that if the client negotiates extended headers, the server must avoid NBD_REPLY_TYPE_BLOCK_STATUS and instead use NBD_REPLY_TYPE_BLOCK_STATUS_EXT which supports 64-bit lengths, even if the reply does not need more than 32 bits. As of this patch, client->mode is still never NBD_MODE_EXTENDED, so the code added here does not take effect until the next patch enables negotiation. For now, all metacontexts that we know how to export never populate more than 32 bits of information, so we don't have to worry about NBD_REP_ERR_EXT_HEADER_REQD or filtering during handshake, and we always send all zeroes for the upper 32 bits of status during NBD_CMD_BLOCK_STATUS. Note that we previously had some interesting size-juggling on call chains, such as: nbd_co_send_block_status(uint32_t length) -> blockstatus_to_extents(uint32_t bytes) -> bdrv_block_status_above(bytes, &uint64_t num) -> nbd_extent_array_add(uint64_t num) -> store num in 32-bit length But we were lucky that it never overflowed: bdrv_block_status_above never sets num larger than bytes, and we had previously been capping 'bytes' at 32 bits (since the protocol does not allow sending a larger request without extended headers). This patch adds some assertions that ensure we continue to avoid overflowing 32 bits for a narrow client, while fully utilizing 64-bits all the way through when the client understands that. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: split conversion to big-endian across two helper functions rather than in-place union [Vladimir] --- nbd/server.c | 104 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 26 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 84c848a31d3..3010ff0dca4 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2111,20 +2111,24 @@ static int coroutine_fn nbd_co_send_sparse_read(NBD= Client *client, } typedef struct NBDExtentArray { - NBDExtent32 *extents; + NBDExtent64 *extents; unsigned int nb_alloc; unsigned int count; uint64_t total_length; + bool extended; bool can_add; bool converted_to_be; } NBDExtentArray; -static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc) +static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc, + NBDMode mode) { NBDExtentArray *ea =3D g_new0(NBDExtentArray, 1); + assert(mode >=3D NBD_MODE_STRUCTURED); ea->nb_alloc =3D nb_alloc; - ea->extents =3D g_new(NBDExtent32, nb_alloc); + ea->extents =3D g_new(NBDExtent64, nb_alloc); + ea->extended =3D mode >=3D NBD_MODE_EXTENDED; ea->can_add =3D true; return ea; @@ -2143,15 +2147,36 @@ static void nbd_extent_array_convert_to_be(NBDExten= tArray *ea) int i; assert(!ea->converted_to_be); + assert(ea->extended); ea->can_add =3D false; ea->converted_to_be =3D true; for (i =3D 0; i < ea->count; i++) { - ea->extents[i].flags =3D cpu_to_be32(ea->extents[i].flags); - ea->extents[i].length =3D cpu_to_be32(ea->extents[i].length); + ea->extents[i].length =3D cpu_to_be64(ea->extents[i].length); + ea->extents[i].flags =3D cpu_to_be64(ea->extents[i].flags); } } +/* Further modifications of the array after conversion are abandoned */ +static NBDExtent32 *nbd_extent_array_convert_to_narrow(NBDExtentArray *ea) +{ + int i; + NBDExtent32 *extents =3D g_new(NBDExtent32, ea->count); + + assert(!ea->converted_to_be); + assert(!ea->extended); + ea->can_add =3D false; + ea->converted_to_be =3D true; + + for (i =3D 0; i < ea->count; i++) { + assert((ea->extents[i].length | ea->extents[i].flags) <=3D UINT32_= MAX); + extents[i].length =3D cpu_to_be32(ea->extents[i].length); + extents[i].flags =3D cpu_to_be32(ea->extents[i].flags); + } + + return extents; +} + /* * Add extent to NBDExtentArray. If extent can't be added (no available sp= ace), * return -1. @@ -2162,19 +2187,23 @@ static void nbd_extent_array_convert_to_be(NBDExten= tArray *ea) * would result in an incorrect range reported to the client) */ static int nbd_extent_array_add(NBDExtentArray *ea, - uint32_t length, uint32_t flags) + uint64_t length, uint32_t flags) { assert(ea->can_add); if (!length) { return 0; } + if (!ea->extended) { + assert(length <=3D UINT32_MAX); + } /* Extend previous extent if flags are the same */ if (ea->count > 0 && flags =3D=3D ea->extents[ea->count - 1].flags) { - uint64_t sum =3D (uint64_t)length + ea->extents[ea->count - 1].len= gth; + uint64_t sum =3D length + ea->extents[ea->count - 1].length; - if (sum <=3D UINT32_MAX) { + assert(sum >=3D length); + if (sum <=3D UINT32_MAX || ea->extended) { ea->extents[ea->count - 1].length =3D sum; ea->total_length +=3D length; return 0; @@ -2187,7 +2216,7 @@ static int nbd_extent_array_add(NBDExtentArray *ea, } ea->total_length +=3D length; - ea->extents[ea->count] =3D (NBDExtent32) {.length =3D length, .flags = =3D flags}; + ea->extents[ea->count] =3D (NBDExtent64) {.length =3D length, .flags = =3D flags}; ea->count++; return 0; @@ -2256,20 +2285,39 @@ nbd_co_send_extents(NBDClient *client, NBDRequest *= request, NBDExtentArray *ea, bool last, uint32_t context_id, Error **errp) { NBDReply hdr; - NBDStructuredMeta chunk; - struct iovec iov[] =3D { - {.iov_base =3D &hdr}, - {.iov_base =3D &chunk, .iov_len =3D sizeof(chunk)}, - {.iov_base =3D ea->extents, .iov_len =3D ea->count * sizeof(ea->ex= tents[0])} - }; - - nbd_extent_array_convert_to_be(ea); + NBDStructuredMeta meta; + NBDExtendedMeta meta_ext; + g_autofree NBDExtent32 *extents =3D NULL; + uint16_t type; + struct iovec iov[] =3D { {.iov_base =3D &hdr}, {0}, {0} }; + + if (client->mode >=3D NBD_MODE_EXTENDED) { + type =3D NBD_REPLY_TYPE_BLOCK_STATUS_EXT; + + iov[1].iov_base =3D &meta_ext; + iov[1].iov_len =3D sizeof(meta_ext); + stl_be_p(&meta_ext.context_id, context_id); + stl_be_p(&meta_ext.count, ea->count); + + nbd_extent_array_convert_to_be(ea); + iov[2].iov_base =3D ea->extents; + iov[2].iov_len =3D ea->count * sizeof(ea->extents[0]); + } else { + type =3D NBD_REPLY_TYPE_BLOCK_STATUS; + + iov[1].iov_base =3D &meta; + iov[1].iov_len =3D sizeof(meta); + stl_be_p(&meta.context_id, context_id); + + extents =3D nbd_extent_array_convert_to_narrow(ea); + iov[2].iov_base =3D extents; + iov[2].iov_len =3D ea->count * sizeof(extents[0]); + } trace_nbd_co_send_extents(request->cookie, ea->count, context_id, ea->total_length, last); - set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, - NBD_REPLY_TYPE_BLOCK_STATUS, request); - stl_be_p(&chunk.context_id, context_id); + set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, type, + request); return nbd_co_send_iov(client, iov, 3, errp); } @@ -2278,13 +2326,14 @@ nbd_co_send_extents(NBDClient *client, NBDRequest *= request, NBDExtentArray *ea, static int coroutine_fn nbd_co_send_block_status(NBDClient *client, NBDRequest *reque= st, BlockBackend *blk, uint64_t offset, - uint32_t length, bool dont_fragment, + uint64_t length, bool dont_fragment, bool last, uint32_t context_id, Error **errp) { int ret; unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; - g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); + g_autoptr(NBDExtentArray) ea =3D + nbd_extent_array_new(nb_extents, client->mode); if (context_id =3D=3D NBD_META_ID_BASE_ALLOCATION) { ret =3D blockstatus_to_extents(blk, offset, length, ea); @@ -2307,11 +2356,12 @@ static void bitmap_to_extents(BdrvDirtyBitmap *bitm= ap, int64_t start, dirty_start, dirty_count; int64_t end =3D offset + length; bool full =3D false; + int64_t bound =3D es->extended ? INT64_MAX : INT32_MAX; bdrv_dirty_bitmap_lock(bitmap); for (start =3D offset; - bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX, + bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, bound, &dirty_start, &dirty_count); start =3D dirty_start + dirty_count) { @@ -2335,12 +2385,13 @@ static int coroutine_fn nbd_co_send_bitmap(NBDClien= t *client, NBDRequest *request, BdrvDirtyBitmap *bitmap, uint64_t offset, - uint32_t length, bool dont_frag= ment, + uint64_t length, bool dont_frag= ment, bool last, uint32_t context_id, Error **errp) { unsigned int nb_extents =3D dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_E= XTENTS; - g_autoptr(NBDExtentArray) ea =3D nbd_extent_array_new(nb_extents); + g_autoptr(NBDExtentArray) ea =3D + nbd_extent_array_new(nb_extents, client->mode); bitmap_to_extents(bitmap, offset, length, ea); @@ -2637,7 +2688,8 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, return nbd_send_generic_reply(client, request, -EINVAL, "need non-zero length", errp); } - assert(request->len <=3D UINT32_MAX); + assert(client->mode >=3D NBD_MODE_EXTENDED || + request->len <=3D UINT32_MAX); if (client->export_meta.count) { bool dont_fragment =3D request->flags & NBD_CMD_FLAG_REQ_ONE; int contexts_remaining =3D client->export_meta.count; --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232690; cv=none; d=zohomail.com; s=zohoarc; b=hMrHqk+NUCndJHS0ccOah5wwit498kfcjzeBlK3r1/tb7PN1gMgudINFtYH37+TOjb/BJnK3FNEnWP25SXdqbmjvJOfBjG3fDbcwGDewD2cHMegg/gAg8fWr99u1XqbgHLdIpCoIYudXxVIs924OTiAT+C3bFOyLD8VP47BEaXQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232690; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=LJ/olBH4Z4JeCmlSKSb6yFt7DuZ6I8b+Ng3vz5BNxtQ=; b=Ab0sso1EdKSFajJo0S47kFtCX/pZSHTmS3KArP8NDHWrf09uUuTZ7ykXMUU+DY1o6Pvf5tY/kpSsNCtW72iGzK9thFTLaFzuK/Fd8u4OURW62AdPEOoYRlEjIhIHuGU5m5J52zEIWeDomM3vgrhPUDkcFMHX0Sdqw7qAMU7olJk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232690783507.5047863020351; Thu, 8 Jun 2023 06:58:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8o-00046Z-Gw; Thu, 08 Jun 2023 09:57:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0003z3-10 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0004jT-9C for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:18 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-187-U6tic5yUMI6Il-BQV0Hpxw-1; Thu, 08 Jun 2023 09:57:09 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC01A811E86; Thu, 8 Jun 2023 13:57:08 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 70AF3492B00; Thu, 8 Jun 2023 13:57:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LJ/olBH4Z4JeCmlSKSb6yFt7DuZ6I8b+Ng3vz5BNxtQ=; b=cGTnqG0EU+aZQTMqfhrBUJy7Hlqiv6YxalC4Thi+VOAh6oGOf7UUBzGsOMx9z7e5l7xx/o 7z2FGgsJHz9jH3O9cO7dICM7xp/dph06jiIFvPHO1goi1gUudRVkwNWuGGQiDOiW/zhHoS UZXSHs21hl0/vRa/HEmmyxdGXhBNZHI= X-MC-Unique: U6tic5yUMI6Il-BQV0Hpxw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru Subject: [PATCH v4 17/24] nbd/server: Enable initial support for extended headers Date: Thu, 8 Jun 2023 08:56:46 -0500 Message-Id: <20230608135653.2918540-18-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232692558100011 Content-Type: text/plain; charset="utf-8" Time to start supporting clients that request extended headers. Now we can finally reach the code added across several previous patches. Even though the NBD spec has been altered to allow us to accept NBD_CMD_READ larger than the max payload size (provided our response is a hole or broken up over more than one data chunk), we are not planning to take advantage of that, and continue to cap NBD_CMD_READ to 32M regardless of header size. For NBD_CMD_WRITE_ZEROES and NBD_CMD_TRIM, the block layer already supports 64-bit operations without any effort on our part. For NBD_CMD_BLOCK_STATUS, the client's length is a hint, and the previous patch took care of implementing the required NBD_REPLY_TYPE_BLOCK_STATUS_EXT. We do not yet support clients that want to do request payload filtering of NBD_CMD_BLOCK_STATUS; that will be added in later patches, but is not essential for qemu as a client since qemu only requests the single context base:allocation. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: split out parts into earlier patches, rebase to earlier changes, simplify handling of generic replies, retitle (compare to v3 9/14) --- docs/interop/nbd.txt | 1 + nbd/server.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/interop/nbd.txt b/docs/interop/nbd.txt index f5ca25174a6..abaf4c28a96 100644 --- a/docs/interop/nbd.txt +++ b/docs/interop/nbd.txt @@ -69,3 +69,4 @@ NBD_CMD_BLOCK_STATUS for "qemu:dirty-bitmap:", NBD_CMD_CA= CHE NBD_CMD_FLAG_FAST_ZERO * 5.2: NBD_CMD_BLOCK_STATUS for "qemu:allocation-depth" * 7.1: NBD_FLAG_CAN_MULTI_CONN for shareable writable exports +* 8.1: NBD_OPT_EXTENDED_HEADERS diff --git a/nbd/server.c b/nbd/server.c index 3010ff0dca4..ae293663ca2 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -482,6 +482,10 @@ static int nbd_negotiate_handle_export_name(NBDClient = *client, bool no_zeroes, [10 .. 133] reserved (0) [unless no_zeroes] */ trace_nbd_negotiate_handle_export_name(); + if (client->mode >=3D NBD_MODE_EXTENDED) { + error_setg(errp, "Extended headers already negotiated"); + return -EINVAL; + } if (client->optlen > NBD_MAX_STRING_SIZE) { error_setg(errp, "Bad length received"); return -EINVAL; @@ -1262,6 +1266,10 @@ static int nbd_negotiate_options(NBDClient *client, = Error **errp) case NBD_OPT_STRUCTURED_REPLY: if (length) { ret =3D nbd_reject_length(client, false, errp); + } else if (client->mode >=3D NBD_MODE_EXTENDED) { + ret =3D nbd_negotiate_send_rep_err( + client, NBD_REP_ERR_EXT_HEADER_REQD, errp, + "extended headers already negotiated"); } else if (client->mode >=3D NBD_MODE_STRUCTURED) { ret =3D nbd_negotiate_send_rep_err( client, NBD_REP_ERR_INVALID, errp, @@ -1278,6 +1286,19 @@ static int nbd_negotiate_options(NBDClient *client, = Error **errp) errp); break; + case NBD_OPT_EXTENDED_HEADERS: + if (length) { + ret =3D nbd_reject_length(client, false, errp); + } else if (client->mode >=3D NBD_MODE_EXTENDED) { + ret =3D nbd_negotiate_send_rep_err( + client, NBD_REP_ERR_INVALID, errp, + "extended headers already negotiated"); + } else { + ret =3D nbd_negotiate_send_rep(client, NBD_REP_ACK, er= rp); + client->mode =3D NBD_MODE_EXTENDED; + } + break; + default: ret =3D nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp, "Unsupported option %" PRIu32 " (%s)", --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232917; cv=none; d=zohomail.com; s=zohoarc; b=KoMfCzbgQjOUn+3mapMWJyJqczw0/0M0SwbqwoNr6c7dgHQmFFDXBaYmSIfHESN+DZnkTq80JqO+eniIbdE+y8iRI70y50BUSp9f02cGVTtGYNSURnZaCpkn3MmIFi7V7MAr1PXLdvY1CbvXMARLBsY9TWajZ/cA6kCNHIRyvNA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232917; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kXlO+2va6l3lJFccCzHTil3AsL3vWsyS3WIIcDGVh0Y=; b=RDxxVcweKS3sj2+0CVFRq8id0gJmm5S1QOJLZ8AhftFJKgyL6HLDbHF9R4JblUpBzPmjOdkS580u94tZjXsaOfK3RqGG4b2rFglqSJKUO97rJA1UnT+LooGdt96bGTaRr3U0gphk+EG9ng+05wCvtQmNFMFw6/1kv5uesZJHuLg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232917613317.6493079532722; Thu, 8 Jun 2023 07:01:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8x-0004EH-Jd; Thu, 08 Jun 2023 09:57:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8i-00041Q-9h for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0004jv-JF for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:23 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-140-1OHfx_KjPfOuTq2ZnnIkPQ-1; Thu, 08 Jun 2023 09:57:10 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A6ECE80231B; Thu, 8 Jun 2023 13:57:09 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 18A6A492B00; Thu, 8 Jun 2023 13:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kXlO+2va6l3lJFccCzHTil3AsL3vWsyS3WIIcDGVh0Y=; b=DennrWKNHI8cLcIdxq/mgkvxNgT3eu5tMdRklRno720Z/VGLmV8bzOMh8uFX/qL46tVE2T ggk0iAx+9lw3mOrjX3qjBvT3H4OzUBAVffaZ95rybjcuZ+vWk8ho/dXgtr+n2zDrfbf0ri R6XGgqZXJxaEZHC4rt5brIZEnrudWv8= X-MC-Unique: 1OHfx_KjPfOuTq2ZnnIkPQ-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 18/24] nbd/client: Plumb errp through nbd_receive_replies Date: Thu, 8 Jun 2023 08:56:47 -0500 Message-Id: <20230608135653.2918540-19-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232920243100001 Content-Type: text/plain; charset="utf-8" Instead of ignoring the low-level error just to refabricate our own message to pass to the caller, we can just plump the caller's errp down to the low level. Signed-off-by: Eric Blake --- v4: new patch [Vladimir] --- block/nbd.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 57123c17f94..c17ce935f17 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -417,7 +417,8 @@ static void coroutine_fn GRAPH_RDLOCK nbd_reconnect_att= empt(BDRVNBDState *s) reconnect_delay_timer_del(s); } -static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cook= ie) +static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t cook= ie, + Error **errp) { int ret; uint64_t ind =3D COOKIE_TO_INDEX(cookie), ind2; @@ -458,9 +459,12 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDSta= te *s, uint64_t cookie) /* We are under mutex and cookie is 0. We have to do the dirty wor= k. */ assert(s->reply.cookie =3D=3D 0); - ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, NULL); - if (ret <=3D 0) { - ret =3D ret ? ret : -EIO; + ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, errp); + if (ret =3D=3D 0) { + ret =3D -EIO; + error_setg(errp, "server dropped connection"); + } + if (ret < 0) { nbd_channel_error(s, ret); return ret; } @@ -843,9 +847,9 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( } *request_ret =3D 0; - ret =3D nbd_receive_replies(s, cookie); + ret =3D nbd_receive_replies(s, cookie, errp); if (ret < 0) { - error_setg(errp, "Connection closed"); + error_prepend(errp, "Connection closed: "); return -EIO; } assert(s->ioc); --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232795; cv=none; d=zohomail.com; s=zohoarc; b=Ko6n7qr33mlcSIAh60EY9gRFfpatFYYtgoeo2YQB9hD7fy4epNmhxXpjjHmuKxuVWYois4NF56l90vIjPp1v3lj2DQr7L9Z/jb2thAZp/iXprCoNtJ0BBVXpSqIKDYJnEnKQQFIaLFYZGqlQ4ths2YtxoG2Oa+pxrh8IyiSc2a4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232795; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=sOrnqo2mMWG+ndEnCDdN3wGZ9Eu5sGtjFX7QG6sSKVc=; b=H0u8gZfrDbNj4sAj5omq2ApNwUY7uzyo2urDvQLewJ2EjwebQYL2zsrYXUUctoqaOumZXtcXIm1B4LK6nOWvCv+Zweqq5OMxuxiwXoC1ozdvQENkqHeYELQsgan8zhlRvDXqNE2+5XFos/EqA3qFXYqOXcRZNaucKg61RbtoJkU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232795968119.55578060389382; Thu, 8 Jun 2023 06:59:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8v-0004BD-E2; Thu, 08 Jun 2023 09:57:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8k-00042l-0p for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8g-0004kC-PM for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:25 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-271-VUMeTBREMl2jmIfVl9bVrQ-1; Thu, 08 Jun 2023 09:57:11 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7611E185A7AD; Thu, 8 Jun 2023 13:57:10 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0403492B00; Thu, 8 Jun 2023 13:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sOrnqo2mMWG+ndEnCDdN3wGZ9Eu5sGtjFX7QG6sSKVc=; b=hGSbxObM0ahcaKXUy/JqiwsyBxxNZAtcBGk2LWQC0grvguFQfwEe7lxCqJrDvnnNalHoZR 9qE6LHGy8YTOEZl3qL/BWPti2BFVSHPYc6gat0IwKk5MXlIJgfwqITASuxflEHXq6HIj0a y0H31g9WtQy2MGaFLyNhXtx4UgyUu6s= X-MC-Unique: VUMeTBREMl2jmIfVl9bVrQ-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 19/24] nbd/client: Initial support for extended headers Date: Thu, 8 Jun 2023 08:56:48 -0500 Message-Id: <20230608135653.2918540-20-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232797208100003 Content-Type: text/plain; charset="utf-8" Update the client code to be able to send an extended request, and parse an extended header from the server. Note that since we reject any structured reply with a too-large payload, we can always normalize a valid header back into the compact form, so that the caller need not deal with two branches of a union. Still, until a later patch lets the client negotiate extended headers, the code added here should not be reached. Note that because of the different magic numbers, it is just as easy to trace and then tolerate a non-compliant server sending the wrong header reply as it would be to insist that the server is compliant. Signed-off-by: Eric Blake --- v4: split off errp handling to separate patch [Vladimir], better function naming [Vladimir] --- include/block/nbd.h | 3 +- block/nbd.c | 2 +- nbd/client.c | 100 +++++++++++++++++++++++++++++--------------- nbd/trace-events | 3 +- 4 files changed, 72 insertions(+), 36 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index dc05f5981fb..af80087e2cd 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -389,7 +389,8 @@ int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportI= nfo *info, Error **errp); int nbd_send_request(QIOChannel *ioc, NBDRequest *request); int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, - NBDReply *reply, Error **errp); + NBDReply *reply, NBDMode mode, + Error **errp); int nbd_client(int fd); int nbd_disconnect(int fd); int nbd_errno_to_system_errno(int err); diff --git a/block/nbd.c b/block/nbd.c index c17ce935f17..e281fac43d1 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -459,7 +459,7 @@ static coroutine_fn int nbd_receive_replies(BDRVNBDStat= e *s, uint64_t cookie, /* We are under mutex and cookie is 0. We have to do the dirty wor= k. */ assert(s->reply.cookie =3D=3D 0); - ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, errp); + ret =3D nbd_receive_reply(s->bs, s->ioc, &s->reply, s->info.mode, = errp); if (ret =3D=3D 0) { ret =3D -EIO; error_setg(errp, "server dropped connection"); diff --git a/nbd/client.c b/nbd/client.c index 1495a9b0ab1..a4598a95427 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -1352,22 +1352,29 @@ int nbd_disconnect(int fd) int nbd_send_request(QIOChannel *ioc, NBDRequest *request) { - uint8_t buf[NBD_REQUEST_SIZE]; + uint8_t buf[NBD_EXTENDED_REQUEST_SIZE]; + size_t len; - assert(request->mode <=3D NBD_MODE_STRUCTURED); /* TODO handle extende= d */ - assert(request->len <=3D UINT32_MAX); trace_nbd_send_request(request->from, request->len, request->cookie, request->flags, request->type, nbd_cmd_lookup(request->type)); - stl_be_p(buf, NBD_REQUEST_MAGIC); stw_be_p(buf + 4, request->flags); stw_be_p(buf + 6, request->type); stq_be_p(buf + 8, request->cookie); stq_be_p(buf + 16, request->from); - stl_be_p(buf + 24, request->len); + if (request->mode >=3D NBD_MODE_EXTENDED) { + stl_be_p(buf, NBD_EXTENDED_REQUEST_MAGIC); + stq_be_p(buf + 24, request->len); + len =3D NBD_EXTENDED_REQUEST_SIZE; + } else { + assert(request->len <=3D UINT32_MAX); + stl_be_p(buf, NBD_REQUEST_MAGIC); + stl_be_p(buf + 24, request->len); + len =3D NBD_REQUEST_SIZE; + } - return nbd_write(ioc, buf, sizeof(buf), NULL); + return nbd_write(ioc, buf, len, NULL); } /* nbd_receive_simple_reply @@ -1394,30 +1401,36 @@ static int nbd_receive_simple_reply(QIOChannel *ioc= , NBDSimpleReply *reply, return 0; } -/* nbd_receive_structured_reply_chunk +/* nbd_receive_reply_chunk_header * Read structured reply chunk except magic field (which should be already - * read). + * read). Normalize into the compact form. * Payload is not read. */ -static int nbd_receive_structured_reply_chunk(QIOChannel *ioc, - NBDStructuredReplyChunk *chu= nk, - Error **errp) +static int nbd_receive_reply_chunk_header(QIOChannel *ioc, NBDReply *chunk, + Error **errp) { int ret; + size_t len; + uint64_t payload_len; - assert(chunk->magic =3D=3D NBD_STRUCTURED_REPLY_MAGIC); + if (chunk->magic =3D=3D NBD_STRUCTURED_REPLY_MAGIC) { + len =3D sizeof(chunk->structured); + } else { + assert(chunk->magic =3D=3D NBD_EXTENDED_REPLY_MAGIC); + len =3D sizeof(chunk->extended); + } ret =3D nbd_read(ioc, (uint8_t *)chunk + sizeof(chunk->magic), - sizeof(*chunk) - sizeof(chunk->magic), "structured chun= k", + len - sizeof(chunk->magic), "structured chunk", errp); if (ret < 0) { return ret; } - chunk->flags =3D be16_to_cpu(chunk->flags); - chunk->type =3D be16_to_cpu(chunk->type); - chunk->cookie =3D be64_to_cpu(chunk->cookie); - chunk->length =3D be32_to_cpu(chunk->length); + /* flags, type, and cookie occupy same space between forms */ + chunk->structured.flags =3D be16_to_cpu(chunk->structured.flags); + chunk->structured.type =3D be16_to_cpu(chunk->structured.type); + chunk->structured.cookie =3D be64_to_cpu(chunk->structured.cookie); /* * Because we use BLOCK_STATUS with REQ_ONE, and cap READ requests @@ -1425,11 +1438,20 @@ static int nbd_receive_structured_reply_chunk(QIOCh= annel *ioc, * this. Even if we stopped using REQ_ONE, sane servers will cap * the number of extents they return for block status. */ - if (chunk->length > NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData= )) { + if (chunk->magic =3D=3D NBD_STRUCTURED_REPLY_MAGIC) { + payload_len =3D be32_to_cpu(chunk->structured.length); + } else { + /* For now, we are ignoring the extended header offset. */ + payload_len =3D be64_to_cpu(chunk->extended.length); + chunk->magic =3D NBD_STRUCTURED_REPLY_MAGIC; + } + if (payload_len > NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData))= { error_setg(errp, "server chunk %" PRIu32 " (%s) payload is too lon= g", - chunk->type, nbd_rep_lookup(chunk->type)); + chunk->structured.type, + nbd_rep_lookup(chunk->structured.type)); return -EINVAL; } + chunk->structured.length =3D payload_len; return 0; } @@ -1476,19 +1498,21 @@ nbd_read_eof(BlockDriverState *bs, QIOChannel *ioc,= void *buffer, size_t size, /* nbd_receive_reply * - * Decreases bs->in_flight while waiting for a new reply. This yield is wh= ere - * we wait indefinitely and the coroutine must be able to be safely reente= red - * for nbd_client_attach_aio_context(). + * Wait for a new reply. If this yields, the coroutine must be able to be + * safely reentered for nbd_client_attach_aio_context(). @mode determines + * which reply magic we are expecting, although this normalizes the result + * so that the caller only has to work with compact headers. * * Returns 1 on success - * 0 on eof, when no data was read (errp is not set) - * negative errno on failure (errp is set) + * 0 on eof, when no data was read + * negative errno on failure */ int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, - NBDReply *reply, Error **errp) + NBDReply *reply, NBDMode mode, Error **= errp) { int ret; const char *type; + uint32_t expected; ret =3D nbd_read_eof(bs, ioc, &reply->magic, sizeof(reply->magic), err= p); if (ret <=3D 0) { @@ -1497,8 +1521,13 @@ int coroutine_fn nbd_receive_reply(BlockDriverState = *bs, QIOChannel *ioc, reply->magic =3D be32_to_cpu(reply->magic); + /* Diagnose but accept wrong-width header */ switch (reply->magic) { case NBD_SIMPLE_REPLY_MAGIC: + if (mode >=3D NBD_MODE_EXTENDED) { + trace_nbd_receive_wrong_header(reply->magic, + nbd_mode_lookup(mode)); + } ret =3D nbd_receive_simple_reply(ioc, &reply->simple, errp); if (ret < 0) { break; @@ -1508,23 +1537,28 @@ int coroutine_fn nbd_receive_reply(BlockDriverState= *bs, QIOChannel *ioc, reply->cookie); break; case NBD_STRUCTURED_REPLY_MAGIC: - ret =3D nbd_receive_structured_reply_chunk(ioc, &reply->structured= , errp); + case NBD_EXTENDED_REPLY_MAGIC: + expected =3D mode >=3D NBD_MODE_EXTENDED ? NBD_EXTENDED_REPLY_MAGIC + : NBD_STRUCTURED_REPLY_MAGIC; + if (reply->magic !=3D expected) { + trace_nbd_receive_wrong_header(reply->magic, + nbd_mode_lookup(mode)); + } + ret =3D nbd_receive_reply_chunk_header(ioc, reply, errp); if (ret < 0) { break; } type =3D nbd_reply_type_lookup(reply->structured.type); - trace_nbd_receive_structured_reply_chunk(reply->structured.flags, - reply->structured.type, t= ype, - reply->structured.cookie, - reply->structured.length); + trace_nbd_receive_reply_chunk_header(reply->structured.flags, + reply->structured.type, type, + reply->structured.cookie, + reply->structured.length); break; default: + trace_nbd_receive_wrong_header(reply->magic, nbd_mode_lookup(mode)= ); error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", reply->magic= ); return -EINVAL; } - if (ret < 0) { - return ret; - } return 1; } diff --git a/nbd/trace-events b/nbd/trace-events index 6a34d7f027a..51bfb129c95 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -33,7 +33,8 @@ nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" nbd_send_request(uint64_t from, uint64_t len, uint64_t cookie, uint16_t fl= ags, uint16_t type, const char *name) "Sending request to server: { .from = =3D %" PRIu64", .len =3D %" PRIu64 ", .cookie =3D %" PRIu64 ", .flags =3D 0= x%" PRIx16 ", .type =3D %" PRIu16 " (%s) }" nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t cook= ie) "Got simple reply: { .error =3D %" PRId32 " (%s), cookie =3D %" PRIu64"= }" -nbd_receive_structured_reply_chunk(uint16_t flags, uint16_t type, const ch= ar *name, uint64_t cookie, uint32_t length) "Got structured reply chunk: { = flags =3D 0x%" PRIx16 ", type =3D %d (%s), cookie =3D %" PRIu64 ", length = =3D %" PRIu32 " }" +nbd_receive_reply_chunk_header(uint16_t flags, uint16_t type, const char *= name, uint64_t cookie, uint32_t length) "Got reply chunk header: { flags = =3D 0x%" PRIx16 ", type =3D %d (%s), cookie =3D %" PRIu64 ", length =3D %" = PRIu32 " }" +nbd_receive_wrong_header(uint32_t magic, const char *mode) "Server sent un= expected magic 0x%" PRIx32 " for negotiated mode %s" # common.c nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232744; cv=none; d=zohomail.com; s=zohoarc; b=Fx6HPsAji+JxysuK1QdmgSyXEaecpGE1CEg2KzAweuxsQFGAkh78F3zuERBAZnNKPAdwUhe5YNPss+tXJnjcXkJGTEgoijqxwkLX63S9uMulLRJc5k+nkEma9/MoGKNffN3juMJZ+cfwzPKauiPLYsyfJz7/u/R3GrPVGdtHRaQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232744; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iZQ+HVGcvzWsqgERR7pKDFlybuYuXSbLPF2TyWZxCTA=; b=diWly1cK2w9Oy/toHmAbC6NPteSpmAf9kcJ/uDLVg3QqX4DTUQXDl2HaZF99Mh4zf8pUw7NIm286aUFOuyXmIrWE3RpkdEy5Hw5HcKTQ0Rz5FA6faDlbk54f2Ywg0YRw+/YpCMc5m9R7C7BHCJGZacnfIa/xcJZdGttIuG+tGFE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232744642398.438372850472; Thu, 8 Jun 2023 06:59:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G97-0004Ry-TB; Thu, 08 Jun 2023 09:57:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8l-000449-0c for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8g-0004kM-PJ for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:26 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-495-cjPI6FoQN2qx0UXFvigENw-1; Thu, 08 Jun 2023 09:57:13 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1EC0C3C1C4C3; Thu, 8 Jun 2023 13:57:12 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5850492B00; Thu, 8 Jun 2023 13:57:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iZQ+HVGcvzWsqgERR7pKDFlybuYuXSbLPF2TyWZxCTA=; b=NtgLvP1wRX94Bo65Gg6XlslEHJD4S9Jc+Xz6vS5Z2oqkgsGTNJyZqOwayxJJftkXj3djf3 lnKLznY1SLktrADtL79ZF3i9LRl269TUlGR3YcWuYbwP661xbgx4R8GmwZqWnNITG27J/4 gl0rkB7gzRGYcdWBzxjtyTgGfl5MLXI= X-MC-Unique: cjPI6FoQN2qx0UXFvigENw-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 20/24] nbd/client: Accept 64-bit block status chunks Date: Thu, 8 Jun 2023 08:56:49 -0500 Message-Id: <20230608135653.2918540-21-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232746830100003 Content-Type: text/plain; charset="utf-8" Once extended mode is enabled, we need to accept 64-bit status replies (even for replies that don't exceed a 32-bit length). It is easier to normalize narrow replies into wide format so that the rest of our code only has to handle one width. Although a server is non-compliant if it sends a 64-bit reply in compact mode, or a 32-bit reply in extended mode, it is still easy enough to tolerate these mismatches. In normal execution, we are only requesting "base:allocation" which never exceeds 32 bits for flag values. But during testing with x-dirty-bitmap, we can force qemu to connect to some other context that might have 64-bit status bit; however, we ignore those upper bits (other than mapping qemu:allocation-depth into something that 'qemu-img map --output=3Djson' can expose), and since that only affects testing, we really don't bother with checking whether more than the two least-significant bits are set. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: tweak comments and error message about count mismatch, fix setting of wide in loop [Vladimir] --- block/nbd.c | 47 ++++++++++++++++++++++++++++++++-------------- block/trace-events | 1 + 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index e281fac43d1..74c0a9d3b8c 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -614,13 +614,16 @@ static int nbd_parse_offset_hole_payload(BDRVNBDState= *s, */ static int nbd_parse_blockstatus_payload(BDRVNBDState *s, NBDStructuredReplyChunk *chunk, - uint8_t *payload, uint64_t orig_l= ength, - NBDExtent32 *extent, Error **errp) + uint8_t *payload, bool wide, + uint64_t orig_length, + NBDExtent64 *extent, Error **errp) { uint32_t context_id; + uint32_t count; + size_t len =3D wide ? sizeof(*extent) : sizeof(NBDExtent32); /* The server succeeded, so it must have sent [at least] one extent */ - if (chunk->length < sizeof(context_id) + sizeof(*extent)) { + if (chunk->length < sizeof(context_id) + wide * sizeof(count) + len) { error_setg(errp, "Protocol error: invalid payload for " "NBD_REPLY_TYPE_BLOCK_STATUS"); return -EINVAL; @@ -635,8 +638,15 @@ static int nbd_parse_blockstatus_payload(BDRVNBDState = *s, return -EINVAL; } - extent->length =3D payload_advance32(&payload); - extent->flags =3D payload_advance32(&payload); + if (wide) { + count =3D payload_advance32(&payload); + extent->length =3D payload_advance64(&payload); + extent->flags =3D payload_advance64(&payload); + } else { + count =3D 0; + extent->length =3D payload_advance32(&payload); + extent->flags =3D payload_advance32(&payload); + } if (extent->length =3D=3D 0) { error_setg(errp, "Protocol error: server sent status chunk with " @@ -671,13 +681,16 @@ static int nbd_parse_blockstatus_payload(BDRVNBDState= *s, /* * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have * sent us any more than one extent, nor should it have included - * status beyond our request in that extent. However, it's easy - * enough to ignore the server's noncompliance without killing the + * status beyond our request in that extent. Furthermore, a wide + * server should have replied with an accurate count (we left + * count at 0 for a narrow server). However, it's easy enough to + * ignore the server's noncompliance without killing the * connection; just ignore trailing extents, and clamp things to * the length of our request. */ - if (chunk->length > sizeof(context_id) + sizeof(*extent)) { - trace_nbd_parse_blockstatus_compliance("more than one extent"); + if (count !=3D wide || + chunk->length > sizeof(context_id) + wide * sizeof(count) + len) { + trace_nbd_parse_blockstatus_compliance("unexpected extent count"); } if (extent->length > orig_length) { extent->length =3D orig_length; @@ -1123,7 +1136,7 @@ nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_= t cookie, static int coroutine_fn nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uint64_t cookie, - uint64_t length, NBDExtent32 *extent, + uint64_t length, NBDExtent64 *extent, int *request_ret, Error **errp) { NBDReplyChunkIter iter; @@ -1136,11 +1149,17 @@ nbd_co_receive_blockstatus_reply(BDRVNBDState *s, u= int64_t cookie, NBD_FOREACH_REPLY_CHUNK(s, iter, cookie, false, NULL, &reply, &payload= ) { int ret; NBDStructuredReplyChunk *chunk =3D &reply.structured; + bool wide; assert(nbd_reply_is_structured(&reply)); switch (chunk->type) { + case NBD_REPLY_TYPE_BLOCK_STATUS_EXT: case NBD_REPLY_TYPE_BLOCK_STATUS: + wide =3D chunk->type =3D=3D NBD_REPLY_TYPE_BLOCK_STATUS_EXT; + if ((s->info.mode >=3D NBD_MODE_EXTENDED) !=3D wide) { + trace_nbd_extended_headers_compliance("block_status"); + } if (received) { nbd_channel_error(s, -EINVAL); error_setg(&local_err, "Several BLOCK_STATUS chunks in rep= ly"); @@ -1148,9 +1167,9 @@ nbd_co_receive_blockstatus_reply(BDRVNBDState *s, uin= t64_t cookie, } received =3D true; - ret =3D nbd_parse_blockstatus_payload(s, &reply.structured, - payload, length, extent, - &local_err); + ret =3D nbd_parse_blockstatus_payload( + s, &reply.structured, payload, wide, + length, extent, &local_err); if (ret < 0) { nbd_channel_error(s, ret); nbd_iter_channel_error(&iter, ret, &local_err); @@ -1380,7 +1399,7 @@ static int coroutine_fn GRAPH_RDLOCK nbd_client_co_bl= ock_status( int64_t *pnum, int64_t *map, BlockDriverState **file) { int ret, request_ret; - NBDExtent32 extent =3D { 0 }; + NBDExtent64 extent =3D { 0 }; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; Error *local_err =3D NULL; diff --git a/block/trace-events b/block/trace-events index 6f121b76365..a0fc79153d3 100644 --- a/block/trace-events +++ b/block/trace-events @@ -166,6 +166,7 @@ iscsi_xcopy(void *src_lun, uint64_t src_off, void *dst_= lun, uint64_t dst_off, ui # nbd.c nbd_parse_blockstatus_compliance(const char *err) "ignoring extra data fro= m non-compliant server: %s" nbd_structured_read_compliance(const char *type) "server sent non-complian= t unaligned read %s chunk" +nbd_extended_headers_compliance(const char *type) "server sent non-complia= nt %s chunk not matching choice of extended headers" nbd_read_reply_entry_fail(int ret, const char *err) "ret =3D %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 =3D %" PRIu64", .len =3D %" PRIu32 ", .handle =3D %" PRIu64= ", .flags =3D 0x%" PRIx16 ", .type =3D %" PRIu16 " (%s) } ret =3D %d, err:= %s" nbd_client_handshake(const char *export_name) "export '%s'" --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232788; cv=none; d=zohomail.com; s=zohoarc; b=WFUSegoVh+e5krrqU+39/24BvIQAcvGFg1QQhp0wb5wis+qELVRm2UNiIxOsOGfvuhg8wlNW2nqlWjDDabu9jdOZEcDZeK+CUY+etcHXL0PnHDHKf9k10muDgLLqnW9O6fg+JuLzEhLTDRbwRImjCt+MVvkhoBf1ydwnFydFwcE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232788; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=TYqTUzXBeWUT2xn3GcgJRA8k2hpxHSOkypgOZksT0Dw=; b=TDaFDGtuwqF1WQk8BNEQBtPgqYogaiD+naisPFL2GSc9XfhwNq1hCEgwStP+r0hB0hMl8/rrfwOH38xxguKZ7ZEeVg9bXGTPK0IfIbHfKqlG6jiZQiC2WiC0JZzErfOD/27fJN0NnQE/KYn/BMwnjwiVm9t6Jxm8z03O2OUWiPY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232788703936.4338184435004; Thu, 8 Jun 2023 06:59:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G97-0004R4-56; Thu, 08 Jun 2023 09:57:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8i-00041r-Lh for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8a-0004kQ-LV for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:24 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-163-MxiW_V1nNDScIpE4ryRAsQ-1; Thu, 08 Jun 2023 09:57:13 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EE7068007D9; Thu, 8 Jun 2023 13:57:12 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63C0D492B00; Thu, 8 Jun 2023 13:57:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TYqTUzXBeWUT2xn3GcgJRA8k2hpxHSOkypgOZksT0Dw=; b=BVqarr4yTGbitzwRfeOHxlyWOkIPvlBvbDlwIJZMdBlg/4doBDKvywmczK2R041lVuKNAK x/QghtdJ+/EnxldX6h/VQPm61tV6MD1JxgW8693KF+yYTcuZ9Y6QOPPNY+nWDYW96p5Lgt j6FXlhNDYe89oCDtyjaK+fXM2JomQSQ= X-MC-Unique: MxiW_V1nNDScIpE4ryRAsQ-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 21/24] nbd/client: Request extended headers during negotiation Date: Thu, 8 Jun 2023 08:56:50 -0500 Message-Id: <20230608135653.2918540-22-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232790711100002 Content-Type: text/plain; charset="utf-8" All the pieces are in place for a client to finally request extended headers. Note that we must not request extended headers when qemu-nbd is used to connect to the kernel module (as nbd.ko does not expect them, but expects us to do the negotiation in userspace before handing the socket over to the kernel), but there is no harm in all other clients requesting them. Extended headers are not essential to the information collected during 'qemu-nbd --list', but probing for it gives us one more piece of information in that output. Update the iotests affected by the new line of output. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: rebase to earlier changes, tweak commit message for why qemu-nbd connection to /dev/nbd cannot use extended mode [Vladimir] --- nbd/client-connection.c | 2 +- nbd/client.c | 20 ++++++++++++++----- qemu-nbd.c | 3 +++ tests/qemu-iotests/223.out | 6 ++++++ tests/qemu-iotests/233.out | 4 ++++ tests/qemu-iotests/241.out | 3 +++ tests/qemu-iotests/307.out | 5 +++++ .../tests/nbd-qemu-allocation.out | 1 + 8 files changed, 38 insertions(+), 6 deletions(-) diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 13e4cb6684b..d9d946da006 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -93,7 +93,7 @@ NBDClientConnection *nbd_client_connection_new(const Sock= etAddress *saddr, .do_negotiation =3D do_negotiation, .initial_info.request_sizes =3D true, - .initial_info.mode =3D NBD_MODE_STRUCTURED, + .initial_info.mode =3D NBD_MODE_EXTENDED, .initial_info.base_allocation =3D true, .initial_info.x_dirty_bitmap =3D g_strdup(x_dirty_bitmap), .initial_info.name =3D g_strdup(export_name ?: "") diff --git a/nbd/client.c b/nbd/client.c index a4598a95427..99c0e5c8114 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -958,15 +958,23 @@ static int nbd_start_negotiate(AioContext *aio_contex= t, QIOChannel *ioc, if (fixedNewStyle) { int result =3D 0; + if (max_mode >=3D NBD_MODE_EXTENDED) { + result =3D nbd_request_simple_option(ioc, + NBD_OPT_EXTENDED_HEADER= S, + false, errp); + if (result) { + return result < 0 ? -EINVAL : NBD_MODE_EXTENDED; + } + } if (max_mode >=3D NBD_MODE_STRUCTURED) { result =3D nbd_request_simple_option(ioc, NBD_OPT_STRUCTURED_REPL= Y, false, errp); - if (result < 0) { - return -EINVAL; + if (result) { + return result < 0 ? -EINVAL : NBD_MODE_STRUCTURED; } } - return result ? NBD_MODE_STRUCTURED : NBD_MODE_SIMPLE; + return NBD_MODE_SIMPLE; } else { return NBD_MODE_EXPORT_NAME; } @@ -1040,6 +1048,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QI= OChannel *ioc, } switch (info->mode) { + case NBD_MODE_EXTENDED: case NBD_MODE_STRUCTURED: if (base_allocation) { result =3D nbd_negotiate_simple_meta_context(ioc, info, errp); @@ -1150,7 +1159,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, *info =3D NULL; result =3D nbd_start_negotiate(NULL, ioc, tlscreds, hostname, &sioc, - NBD_MODE_STRUCTURED, NULL, errp); + NBD_MODE_EXTENDED, NULL, errp); if (tlscreds && sioc) { ioc =3D sioc; } @@ -1161,6 +1170,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, switch ((NBDMode)result) { case NBD_MODE_SIMPLE: case NBD_MODE_STRUCTURED: + case NBD_MODE_EXTENDED: /* newstyle - use NBD_OPT_LIST to populate array, then try * NBD_OPT_INFO on each array member. If structured replies * are enabled, also try NBD_OPT_LIST_META_CONTEXT. */ @@ -1197,7 +1207,7 @@ int nbd_receive_export_list(QIOChannel *ioc, QCryptoT= LSCreds *tlscreds, break; } - if (result =3D=3D NBD_MODE_STRUCTURED && + if (result >=3D NBD_MODE_STRUCTURED && nbd_list_meta_contexts(ioc, &array[i], errp) < 0) { goto out; } diff --git a/qemu-nbd.c b/qemu-nbd.c index 3ddd0bf02b4..1d155fc2c66 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -238,6 +238,9 @@ static int qemu_nbd_client_list(SocketAddress *saddr, Q= CryptoTLSCreds *tls, printf(" opt block: %u\n", list[i].opt_block); printf(" max block: %u\n", list[i].max_block); } + printf(" transaction size: %s\n", + list[i].mode >=3D NBD_MODE_EXTENDED ? + "64-bit" : "32-bit"); if (list[i].n_contexts) { printf(" available meta contexts: %d\n", list[i].n_contexts); for (j =3D 0; j < list[i].n_contexts; j++) { diff --git a/tests/qemu-iotests/223.out b/tests/qemu-iotests/223.out index 26fb347c5da..b98582c38ea 100644 --- a/tests/qemu-iotests/223.out +++ b/tests/qemu-iotests/223.out @@ -87,6 +87,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b @@ -97,6 +98,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b2 @@ -106,6 +108,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b3 @@ -206,6 +209,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b @@ -216,6 +220,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b2 @@ -225,6 +230,7 @@ exports available: 3 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:dirty-bitmap:b3 diff --git a/tests/qemu-iotests/233.out b/tests/qemu-iotests/233.out index 237c82767ea..1910f7df20f 100644 --- a/tests/qemu-iotests/233.out +++ b/tests/qemu-iotests/233.out @@ -39,6 +39,7 @@ exports available: 1 export: '' size: 67108864 min block: 1 + transaction size: 64-bit =3D=3D check TLS fail over TCP with mismatched hostname =3D=3D qemu-img: Could not open 'driver=3Dnbd,host=3Dlocalhost,port=3DPORT,tls-cr= eds=3Dtls0': Certificate does not match the hostname localhost @@ -53,6 +54,7 @@ exports available: 1 export: '' size: 67108864 min block: 1 + transaction size: 64-bit =3D=3D check TLS with different CA fails =3D=3D qemu-img: Could not open 'driver=3Dnbd,host=3D127.0.0.1,port=3DPORT,tls-cr= eds=3Dtls0': The certificate hasn't got a known issuer @@ -83,6 +85,7 @@ exports available: 1 export: '' size: 67108864 min block: 1 + transaction size: 64-bit =3D=3D check TLS works over UNIX with PSK =3D=3D image: nbd+unix://?socket=3DSOCK_DIR/qemu-nbd.sock @@ -93,6 +96,7 @@ exports available: 1 export: '' size: 67108864 min block: 1 + transaction size: 64-bit =3D=3D check TLS fails over UNIX with mismatch PSK =3D=3D qemu-img: Could not open 'driver=3Dnbd,path=3DSOCK_DIR/qemu-nbd.sock,tls-c= reds=3Dtls0': TLS handshake failed: The TLS connection was non-properly ter= minated. diff --git a/tests/qemu-iotests/241.out b/tests/qemu-iotests/241.out index 88e8cfcd7e2..a9efb876521 100644 --- a/tests/qemu-iotests/241.out +++ b/tests/qemu-iotests/241.out @@ -6,6 +6,7 @@ exports available: 1 export: '' size: 1024 min block: 1 + transaction size: 64-bit [{ "start": 0, "length": 1000, "depth": 0, "present": true, "zero": false,= "data": true, "offset": OFFSET}, { "start": 1000, "length": 24, "depth": 0, "present": true, "zero": true, = "data": false, "offset": OFFSET}] 1 KiB (0x400) bytes allocated at offset 0 bytes (0x0) @@ -16,6 +17,7 @@ exports available: 1 export: '' size: 1024 min block: 512 + transaction size: 64-bit [{ "start": 0, "length": 1024, "depth": 0, "present": true, "zero": false,= "data": true, "offset": OFFSET}] 1 KiB (0x400) bytes allocated at offset 0 bytes (0x0) WARNING: Image format was not specified for 'TEST_DIR/t.raw' and probing g= uessed raw. @@ -28,6 +30,7 @@ exports available: 1 export: '' size: 1024 min block: 1 + transaction size: 64-bit [{ "start": 0, "length": 1000, "depth": 0, "present": true, "zero": false,= "data": true, "offset": OFFSET}, { "start": 1000, "length": 24, "depth": 0, "present": true, "zero": true, = "data": false, "offset": OFFSET}] 1 KiB (0x400) bytes allocated at offset 0 bytes (0x0) diff --git a/tests/qemu-iotests/307.out b/tests/qemu-iotests/307.out index 390f05d1b78..2b9a6a67a1a 100644 --- a/tests/qemu-iotests/307.out +++ b/tests/qemu-iotests/307.out @@ -19,6 +19,7 @@ exports available: 1 min block: XXX opt block: XXX max block: XXX + transaction size: 64-bit available meta contexts: 1 base:allocation @@ -47,6 +48,7 @@ exports available: 1 min block: XXX opt block: XXX max block: XXX + transaction size: 64-bit available meta contexts: 1 base:allocation @@ -78,6 +80,7 @@ exports available: 2 min block: XXX opt block: XXX max block: XXX + transaction size: 64-bit available meta contexts: 1 base:allocation export: 'export1' @@ -87,6 +90,7 @@ exports available: 2 min block: XXX opt block: XXX max block: XXX + transaction size: 64-bit available meta contexts: 1 base:allocation @@ -113,6 +117,7 @@ exports available: 1 min block: XXX opt block: XXX max block: XXX + transaction size: 64-bit available meta contexts: 1 base:allocation diff --git a/tests/qemu-iotests/tests/nbd-qemu-allocation.out b/tests/qemu-= iotests/tests/nbd-qemu-allocation.out index 9d938db24e6..659276032b0 100644 --- a/tests/qemu-iotests/tests/nbd-qemu-allocation.out +++ b/tests/qemu-iotests/tests/nbd-qemu-allocation.out @@ -21,6 +21,7 @@ exports available: 1 min block: 1 opt block: 4096 max block: 33554432 + transaction size: 64-bit available meta contexts: 2 base:allocation qemu:allocation-depth --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686236013; cv=none; d=zohomail.com; s=zohoarc; b=WPtFe2T091QYQTzdqbkh99yWY4Pu5p/r/xHMLCCl8jPaGIPvmwTmMGlIPr+UmLmcolG+JLlvXLaNvB13Rez1sp3r8ldudY6eo7ZctlzNg/qtIPmVB4ij7Y8e/B8a1fa/Ng/DvlNkV8UjNqnxG17ch1wPmpAyaGXh07xAvKH/Cow= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686236013; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hTYoHZ5/+Lhof/7wgPVRDTj3CfWlRezlcs8iQl1RZz0=; b=hpwSFLjM2kPMc0QEaLa1e+OThrSnHXMqgqugQrf13YvZ2SKE+iXMoLodMzY1NMPHYVHiwQe2KhkVEiRbtwHSx9XxQWfoEppNL0PM0TxSUeYGYv7o8VIzkQ96BAKTE6XCXBefW24bxxukM6bsWJclGB0Z75F0rcXZc7g5+4xCtFg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686236013706256.08567656244486; Thu, 8 Jun 2023 07:53:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8x-0004EW-NQ; Thu, 08 Jun 2023 09:57:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8m-000450-V5 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0004lx-Ef for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:28 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-440-NTVU4jFfPs2vhm-W33U15Q-1; Thu, 08 Jun 2023 09:57:15 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B03B285A5BD; Thu, 8 Jun 2023 13:57:13 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2650A492B00; Thu, 8 Jun 2023 13:57:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hTYoHZ5/+Lhof/7wgPVRDTj3CfWlRezlcs8iQl1RZz0=; b=jNjCUb16H0h0SHsqjOwy/rcrofPjF4PNuQQ9pqW14lp7Dv+judtlktt3oRN+C4nvoaDcB7 BFdSQ9Z19YCwjfuXs9hvSGrWvKR+qWeXx3A75gCufnEzFnKO7vCODwZOLks2NmmWsemy+p 5E/GaujTyBSl5aTUdV8BB3nLiZ/iaP4= X-MC-Unique: NTVU4jFfPs2vhm-W33U15Q-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 22/24] nbd/server: Refactor list of negotiated meta contexts Date: Thu, 8 Jun 2023 08:56:51 -0500 Message-Id: <20230608135653.2918540-23-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686236015341100001 Content-Type: text/plain; charset="utf-8" Peform several minor refactorings of how the list of negotiated meta contexts is managed, to make upcoming patches easier: Promote the internal type NBDExportMetaContexts to the public opaque type NBDMetaContexts, and mark exp const. Use a shorter member name in NBDClient. Hoist calls to nbd_check_meta_context() earlier in their callers, as the number of negotiated contexts may impact the flags exposed in regards to an export, which in turn requires a new parameter. Drop a redundant parameter to nbd_negotiate_meta_queries. No semantic change intended. Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- v4: new patch split out from v3 13/14, with smaller impact (quit trying to separate exp outside of NBDMeataContexts) --- include/block/nbd.h | 1 + nbd/server.c | 55 ++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index af80087e2cd..f240707f646 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -28,6 +28,7 @@ typedef struct NBDExport NBDExport; typedef struct NBDClient NBDClient; +typedef struct NBDMetaContexts NBDMetaContexts; extern const BlockExportDriver blk_exp_nbd; diff --git a/nbd/server.c b/nbd/server.c index ae293663ca2..42a4300c95e 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -105,11 +105,13 @@ struct NBDExport { static QTAILQ_HEAD(, NBDExport) exports =3D QTAILQ_HEAD_INITIALIZER(export= s); -/* NBDExportMetaContexts represents a list of contexts to be exported, +/* + * NBDMetaContexts represents a list of meta contexts in use, * as selected by NBD_OPT_SET_META_CONTEXT. Also used for - * NBD_OPT_LIST_META_CONTEXT. */ -typedef struct NBDExportMetaContexts { - NBDExport *exp; + * NBD_OPT_LIST_META_CONTEXT. + */ +struct NBDMetaContexts { + const NBDExport *exp; /* associated export */ size_t count; /* number of negotiated contexts */ bool base_allocation; /* export base:allocation context (block status)= */ bool allocation_depth; /* export qemu:allocation-depth */ @@ -117,7 +119,7 @@ typedef struct NBDExportMetaContexts { * export qemu:dirty-bitmap:, * sized by exp->nr_export_bitmaps */ -} NBDExportMetaContexts; +}; struct NBDClient { int refcount; @@ -144,7 +146,7 @@ struct NBDClient { uint32_t check_align; /* If non-zero, check for aligned client request= s */ NBDMode mode; - NBDExportMetaContexts export_meta; + NBDMetaContexts contexts; /* Negotiated meta contexts */ uint32_t opt; /* Current option being negotiated */ uint32_t optlen; /* remaining length of data in ioc for the option bei= ng @@ -455,10 +457,10 @@ static int nbd_negotiate_handle_list(NBDClient *clien= t, Error **errp) return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp); } -static void nbd_check_meta_export(NBDClient *client) +static void nbd_check_meta_export(NBDClient *client, NBDExport *exp) { - if (client->exp !=3D client->export_meta.exp) { - client->export_meta.count =3D 0; + if (exp !=3D client->contexts.exp) { + client->contexts.count =3D 0; } } @@ -504,6 +506,7 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, bool no_zeroes, error_setg(errp, "export not found"); return -EINVAL; } + nbd_check_meta_export(client, client->exp); myflags =3D client->exp->nbdflags; if (client->mode >=3D NBD_MODE_STRUCTURED) { @@ -521,7 +524,6 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, bool no_zeroes, QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); blk_exp_ref(&client->exp->common); - nbd_check_meta_export(client); return 0; } @@ -641,6 +643,9 @@ static int nbd_negotiate_handle_info(NBDClient *client,= Error **errp) errp, "export '%s' not present", sane_name); } + if (client->opt =3D=3D NBD_OPT_GO) { + nbd_check_meta_export(client, exp); + } /* Don't bother sending NBD_INFO_NAME unless client requested it */ if (sendname) { @@ -729,7 +734,6 @@ static int nbd_negotiate_handle_info(NBDClient *client,= Error **errp) client->check_align =3D check_align; QTAILQ_INSERT_TAIL(&client->exp->clients, client, next); blk_exp_ref(&client->exp->common); - nbd_check_meta_export(client); rc =3D 1; } return rc; @@ -852,7 +856,7 @@ static bool nbd_strshift(const char **str, const char *= prefix) * Handle queries to 'base' namespace. For now, only the base:allocation * context is available. Return true if @query has been handled. */ -static bool nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *= meta, +static bool nbd_meta_base_query(NBDClient *client, NBDMetaContexts *meta, const char *query) { if (!nbd_strshift(&query, "base:")) { @@ -872,7 +876,7 @@ static bool nbd_meta_base_query(NBDClient *client, NBDE= xportMetaContexts *meta, * and qemu:allocation-depth contexts are available. Return true if @query * has been handled. */ -static bool nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *= meta, +static bool nbd_meta_qemu_query(NBDClient *client, NBDMetaContexts *meta, const char *query) { size_t i; @@ -938,7 +942,7 @@ static bool nbd_meta_qemu_query(NBDClient *client, NBDE= xportMetaContexts *meta, * Return -errno on I/O error, 0 if option was completely handled by * sending a reply about inconsistent lengths, or 1 on success. */ static int nbd_negotiate_meta_query(NBDClient *client, - NBDExportMetaContexts *meta, Error **e= rrp) + NBDMetaContexts *meta, Error **errp) { int ret; g_autofree char *query =3D NULL; @@ -977,14 +981,14 @@ static int nbd_negotiate_meta_query(NBDClient *client, * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT * * Return -errno on I/O error, or 0 if option was completely handled. */ -static int nbd_negotiate_meta_queries(NBDClient *client, - NBDExportMetaContexts *meta, Error *= *errp) +static int nbd_negotiate_meta_queries(NBDClient *client, Error **errp) { int ret; g_autofree char *export_name =3D NULL; /* Mark unused to work around https://bugs.llvm.org/show_bug.cgi?id=3D= 3888 */ g_autofree G_GNUC_UNUSED bool *bitmaps =3D NULL; - NBDExportMetaContexts local_meta =3D {0}; + NBDMetaContexts local_meta =3D {0}; + NBDMetaContexts *meta; uint32_t nb_queries; size_t i; size_t count =3D 0; @@ -1000,6 +1004,8 @@ static int nbd_negotiate_meta_queries(NBDClient *clie= nt, if (client->opt =3D=3D NBD_OPT_LIST_META_CONTEXT) { /* Only change the caller's meta on SET. */ meta =3D &local_meta; + } else { + meta =3D &client->contexts; } g_free(meta->bitmaps); @@ -1282,8 +1288,7 @@ static int nbd_negotiate_options(NBDClient *client, E= rror **errp) case NBD_OPT_LIST_META_CONTEXT: case NBD_OPT_SET_META_CONTEXT: - ret =3D nbd_negotiate_meta_queries(client, &client->export= _meta, - errp); + ret =3D nbd_negotiate_meta_queries(client, errp); break; case NBD_OPT_EXTENDED_HEADERS: @@ -1515,7 +1520,7 @@ void nbd_client_put(NBDClient *client) QTAILQ_REMOVE(&client->exp->clients, client, next); blk_exp_unref(&client->exp->common); } - g_free(client->export_meta.bitmaps); + g_free(client->contexts.bitmaps); g_free(client); } } @@ -2711,11 +2716,11 @@ static coroutine_fn int nbd_handle_request(NBDClien= t *client, } assert(client->mode >=3D NBD_MODE_EXTENDED || request->len <=3D UINT32_MAX); - if (client->export_meta.count) { + if (client->contexts.count) { bool dont_fragment =3D request->flags & NBD_CMD_FLAG_REQ_ONE; - int contexts_remaining =3D client->export_meta.count; + int contexts_remaining =3D client->contexts.count; - if (client->export_meta.base_allocation) { + if (client->contexts.base_allocation) { ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, @@ -2728,7 +2733,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } } - if (client->export_meta.allocation_depth) { + if (client->contexts.allocation_depth) { ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, request->len, @@ -2742,7 +2747,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } for (i =3D 0; i < client->exp->nr_export_bitmaps; i++) { - if (!client->export_meta.bitmaps[i]) { + if (!client->contexts.bitmaps[i]) { continue; } ret =3D nbd_co_send_bitmap(client, request, --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686232952; cv=none; d=zohomail.com; s=zohoarc; b=ddXEh8EVF0t5dNS7IQNlDosqmtJ/rjOiWuTFc3WlpXoMet/AlWkbmoZYppvT9FH2z7Q8A/pthJJqWZN9+kHCKGofFxiOn9RZ75UKY0G9p0VxY89RXhVXgp/MPiTPcGLHIYGR/nEgJ7k2ymrqgUntystkNxYzE7g78i68g7xYvPY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686232952; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/QnKxV2pN16lLcAjxkXlbh+ajdIrWM1LhBM7kNJsoY4=; b=cJ8jOQaEz0Wy/Kw3CP0iEV+qObZbiJQtrA5NvCvjhWZJoGVmrx/w6b4BXtkmk4gX1xLn2Btk5s9qC8F9Hzh6etpkNDvEif/OzMm0t9RjkuGu3KW7jGdLdj8LyejurT1dL8cFVR6b5ojicsjVVGSiD5g0AvRZ+YITS5Nxs+gXEi4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686232952046136.00483028445933; Thu, 8 Jun 2023 07:02:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8u-0004AB-Lt; Thu, 08 Jun 2023 09:57:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8j-00042h-NX for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8g-0004ko-Lw for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:25 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-331-5wP7wlCEPHyhTaitPIesUQ-1; Thu, 08 Jun 2023 09:57:14 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6DD8C185A78B; Thu, 8 Jun 2023 13:57:14 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id DACB3492B00; Thu, 8 Jun 2023 13:57:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/QnKxV2pN16lLcAjxkXlbh+ajdIrWM1LhBM7kNJsoY4=; b=N5cJvZ3iUN2LdWnFOnBoZYQi7pQbDfDu5+VTzLvdOx/PweKrf6cDYl0vEQ6uWDSSzZsBu+ qhO/cNymz5NN+GmbhQvkf5XgTDamF4fZC7LpHuvLIkZ1A6p099Fu8MGV98Lh6171XhPdu7 e13gr09P1+Dk2ppPmWPQq4qSBtDfHQ4= X-MC-Unique: 5wP7wlCEPHyhTaitPIesUQ-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 23/24] nbd/server: Prepare for per-request filtering of BLOCK_STATUS Date: Thu, 8 Jun 2023 08:56:52 -0500 Message-Id: <20230608135653.2918540-24-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686232954031100001 Content-Type: text/plain; charset="utf-8" The next commit will add support for the optional extension NBD_CMD_FLAG_PAYLOAD during NBD_CMD_BLOCK_STATUS, where the client can request that the server only return a subset of negotiated contexts, rather than all contexts. To make that task easier, this patch populates the list of contexts to return on a per-command basis (for now, identical to the full set of negotiated contexts). Signed-off-by: Eric Blake --- v4: split out NBDMetaContexts refactoring to its own patch, track NBDRequests.contexts as a pointer rather than inline --- include/block/nbd.h | 1 + nbd/server.c | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index f240707f646..47850be5a66 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -76,6 +76,7 @@ typedef struct NBDRequest { uint16_t flags; /* NBD_CMD_FLAG_* */ uint16_t type; /* NBD_CMD_* */ NBDMode mode; /* Determines which network representation to use */ + NBDMetaContexts *contexts; /* Used by NBD_CMD_BLOCK_STATUS */ } NBDRequest; typedef struct NBDSimpleReply { diff --git a/nbd/server.c b/nbd/server.c index 42a4300c95e..308846fe46b 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2491,6 +2491,8 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * error_setg(errp, "No memory"); return -ENOMEM; } + } else if (request->type =3D=3D NBD_CMD_BLOCK_STATUS) { + request->contexts =3D &client->contexts; } if (payload_len) { @@ -2716,11 +2718,11 @@ static coroutine_fn int nbd_handle_request(NBDClien= t *client, } assert(client->mode >=3D NBD_MODE_EXTENDED || request->len <=3D UINT32_MAX); - if (client->contexts.count) { + if (request->contexts->count) { bool dont_fragment =3D request->flags & NBD_CMD_FLAG_REQ_ONE; - int contexts_remaining =3D client->contexts.count; + int contexts_remaining =3D request->contexts->count; - if (client->contexts.base_allocation) { + if (request->contexts->base_allocation) { ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, @@ -2733,7 +2735,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } } - if (client->contexts.allocation_depth) { + if (request->contexts->allocation_depth) { ret =3D nbd_co_send_block_status(client, request, exp->common.blk, request->from, request->len, @@ -2746,8 +2748,9 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, } } + assert(request->contexts->exp =3D=3D client->exp); for (i =3D 0; i < client->exp->nr_export_bitmaps; i++) { - if (!client->contexts.bitmaps[i]) { + if (!request->contexts->bitmaps[i]) { continue; } ret =3D nbd_co_send_bitmap(client, request, @@ -2763,6 +2766,10 @@ static coroutine_fn int nbd_handle_request(NBDClient= *client, assert(!contexts_remaining); return 0; + } else if (client->contexts.count) { + return nbd_send_generic_reply(client, request, -EINVAL, + "CMD_BLOCK_STATUS payload not va= lid", + errp); } else { return nbd_send_generic_reply(client, request, -EINVAL, "CMD_BLOCK_STATUS not negotiated= ", @@ -2841,6 +2848,11 @@ static coroutine_fn void nbd_trip(void *opaque) } else { ret =3D nbd_handle_request(client, &request, req->data, &local_err= ); } + if (request.type =3D=3D NBD_CMD_BLOCK_STATUS && + request.contexts !=3D &client->contexts) { + g_free(request.contexts->bitmaps); + g_free(request.contexts); + } if (ret < 0) { error_prepend(&local_err, "Failed to send reply: "); goto disconnect; --=20 2.40.1 From nobody Thu May 2 03:29:30 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1686234235; cv=none; d=zohomail.com; s=zohoarc; b=UnoMKYYilduKKnuv7pSooxocoP7BKaGXf/wUIvtYi9SM64zZOh/7D1xN0bcIcK+Or352nPxIfypAhpZGes1X7yxBMvLTfPSLljrNXKt/VQE3k3c58U3JnJiK774ycUKNrZ7Qb7atBOlfH8blqBeJ2Q7feQ737n8eSgr6RF0BHaA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686234235; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lEAKWZEszZA4tXMfJjzDLgqJd+7FdPyDGj/0I7CSkxc=; b=Zyal9gImgHr0SAT6TXQncZ+zq2nT28fdlVCcw7GzOeCX4L9btgWg+2evdRpMSQNaTj+8jEng4uWBbGEDoMF/knkIhVqSmgKLu7Kv++dE/6oW4Jb72cLvHvdovgNSvEvJN6SRdII9O8kTNKrJltQaJeBEZb1lekmGsDO2JH1yBgY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 168623423502823.149128779636726; Thu, 8 Jun 2023 07:23:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7G8t-00049J-Nj; Thu, 08 Jun 2023 09:57:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8m-00044Z-48 for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7G8h-0004lN-DA for qemu-devel@nongnu.org; Thu, 08 Jun 2023 09:57:27 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-592-2WygfT-cPBKnlJswXeFsdg-1; Thu, 08 Jun 2023 09:57:15 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3C6383825BC4; Thu, 8 Jun 2023 13:57:15 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id A6576515540; Thu, 8 Jun 2023 13:57:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686232636; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lEAKWZEszZA4tXMfJjzDLgqJd+7FdPyDGj/0I7CSkxc=; b=Rx1sokpf8RFeOyffJXNKNTBnwF+yoNT32nriTgqNNrhx5DyJjrvPLfbXr1PvE5vyC7c8qA bScD6i/ABomGHiRY1OOmf5kbrKnhObJNAwX4ksfESYg3cCehgB4uTa8RvchbvEFM65FbHr YczJNjUHLCFbGsDHPcbGRwD/tZQT5JU= X-MC-Unique: 2WygfT-cPBKnlJswXeFsdg-1 From: Eric Blake To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, libguestfs@redhat.com, vsementsov@yandex-team.ru, Kevin Wolf , Hanna Reitz Subject: [PATCH v4 24/24] nbd/server: Add FLAG_PAYLOAD support to CMD_BLOCK_STATUS Date: Thu, 8 Jun 2023 08:56:53 -0500 Message-Id: <20230608135653.2918540-25-eblake@redhat.com> In-Reply-To: <20230608135653.2918540-1-eblake@redhat.com> References: <20230608135653.2918540-1-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1686234236296100001 Content-Type: text/plain; charset="utf-8" Allow a client to request a subset of negotiated meta contexts. For example, a client may ask to use a single connection to learn about both block status and dirty bitmaps, but where the dirty bitmap queries only need to be performed on a subset of the disk; forcing the server to compute that information on block status queries in the rest of the disk is wasted effort (both at the server, and on the amount of traffic sent over the wire to be parsed and ignored by the client). Qemu as an NBD client never requests to use more than one meta context, so it has no need to use block status payloads. Testing this instead requires support from libnbd, which CAN access multiple meta contexts in parallel from a single NBD connection; an interop test submitted to the libnbd project at the same time as this patch demonstrates the feature working, as well as testing some corner cases (for example, when the payload length is longer than the export length), although other corner cases (like passing the same id duplicated) requires a protocol fuzzer because libnbd is not wired up to break the protocol that badly. This also includes tweaks to 'qemu-nbd --list' to show when a server is advertising the capability, and to the testsuite to reflect the addition to that output. Signed-off-by: Eric Blake --- docs/interop/nbd.txt | 2 +- nbd/server.c | 99 ++++++++++++++++++- qemu-nbd.c | 1 + nbd/trace-events | 1 + tests/qemu-iotests/223.out | 12 +-- tests/qemu-iotests/307.out | 10 +- .../tests/nbd-qemu-allocation.out | 2 +- 7 files changed, 111 insertions(+), 16 deletions(-) diff --git a/docs/interop/nbd.txt b/docs/interop/nbd.txt index abaf4c28a96..83d85ce8d13 100644 --- a/docs/interop/nbd.txt +++ b/docs/interop/nbd.txt @@ -69,4 +69,4 @@ NBD_CMD_BLOCK_STATUS for "qemu:dirty-bitmap:", NBD_CMD_CA= CHE NBD_CMD_FLAG_FAST_ZERO * 5.2: NBD_CMD_BLOCK_STATUS for "qemu:allocation-depth" * 7.1: NBD_FLAG_CAN_MULTI_CONN for shareable writable exports -* 8.1: NBD_OPT_EXTENDED_HEADERS +* 8.1: NBD_OPT_EXTENDED_HEADERS, NBD_FLAG_BLOCK_STATUS_PAYLOAD diff --git a/nbd/server.c b/nbd/server.c index 308846fe46b..696afcf5c46 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -512,6 +512,9 @@ static int nbd_negotiate_handle_export_name(NBDClient *= client, bool no_zeroes, if (client->mode >=3D NBD_MODE_STRUCTURED) { myflags |=3D NBD_FLAG_SEND_DF; } + if (client->mode >=3D NBD_MODE_EXTENDED && client->contexts.count) { + myflags |=3D NBD_FLAG_BLOCK_STAT_PAYLOAD; + } trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags); stq_be_p(buf, client->exp->size); stw_be_p(buf + 8, myflags); @@ -699,6 +702,10 @@ static int nbd_negotiate_handle_info(NBDClient *client= , Error **errp) if (client->mode >=3D NBD_MODE_STRUCTURED) { myflags |=3D NBD_FLAG_SEND_DF; } + if (client->mode >=3D NBD_MODE_EXTENDED && + (client->contexts.count || client->opt =3D=3D NBD_OPT_INFO)) { + myflags |=3D NBD_FLAG_BLOCK_STAT_PAYLOAD; + } trace_nbd_negotiate_new_style_size_flags(exp->size, myflags); stq_be_p(buf, exp->size); stw_be_p(buf + 8, myflags); @@ -2424,6 +2431,81 @@ static int coroutine_fn nbd_co_send_bitmap(NBDClient= *client, return nbd_co_send_extents(client, request, ea, last, context_id, errp= ); } +/* + * nbd_co_block_status_payload_read + * Called when a client wants a subset of negotiated contexts via a + * BLOCK_STATUS payload. Check the payload for valid length and + * contents. On success, return 0 with request updated to effective + * length. If request was invalid but payload consumed, return 0 with + * request->len and request->contexts->count set to 0 (which will + * trigger an appropriate NBD_EINVAL response later on). On I/O + * error, return -EIO. + */ +static int +nbd_co_block_status_payload_read(NBDClient *client, NBDRequest *request, + Error **errp) +{ + int payload_len =3D request->len; + g_autofree char *buf =3D NULL; + size_t count, i, nr_bitmaps; + uint32_t id; + + assert(request->len <=3D NBD_MAX_BUFFER_SIZE); + assert(client->contexts.exp =3D=3D client->exp); + nr_bitmaps =3D client->exp->nr_export_bitmaps; + request->contexts =3D g_new0(NBDMetaContexts, 1); + request->contexts->exp =3D client->exp; + + if (payload_len % sizeof(uint32_t) || + payload_len < sizeof(NBDBlockStatusPayload) || + payload_len > (sizeof(NBDBlockStatusPayload) + + sizeof(id) * client->contexts.count)) { + goto skip; + } + + buf =3D g_malloc(payload_len); + if (nbd_read(client->ioc, buf, payload_len, + "CMD_BLOCK_STATUS data", errp) < 0) { + return -EIO; + } + trace_nbd_co_receive_request_payload_received(request->cookie, + payload_len); + request->contexts->bitmaps =3D g_new0(bool, nr_bitmaps); + count =3D (payload_len - sizeof(NBDBlockStatusPayload)) / sizeof(id); + payload_len =3D 0; + + for (i =3D 0; i < count; i++) { + id =3D ldl_be_p(buf + sizeof(NBDBlockStatusPayload) + sizeof(id) *= i); + if (id =3D=3D NBD_META_ID_BASE_ALLOCATION) { + if (request->contexts->base_allocation) { + goto skip; + } + request->contexts->base_allocation =3D true; + } else if (id =3D=3D NBD_META_ID_ALLOCATION_DEPTH) { + if (request->contexts->allocation_depth) { + goto skip; + } + request->contexts->allocation_depth =3D true; + } else { + if (id - NBD_META_ID_DIRTY_BITMAP > nr_bitmaps || + request->contexts->bitmaps[id - NBD_META_ID_DIRTY_BITMAP])= { + goto skip; + } + request->contexts->bitmaps[id - NBD_META_ID_DIRTY_BITMAP] =3D = true; + } + } + + request->len =3D ldq_be_p(buf); + request->contexts->count =3D count; + return 0; + + skip: + trace_nbd_co_receive_block_status_payload_compliance(request->from, + request->len); + request->len =3D request->contexts->count =3D 0; + return nbd_drop(client->ioc, payload_len, errp); +} + /* nbd_co_receive_request * Collect a client request. Return 0 if request looks valid, -EIO to drop * connection right away, -EAGAIN to indicate we were interrupted and the @@ -2470,7 +2552,13 @@ static int coroutine_fn nbd_co_receive_request(NBDRe= questData *req, NBDRequest * if (request->type =3D=3D NBD_CMD_WRITE || extended_with_payload) { payload_len =3D request->len; - if (request->type !=3D NBD_CMD_WRITE) { + if (request->type =3D=3D NBD_CMD_BLOCK_STATUS) { + ret =3D nbd_co_block_status_payload_read(client, request, errp= ); + if (ret < 0) { + return ret; + } + payload_len =3D 0; + } else if (request->type !=3D NBD_CMD_WRITE) { /* * For now, we don't support payloads on other * commands; but we can keep the connection alive. @@ -2491,7 +2579,8 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * error_setg(errp, "No memory"); return -ENOMEM; } - } else if (request->type =3D=3D NBD_CMD_BLOCK_STATUS) { + } else if (request->type =3D=3D NBD_CMD_BLOCK_STATUS && + !extended_with_payload) { request->contexts =3D &client->contexts; } @@ -2547,6 +2636,9 @@ static int coroutine_fn nbd_co_receive_request(NBDReq= uestData *req, NBDRequest * valid_flags |=3D NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO; } else if (request->type =3D=3D NBD_CMD_BLOCK_STATUS) { valid_flags |=3D NBD_CMD_FLAG_REQ_ONE; + if (client->mode >=3D NBD_MODE_EXTENDED && client->contexts.count)= { + valid_flags |=3D NBD_CMD_FLAG_PAYLOAD_LEN; + } } if (request->flags & ~valid_flags) { error_setg(errp, "unsupported flags for command %s (got 0x%x)", @@ -2712,7 +2804,8 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, "discard failed", errp); case NBD_CMD_BLOCK_STATUS: - if (!request->len) { + assert(request->contexts); + if (!request->len && !(request->flags & NBD_CMD_FLAG_PAYLOAD_LEN))= { return nbd_send_generic_reply(client, request, -EINVAL, "need non-zero length", errp); } diff --git a/qemu-nbd.c b/qemu-nbd.c index 1d155fc2c66..cbca0eeee62 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -222,6 +222,7 @@ static int qemu_nbd_client_list(SocketAddress *saddr, Q= CryptoTLSCreds *tls, [NBD_FLAG_SEND_RESIZE_BIT] =3D "resize", [NBD_FLAG_SEND_CACHE_BIT] =3D "cache", [NBD_FLAG_SEND_FAST_ZERO_BIT] =3D "fast-zero", + [NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT] =3D "block-status-payl= oad", }; printf(" size: %" PRIu64 "\n", list[i].size); diff --git a/nbd/trace-events b/nbd/trace-events index 51bfb129c95..a1af6d003b4 100644 --- a/nbd/trace-events +++ b/nbd/trace-events @@ -70,6 +70,7 @@ nbd_co_send_chunk_read(uint64_t cookie, uint64_t offset, = void *data, size_t size nbd_co_send_chunk_read_hole(uint64_t cookie, uint64_t offset, size_t size)= "Send structured read hole reply: cookie =3D %" PRIu64 ", offset =3D %" PR= Iu64 ", len =3D %zu" nbd_co_send_extents(uint64_t cookie, unsigned int extents, uint32_t id, ui= nt64_t length, int last) "Send block status reply: cookie =3D %" PRIu64 ", = extents =3D %u, context =3D %d (extents cover %" PRIu64 " bytes, last chunk= =3D %d)" nbd_co_send_chunk_error(uint64_t cookie, int err, const char *errname, con= st char *msg) "Send structured error reply: cookie =3D %" PRIu64 ", error = =3D %d (%s), msg =3D '%s'" +nbd_co_receive_block_status_payload_compliance(uint64_t from, int len) "cl= ient sent unusable block status payload: from=3D0x%" PRIx64 ", len=3D0x%x" nbd_co_receive_request_decode_type(uint64_t cookie, uint16_t type, const c= har *name) "Decoding type: cookie =3D %" PRIu64 ", type =3D %" PRIu16 " (%s= )" nbd_co_receive_request_payload_received(uint64_t cookie, uint64_t len) "Pa= yload received: cookie =3D %" PRIu64 ", len =3D %" PRIu64 nbd_co_receive_ext_payload_compliance(uint64_t from, uint64_t len) "client= sent non-compliant write without payload flag: from=3D0x%" PRIx64 ", len= =3D0x%" PRIx64 diff --git a/tests/qemu-iotests/223.out b/tests/qemu-iotests/223.out index b98582c38ea..b38f0b7963b 100644 --- a/tests/qemu-iotests/223.out +++ b/tests/qemu-iotests/223.out @@ -83,7 +83,7 @@ exports available: 0 exports available: 3 export: 'n' size: 4194304 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: 1 opt block: 4096 max block: 33554432 @@ -94,7 +94,7 @@ exports available: 3 export: 'n2' description: some text size: 4194304 - flags: 0xded ( flush fua trim zeroes df multi cache fast-zero ) + flags: 0x1ded ( flush fua trim zeroes df multi cache fast-zero block-sta= tus-payload ) min block: 1 opt block: 4096 max block: 33554432 @@ -104,7 +104,7 @@ exports available: 3 qemu:dirty-bitmap:b2 export: 'n3' size: 4194304 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: 1 opt block: 4096 max block: 33554432 @@ -205,7 +205,7 @@ exports available: 0 exports available: 3 export: 'n' size: 4194304 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: 1 opt block: 4096 max block: 33554432 @@ -216,7 +216,7 @@ exports available: 3 export: 'n2' description: some text size: 4194304 - flags: 0xded ( flush fua trim zeroes df multi cache fast-zero ) + flags: 0x1ded ( flush fua trim zeroes df multi cache fast-zero block-sta= tus-payload ) min block: 1 opt block: 4096 max block: 33554432 @@ -226,7 +226,7 @@ exports available: 3 qemu:dirty-bitmap:b2 export: 'n3' size: 4194304 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: 1 opt block: 4096 max block: 33554432 diff --git a/tests/qemu-iotests/307.out b/tests/qemu-iotests/307.out index 2b9a6a67a1a..f645f3315f8 100644 --- a/tests/qemu-iotests/307.out +++ b/tests/qemu-iotests/307.out @@ -15,7 +15,7 @@ wrote 4096/4096 bytes at offset 0 exports available: 1 export: 'fmt' size: 67108864 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: XXX opt block: XXX max block: XXX @@ -44,7 +44,7 @@ exports available: 1 exports available: 1 export: 'fmt' size: 67108864 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: XXX opt block: XXX max block: XXX @@ -76,7 +76,7 @@ exports available: 1 exports available: 2 export: 'fmt' size: 67108864 - flags: 0x58f ( readonly flush fua df multi cache ) + flags: 0x158f ( readonly flush fua df multi cache block-status-payload ) min block: XXX opt block: XXX max block: XXX @@ -86,7 +86,7 @@ exports available: 2 export: 'export1' description: This is the writable second export size: 67108864 - flags: 0xded ( flush fua trim zeroes df multi cache fast-zero ) + flags: 0x1ded ( flush fua trim zeroes df multi cache fast-zero block-sta= tus-payload ) min block: XXX opt block: XXX max block: XXX @@ -113,7 +113,7 @@ exports available: 1 export: 'export1' description: This is the writable second export size: 67108864 - flags: 0xded ( flush fua trim zeroes df multi cache fast-zero ) + flags: 0x1ded ( flush fua trim zeroes df multi cache fast-zero block-sta= tus-payload ) min block: XXX opt block: XXX max block: XXX diff --git a/tests/qemu-iotests/tests/nbd-qemu-allocation.out b/tests/qemu-= iotests/tests/nbd-qemu-allocation.out index 659276032b0..794d1bfce62 100644 --- a/tests/qemu-iotests/tests/nbd-qemu-allocation.out +++ b/tests/qemu-iotests/tests/nbd-qemu-allocation.out @@ -17,7 +17,7 @@ wrote 2097152/2097152 bytes at offset 1048576 exports available: 1 export: '' size: 4194304 - flags: 0x48f ( readonly flush fua df cache ) + flags: 0x148f ( readonly flush fua df cache block-status-payload ) min block: 1 opt block: 4096 max block: 33554432 --=20 2.40.1