From nobody Thu Apr 25 06:55:40 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653296295; cv=none; d=zohomail.com; s=zohoarc; b=iLJVXJlvHndpC/tu5Q+KMH0RDT02/tdlxsRrt1fwcSbJ4yKtYTRC2+UCd+LfIR+q+oDV8rZaDWJWzfSSifKKH2I9LIguKVMUNYFvpkWX+nmsm7GQAJaJ/UB34JQK04ibHTfo7zLc+eSr3tjq+V1lv6RRVW22Mb8cYpSABEom7wA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653296295; h=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=4e8cWc0LcmrlXhvfwWsJ902iaVX9k0U46lLMhIYltgQ=; b=D1PhnkO7nh0fXwRDINRszvwbnWau4M22omRBcf82ihLTYTOF2KODNuXEEmYbEgqJcqCHIupVHQQqnc+9frbSgLpy3TqZ/dk+25YgW9fqswbWIiO9Yrrj3ruTlZ3GEpRr7xOSV/JcVOVLQuGQU3oGADNmyjjqQ41dvn5e/Ppxe64= 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=fail 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 1653296295824147.88481809732718; Mon, 23 May 2022 01:58:15 -0700 (PDT) Received: from localhost ([::1]:44544 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt3tG-0005Tv-BF for importer2@patchew.org; Mon, 23 May 2022 04:58:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3md-00023i-Vg for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:30 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]:37476) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mZ-0000Zv-4t for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:22 -0400 Received: by mail-pl1-x633.google.com with SMTP id m12so12498694plb.4 for ; Mon, 23 May 2022 01:51:07 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id i3-20020aa796e3000000b0050dc76281c8sm6523275pfq.162.2022.05.23.01.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4e8cWc0LcmrlXhvfwWsJ902iaVX9k0U46lLMhIYltgQ=; b=d9ApPTzr45xs9AmwbWnDBwlrveJsdigEeEcLB1Ywx4ZMTNXLgYzUN1Yr8fhbxCipZk ls02C2sAUoDqIlTlKAwy8UP5NLYLnpId5nr+yZ8TLoe7JdezGmjNRj058LqjXYr0Xz5S 52HuN6Xz6pv6e1I2Al9YwkXHfvG//PrPZvxC6XAAn6dE/ehWbAqobeFPa+FopNJp8YMK VccC7/OR0S7e3/erM3q/MAuTTMDDxPNHQ3EyDEgAqgPnSXHQc19VpSYgrjVn1gBECEgc nxoFlu0+7R8PALq+wbnos+mFwfNLGf1RfXuF2Z0Sfk5Rjts/9VPTV8xFsjUzjejUXk52 VKKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4e8cWc0LcmrlXhvfwWsJ902iaVX9k0U46lLMhIYltgQ=; b=YT61uUgdAKjB8hxcOY/DLqd8QNEv2m9iJfP/vRJ2CuZPiPylzgpxQDW0Vy+dhaaw3n KLr4XsmRzjx0WxuGEbNyU57uZWX1wwAayeFutJD+hoCqSPNXNU9xWs4nVYtUeA5OFCDR 7NMHZw2/6ie2+PPxG3KF2JHX2LpxppKnDdpZdP0nYm9uF1i7iPc62XPPmvQBOn7ZdN5J BgkIeHZUSAEetiDbqH3eWX04JOLIfMiWgv65k5t334TJ93EOtpmqiLVytV673qaKPU2O 26fwqPLtSHeKW9nQaewKCnKlwZNOBKHsgl3lHlm9yB7I4gPPQeUMOPUVwM4LprpnebE1 V1UQ== X-Gm-Message-State: AOAM531/HPYdKZIDrVD8h+xiRdo+S8iYVwg64i+efe4/zzSpuSZTszQ/ XK1zUwHq1mJW6gWexcO8Zlex X-Google-Smtp-Source: ABdhPJxlNTIkG1AKTAy/DNtzyQm8xKT10DYROo6vgg7hbLuetWZZk2gTUXnq8tqMykyvmTcRCBFzWg== X-Received: by 2002:a17:902:778e:b0:162:2cf7:28be with SMTP id o14-20020a170902778e00b001622cf728bemr2254547pll.0.1653295866596; Mon, 23 May 2022 01:51:06 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 1/8] block: Support passing NULL ops to blk_set_dev_ops() Date: Mon, 23 May 2022 16:46:04 +0800 Message-Id: <20220523084611.91-2-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::633; envelope-from=xieyongji@bytedance.com; helo=mail-pl1-x633.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653296296314100001 Content-Type: text/plain; charset="utf-8" This supports passing NULL ops to blk_set_dev_ops() so that we can remove stale ops in some cases. Signed-off-by: Xie Yongji Reviewed-by: Stefan Hajnoczi --- block/block-backend.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/block-backend.c b/block/block-backend.c index e0e1aff4b1..35457a6a1d 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1062,7 +1062,7 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDe= vOps *ops, blk->dev_opaque =3D opaque; =20 /* Are we currently quiesced? Should we enforce this right now? */ - if (blk->quiesce_counter && ops->drained_begin) { + if (blk->quiesce_counter && ops && ops->drained_begin) { ops->drained_begin(opaque); } } --=20 2.20.1 From nobody Thu Apr 25 06:55:40 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653298171; cv=none; d=zohomail.com; s=zohoarc; b=VcparWWY+fbqCw6nCWTqglInSJ2AW5aEgBtVOmrRsrrYaMiqUnFCtPmKbAUHM1rQ1B/e1i9m9VwpfJC/PKIolG7RYLpgob/ihZ94ugG213Z1VsGT+BOQvhkq2z/f25XaZSEtdf12fpibGJRpIe1QVr09/1DDglu9uUGW67uaeyU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653298171; h=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=rXBU7oVXT2+sbEtRKBkShF9MxMyy1u1HYI/7VyL+0mw=; b=N1uGUpijoT6oieq2ahrWdfLbObbL8BH+rj3lFXlGN6GufbtSsT+UfjnIwOCye6jPglx+Si2FxBU4/0VUahkJK98javBzTavimwGprhE30//S+18e/FmuTxHu1upbQscYPD7ZvM/mSsTS1/N9tHinI9Q1sCvwoYonKXYqmHNfdyk= 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=fail 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 1653298171811781.2461265302229; Mon, 23 May 2022 02:29:31 -0700 (PDT) Received: from localhost ([::1]:44128 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt4NW-0001ZC-OP for importer2@patchew.org; Mon, 23 May 2022 05:29:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3mf-00023j-HN for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:36 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]:37399) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mZ-0000aE-IV for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:22 -0400 Received: by mail-pf1-x436.google.com with SMTP id bo5so13090227pfb.4 for ; Mon, 23 May 2022 01:51:11 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id p39-20020a056a000a2700b0050dc76281f8sm6691320pfh.210.2022.05.23.01.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rXBU7oVXT2+sbEtRKBkShF9MxMyy1u1HYI/7VyL+0mw=; b=U5Ar90cAck6kyum9aO3ASjcPyKTKbT+fI1d4w9TDEQuSADXxk6eVeARnbMMwLhu1ue hD75sTHq6mGdEn/XqxjlVizxa0w/7b7PWEXDDWHThwb/FPFi6pSxEHPmkQFnnEKUgxMY /k3OZuNQqfRpboNCCJ5Y3vkCnVcxD0aGDitBCszF1P0DP1qWCBpGXinl6B2qqCFRPIFs gq98fxUrjKiE/nHmNZdtM+AFURn5hX6s6/q/byHsehqBEn9WaGmlD9iFmnYBrbj15N6W 8qe3FGk0SRomjjbGNHPn+J/EY+uKoLhli3PvzJOZJVoGyok7mLYSWpTpfzd2hHqyO5R0 7xNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rXBU7oVXT2+sbEtRKBkShF9MxMyy1u1HYI/7VyL+0mw=; b=q6n84s1SaLH0eCcLX6IcTSeuPUlu9Lh87pGhO09BQUqujdd0GbusDfhCv3b9spKvOv ZSYZrISM3BlNDLzhVuS5K0c9HANPhPfcwjvWj/WVBqc7Z3GZ+pqgUZCGEpF+8VPPvOsw dMeHzpevyzfdE7KaZs8VHXskABjrYSMiGkIYKLWAdL7W7v7x3aJ72RnaPPYdZTyB+536 ccop/xD8LQtCAI2AGucu+W29uCmsNDG1ZEwzCiFISx1RZQCndweSPa9BT3DM/ds+h5jW SUmW730bFtPdLk1bjj003/qm7cotK3vd68ckRHFAvg1VM8tzeTUbdDBSEdjM8wTbqSkn 5nWA== X-Gm-Message-State: AOAM53036cmm5cD1OYZfpNSIIJOOSvK6/zWUMy6MONmfIqxKobrH6/Bz D8xjFQwxqrswA5to8e5FaePh X-Google-Smtp-Source: ABdhPJxNJaTj463gFZFc+da0m+9jrWWaztGyiyXB4TF2ifZhblaToNJIJSssPrnT2gs5NaAGSrsiCA== X-Received: by 2002:aa7:8258:0:b0:50d:d365:70c8 with SMTP id e24-20020aa78258000000b0050dd36570c8mr22874180pfn.50.1653295870324; Mon, 23 May 2022 01:51:10 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 2/8] block/export: Fix incorrect length passed to vu_queue_push() Date: Mon, 23 May 2022 16:46:05 +0800 Message-Id: <20220523084611.91-3-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::436; envelope-from=xieyongji@bytedance.com; helo=mail-pf1-x436.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653298173950100001 Content-Type: text/plain; charset="utf-8" Now the req->size is set to the correct value only when handling VIRTIO_BLK_T_GET_ID request. This patch fixes it. Signed-off-by: Xie Yongji --- block/export/vhost-user-blk-server.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/block/export/vhost-user-blk-server.c b/block/export/vhost-user= -blk-server.c index b2e458ade3..19c6ee51d3 100644 --- a/block/export/vhost-user-blk-server.c +++ b/block/export/vhost-user-blk-server.c @@ -60,8 +60,7 @@ static void vu_blk_req_complete(VuBlkReq *req) { VuDev *vu_dev =3D &req->server->vu_dev; =20 - /* IO size with 1 extra status byte */ - vu_queue_push(vu_dev, req->vq, &req->elem, req->size + 1); + vu_queue_push(vu_dev, req->vq, &req->elem, req->size); vu_queue_notify(vu_dev, req->vq); =20 free(req); @@ -207,6 +206,7 @@ static void coroutine_fn vu_blk_virtio_process_req(void= *opaque) goto err; } =20 + req->size =3D iov_size(in_iov, in_num); /* We always touch the last byte, so just see how big in_iov is. */ req->in =3D (void *)in_iov[in_num - 1].iov_base + in_iov[in_num - 1].iov_len @@ -267,7 +267,6 @@ static void coroutine_fn vu_blk_virtio_process_req(void= *opaque) VIRTIO_BLK_ID_BYTES); snprintf(elem->in_sg[0].iov_base, size, "%s", "vhost_user_blk"); req->in->status =3D VIRTIO_BLK_S_OK; - req->size =3D elem->in_sg[0].iov_len; break; } case VIRTIO_BLK_T_DISCARD: --=20 2.20.1 From nobody Thu Apr 25 06:55:40 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653297421; cv=none; d=zohomail.com; s=zohoarc; b=nfQZjWXapcpp/GvRFgJYk05r6OyPlbRs1V7huaIoOqUfgJ/Oyp0IdX92iDa4gG9R9BwmuonMLyzAFgxpquTTgXKJ3X/XysA/qS3uNd1CUdT3DePLK30cV8GUeRkLRFDkyJhN15LBF9Rwf9cmMffE44KVH17CHhEBG2L81WE/uak= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653297421; h=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=v+n7UCk+0NaYrj/ZVdqCc6EnoBH7oWiyfG4sjNNhhO8=; b=YjMaIz8TR3j8zMwmRKmhuoYg97B/BEXVdwNhZZQpjy3Kt6KiZMab2xznpI/naovqv12ir9DX3yQvzE+0fjV3cfBZ0aDfSv5n1Un4wkp+wsNhxaZpkJxNFN8ajpHrol3SCAspWRndwk8Tu/RtQ9xtdU0LPeYymHJuJ/iuia1EciQ= 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=fail 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 1653297421705850.4100798433356; Mon, 23 May 2022 02:17:01 -0700 (PDT) Received: from localhost ([::1]:36422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt4BI-0003qi-1m for importer2@patchew.org; Mon, 23 May 2022 05:16:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56928) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3mf-00023v-6j for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:32 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]:46714) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mZ-0000aa-JN for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:24 -0400 Received: by mail-pj1-x102e.google.com with SMTP id v5-20020a17090a7c0500b001df84fa82f8so13042245pjf.5 for ; Mon, 23 May 2022 01:51:15 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id 16-20020a17090a0f1000b001e0484b2181sm1990072pjy.23.2022.05.23.01.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=v+n7UCk+0NaYrj/ZVdqCc6EnoBH7oWiyfG4sjNNhhO8=; b=fErrOSfeyf7nOsR7kwy2/jAPqpMdoFpcvHbNygoHWyOJx0IVDZ9TZMNpGSjAhY1puW iMLTj+F9+tgERLt8kxkpSV5p1wsLPUj5W3MVRz8AF6LOBxOV5D9EDB/CGJ5wiGihiqec TpmYtNTSsvftM49j/iWstxppxRXXQKRix3sFM1zIFnfkM5NyZAJi4ENHYvAs+AV/+yEj DTW7ttEuAM54eE22utYP/ZPqY8ag27lQEGrUsSBc5yxehQ1sel2ivnpdxQlF8yZE3syX iDYj3kdnUYkw5da23JV+zAIXjGQu/JTrqRh2xd193LLhUqJ7G9XH3yBwiN7oegx0zrN3 OEgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=v+n7UCk+0NaYrj/ZVdqCc6EnoBH7oWiyfG4sjNNhhO8=; b=O6vlRdVPzdCdkKXob9zTTF/kcxKPziaruK2sBUXr5qVLYDI0uvs2oYyLb0L5HgiSoZ HV9ltO6m+FtZIw96kD3YWsf4j/8awgsZ2Up3ZNcCSfnrZ/VmfGdNPgRUX4GlSRtP/EO6 DTNYj5gktIBPUBJr9roz+E4BWrsFx3PnHrB7stM0T5defv5rBirgxUpX28X0XBIb+QJl 7nM9iwC/OBlrJixDNo9kMVrsoG79CZ3cLPK3lwr0cwUxpIU+jh5epDnASDfVIegRNWcv UDEYEZXD5GUgRbTTgTXenj3UN4X7jBNTCiJzoCD3pFESp7w6Qf0rDCXUE41RjoYjZ07l alhg== X-Gm-Message-State: AOAM533I5XBchqQZqGOTIbQjoleO7JO6MGcaLlLYDqbRu82F9Q9yBTJs hTF9uXSXSl0FAfgQUg6kyMzw8iVb7HoJzeo= X-Google-Smtp-Source: ABdhPJzA4t3fBu7hTDaqBVHwz5tqcgmLOGkoV78g//1llEQeJ6TrSm8fluCURR9FdTEhoZw/73oXZQ== X-Received: by 2002:a17:902:9a4c:b0:156:6735:b438 with SMTP id x12-20020a1709029a4c00b001566735b438mr21565105plv.46.1653295874116; Mon, 23 May 2022 01:51:14 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 3/8] block/export: Abstract out the logic of virtio-blk I/O process Date: Mon, 23 May 2022 16:46:06 +0800 Message-Id: <20220523084611.91-4-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::102e; envelope-from=xieyongji@bytedance.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653297423591100001 Content-Type: text/plain; charset="utf-8" Abstract the common logic of virtio-blk I/O process to a function named virtio_blk_process_req(). It's needed for the following commit. Signed-off-by: Xie Yongji --- MAINTAINERS | 2 + block/export/meson.build | 2 +- block/export/vhost-user-blk-server.c | 259 +++------------------------ block/export/virtio-blk-handler.c | 240 +++++++++++++++++++++++++ block/export/virtio-blk-handler.h | 37 ++++ 5 files changed, 301 insertions(+), 239 deletions(-) create mode 100644 block/export/virtio-blk-handler.c create mode 100644 block/export/virtio-blk-handler.h diff --git a/MAINTAINERS b/MAINTAINERS index dff0200f70..01fb25421b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3540,6 +3540,8 @@ M: Coiby Xu S: Maintained F: block/export/vhost-user-blk-server.c F: block/export/vhost-user-blk-server.h +F: block/export/virtio-blk-handler.c +F: block/export/virtio-blk-handler.h F: include/qemu/vhost-user-server.h F: tests/qtest/libqos/vhost-user-blk.c F: tests/qtest/libqos/vhost-user-blk.h diff --git a/block/export/meson.build b/block/export/meson.build index 0a08e384c7..431e47ca51 100644 --- a/block/export/meson.build +++ b/block/export/meson.build @@ -1,7 +1,7 @@ blockdev_ss.add(files('export.c')) =20 if have_vhost_user_blk_server - blockdev_ss.add(files('vhost-user-blk-server.c')) + blockdev_ss.add(files('vhost-user-blk-server.c', 'virtio-blk-handler.c= ')) endif =20 blockdev_ss.add(when: fuse, if_true: files('fuse.c')) diff --git a/block/export/vhost-user-blk-server.c b/block/export/vhost-user= -blk-server.c index 19c6ee51d3..c9c290cc4c 100644 --- a/block/export/vhost-user-blk-server.c +++ b/block/export/vhost-user-blk-server.c @@ -17,31 +17,15 @@ #include "vhost-user-blk-server.h" #include "qapi/error.h" #include "qom/object_interfaces.h" -#include "sysemu/block-backend.h" #include "util/block-helpers.h" - -/* - * Sector units are 512 bytes regardless of the - * virtio_blk_config->blk_size value. - */ -#define VIRTIO_BLK_SECTOR_BITS 9 -#define VIRTIO_BLK_SECTOR_SIZE (1ull << VIRTIO_BLK_SECTOR_BITS) +#include "virtio-blk-handler.h" =20 enum { VHOST_USER_BLK_NUM_QUEUES_DEFAULT =3D 1, - VHOST_USER_BLK_MAX_DISCARD_SECTORS =3D 32768, - VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS =3D 32768, -}; -struct virtio_blk_inhdr { - unsigned char status; }; =20 typedef struct VuBlkReq { VuVirtqElement elem; - int64_t sector_num; - size_t size; - struct virtio_blk_inhdr *in; - struct virtio_blk_outhdr out; VuServer *server; struct VuVirtq *vq; } VuBlkReq; @@ -50,247 +34,44 @@ typedef struct VuBlkReq { typedef struct { BlockExport export; VuServer vu_server; - uint32_t blk_size; + VirtioBlkHandler handler; QIOChannelSocket *sioc; struct virtio_blk_config blkcfg; - bool writable; } VuBlkExport; =20 -static void vu_blk_req_complete(VuBlkReq *req) +static void vu_blk_req_complete(VuBlkReq *req, size_t in_len) { VuDev *vu_dev =3D &req->server->vu_dev; =20 - vu_queue_push(vu_dev, req->vq, &req->elem, req->size); + vu_queue_push(vu_dev, req->vq, &req->elem, in_len); vu_queue_notify(vu_dev, req->vq); =20 free(req); } =20 -static bool vu_blk_sect_range_ok(VuBlkExport *vexp, uint64_t sector, - size_t size) -{ - uint64_t nb_sectors; - uint64_t total_sectors; - - if (size % VIRTIO_BLK_SECTOR_SIZE) { - return false; - } - - nb_sectors =3D size >> VIRTIO_BLK_SECTOR_BITS; - - QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE !=3D VIRTIO_BLK_SECTOR_SIZE); - if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { - return false; - } - if ((sector << VIRTIO_BLK_SECTOR_BITS) % vexp->blk_size) { - return false; - } - blk_get_geometry(vexp->export.blk, &total_sectors); - if (sector > total_sectors || nb_sectors > total_sectors - sector) { - return false; - } - return true; -} - -static int coroutine_fn -vu_blk_discard_write_zeroes(VuBlkExport *vexp, struct iovec *iov, - uint32_t iovcnt, uint32_t type) -{ - BlockBackend *blk =3D vexp->export.blk; - struct virtio_blk_discard_write_zeroes desc; - ssize_t size; - uint64_t sector; - uint32_t num_sectors; - uint32_t max_sectors; - uint32_t flags; - int bytes; - - /* Only one desc is currently supported */ - if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) { - return VIRTIO_BLK_S_UNSUPP; - } - - size =3D iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); - if (unlikely(size !=3D sizeof(desc))) { - error_report("Invalid size %zd, expected %zu", size, sizeof(desc)); - return VIRTIO_BLK_S_IOERR; - } - - sector =3D le64_to_cpu(desc.sector); - num_sectors =3D le32_to_cpu(desc.num_sectors); - flags =3D le32_to_cpu(desc.flags); - max_sectors =3D (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) ? - VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS : - VHOST_USER_BLK_MAX_DISCARD_SECTORS; - - /* This check ensures that 'bytes' fits in an int */ - if (unlikely(num_sectors > max_sectors)) { - return VIRTIO_BLK_S_IOERR; - } - - bytes =3D num_sectors << VIRTIO_BLK_SECTOR_BITS; - - if (unlikely(!vu_blk_sect_range_ok(vexp, sector, bytes))) { - return VIRTIO_BLK_S_IOERR; - } - - /* - * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for disc= ard - * and write zeroes commands if any unknown flag is set. - */ - if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { - return VIRTIO_BLK_S_UNSUPP; - } - - if (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) { - int blk_flags =3D 0; - - if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { - blk_flags |=3D BDRV_REQ_MAY_UNMAP; - } - - if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS, - bytes, blk_flags) =3D=3D 0) { - return VIRTIO_BLK_S_OK; - } - } else if (type =3D=3D VIRTIO_BLK_T_DISCARD) { - /* - * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for - * discard commands if the unmap flag is set. - */ - if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { - return VIRTIO_BLK_S_UNSUPP; - } - - if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS, - bytes) =3D=3D 0) { - return VIRTIO_BLK_S_OK; - } - } - - return VIRTIO_BLK_S_IOERR; -} - /* Called with server refcount increased, must decrease before returning */ static void coroutine_fn vu_blk_virtio_process_req(void *opaque) { VuBlkReq *req =3D opaque; VuServer *server =3D req->server; VuVirtqElement *elem =3D &req->elem; - uint32_t type; - VuBlkExport *vexp =3D container_of(server, VuBlkExport, vu_server); - BlockBackend *blk =3D vexp->export.blk; - + VirtioBlkHandler *handler =3D &vexp->handler; struct iovec *in_iov =3D elem->in_sg; struct iovec *out_iov =3D elem->out_sg; unsigned in_num =3D elem->in_num; unsigned out_num =3D elem->out_num; - - /* refer to hw/block/virtio_blk.c */ - if (elem->out_num < 1 || elem->in_num < 1) { - error_report("virtio-blk request missing headers"); - goto err; - } - - if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out, - sizeof(req->out)) !=3D sizeof(req->out))) { - error_report("virtio-blk request outhdr too short"); - goto err; - } - - iov_discard_front(&out_iov, &out_num, sizeof(req->out)); - - if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { - error_report("virtio-blk request inhdr too short"); - goto err; - } - - req->size =3D iov_size(in_iov, in_num); - /* We always touch the last byte, so just see how big in_iov is. */ - req->in =3D (void *)in_iov[in_num - 1].iov_base - + in_iov[in_num - 1].iov_len - - sizeof(struct virtio_blk_inhdr); - iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); - - type =3D le32_to_cpu(req->out.type); - switch (type & ~VIRTIO_BLK_T_BARRIER) { - case VIRTIO_BLK_T_IN: - case VIRTIO_BLK_T_OUT: { - QEMUIOVector qiov; - int64_t offset; - ssize_t ret =3D 0; - bool is_write =3D type & VIRTIO_BLK_T_OUT; - req->sector_num =3D le64_to_cpu(req->out.sector); - - if (is_write && !vexp->writable) { - req->in->status =3D VIRTIO_BLK_S_IOERR; - break; - } - - if (is_write) { - qemu_iovec_init_external(&qiov, out_iov, out_num); - } else { - qemu_iovec_init_external(&qiov, in_iov, in_num); - } - - if (unlikely(!vu_blk_sect_range_ok(vexp, - req->sector_num, - qiov.size))) { - req->in->status =3D VIRTIO_BLK_S_IOERR; - break; - } - - offset =3D req->sector_num << VIRTIO_BLK_SECTOR_BITS; - - if (is_write) { - ret =3D blk_co_pwritev(blk, offset, qiov.size, &qiov, 0); - } else { - ret =3D blk_co_preadv(blk, offset, qiov.size, &qiov, 0); - } - if (ret >=3D 0) { - req->in->status =3D VIRTIO_BLK_S_OK; - } else { - req->in->status =3D VIRTIO_BLK_S_IOERR; - } - break; - } - case VIRTIO_BLK_T_FLUSH: - if (blk_co_flush(blk) =3D=3D 0) { - req->in->status =3D VIRTIO_BLK_S_OK; - } else { - req->in->status =3D VIRTIO_BLK_S_IOERR; - } - break; - case VIRTIO_BLK_T_GET_ID: { - size_t size =3D MIN(iov_size(&elem->in_sg[0], in_num), - VIRTIO_BLK_ID_BYTES); - snprintf(elem->in_sg[0].iov_base, size, "%s", "vhost_user_blk"); - req->in->status =3D VIRTIO_BLK_S_OK; - break; + int in_len; + + in_len =3D virtio_blk_process_req(handler, in_iov, out_iov, + in_num, out_num); + if (in_len < 0) { + free(req); + vhost_user_server_unref(server); + return; } - case VIRTIO_BLK_T_DISCARD: - case VIRTIO_BLK_T_WRITE_ZEROES: { - if (!vexp->writable) { - req->in->status =3D VIRTIO_BLK_S_IOERR; - break; - } - - req->in->status =3D vu_blk_discard_write_zeroes(vexp, out_iov, out= _num, - type); - break; - } - default: - req->in->status =3D VIRTIO_BLK_S_UNSUPP; - break; - } - - vu_blk_req_complete(req); - vhost_user_server_unref(server); - return; =20 -err: - free(req); + vu_blk_req_complete(req, in_len); vhost_user_server_unref(server); } =20 @@ -347,7 +128,7 @@ static uint64_t vu_blk_get_features(VuDev *dev) 1ull << VIRTIO_RING_F_EVENT_IDX | 1ull << VHOST_USER_F_PROTOCOL_FEATURES; =20 - if (!vexp->writable) { + if (!vexp->handler.writable) { features |=3D 1ull << VIRTIO_BLK_F_RO; } =20 @@ -454,12 +235,12 @@ vu_blk_initialize_config(BlockDriverState *bs, config->opt_io_size =3D cpu_to_le32(1); config->num_queues =3D cpu_to_le16(num_queues); config->max_discard_sectors =3D - cpu_to_le32(VHOST_USER_BLK_MAX_DISCARD_SECTORS); + cpu_to_le32(VIRTIO_BLK_MAX_DISCARD_SECTORS); config->max_discard_seg =3D cpu_to_le32(1); config->discard_sector_alignment =3D cpu_to_le32(blk_size >> VIRTIO_BLK_SECTOR_BITS); config->max_write_zeroes_sectors - =3D cpu_to_le32(VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS); + =3D cpu_to_le32(VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS); config->max_write_zeroes_seg =3D cpu_to_le32(1); } =20 @@ -479,7 +260,6 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExp= ortOptions *opts, uint64_t logical_block_size; uint16_t num_queues =3D VHOST_USER_BLK_NUM_QUEUES_DEFAULT; =20 - vexp->writable =3D opts->writable; vexp->blkcfg.wce =3D 0; =20 if (vu_opts->has_logical_block_size) { @@ -493,7 +273,6 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExp= ortOptions *opts, error_propagate(errp, local_err); return -EINVAL; } - vexp->blk_size =3D logical_block_size; =20 if (vu_opts->has_num_queues) { num_queues =3D vu_opts->num_queues; @@ -502,6 +281,10 @@ static int vu_blk_exp_create(BlockExport *exp, BlockEx= portOptions *opts, error_setg(errp, "num-queues must be greater than 0"); return -EINVAL; } + vexp->handler.blk =3D exp->blk; + vexp->handler.serial =3D "vhost_user_blk"; + vexp->handler.logical_block_size =3D logical_block_size; + vexp->handler.writable =3D opts->writable; =20 vu_blk_initialize_config(blk_bs(exp->blk), &vexp->blkcfg, logical_block_size, num_queues); diff --git a/block/export/virtio-blk-handler.c b/block/export/virtio-blk-ha= ndler.c new file mode 100644 index 0000000000..313666e8ab --- /dev/null +++ b/block/export/virtio-blk-handler.c @@ -0,0 +1,240 @@ +/* + * Handler for virtio-blk I/O + * + * Copyright (c) 2020 Red Hat, Inc. + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * + * Author: + * Coiby Xu + * Xie Yongji + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "virtio-blk-handler.h" + +#include "standard-headers/linux/virtio_blk.h" + +struct virtio_blk_inhdr { + unsigned char status; +}; + +static bool virtio_blk_sect_range_ok(BlockBackend *blk, uint32_t block_siz= e, + uint64_t sector, size_t size) +{ + uint64_t nb_sectors; + uint64_t total_sectors; + + if (size % VIRTIO_BLK_SECTOR_SIZE) { + return false; + } + + nb_sectors =3D size >> VIRTIO_BLK_SECTOR_BITS; + + QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE !=3D VIRTIO_BLK_SECTOR_SIZE); + if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { + return false; + } + if ((sector << VIRTIO_BLK_SECTOR_BITS) % block_size) { + return false; + } + blk_get_geometry(blk, &total_sectors); + if (sector > total_sectors || nb_sectors > total_sectors - sector) { + return false; + } + return true; +} + +static int coroutine_fn +virtio_blk_discard_write_zeroes(VirtioBlkHandler *handler, struct iovec *i= ov, + uint32_t iovcnt, uint32_t type) +{ + BlockBackend *blk =3D handler->blk; + struct virtio_blk_discard_write_zeroes desc; + ssize_t size; + uint64_t sector; + uint32_t num_sectors; + uint32_t max_sectors; + uint32_t flags; + int bytes; + + /* Only one desc is currently supported */ + if (unlikely(iov_size(iov, iovcnt) > sizeof(desc))) { + return VIRTIO_BLK_S_UNSUPP; + } + + size =3D iov_to_buf(iov, iovcnt, 0, &desc, sizeof(desc)); + if (unlikely(size !=3D sizeof(desc))) { + error_report("Invalid size %zd, expected %zu", size, sizeof(desc)); + return VIRTIO_BLK_S_IOERR; + } + + sector =3D le64_to_cpu(desc.sector); + num_sectors =3D le32_to_cpu(desc.num_sectors); + flags =3D le32_to_cpu(desc.flags); + max_sectors =3D (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) ? + VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS : + VIRTIO_BLK_MAX_DISCARD_SECTORS; + + /* This check ensures that 'bytes' fits in an int */ + if (unlikely(num_sectors > max_sectors)) { + return VIRTIO_BLK_S_IOERR; + } + + bytes =3D num_sectors << VIRTIO_BLK_SECTOR_BITS; + + if (unlikely(!virtio_blk_sect_range_ok(blk, handler->logical_block_siz= e, + sector, bytes))) { + return VIRTIO_BLK_S_IOERR; + } + + /* + * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for disc= ard + * and write zeroes commands if any unknown flag is set. + */ + if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { + return VIRTIO_BLK_S_UNSUPP; + } + + if (type =3D=3D VIRTIO_BLK_T_WRITE_ZEROES) { + int blk_flags =3D 0; + + if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { + blk_flags |=3D BDRV_REQ_MAY_UNMAP; + } + + if (blk_co_pwrite_zeroes(blk, sector << VIRTIO_BLK_SECTOR_BITS, + bytes, blk_flags) =3D=3D 0) { + return VIRTIO_BLK_S_OK; + } + } else if (type =3D=3D VIRTIO_BLK_T_DISCARD) { + /* + * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for + * discard commands if the unmap flag is set. + */ + if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { + return VIRTIO_BLK_S_UNSUPP; + } + + if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS, + bytes) =3D=3D 0) { + return VIRTIO_BLK_S_OK; + } + } + + return VIRTIO_BLK_S_IOERR; +} + +int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler, + struct iovec *in_iov, + struct iovec *out_iov, + unsigned int in_num, + unsigned int out_num) +{ + BlockBackend *blk =3D handler->blk; + struct virtio_blk_inhdr *in; + struct virtio_blk_outhdr out; + uint32_t type; + int in_len; + + if (out_num < 1 || in_num < 1) { + error_report("virtio-blk request missing headers"); + return -EINVAL; + } + + if (unlikely(iov_to_buf(out_iov, out_num, 0, &out, + sizeof(out)) !=3D sizeof(out))) { + error_report("virtio-blk request outhdr too short"); + return -EINVAL; + } + + iov_discard_front(&out_iov, &out_num, sizeof(out)); + + if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { + error_report("virtio-blk request inhdr too short"); + return -EINVAL; + } + + /* We always touch the last byte, so just see how big in_iov is. */ + in_len =3D iov_size(in_iov, in_num); + in =3D (void *)in_iov[in_num - 1].iov_base + + in_iov[in_num - 1].iov_len + - sizeof(struct virtio_blk_inhdr); + iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); + + type =3D le32_to_cpu(out.type); + switch (type & ~VIRTIO_BLK_T_BARRIER) { + case VIRTIO_BLK_T_IN: + case VIRTIO_BLK_T_OUT: { + QEMUIOVector qiov; + int64_t offset; + ssize_t ret =3D 0; + bool is_write =3D type & VIRTIO_BLK_T_OUT; + int64_t sector_num =3D le64_to_cpu(out.sector); + + if (is_write && !handler->writable) { + in->status =3D VIRTIO_BLK_S_IOERR; + break; + } + + if (is_write) { + qemu_iovec_init_external(&qiov, out_iov, out_num); + } else { + qemu_iovec_init_external(&qiov, in_iov, in_num); + } + + if (unlikely(!virtio_blk_sect_range_ok(blk, + handler->logical_block_size, + sector_num, qiov.size))) { + in->status =3D VIRTIO_BLK_S_IOERR; + break; + } + + offset =3D sector_num << VIRTIO_BLK_SECTOR_BITS; + + if (is_write) { + ret =3D blk_co_pwritev(blk, offset, qiov.size, &qiov, 0); + } else { + ret =3D blk_co_preadv(blk, offset, qiov.size, &qiov, 0); + } + if (ret >=3D 0) { + in->status =3D VIRTIO_BLK_S_OK; + } else { + in->status =3D VIRTIO_BLK_S_IOERR; + } + break; + } + case VIRTIO_BLK_T_FLUSH: + if (blk_co_flush(blk) =3D=3D 0) { + in->status =3D VIRTIO_BLK_S_OK; + } else { + in->status =3D VIRTIO_BLK_S_IOERR; + } + break; + case VIRTIO_BLK_T_GET_ID: { + size_t size =3D MIN(strlen(handler->serial) + 1, + MIN(iov_size(in_iov, in_num), + VIRTIO_BLK_ID_BYTES)); + iov_from_buf(in_iov, in_num, 0, handler->serial, size); + in->status =3D VIRTIO_BLK_S_OK; + break; + } + case VIRTIO_BLK_T_DISCARD: + case VIRTIO_BLK_T_WRITE_ZEROES: + if (!handler->writable) { + in->status =3D VIRTIO_BLK_S_IOERR; + break; + } + in->status =3D virtio_blk_discard_write_zeroes(handler, out_iov, + out_num, type); + break; + default: + in->status =3D VIRTIO_BLK_S_UNSUPP; + break; + } + + return in_len; +} diff --git a/block/export/virtio-blk-handler.h b/block/export/virtio-blk-ha= ndler.h new file mode 100644 index 0000000000..1c7a5e32ad --- /dev/null +++ b/block/export/virtio-blk-handler.h @@ -0,0 +1,37 @@ +/* + * Handler for virtio-blk I/O + * + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * + * Author: + * Xie Yongji + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#ifndef VIRTIO_BLK_HANDLER_H +#define VIRTIO_BLK_HANDLER_H + +#include "sysemu/block-backend.h" + +#define VIRTIO_BLK_SECTOR_BITS 9 +#define VIRTIO_BLK_SECTOR_SIZE (1ULL << VIRTIO_BLK_SECTOR_BITS) + +#define VIRTIO_BLK_MAX_DISCARD_SECTORS 32768 +#define VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS 32768 + +typedef struct { + BlockBackend *blk; + const char *serial; + uint32_t logical_block_size; + bool writable; +} VirtioBlkHandler; + +int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler, + struct iovec *in_iov, + struct iovec *out_iov, + unsigned int in_num, + unsigned int out_num); + +#endif /* VIRTIO_BLK_HANDLER_H */ --=20 2.20.1 From nobody Thu Apr 25 06:55:41 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653296803; cv=none; d=zohomail.com; s=zohoarc; b=KKXCW8dlaWObm56bR6t2Ngw0AyZCTn3ySbkt2Zv3bQxkbggbqopyena/syCW1LsFHTOz8A466auTuPzi3i3KGpC2oIt5/t6rjfN7NhbTUFFutbOYjYgV73GGfulkCa97ijsXVYyWYQK8OT+cGeK/E+865FG5PHBXUngKLqaZW58= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653296803; h=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=wogOiPNbgQLtSyKnX8RntgQOk505JZd/zCeLZIHT2C0=; b=O5FJH/FoV9Ydlbm/2L4PlzyRT5pL5Fh3uYxz1nNTY4N2w83scOVG7SYz1rK6Z4yDzkixNIOchfFYUYd5s+7omqjtPDI4a/THt65fQNjGM01w+vwP4ietkuzZDu7a7S8hBhrj8xwRQPTFLpNOgvyZfE74i1hhBKQxDCbt2UbcGbU= 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=fail 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 1653296803386860.0130233183893; Mon, 23 May 2022 02:06:43 -0700 (PDT) Received: from localhost ([::1]:50350 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt41S-0001uh-8n for importer2@patchew.org; Mon, 23 May 2022 05:06:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56938) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3mh-00023z-EU for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:36 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]:37479) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mZ-0000an-Mn for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:25 -0400 Received: by mail-pl1-x635.google.com with SMTP id m12so12499059plb.4 for ; Mon, 23 May 2022 01:51:18 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id k12-20020a17090a39cc00b001cd4989fed3sm6628834pjf.31.2022.05.23.01.51.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wogOiPNbgQLtSyKnX8RntgQOk505JZd/zCeLZIHT2C0=; b=5/8DLHkM6UJbyZl5iKAu5wzUb6rSJ7qKlcyywnSQ1mlXwoOZFBl2ac7q1q7vOY4gCr f+rMHfThvNhh4zS0DvAxHtGXEbQhns87HMnXsSU6DgBKGa53WbJ1U7u56vk2Yn6mmlEa OoMRu5MjjVdX9+EZMlK0tuFHhfDPvUDIWPU3RkBBzPJnOKKPw+06LyBKZ1FShxwN7Gan lAbxZnJNBnh0xklQvbd9dsqszGHQERCF6HcVXLwnDFGyP2vqsU163Iix9cNnShZXzF/y RmQDUK7TZvLwMH9pX87ZV0m0/xhukeFC9aXAJ6RkTV1ueJh9mEahB7F/pTmwt4PrJb2Q 2MRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wogOiPNbgQLtSyKnX8RntgQOk505JZd/zCeLZIHT2C0=; b=LZw9Xve8T8RHQ7Bqsemjf6uwCg+DNfE0AIyglwyrtTSx/8/aFJYgKBSVPNP6IOhs2J oD38VW+lywL+0DoGEiYGOS1g6ZL3CQQmtdi8iXasNNgyq/AxA9dMy8PMRKdcHHHUuPmt KSFJiNfPG4PK2a6n4Bn9BevlL+rWPyL4qfCGDTkT76SDo9Cuwmysoj3HJBDKO1MT0pQf iIsh5HpMfZdcA/SBVQmaWwStyH6FWnP2dEIbMj4d87KaHp3jAakIAbCJBBbopHV1iTdy zVgCCP3EQfElx0YF7OyH2i+k12CZXGoc98aCAAEYCeuIoZsKIWUKBwQNGrdpzUT6wRxt niwA== X-Gm-Message-State: AOAM5324CUJcTTXzBKw6CixTHg9J/ik6+VtIBEBkqBxHRQoSbt5ADSll SfJghxvD/jic8RcbeJ0zzFN0 X-Google-Smtp-Source: ABdhPJz1ReAK0AKHaaFNbYVd+DWIcvOI6giZ+/jHcqzjMHBZ+p6zB3HPYPP/Kl1Di6q9vNSjI4VM4w== X-Received: by 2002:a17:90a:7441:b0:1df:5f54:502c with SMTP id o1-20020a17090a744100b001df5f54502cmr25987956pjk.129.1653295877958; Mon, 23 May 2022 01:51:17 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 4/8] linux-headers: Add vduse.h Date: Mon, 23 May 2022 16:46:07 +0800 Message-Id: <20220523084611.91-5-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::635; envelope-from=xieyongji@bytedance.com; helo=mail-pl1-x635.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653296805434100001 Content-Type: text/plain; charset="utf-8" This adds vduse header to linux headers so that the relevant VDUSE API can be used in subsequent patches. Signed-off-by: Xie Yongji Reviewed-by: Stefan Hajnoczi --- linux-headers/linux/vduse.h | 306 ++++++++++++++++++++++++++++++++ scripts/update-linux-headers.sh | 2 +- 2 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 linux-headers/linux/vduse.h diff --git a/linux-headers/linux/vduse.h b/linux-headers/linux/vduse.h new file mode 100644 index 0000000000..d47b004ce6 --- /dev/null +++ b/linux-headers/linux/vduse.h @@ -0,0 +1,306 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _VDUSE_H_ +#define _VDUSE_H_ + +#include + +#define VDUSE_BASE 0x81 + +/* The ioctls for control device (/dev/vduse/control) */ + +#define VDUSE_API_VERSION 0 + +/* + * Get the version of VDUSE API that kernel supported (VDUSE_API_VERSION). + * This is used for future extension. + */ +#define VDUSE_GET_API_VERSION _IOR(VDUSE_BASE, 0x00, __u64) + +/* Set the version of VDUSE API that userspace supported. */ +#define VDUSE_SET_API_VERSION _IOW(VDUSE_BASE, 0x01, __u64) + +/** + * struct vduse_dev_config - basic configuration of a VDUSE device + * @name: VDUSE device name, needs to be NUL terminated + * @vendor_id: virtio vendor id + * @device_id: virtio device id + * @features: virtio features + * @vq_num: the number of virtqueues + * @vq_align: the allocation alignment of virtqueue's metadata + * @reserved: for future use, needs to be initialized to zero + * @config_size: the size of the configuration space + * @config: the buffer of the configuration space + * + * Structure used by VDUSE_CREATE_DEV ioctl to create VDUSE device. + */ +struct vduse_dev_config { +#define VDUSE_NAME_MAX 256 + char name[VDUSE_NAME_MAX]; + __u32 vendor_id; + __u32 device_id; + __u64 features; + __u32 vq_num; + __u32 vq_align; + __u32 reserved[13]; + __u32 config_size; + __u8 config[]; +}; + +/* Create a VDUSE device which is represented by a char device (/dev/vduse= /$NAME) */ +#define VDUSE_CREATE_DEV _IOW(VDUSE_BASE, 0x02, struct vduse_dev_config) + +/* + * Destroy a VDUSE device. Make sure there are no more references + * to the char device (/dev/vduse/$NAME). + */ +#define VDUSE_DESTROY_DEV _IOW(VDUSE_BASE, 0x03, char[VDUSE_NAME_MAX]) + +/* The ioctls for VDUSE device (/dev/vduse/$NAME) */ + +/** + * struct vduse_iotlb_entry - entry of IOTLB to describe one IOVA region [= start, last] + * @offset: the mmap offset on returned file descriptor + * @start: start of the IOVA region + * @last: last of the IOVA region + * @perm: access permission of the IOVA region + * + * Structure used by VDUSE_IOTLB_GET_FD ioctl to find an overlapped IOVA r= egion. + */ +struct vduse_iotlb_entry { + __u64 offset; + __u64 start; + __u64 last; +#define VDUSE_ACCESS_RO 0x1 +#define VDUSE_ACCESS_WO 0x2 +#define VDUSE_ACCESS_RW 0x3 + __u8 perm; +}; + +/* + * Find the first IOVA region that overlaps with the range [start, last] + * and return the corresponding file descriptor. Return -EINVAL means the + * IOVA region doesn't exist. Caller should set start and last fields. + */ +#define VDUSE_IOTLB_GET_FD _IOWR(VDUSE_BASE, 0x10, struct vduse_iotlb_entr= y) + +/* + * Get the negotiated virtio features. It's a subset of the features in + * struct vduse_dev_config which can be accepted by virtio driver. It's + * only valid after FEATURES_OK status bit is set. + */ +#define VDUSE_DEV_GET_FEATURES _IOR(VDUSE_BASE, 0x11, __u64) + +/** + * struct vduse_config_data - data used to update configuration space + * @offset: the offset from the beginning of configuration space + * @length: the length to write to configuration space + * @buffer: the buffer used to write from + * + * Structure used by VDUSE_DEV_SET_CONFIG ioctl to update device + * configuration space. + */ +struct vduse_config_data { + __u32 offset; + __u32 length; + __u8 buffer[]; +}; + +/* Set device configuration space */ +#define VDUSE_DEV_SET_CONFIG _IOW(VDUSE_BASE, 0x12, struct vduse_config_da= ta) + +/* + * Inject a config interrupt. It's usually used to notify virtio driver + * that device configuration space has changed. + */ +#define VDUSE_DEV_INJECT_CONFIG_IRQ _IO(VDUSE_BASE, 0x13) + +/** + * struct vduse_vq_config - basic configuration of a virtqueue + * @index: virtqueue index + * @max_size: the max size of virtqueue + * @reserved: for future use, needs to be initialized to zero + * + * Structure used by VDUSE_VQ_SETUP ioctl to setup a virtqueue. + */ +struct vduse_vq_config { + __u32 index; + __u16 max_size; + __u16 reserved[13]; +}; + +/* + * Setup the specified virtqueue. Make sure all virtqueues have been + * configured before the device is attached to vDPA bus. + */ +#define VDUSE_VQ_SETUP _IOW(VDUSE_BASE, 0x14, struct vduse_vq_config) + +/** + * struct vduse_vq_state_split - split virtqueue state + * @avail_index: available index + */ +struct vduse_vq_state_split { + __u16 avail_index; +}; + +/** + * struct vduse_vq_state_packed - packed virtqueue state + * @last_avail_counter: last driver ring wrap counter observed by device + * @last_avail_idx: device available index + * @last_used_counter: device ring wrap counter + * @last_used_idx: used index + */ +struct vduse_vq_state_packed { + __u16 last_avail_counter; + __u16 last_avail_idx; + __u16 last_used_counter; + __u16 last_used_idx; +}; + +/** + * struct vduse_vq_info - information of a virtqueue + * @index: virtqueue index + * @num: the size of virtqueue + * @desc_addr: address of desc area + * @driver_addr: address of driver area + * @device_addr: address of device area + * @split: split virtqueue state + * @packed: packed virtqueue state + * @ready: ready status of virtqueue + * + * Structure used by VDUSE_VQ_GET_INFO ioctl to get virtqueue's informatio= n. + */ +struct vduse_vq_info { + __u32 index; + __u32 num; + __u64 desc_addr; + __u64 driver_addr; + __u64 device_addr; + union { + struct vduse_vq_state_split split; + struct vduse_vq_state_packed packed; + }; + __u8 ready; +}; + +/* Get the specified virtqueue's information. Caller should set index fiel= d. */ +#define VDUSE_VQ_GET_INFO _IOWR(VDUSE_BASE, 0x15, struct vduse_vq_info) + +/** + * struct vduse_vq_eventfd - eventfd configuration for a virtqueue + * @index: virtqueue index + * @fd: eventfd, -1 means de-assigning the eventfd + * + * Structure used by VDUSE_VQ_SETUP_KICKFD ioctl to setup kick eventfd. + */ +struct vduse_vq_eventfd { + __u32 index; +#define VDUSE_EVENTFD_DEASSIGN -1 + int fd; +}; + +/* + * Setup kick eventfd for specified virtqueue. The kick eventfd is used + * by VDUSE kernel module to notify userspace to consume the avail vring. + */ +#define VDUSE_VQ_SETUP_KICKFD _IOW(VDUSE_BASE, 0x16, struct vduse_vq_event= fd) + +/* + * Inject an interrupt for specific virtqueue. It's used to notify virtio = driver + * to consume the used vring. + */ +#define VDUSE_VQ_INJECT_IRQ _IOW(VDUSE_BASE, 0x17, __u32) + +/* The control messages definition for read(2)/write(2) on /dev/vduse/$NAM= E */ + +/** + * enum vduse_req_type - request type + * @VDUSE_GET_VQ_STATE: get the state for specified virtqueue from userspa= ce + * @VDUSE_SET_STATUS: set the device status + * @VDUSE_UPDATE_IOTLB: Notify userspace to update the memory mapping for + * specified IOVA range via VDUSE_IOTLB_GET_FD ioctl + */ +enum vduse_req_type { + VDUSE_GET_VQ_STATE, + VDUSE_SET_STATUS, + VDUSE_UPDATE_IOTLB, +}; + +/** + * struct vduse_vq_state - virtqueue state + * @index: virtqueue index + * @split: split virtqueue state + * @packed: packed virtqueue state + */ +struct vduse_vq_state { + __u32 index; + union { + struct vduse_vq_state_split split; + struct vduse_vq_state_packed packed; + }; +}; + +/** + * struct vduse_dev_status - device status + * @status: device status + */ +struct vduse_dev_status { + __u8 status; +}; + +/** + * struct vduse_iova_range - IOVA range [start, last] + * @start: start of the IOVA range + * @last: last of the IOVA range + */ +struct vduse_iova_range { + __u64 start; + __u64 last; +}; + +/** + * struct vduse_dev_request - control request + * @type: request type + * @request_id: request id + * @reserved: for future use + * @vq_state: virtqueue state, only index field is available + * @s: device status + * @iova: IOVA range for updating + * @padding: padding + * + * Structure used by read(2) on /dev/vduse/$NAME. + */ +struct vduse_dev_request { + __u32 type; + __u32 request_id; + __u32 reserved[4]; + union { + struct vduse_vq_state vq_state; + struct vduse_dev_status s; + struct vduse_iova_range iova; + __u32 padding[32]; + }; +}; + +/** + * struct vduse_dev_response - response to control request + * @request_id: corresponding request id + * @result: the result of request + * @reserved: for future use, needs to be initialized to zero + * @vq_state: virtqueue state + * @padding: padding + * + * Structure used by write(2) on /dev/vduse/$NAME. + */ +struct vduse_dev_response { + __u32 request_id; +#define VDUSE_REQ_RESULT_OK 0x00 +#define VDUSE_REQ_RESULT_FAILED 0x01 + __u32 result; + __u32 reserved[4]; + union { + struct vduse_vq_state vq_state; + __u32 padding[32]; + }; +}; + +#endif /* _VDUSE_H_ */ diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers= .sh index 839a5ec614..b1ad99cba8 100755 --- a/scripts/update-linux-headers.sh +++ b/scripts/update-linux-headers.sh @@ -161,7 +161,7 @@ done rm -rf "$output/linux-headers/linux" mkdir -p "$output/linux-headers/linux" for header in kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ - psci.h psp-sev.h userfaultfd.h mman.h; do + psci.h psp-sev.h userfaultfd.h mman.h vduse.h; do cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" done =20 --=20 2.20.1 From nobody Thu Apr 25 06:55:41 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653298531; cv=none; d=zohomail.com; s=zohoarc; b=agpOSKDwQjFtJIw8N9UOq17ooExsLiMAgKwg9PQb84TtZQfIeCsgV88pXr3q77Acq/HmXVa5VKoR+6xvJkxKg5Nz3HzjFJGqbwEh2ATsQSy6U5UoJpJgafrUND7IkUEDI3mt9PK5iQHRUzOuBS3nKxJ+9ghAfBJ+8m4XGVXsieM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653298531; 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=B8hn08XGeY58Kr/jDDyecRoUQam3vFMeJExxi7rM+bw=; b=JhFF319SkSqa1u4s7a/WPIGIJ69i20kregdgqMVMsPnse8G+Udlq650i4JHj/Y8sgakl1Vhcrn0hpqVgPTMygpDxebf2W4hTxceaOaqnMtivYGrik0iKZl0ysKxg4a561eXvtTSddAQixoxyKL45U8Rq0hsOhOy8/UbwYg7hrvY= 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=fail 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 165329853107275.36078699163272; Mon, 23 May 2022 02:35:31 -0700 (PDT) Received: from localhost ([::1]:50716 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt4TH-0006Eq-Vv for importer2@patchew.org; Mon, 23 May 2022 05:35:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3mt-00026k-Hi for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:39 -0400 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]:40580) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3me-0000bS-Tv for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:28 -0400 Received: by mail-pj1-x1035.google.com with SMTP id nr2-20020a17090b240200b001df2b1bfc40so16878813pjb.5 for ; Mon, 23 May 2022 01:51:23 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id t71-20020a63784a000000b003db610ebdd0sm4239010pgc.65.2022.05.23.01.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B8hn08XGeY58Kr/jDDyecRoUQam3vFMeJExxi7rM+bw=; b=DX+xLGXKuQtLC7PbiZB37DUHzeKEB4hEOurfWW+DqDlqM73vz0Y09cxsjbI1KhvBLA FPjhT4ruPG7ULqyzGGNJsPI/nroe6skimB/2PYa7GQkS2IO/TL6x4RtX8ziQ/buyGvH7 CNAeixDYHP6cFY1HShg+pgQq/b33pDqMciqaCm56KuBGEbvAoSP5fpc8kV515vIxjvZi zDtusQDz80v9L6nyzE+lE2pf3OKJVKBP/Ofzc5ysBLVfSuqRiCjAPysmkeDGkaVtZErJ o5w5CPlG8mVQlQtqyDKRemJzh46p1j8NPL7BxFJFLt3CVeShy2y3VsuX3iQDAXfSBpkD 5/wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B8hn08XGeY58Kr/jDDyecRoUQam3vFMeJExxi7rM+bw=; b=MZla+N9oD//ydD9yA2NU4x3kRwP3KThrk6sghnE+y/4AR5d9LBtkfJUCob6R4WFcN1 GrHv6e9RGS/uoJLXBxbVwQYO5pp0HDm9W6lQCP64xo+XGkdPP3PvbG3yp2UO4gtrt9eV G5ZyMAAaKQ5WolqKphEhGS+JtbUTlyak2EeJGUiYRn5xgPVKkNcCpD2vxt1/XSdze3yJ orowolteGjw7QQAR9cAusXFNrdin9pvz8OTZiGPx6eK2T22AQ5fNwazhI/KjMEhroQd8 PP6iO3qfzs2oCmSyd0EpT/DICNrJr+eE8qsOTywEf4AZPSs1XlPc0kZgiJlLEu2sL6nV rx8A== X-Gm-Message-State: AOAM533GWkLIDGEC53dOtDx8Xm7CDwi+ImiJJFUMTRhNMeSb3dk9ZuX6 0tl/K7kLyhRkCsshOd48u6lm X-Google-Smtp-Source: ABdhPJwiKCkzUVtrxXiBMbD8c3D+QET/D1z0z+ZAw24n5xt5YyRNU1IouaXLcZ7ABjZr7mgv+3cRew== X-Received: by 2002:a17:90b:4f91:b0:1cd:3a73:3a5d with SMTP id qe17-20020a17090b4f9100b001cd3a733a5dmr25088173pjb.98.1653295882081; Mon, 23 May 2022 01:51:22 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 5/8] libvduse: Add VDUSE (vDPA Device in Userspace) library Date: Mon, 23 May 2022 16:46:08 +0800 Message-Id: <20220523084611.91-6-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::1035; envelope-from=xieyongji@bytedance.com; helo=mail-pj1-x1035.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653298532837100001 VDUSE [1] is a linux framework that makes it possible to implement software-emulated vDPA devices in userspace. This adds a library as a subproject to help implementing VDUSE backends in QEMU. [1] https://www.kernel.org/doc/html/latest/userspace-api/vduse.html Signed-off-by: Xie Yongji --- MAINTAINERS | 5 + meson.build | 15 + meson_options.txt | 2 + scripts/meson-buildoptions.sh | 3 + subprojects/libvduse/include/atomic.h | 1 + subprojects/libvduse/include/compiler.h | 1 + subprojects/libvduse/libvduse.c | 1167 +++++++++++++++++++ subprojects/libvduse/libvduse.h | 235 ++++ subprojects/libvduse/linux-headers/linux | 1 + subprojects/libvduse/meson.build | 10 + subprojects/libvduse/standard-headers/linux | 1 + 11 files changed, 1441 insertions(+) create mode 120000 subprojects/libvduse/include/atomic.h create mode 120000 subprojects/libvduse/include/compiler.h create mode 100644 subprojects/libvduse/libvduse.c create mode 100644 subprojects/libvduse/libvduse.h create mode 120000 subprojects/libvduse/linux-headers/linux create mode 100644 subprojects/libvduse/meson.build create mode 120000 subprojects/libvduse/standard-headers/linux diff --git a/MAINTAINERS b/MAINTAINERS index 01fb25421b..966e07b7a0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3554,6 +3554,11 @@ L: qemu-block@nongnu.org S: Supported F: block/export/fuse.c =20 +VDUSE library +M: Xie Yongji +S: Maintained +F: subprojects/libvduse/ + Replication M: Wen Congyang M: Xie Changlong diff --git a/meson.build b/meson.build index 9ebc00f032..3f70ccec60 100644 --- a/meson.build +++ b/meson.build @@ -1514,6 +1514,21 @@ if get_option('fuse_lseek').allowed() endif endif =20 +have_libvduse =3D (targetos =3D=3D 'linux') +if get_option('libvduse').enabled() + if targetos !=3D 'linux' + error('libvduse requires linux') + endif +elif get_option('libvduse').disabled() + have_libvduse =3D false +endif + +libvduse =3D not_found +if have_libvduse + libvduse_proj =3D subproject('libvduse') + libvduse =3D libvduse_proj.get_variable('libvduse_dep') +endif + # libbpf libbpf =3D dependency('libbpf', required: get_option('bpf'), method: 'pkg-= config') if libbpf.found() and not cc.links(''' diff --git a/meson_options.txt b/meson_options.txt index 2de94af037..50da8dea94 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -253,6 +253,8 @@ option('virtfs', type: 'feature', value: 'auto', description: 'virtio-9p support') option('virtiofsd', type: 'feature', value: 'auto', description: 'build virtiofs daemon (virtiofsd)') +option('libvduse', type: 'feature', value: 'auto', + description: 'build VDUSE Library') =20 option('capstone', type: 'feature', value: 'auto', description: 'Whether and how to find the capstone library') diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 731e5ea1cf..957a46ec89 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -109,6 +109,7 @@ meson_options_help() { printf "%s\n" ' libssh ssh block device support' printf "%s\n" ' libudev Use libudev to enumerate host devices' printf "%s\n" ' libusb libusb support for USB passthrough' + printf "%s\n" ' libvduse build VDUSE Library' printf "%s\n" ' linux-aio Linux AIO support' printf "%s\n" ' linux-io-uring Linux io_uring support' printf "%s\n" ' live-block-migration' @@ -302,6 +303,8 @@ _meson_option_parse() { --disable-libudev) printf "%s" -Dlibudev=3Ddisabled ;; --enable-libusb) printf "%s" -Dlibusb=3Denabled ;; --disable-libusb) printf "%s" -Dlibusb=3Ddisabled ;; + --enable-libvduse) printf "%s" -Dlibvduse=3Denabled ;; + --disable-libvduse) printf "%s" -Dlibvduse=3Ddisabled ;; --enable-linux-aio) printf "%s" -Dlinux_aio=3Denabled ;; --disable-linux-aio) printf "%s" -Dlinux_aio=3Ddisabled ;; --enable-linux-io-uring) printf "%s" -Dlinux_io_uring=3Denabled ;; diff --git a/subprojects/libvduse/include/atomic.h b/subprojects/libvduse/i= nclude/atomic.h new file mode 120000 index 0000000000..8c2be64f7b --- /dev/null +++ b/subprojects/libvduse/include/atomic.h @@ -0,0 +1 @@ +../../../include/qemu/atomic.h \ No newline at end of file diff --git a/subprojects/libvduse/include/compiler.h b/subprojects/libvduse= /include/compiler.h new file mode 120000 index 0000000000..de7b70697c --- /dev/null +++ b/subprojects/libvduse/include/compiler.h @@ -0,0 +1 @@ +../../../include/qemu/compiler.h \ No newline at end of file diff --git a/subprojects/libvduse/libvduse.c b/subprojects/libvduse/libvdus= e.c new file mode 100644 index 0000000000..fa4822b9a9 --- /dev/null +++ b/subprojects/libvduse/libvduse.c @@ -0,0 +1,1167 @@ +/* + * VDUSE (vDPA Device in Userspace) library + * + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * Portions of codes and concepts borrowed from libvhost-user.c, so: + * Copyright IBM, Corp. 2007 + * Copyright (c) 2016 Red Hat, Inc. + * + * Author: + * Xie Yongji + * Anthony Liguori + * Marc-Andr=C3=A9 Lureau + * Victor Kaplansky + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "include/atomic.h" +#include "linux-headers/linux/virtio_ring.h" +#include "linux-headers/linux/virtio_config.h" +#include "linux-headers/linux/vduse.h" +#include "libvduse.h" + +#define VDUSE_VQ_ALIGN 4096 +#define MAX_IOVA_REGIONS 256 + +/* Round number down to multiple */ +#define ALIGN_DOWN(n, m) ((n) / (m) * (m)) + +/* Round number up to multiple */ +#define ALIGN_UP(n, m) ALIGN_DOWN((n) + (m) - 1, (m)) + +#ifndef unlikely +#define unlikely(x) __builtin_expect(!!(x), 0) +#endif + +typedef struct VduseRing { + unsigned int num; + uint64_t desc_addr; + uint64_t avail_addr; + uint64_t used_addr; + struct vring_desc *desc; + struct vring_avail *avail; + struct vring_used *used; +} VduseRing; + +struct VduseVirtq { + VduseRing vring; + uint16_t last_avail_idx; + uint16_t shadow_avail_idx; + uint16_t used_idx; + uint16_t signalled_used; + bool signalled_used_valid; + int index; + int inuse; + bool ready; + int fd; + VduseDev *dev; +}; + +typedef struct VduseIovaRegion { + uint64_t iova; + uint64_t size; + uint64_t mmap_offset; + uint64_t mmap_addr; +} VduseIovaRegion; + +struct VduseDev { + VduseVirtq *vqs; + VduseIovaRegion regions[MAX_IOVA_REGIONS]; + int num_regions; + char *name; + uint32_t device_id; + uint32_t vendor_id; + uint16_t num_queues; + uint16_t queue_size; + uint64_t features; + const VduseOps *ops; + int fd; + int ctrl_fd; + void *priv; +}; + +static inline bool has_feature(uint64_t features, unsigned int fbit) +{ + assert(fbit < 64); + return !!(features & (1ULL << fbit)); +} + +static inline bool vduse_dev_has_feature(VduseDev *dev, unsigned int fbit) +{ + return has_feature(dev->features, fbit); +} + +uint64_t vduse_get_virtio_features(void) +{ + return (1ULL << VIRTIO_F_IOMMU_PLATFORM) | + (1ULL << VIRTIO_F_VERSION_1) | + (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | + (1ULL << VIRTIO_RING_F_EVENT_IDX) | + (1ULL << VIRTIO_RING_F_INDIRECT_DESC); +} + +VduseDev *vduse_queue_get_dev(VduseVirtq *vq) +{ + return vq->dev; +} + +int vduse_queue_get_fd(VduseVirtq *vq) +{ + return vq->fd; +} + +void *vduse_dev_get_priv(VduseDev *dev) +{ + return dev->priv; +} + +VduseVirtq *vduse_dev_get_queue(VduseDev *dev, int index) +{ + return &dev->vqs[index]; +} + +int vduse_dev_get_fd(VduseDev *dev) +{ + return dev->fd; +} + +static int vduse_inject_irq(VduseDev *dev, int index) +{ + return ioctl(dev->fd, VDUSE_VQ_INJECT_IRQ, &index); +} + +static void vduse_iova_remove_region(VduseDev *dev, uint64_t start, + uint64_t last) +{ + int i; + + if (last =3D=3D start) { + return; + } + + for (i =3D 0; i < MAX_IOVA_REGIONS; i++) { + if (!dev->regions[i].mmap_addr) { + continue; + } + + if (start <=3D dev->regions[i].iova && + last >=3D (dev->regions[i].iova + dev->regions[i].size - 1)) { + munmap((void *)dev->regions[i].mmap_addr, + dev->regions[i].mmap_offset + dev->regions[i].size); + dev->regions[i].mmap_addr =3D 0; + dev->num_regions--; + } + } +} + +static int vduse_iova_add_region(VduseDev *dev, int fd, + uint64_t offset, uint64_t start, + uint64_t last, int prot) +{ + int i; + uint64_t size =3D last - start + 1; + void *mmap_addr =3D mmap(0, size + offset, prot, MAP_SHARED, fd, 0); + + if (mmap_addr =3D=3D MAP_FAILED) { + close(fd); + return -EINVAL; + } + + for (i =3D 0; i < MAX_IOVA_REGIONS; i++) { + if (!dev->regions[i].mmap_addr) { + dev->regions[i].mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; + dev->regions[i].mmap_offset =3D offset; + dev->regions[i].iova =3D start; + dev->regions[i].size =3D size; + dev->num_regions++; + break; + } + } + assert(i < MAX_IOVA_REGIONS); + close(fd); + + return 0; +} + +static int perm_to_prot(uint8_t perm) +{ + int prot =3D 0; + + switch (perm) { + case VDUSE_ACCESS_WO: + prot |=3D PROT_WRITE; + break; + case VDUSE_ACCESS_RO: + prot |=3D PROT_READ; + break; + case VDUSE_ACCESS_RW: + prot |=3D PROT_READ | PROT_WRITE; + break; + default: + break; + } + + return prot; +} + +static inline void *iova_to_va(VduseDev *dev, uint64_t *plen, uint64_t iov= a) +{ + int i, ret; + struct vduse_iotlb_entry entry; + + for (i =3D 0; i < MAX_IOVA_REGIONS; i++) { + VduseIovaRegion *r =3D &dev->regions[i]; + + if (!r->mmap_addr) { + continue; + } + + if ((iova >=3D r->iova) && (iova < (r->iova + r->size))) { + if ((iova + *plen) > (r->iova + r->size)) { + *plen =3D r->iova + r->size - iova; + } + return (void *)(uintptr_t)(iova - r->iova + + r->mmap_addr + r->mmap_offset); + } + } + + entry.start =3D iova; + entry.last =3D iova + 1; + ret =3D ioctl(dev->fd, VDUSE_IOTLB_GET_FD, &entry); + if (ret < 0) { + return NULL; + } + + if (!vduse_iova_add_region(dev, ret, entry.offset, entry.start, + entry.last, perm_to_prot(entry.perm))) { + return iova_to_va(dev, plen, iova); + } + + return NULL; +} + +static inline uint16_t vring_avail_flags(VduseVirtq *vq) +{ + return le16toh(vq->vring.avail->flags); +} + +static inline uint16_t vring_avail_idx(VduseVirtq *vq) +{ + vq->shadow_avail_idx =3D le16toh(vq->vring.avail->idx); + + return vq->shadow_avail_idx; +} + +static inline uint16_t vring_avail_ring(VduseVirtq *vq, int i) +{ + return le16toh(vq->vring.avail->ring[i]); +} + +static inline uint16_t vring_get_used_event(VduseVirtq *vq) +{ + return vring_avail_ring(vq, vq->vring.num); +} + +static bool vduse_queue_get_head(VduseVirtq *vq, unsigned int idx, + unsigned int *head) +{ + /* + * Grab the next descriptor number they're advertising, and increment + * the index we've seen. + */ + *head =3D vring_avail_ring(vq, idx % vq->vring.num); + + /* If their number is silly, that's a fatal mistake. */ + if (*head >=3D vq->vring.num) { + fprintf(stderr, "Guest says index %u is available\n", *head); + return false; + } + + return true; +} + +static int +vduse_queue_read_indirect_desc(VduseDev *dev, struct vring_desc *desc, + uint64_t addr, size_t len) +{ + struct vring_desc *ori_desc; + uint64_t read_len; + + if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) { + return -1; + } + + if (len =3D=3D 0) { + return -1; + } + + while (len) { + read_len =3D len; + ori_desc =3D iova_to_va(dev, &read_len, addr); + if (!ori_desc) { + return -1; + } + + memcpy(desc, ori_desc, read_len); + len -=3D read_len; + addr +=3D read_len; + desc +=3D read_len; + } + + return 0; +} + +enum { + VIRTQUEUE_READ_DESC_ERROR =3D -1, + VIRTQUEUE_READ_DESC_DONE =3D 0, /* end of chain */ + VIRTQUEUE_READ_DESC_MORE =3D 1, /* more buffers in chain */ +}; + +static int vduse_queue_read_next_desc(struct vring_desc *desc, int i, + unsigned int max, unsigned int *next) +{ + /* If this descriptor says it doesn't chain, we're done. */ + if (!(le16toh(desc[i].flags) & VRING_DESC_F_NEXT)) { + return VIRTQUEUE_READ_DESC_DONE; + } + + /* Check they're not leading us off end of descriptors. */ + *next =3D desc[i].next; + /* Make sure compiler knows to grab that: we don't want it changing! */ + smp_wmb(); + + if (*next >=3D max) { + fprintf(stderr, "Desc next is %u\n", *next); + return VIRTQUEUE_READ_DESC_ERROR; + } + + return VIRTQUEUE_READ_DESC_MORE; +} + +/* + * Fetch avail_idx from VQ memory only when we really need to know if + * guest has added some buffers. + */ +static bool vduse_queue_empty(VduseVirtq *vq) +{ + if (unlikely(!vq->vring.avail)) { + return true; + } + + if (vq->shadow_avail_idx !=3D vq->last_avail_idx) { + return false; + } + + return vring_avail_idx(vq) =3D=3D vq->last_avail_idx; +} + +static bool vduse_queue_should_notify(VduseVirtq *vq) +{ + VduseDev *dev =3D vq->dev; + uint16_t old, new; + bool v; + + /* We need to expose used array entries before checking used event. */ + smp_mb(); + + /* Always notify when queue is empty (when feature acknowledge) */ + if (vduse_dev_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) && + !vq->inuse && vduse_queue_empty(vq)) { + return true; + } + + if (!vduse_dev_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) { + return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); + } + + v =3D vq->signalled_used_valid; + vq->signalled_used_valid =3D true; + old =3D vq->signalled_used; + new =3D vq->signalled_used =3D vq->used_idx; + return !v || vring_need_event(vring_get_used_event(vq), new, old); +} + +void vduse_queue_notify(VduseVirtq *vq) +{ + VduseDev *dev =3D vq->dev; + + if (unlikely(!vq->vring.avail)) { + return; + } + + if (!vduse_queue_should_notify(vq)) { + return; + } + + if (vduse_inject_irq(dev, vq->index) < 0) { + fprintf(stderr, "Error inject irq for vq %d: %s\n", + vq->index, strerror(errno)); + } +} + +static inline void vring_used_flags_set_bit(VduseVirtq *vq, int mask) +{ + uint16_t *flags; + + flags =3D (uint16_t *)((char*)vq->vring.used + + offsetof(struct vring_used, flags)); + *flags =3D htole16(le16toh(*flags) | mask); +} + +static inline void vring_used_flags_unset_bit(VduseVirtq *vq, int mask) +{ + uint16_t *flags; + + flags =3D (uint16_t *)((char*)vq->vring.used + + offsetof(struct vring_used, flags)); + *flags =3D htole16(le16toh(*flags) & ~mask); +} + +static inline void vring_set_avail_event(VduseVirtq *vq, uint16_t val) +{ + *((uint16_t *)&vq->vring.used->ring[vq->vring.num]) =3D htole16(val); +} + +static bool vduse_queue_map_single_desc(VduseVirtq *vq, unsigned int *p_nu= m_sg, + struct iovec *iov, unsigned int max_num= _sg, + bool is_write, uint64_t pa, size_t sz) +{ + unsigned num_sg =3D *p_num_sg; + VduseDev *dev =3D vq->dev; + + assert(num_sg <=3D max_num_sg); + + if (!sz) { + fprintf(stderr, "virtio: zero sized buffers are not allowed\n"); + return false; + } + + while (sz) { + uint64_t len =3D sz; + + if (num_sg =3D=3D max_num_sg) { + fprintf(stderr, + "virtio: too many descriptors in indirect table\n"); + return false; + } + + iov[num_sg].iov_base =3D iova_to_va(dev, &len, pa); + if (iov[num_sg].iov_base =3D=3D NULL) { + fprintf(stderr, "virtio: invalid address for buffers\n"); + return false; + } + iov[num_sg++].iov_len =3D len; + sz -=3D len; + pa +=3D len; + } + + *p_num_sg =3D num_sg; + return true; +} + +static void *vduse_queue_alloc_element(size_t sz, unsigned out_num, + unsigned in_num) +{ + VduseVirtqElement *elem; + size_t in_sg_ofs =3D ALIGN_UP(sz, __alignof__(elem->in_sg[0])); + size_t out_sg_ofs =3D in_sg_ofs + in_num * sizeof(elem->in_sg[0]); + size_t out_sg_end =3D out_sg_ofs + out_num * sizeof(elem->out_sg[0]); + + assert(sz >=3D sizeof(VduseVirtqElement)); + elem =3D malloc(out_sg_end); + if (!elem) { + return NULL; + } + elem->out_num =3D out_num; + elem->in_num =3D in_num; + elem->in_sg =3D (void *)elem + in_sg_ofs; + elem->out_sg =3D (void *)elem + out_sg_ofs; + return elem; +} + +static void *vduse_queue_map_desc(VduseVirtq *vq, unsigned int idx, size_t= sz) +{ + struct vring_desc *desc =3D vq->vring.desc; + VduseDev *dev =3D vq->dev; + uint64_t desc_addr, read_len; + unsigned int desc_len; + unsigned int max =3D vq->vring.num; + unsigned int i =3D idx; + VduseVirtqElement *elem; + struct iovec iov[VIRTQUEUE_MAX_SIZE]; + struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE]; + unsigned int out_num =3D 0, in_num =3D 0; + int rc; + + if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) { + if (le32toh(desc[i].len) % sizeof(struct vring_desc)) { + fprintf(stderr, "Invalid size for indirect buffer table\n"); + return NULL; + } + + /* loop over the indirect descriptor table */ + desc_addr =3D le64toh(desc[i].addr); + desc_len =3D le32toh(desc[i].len); + max =3D desc_len / sizeof(struct vring_desc); + read_len =3D desc_len; + desc =3D iova_to_va(dev, &read_len, desc_addr); + if (unlikely(desc && read_len !=3D desc_len)) { + /* Failed to use zero copy */ + desc =3D NULL; + if (!vduse_queue_read_indirect_desc(dev, desc_buf, + desc_addr, + desc_len)) { + desc =3D desc_buf; + } + } + if (!desc) { + fprintf(stderr, "Invalid indirect buffer table\n"); + return NULL; + } + i =3D 0; + } + + /* Collect all the descriptors */ + do { + if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) { + if (!vduse_queue_map_single_desc(vq, &in_num, iov + out_num, + VIRTQUEUE_MAX_SIZE - out_num, + true, le64toh(desc[i].addr), + le32toh(desc[i].len))) { + return NULL; + } + } else { + if (in_num) { + fprintf(stderr, "Incorrect order for descriptors\n"); + return NULL; + } + if (!vduse_queue_map_single_desc(vq, &out_num, iov, + VIRTQUEUE_MAX_SIZE, false, + le64toh(desc[i].addr), + le32toh(desc[i].len))) { + return NULL; + } + } + + /* If we've got too many, that implies a descriptor loop. */ + if ((in_num + out_num) > max) { + fprintf(stderr, "Looped descriptor\n"); + return NULL; + } + rc =3D vduse_queue_read_next_desc(desc, i, max, &i); + } while (rc =3D=3D VIRTQUEUE_READ_DESC_MORE); + + if (rc =3D=3D VIRTQUEUE_READ_DESC_ERROR) { + fprintf(stderr, "read descriptor error\n"); + return NULL; + } + + /* Now copy what we have collected and mapped */ + elem =3D vduse_queue_alloc_element(sz, out_num, in_num); + if (!elem) { + fprintf(stderr, "read descriptor error\n"); + return NULL; + } + elem->index =3D idx; + for (i =3D 0; i < out_num; i++) { + elem->out_sg[i] =3D iov[i]; + } + for (i =3D 0; i < in_num; i++) { + elem->in_sg[i] =3D iov[out_num + i]; + } + + return elem; +} + +void *vduse_queue_pop(VduseVirtq *vq, size_t sz) +{ + unsigned int head; + VduseVirtqElement *elem; + VduseDev *dev =3D vq->dev; + + if (unlikely(!vq->vring.avail)) { + return NULL; + } + + if (vduse_queue_empty(vq)) { + return NULL; + } + /* Needed after virtio_queue_empty() */ + smp_rmb(); + + if (vq->inuse >=3D vq->vring.num) { + fprintf(stderr, "Virtqueue size exceeded: %d\n", vq->inuse); + return NULL; + } + + if (!vduse_queue_get_head(vq, vq->last_avail_idx++, &head)) { + return NULL; + } + + if (vduse_dev_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) { + vring_set_avail_event(vq, vq->last_avail_idx); + } + + elem =3D vduse_queue_map_desc(vq, head, sz); + + if (!elem) { + return NULL; + } + + vq->inuse++; + + return elem; +} + +static inline void vring_used_write(VduseVirtq *vq, + struct vring_used_elem *uelem, int i) +{ + struct vring_used *used =3D vq->vring.used; + + used->ring[i] =3D *uelem; +} + +static void vduse_queue_fill(VduseVirtq *vq, const VduseVirtqElement *elem, + unsigned int len, unsigned int idx) +{ + struct vring_used_elem uelem; + + if (unlikely(!vq->vring.used)) { + return; + } + + idx =3D (idx + vq->used_idx) % vq->vring.num; + + uelem.id =3D htole32(elem->index); + uelem.len =3D htole32(len); + vring_used_write(vq, &uelem, idx); +} + +static inline void vring_used_idx_set(VduseVirtq *vq, uint16_t val) +{ + vq->vring.used->idx =3D htole16(val); + vq->used_idx =3D val; +} + +static void vduse_queue_flush(VduseVirtq *vq, unsigned int count) +{ + uint16_t old, new; + + if (unlikely(!vq->vring.used)) { + return; + } + + /* Make sure buffer is written before we update index. */ + smp_wmb(); + + old =3D vq->used_idx; + new =3D old + count; + vring_used_idx_set(vq, new); + vq->inuse -=3D count; + if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - ol= d))) { + vq->signalled_used_valid =3D false; + } +} + +void vduse_queue_push(VduseVirtq *vq, const VduseVirtqElement *elem, + unsigned int len) +{ + vduse_queue_fill(vq, elem, len, 0); + vduse_queue_flush(vq, 1); +} + +static int vduse_queue_update_vring(VduseVirtq *vq, uint64_t desc_addr, + uint64_t avail_addr, uint64_t used_add= r) +{ + struct VduseDev *dev =3D vq->dev; + uint64_t len; + + len =3D sizeof(struct vring_desc); + vq->vring.desc =3D iova_to_va(dev, &len, desc_addr); + if (len !=3D sizeof(struct vring_desc)) { + return -EINVAL; + } + + len =3D sizeof(struct vring_avail); + vq->vring.avail =3D iova_to_va(dev, &len, avail_addr); + if (len !=3D sizeof(struct vring_avail)) { + return -EINVAL; + } + + len =3D sizeof(struct vring_used); + vq->vring.used =3D iova_to_va(dev, &len, used_addr); + if (len !=3D sizeof(struct vring_used)) { + return -EINVAL; + } + + if (!vq->vring.desc || !vq->vring.avail || !vq->vring.used) { + fprintf(stderr, "Failed to get vq[%d] iova mapping\n", vq->index); + return -EINVAL; + } + + return 0; +} + +static void vduse_queue_enable(VduseVirtq *vq) +{ + struct VduseDev *dev =3D vq->dev; + struct vduse_vq_info vq_info; + struct vduse_vq_eventfd vq_eventfd; + int fd; + + vq_info.index =3D vq->index; + if (ioctl(dev->fd, VDUSE_VQ_GET_INFO, &vq_info)) { + fprintf(stderr, "Failed to get vq[%d] info: %s\n", + vq->index, strerror(errno)); + return; + } + + if (!vq_info.ready) { + return; + } + + vq->vring.num =3D vq_info.num; + vq->vring.desc_addr =3D vq_info.desc_addr; + vq->vring.avail_addr =3D vq_info.driver_addr; + vq->vring.used_addr =3D vq_info.device_addr; + + if (vduse_queue_update_vring(vq, vq_info.desc_addr, + vq_info.driver_addr, vq_info.device_addr)= ) { + fprintf(stderr, "Failed to update vring for vq[%d]\n", vq->index); + return; + } + + fd =3D eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + if (fd < 0) { + fprintf(stderr, "Failed to init eventfd for vq[%d]\n", vq->index); + return; + } + + vq_eventfd.index =3D vq->index; + vq_eventfd.fd =3D fd; + if (ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &vq_eventfd)) { + fprintf(stderr, "Failed to setup kick fd for vq[%d]\n", vq->index); + close(fd); + return; + } + + vq->fd =3D fd; + vq->shadow_avail_idx =3D vq->last_avail_idx =3D vq_info.split.avail_in= dex; + vq->inuse =3D 0; + vq->used_idx =3D 0; + vq->signalled_used_valid =3D false; + vq->ready =3D true; + + dev->ops->enable_queue(dev, vq); +} + +static void vduse_queue_disable(VduseVirtq *vq) +{ + struct VduseDev *dev =3D vq->dev; + struct vduse_vq_eventfd eventfd; + + if (!vq->ready) { + return; + } + + dev->ops->disable_queue(dev, vq); + + eventfd.index =3D vq->index; + eventfd.fd =3D VDUSE_EVENTFD_DEASSIGN; + ioctl(dev->fd, VDUSE_VQ_SETUP_KICKFD, &eventfd); + close(vq->fd); + + assert(vq->inuse =3D=3D 0); + + vq->vring.num =3D 0; + vq->vring.desc_addr =3D 0; + vq->vring.avail_addr =3D 0; + vq->vring.used_addr =3D 0; + vq->vring.desc =3D 0; + vq->vring.avail =3D 0; + vq->vring.used =3D 0; + vq->ready =3D false; + vq->fd =3D -1; +} + +static void vduse_dev_start_dataplane(VduseDev *dev) +{ + int i; + + if (ioctl(dev->fd, VDUSE_DEV_GET_FEATURES, &dev->features)) { + fprintf(stderr, "Failed to get features: %s\n", strerror(errno)); + return; + } + assert(vduse_dev_has_feature(dev, VIRTIO_F_VERSION_1)); + + for (i =3D 0; i < dev->num_queues; i++) { + vduse_queue_enable(&dev->vqs[i]); + } +} + +static void vduse_dev_stop_dataplane(VduseDev *dev) +{ + int i; + + for (i =3D 0; i < dev->num_queues; i++) { + vduse_queue_disable(&dev->vqs[i]); + } + dev->features =3D 0; + vduse_iova_remove_region(dev, 0, ULONG_MAX); +} + +int vduse_dev_handler(VduseDev *dev) +{ + struct vduse_dev_request req; + struct vduse_dev_response resp =3D { 0 }; + VduseVirtq *vq; + int i, ret; + + ret =3D read(dev->fd, &req, sizeof(req)); + if (ret !=3D sizeof(req)) { + fprintf(stderr, "Read request error [%d]: %s\n", + ret, strerror(errno)); + return -errno; + } + resp.request_id =3D req.request_id; + + switch (req.type) { + case VDUSE_GET_VQ_STATE: + vq =3D &dev->vqs[req.vq_state.index]; + resp.vq_state.split.avail_index =3D vq->last_avail_idx; + resp.result =3D VDUSE_REQ_RESULT_OK; + break; + case VDUSE_SET_STATUS: + if (req.s.status & VIRTIO_CONFIG_S_DRIVER_OK) { + vduse_dev_start_dataplane(dev); + } else if (req.s.status =3D=3D 0) { + vduse_dev_stop_dataplane(dev); + } + resp.result =3D VDUSE_REQ_RESULT_OK; + break; + case VDUSE_UPDATE_IOTLB: + /* The iova will be updated by iova_to_va() later, so just remove = it */ + vduse_iova_remove_region(dev, req.iova.start, req.iova.last); + for (i =3D 0; i < dev->num_queues; i++) { + VduseVirtq *vq =3D &dev->vqs[i]; + if (vq->ready) { + if (vduse_queue_update_vring(vq, vq->vring.desc_addr, + vq->vring.avail_addr, + vq->vring.used_addr)) { + fprintf(stderr, "Failed to update vring for vq[%d]\n", + vq->index); + } + } + } + resp.result =3D VDUSE_REQ_RESULT_OK; + break; + default: + resp.result =3D VDUSE_REQ_RESULT_FAILED; + break; + } + + ret =3D write(dev->fd, &resp, sizeof(resp)); + if (ret !=3D sizeof(resp)) { + fprintf(stderr, "Write request %d error [%d]: %s\n", + req.type, ret, strerror(errno)); + return -errno; + } + return 0; +} + +int vduse_dev_update_config(VduseDev *dev, uint32_t size, + uint32_t offset, char *buffer) +{ + int ret; + struct vduse_config_data *data; + + data =3D malloc(offsetof(struct vduse_config_data, buffer) + size); + if (!data) { + return -ENOMEM; + } + + data->offset =3D offset; + data->length =3D size; + memcpy(data->buffer, buffer, size); + + ret =3D ioctl(dev->fd, VDUSE_DEV_SET_CONFIG, data); + free(data); + + if (ret) { + return -errno; + } + + if (ioctl(dev->fd, VDUSE_DEV_INJECT_CONFIG_IRQ)) { + return -errno; + } + + return 0; +} + +int vduse_dev_setup_queue(VduseDev *dev, int index, int max_size) +{ + VduseVirtq *vq =3D &dev->vqs[index]; + struct vduse_vq_config vq_config =3D { 0 }; + + if (max_size > VIRTQUEUE_MAX_SIZE) { + return -EINVAL; + } + + vq_config.index =3D vq->index; + vq_config.max_size =3D max_size; + + if (ioctl(dev->fd, VDUSE_VQ_SETUP, &vq_config)) { + return -errno; + } + + return 0; +} + +static int vduse_dev_init_vqs(VduseDev *dev, uint16_t num_queues) +{ + VduseVirtq *vqs; + int i; + + vqs =3D calloc(sizeof(VduseVirtq), num_queues); + if (!vqs) { + return -ENOMEM; + } + + for (i =3D 0; i < num_queues; i++) { + vqs[i].index =3D i; + vqs[i].dev =3D dev; + vqs[i].fd =3D -1; + } + dev->vqs =3D vqs; + + return 0; +} + +static int vduse_dev_init(VduseDev *dev, const char *name, + uint16_t num_queues, const VduseOps *ops, + void *priv) +{ + char *dev_path, *dev_name; + int ret, fd; + + dev_path =3D malloc(strlen(name) + strlen("/dev/vduse/") + 1); + if (!dev_path) { + return -ENOMEM; + } + sprintf(dev_path, "/dev/vduse/%s", name); + + fd =3D open(dev_path, O_RDWR); + free(dev_path); + if (fd < 0) { + fprintf(stderr, "Failed to open vduse dev %s: %s\n", + name, strerror(errno)); + return -errno; + } + + dev_name =3D strdup(name); + if (!dev_name) { + close(fd); + return -ENOMEM; + } + + ret =3D vduse_dev_init_vqs(dev, num_queues); + if (ret) { + free(dev_name); + close(fd); + return ret; + } + + dev->name =3D dev_name; + dev->num_queues =3D num_queues; + dev->fd =3D fd; + dev->ops =3D ops; + dev->priv =3D priv; + + return 0; +} + +static inline bool vduse_name_is_valid(const char *name) +{ + return strlen(name) >=3D VDUSE_NAME_MAX || strstr(name, ".."); +} + +VduseDev *vduse_dev_create_by_fd(int fd, uint16_t num_queues, + const VduseOps *ops, void *priv) +{ + VduseDev *dev; + int ret; + + if (!ops || !ops->enable_queue || !ops->disable_queue) { + fprintf(stderr, "Invalid parameter for vduse\n"); + return NULL; + } + + dev =3D calloc(sizeof(VduseDev), 1); + if (!dev) { + fprintf(stderr, "Failed to allocate vduse device\n"); + return NULL; + } + + ret =3D vduse_dev_init_vqs(dev, num_queues); + if (ret) { + fprintf(stderr, "Failed to init vqs\n"); + free(dev); + return NULL; + } + + dev->num_queues =3D num_queues; + dev->fd =3D fd; + dev->ops =3D ops; + dev->priv =3D priv; + + return dev; +} + +VduseDev *vduse_dev_create_by_name(const char *name, uint16_t num_queues, + const VduseOps *ops, void *priv) +{ + VduseDev *dev; + int ret; + + if (!name || vduse_name_is_valid(name) || !ops || + !ops->enable_queue || !ops->disable_queue) { + fprintf(stderr, "Invalid parameter for vduse\n"); + return NULL; + } + + dev =3D calloc(sizeof(VduseDev), 1); + if (!dev) { + fprintf(stderr, "Failed to allocate vduse device\n"); + return NULL; + } + + ret =3D vduse_dev_init(dev, name, num_queues, ops, priv); + if (ret < 0) { + fprintf(stderr, "Failed to init vduse device %s: %s\n", + name, strerror(ret)); + free(dev); + return NULL; + } + + return dev; +} + +VduseDev *vduse_dev_create(const char *name, uint32_t device_id, + uint32_t vendor_id, uint64_t features, + uint16_t num_queues, uint32_t config_size, + char *config, const VduseOps *ops, void *priv) +{ + VduseDev *dev; + int ret, ctrl_fd; + uint64_t version; + struct vduse_dev_config *dev_config; + size_t size =3D offsetof(struct vduse_dev_config, config); + + if (!name || vduse_name_is_valid(name) || + !has_feature(features, VIRTIO_F_VERSION_1) || !config || + !config_size || !ops || !ops->enable_queue || !ops->disable_queue)= { + fprintf(stderr, "Invalid parameter for vduse\n"); + return NULL; + } + + dev =3D calloc(sizeof(VduseDev), 1); + if (!dev) { + fprintf(stderr, "Failed to allocate vduse device\n"); + return NULL; + } + + ctrl_fd =3D open("/dev/vduse/control", O_RDWR); + if (ctrl_fd < 0) { + fprintf(stderr, "Failed to open /dev/vduse/control: %s\n", + strerror(errno)); + goto err_ctrl; + } + + version =3D VDUSE_API_VERSION; + if (ioctl(ctrl_fd, VDUSE_SET_API_VERSION, &version)) { + fprintf(stderr, "Failed to set api version %lu: %s\n", + version, strerror(errno)); + goto err_dev; + } + + dev_config =3D calloc(size + config_size, 1); + if (!dev_config) { + fprintf(stderr, "Failed to allocate config space\n"); + goto err_dev; + } + + strcpy(dev_config->name, name); + dev_config->device_id =3D device_id; + dev_config->vendor_id =3D vendor_id; + dev_config->features =3D features; + dev_config->vq_num =3D num_queues; + dev_config->vq_align =3D VDUSE_VQ_ALIGN; + dev_config->config_size =3D config_size; + memcpy(dev_config->config, config, config_size); + + ret =3D ioctl(ctrl_fd, VDUSE_CREATE_DEV, dev_config); + free(dev_config); + if (ret < 0) { + fprintf(stderr, "Failed to create vduse device %s: %s\n", + name, strerror(errno)); + goto err_dev; + } + dev->ctrl_fd =3D ctrl_fd; + + ret =3D vduse_dev_init(dev, name, num_queues, ops, priv); + if (ret < 0) { + fprintf(stderr, "Failed to init vduse device %s: %s\n", + name, strerror(ret)); + goto err; + } + + return dev; +err: + ioctl(ctrl_fd, VDUSE_DESTROY_DEV, name); +err_dev: + close(ctrl_fd); +err_ctrl: + free(dev); + + return NULL; +} + +int vduse_dev_destroy(VduseDev *dev) +{ + int ret =3D 0; + + free(dev->vqs); + if (dev->fd > 0) { + close(dev->fd); + dev->fd =3D -1; + } + if (dev->ctrl_fd > 0) { + if (ioctl(dev->ctrl_fd, VDUSE_DESTROY_DEV, dev->name)) { + ret =3D -errno; + } + close(dev->ctrl_fd); + dev->ctrl_fd =3D -1; + } + free(dev->name); + free(dev); + + return ret; +} diff --git a/subprojects/libvduse/libvduse.h b/subprojects/libvduse/libvdus= e.h new file mode 100644 index 0000000000..6c2fe98213 --- /dev/null +++ b/subprojects/libvduse/libvduse.h @@ -0,0 +1,235 @@ +/* + * VDUSE (vDPA Device in Userspace) library + * + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * + * Author: + * Xie Yongji + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#ifndef LIBVDUSE_H +#define LIBVDUSE_H + +#include +#include + +#define VIRTQUEUE_MAX_SIZE 1024 + +/* VDUSE device structure */ +typedef struct VduseDev VduseDev; + +/* Virtqueue structure */ +typedef struct VduseVirtq VduseVirtq; + +/* Some operation of VDUSE backend */ +typedef struct VduseOps { + /* Called when virtqueue can be processed */ + void (*enable_queue)(VduseDev *dev, VduseVirtq *vq); + /* Called when virtqueue processing should be stopped */ + void (*disable_queue)(VduseDev *dev, VduseVirtq *vq); +} VduseOps; + +/* Describing elements of the I/O buffer */ +typedef struct VduseVirtqElement { + /* Descriptor table index */ + unsigned int index; + /* Number of physically-contiguous device-readable descriptors */ + unsigned int out_num; + /* Number of physically-contiguous device-writable descriptors */ + unsigned int in_num; + /* Array to store physically-contiguous device-writable descriptors */ + struct iovec *in_sg; + /* Array to store physically-contiguous device-readable descriptors */ + struct iovec *out_sg; +} VduseVirtqElement; + + +/** + * vduse_get_virtio_features: + * + * Get supported virtio features + * + * Returns: supported feature bits + */ +uint64_t vduse_get_virtio_features(void); + +/** + * vduse_queue_get_dev: + * @vq: specified virtqueue + * + * Get corresponding VDUSE device from the virtqueue. + * + * Returns: a pointer to VDUSE device on success, NULL on failure. + */ +VduseDev *vduse_queue_get_dev(VduseVirtq *vq); + +/** + * vduse_queue_get_fd: + * @vq: specified virtqueue + * + * Get the kick fd for the virtqueue. + * + * Returns: file descriptor on success, -1 on failure. + */ +int vduse_queue_get_fd(VduseVirtq *vq); + +/** + * vduse_queue_pop: + * @vq: specified virtqueue + * @sz: the size of struct to return (must be >=3D VduseVirtqElement) + * + * Pop an element from virtqueue available ring. + * + * Returns: a pointer to a structure containing VduseVirtqElement on succe= ss, + * NULL on failure. + */ +void *vduse_queue_pop(VduseVirtq *vq, size_t sz); + +/** + * vduse_queue_push: + * @vq: specified virtqueue + * @elem: pointer to VduseVirtqElement returned by vduse_queue_pop() + * @len: length in bytes to write + * + * Push an element to virtqueue used ring. + */ +void vduse_queue_push(VduseVirtq *vq, const VduseVirtqElement *elem, + unsigned int len); +/** + * vduse_queue_notify: + * @vq: specified virtqueue + * + * Request to notify the queue. + */ +void vduse_queue_notify(VduseVirtq *vq); + +/** + * vduse_dev_get_priv: + * @dev: VDUSE device + * + * Get the private pointer passed to vduse_dev_create(). + * + * Returns: private pointer on success, NULL on failure. + */ +void *vduse_dev_get_priv(VduseDev *dev); + +/** + * vduse_dev_get_queue: + * @dev: VDUSE device + * @index: virtqueue index + * + * Get the specified virtqueue. + * + * Returns: a pointer to the virtqueue on success, NULL on failure. + */ +VduseVirtq *vduse_dev_get_queue(VduseDev *dev, int index); + +/** + * vduse_dev_get_fd: + * @dev: VDUSE device + * + * Get the control message fd for the VDUSE device. + * + * Returns: file descriptor on success, -1 on failure. + */ +int vduse_dev_get_fd(VduseDev *dev); + +/** + * vduse_dev_handler: + * @dev: VDUSE device + * + * Used to process the control message. + * + * Returns: file descriptor on success, -errno on failure. + */ +int vduse_dev_handler(VduseDev *dev); + +/** + * vduse_dev_update_config: + * @dev: VDUSE device + * @size: the size to write to configuration space + * @offset: the offset from the beginning of configuration space + * @buffer: the buffer used to write from + * + * Update device configuration space and inject a config interrupt. + * + * Returns: 0 on success, -errno on failure. + */ +int vduse_dev_update_config(VduseDev *dev, uint32_t size, + uint32_t offset, char *buffer); + +/** + * vduse_dev_setup_queue: + * @dev: VDUSE device + * @index: virtqueue index + * @max_size: the max size of virtqueue + * + * Setup the specified virtqueue. + * + * Returns: 0 on success, -errno on failure. + */ +int vduse_dev_setup_queue(VduseDev *dev, int index, int max_size); + +/** + * vduse_dev_create_by_fd: + * @fd: passed file descriptor + * @num_queues: the number of virtqueues + * @ops: the operation of VDUSE backend + * @priv: private pointer + * + * Create VDUSE device from a passed file descriptor. + * + * Returns: pointer to VDUSE device on success, NULL on failure. + */ +VduseDev *vduse_dev_create_by_fd(int fd, uint16_t num_queues, + const VduseOps *ops, void *priv); + +/** + * vduse_dev_create_by_name: + * @name: VDUSE device name + * @num_queues: the number of virtqueues + * @ops: the operation of VDUSE backend + * @priv: private pointer + * + * Create VDUSE device on /dev/vduse/$NAME. + * + * Returns: pointer to VDUSE device on success, NULL on failure. + */ +VduseDev *vduse_dev_create_by_name(const char *name, uint16_t num_queues, + const VduseOps *ops, void *priv); + +/** + * vduse_dev_create: + * @name: VDUSE device name + * @device_id: virtio device id + * @vendor_id: virtio vendor id + * @features: virtio features + * @num_queues: the number of virtqueues + * @config_size: the size of the configuration space + * @config: the buffer of the configuration space + * @ops: the operation of VDUSE backend + * @priv: private pointer + * + * Create VDUSE device. + * + * Returns: pointer to VDUSE device on success, NULL on failure. + */ +VduseDev *vduse_dev_create(const char *name, uint32_t device_id, + uint32_t vendor_id, uint64_t features, + uint16_t num_queues, uint32_t config_size, + char *config, const VduseOps *ops, void *priv); + +/** + * vduse_dev_destroy: + * @dev: VDUSE device + * + * Destroy the VDUSE device. + * + * Returns: 0 on success, -errno on failure. + */ +int vduse_dev_destroy(VduseDev *dev); + +#endif diff --git a/subprojects/libvduse/linux-headers/linux b/subprojects/libvdus= e/linux-headers/linux new file mode 120000 index 0000000000..04f3304f79 --- /dev/null +++ b/subprojects/libvduse/linux-headers/linux @@ -0,0 +1 @@ +../../../linux-headers/linux/ \ No newline at end of file diff --git a/subprojects/libvduse/meson.build b/subprojects/libvduse/meson.= build new file mode 100644 index 0000000000..ba08f5ee1a --- /dev/null +++ b/subprojects/libvduse/meson.build @@ -0,0 +1,10 @@ +project('libvduse', 'c', + license: 'GPL-2.0-or-later', + default_options: ['c_std=3Dgnu99']) + +libvduse =3D static_library('vduse', + files('libvduse.c'), + c_args: '-D_GNU_SOURCE') + +libvduse_dep =3D declare_dependency(link_with: libvduse, + include_directories: include_directories= ('.')) diff --git a/subprojects/libvduse/standard-headers/linux b/subprojects/libv= duse/standard-headers/linux new file mode 120000 index 0000000000..c416f068ac --- /dev/null +++ b/subprojects/libvduse/standard-headers/linux @@ -0,0 +1 @@ +../../../include/standard-headers/linux/ \ No newline at end of file --=20 2.20.1 From nobody Thu Apr 25 06:55:41 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653296911; cv=none; d=zohomail.com; s=zohoarc; b=jeOARdgANF6GF5Yki8uKmM3Le7gpgRYZ+e+IiTC5BcO8585e4mtMOWnIFgUDr4Ymuhdghe/YHj49apVKSZIMdFDYzqv0faqjrmqBebed/YAz7jP+iOL13ZN4iA3R2OCZOO5V9bKfrfYukqLdAWvGStNfus6eW3rUdhE+pQMG6No= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653296911; h=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=PaIOxkLwjZNkboc5VdvhVflkFE+nJVqmqztmLhz5Ecs=; b=OBl8CXM577GrK3Ua1HuRQJsGzDCJL0P59VURXtqcERNNnrH939buuKqGJhR9RlqDSpeEyuHbYHwg7JCkWVR8te1uPVPJ81Ltj3Hnut2Bw9Qgq1qniqgzMUbqKdKFBVN31x51qhl0KLgcUk/Qw0ARoTSO8+laMDoQadeXWsYZCbo= 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=fail 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 1653296911388853.9492718720467; Mon, 23 May 2022 02:08:31 -0700 (PDT) Received: from localhost ([::1]:54338 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt43B-0005Dj-NS for importer2@patchew.org; Mon, 23 May 2022 05:08:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3mw-000295-Qo for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:45 -0400 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]:45595) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mt-0000cG-8G for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:41 -0400 Received: by mail-pg1-x535.google.com with SMTP id a9so11074140pgv.12 for ; Mon, 23 May 2022 01:51:28 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id ms16-20020a17090b235000b001d5943e826asm6854892pjb.20.2022.05.23.01.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PaIOxkLwjZNkboc5VdvhVflkFE+nJVqmqztmLhz5Ecs=; b=HshTDXedG5dWkV22c5uGBHn/V7xI6VwDRoAtOcZQXtepgvVptDY8OWyzZhL7XQt97c r9njXiWZQeBvCH9NniPPe9ukStRc1ImpETjf1e+Jw/B79Sprvms7ktcslorQL6hinQ09 Fa2n6iCv4a6cpbCV76142MMeAAH7zL6pW/h3N99os9lg1bwIrTghk9+UNFVPTRUt4OSC ET7sNAqHVL/wBbzmiZmIvcuXHXpowKsroF2VMDHiUNsMJD7LofQthEzPMy0LMt3Hp1zu 1QsWZQa5FW1U7vrYz3IqV3enzM1wvsiYrE8dQANnt7mEkswXYS4rIv+kz1n2s9j1TOZo afAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PaIOxkLwjZNkboc5VdvhVflkFE+nJVqmqztmLhz5Ecs=; b=jYeo+OogaSCuYrZeDcfkTmgUxf8lughnkEnTB9hSEu9wPQbFe7O/+tPuSI+tx6L8Ti DjmavmUVU0LH+lujURaoBNFfFdyffzgEEtN490PHPVbGcNo4vQG07HElGOfy7PdifO8h KFJi1xcuOxNPqOQ2O1SJCxWh8VZPhLInyS4rtYhfapAT6jsl37MmI3CxpavhBDPeCMMB EmztMMliXHPmdyv6VyAfPXpsItmrYZGjQF5O+9W9K1n1VGZjU3009LLCS4lM22YPAbze usfLFhxztnX8GE2YPzIbqIvl+GeGPRmDSxL8h3VjWtbOxqQTkLcplfa96a1d4vRAXqLr dTPw== X-Gm-Message-State: AOAM531jgW5/3i+ugjFNubFiXrthlX+Tm70REqQGGDkbqYA819VSHdJB fl0vP3kmkbS7a5iUdLiAa+ZE X-Google-Smtp-Source: ABdhPJw3TFNoxCdPQysRD630etaISrhYgd0zCZGHhmrFuoiI4ArQPgqaw+RkMZTwY+YgXuxeZhj26Q== X-Received: by 2002:a63:91c3:0:b0:3f9:caa5:fa56 with SMTP id l186-20020a6391c3000000b003f9caa5fa56mr11489816pge.418.1653295887075; Mon, 23 May 2022 01:51:27 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 6/8] vduse-blk: Implement vduse-blk export Date: Mon, 23 May 2022 16:46:09 +0800 Message-Id: <20220523084611.91-7-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::535; envelope-from=xieyongji@bytedance.com; helo=mail-pg1-x535.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653296914553100001 Content-Type: text/plain; charset="utf-8" This implements a VDUSE block backends based on the libvduse library. We can use it to export the BDSs for both VM and container (host) usage. The new command-line syntax is: $ qemu-storage-daemon \ --blockdev file,node-name=3Ddrive0,filename=3Dtest.img \ --export vduse-blk,node-name=3Ddrive0,id=3Dvduse-export0,writable=3Don After the qemu-storage-daemon started, we need to use the "vdpa" command to attach the device to vDPA bus: $ vdpa dev add name vduse-export0 mgmtdev vduse Also the device must be removed via the "vdpa" command before we stop the qemu-storage-daemon. Signed-off-by: Xie Yongji Reviewed-by: Stefan Hajnoczi --- MAINTAINERS | 4 +- block/export/export.c | 6 + block/export/meson.build | 5 + block/export/vduse-blk.c | 307 ++++++++++++++++++++++++++++++++++ block/export/vduse-blk.h | 20 +++ meson.build | 13 ++ meson_options.txt | 2 + qapi/block-export.json | 28 +++- scripts/meson-buildoptions.sh | 4 + 9 files changed, 385 insertions(+), 4 deletions(-) create mode 100644 block/export/vduse-blk.c create mode 100644 block/export/vduse-blk.h diff --git a/MAINTAINERS b/MAINTAINERS index 966e07b7a0..d6fc0285a1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3554,10 +3554,12 @@ L: qemu-block@nongnu.org S: Supported F: block/export/fuse.c =20 -VDUSE library +VDUSE library and block device exports M: Xie Yongji S: Maintained F: subprojects/libvduse/ +F: block/export/vduse-blk.c +F: block/export/vduse-blk.h =20 Replication M: Wen Congyang diff --git a/block/export/export.c b/block/export/export.c index 7253af3bc3..4744862915 100644 --- a/block/export/export.c +++ b/block/export/export.c @@ -26,6 +26,9 @@ #ifdef CONFIG_VHOST_USER_BLK_SERVER #include "vhost-user-blk-server.h" #endif +#ifdef CONFIG_VDUSE_BLK_EXPORT +#include "vduse-blk.h" +#endif =20 static const BlockExportDriver *blk_exp_drivers[] =3D { &blk_exp_nbd, @@ -35,6 +38,9 @@ static const BlockExportDriver *blk_exp_drivers[] =3D { #ifdef CONFIG_FUSE &blk_exp_fuse, #endif +#ifdef CONFIG_VDUSE_BLK_EXPORT + &blk_exp_vduse_blk, +#endif }; =20 /* Only accessed from the main thread */ diff --git a/block/export/meson.build b/block/export/meson.build index 431e47ca51..c60116f455 100644 --- a/block/export/meson.build +++ b/block/export/meson.build @@ -5,3 +5,8 @@ if have_vhost_user_blk_server endif =20 blockdev_ss.add(when: fuse, if_true: files('fuse.c')) + +if have_vduse_blk_export + blockdev_ss.add(files('vduse-blk.c', 'virtio-blk-handler.c')) + blockdev_ss.add(libvduse) +endif diff --git a/block/export/vduse-blk.c b/block/export/vduse-blk.c new file mode 100644 index 0000000000..143d58a3f2 --- /dev/null +++ b/block/export/vduse-blk.c @@ -0,0 +1,307 @@ +/* + * Export QEMU block device via VDUSE + * + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * + * Author: + * Xie Yongji + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#include + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "block/export.h" +#include "qemu/error-report.h" +#include "util/block-helpers.h" +#include "subprojects/libvduse/libvduse.h" +#include "virtio-blk-handler.h" + +#include "standard-headers/linux/virtio_blk.h" + +#define VDUSE_DEFAULT_NUM_QUEUE 1 +#define VDUSE_DEFAULT_QUEUE_SIZE 256 + +typedef struct VduseBlkExport { + BlockExport export; + VirtioBlkHandler handler; + VduseDev *dev; + uint16_t num_queues; +} VduseBlkExport; + +typedef struct VduseBlkReq { + VduseVirtqElement elem; + VduseVirtq *vq; +} VduseBlkReq; + +static void vduse_blk_req_complete(VduseBlkReq *req, size_t in_len) +{ + vduse_queue_push(req->vq, &req->elem, in_len); + vduse_queue_notify(req->vq); + + free(req); +} + +static void coroutine_fn vduse_blk_virtio_process_req(void *opaque) +{ + VduseBlkReq *req =3D opaque; + VduseVirtq *vq =3D req->vq; + VduseDev *dev =3D vduse_queue_get_dev(vq); + VduseBlkExport *vblk_exp =3D vduse_dev_get_priv(dev); + VirtioBlkHandler *handler =3D &vblk_exp->handler; + VduseVirtqElement *elem =3D &req->elem; + struct iovec *in_iov =3D elem->in_sg; + struct iovec *out_iov =3D elem->out_sg; + unsigned in_num =3D elem->in_num; + unsigned out_num =3D elem->out_num; + int in_len; + + in_len =3D virtio_blk_process_req(handler, in_iov, + out_iov, in_num, out_num); + if (in_len < 0) { + free(req); + return; + } + + vduse_blk_req_complete(req, in_len); +} + +static void vduse_blk_vq_handler(VduseDev *dev, VduseVirtq *vq) +{ + while (1) { + VduseBlkReq *req; + + req =3D vduse_queue_pop(vq, sizeof(VduseBlkReq)); + if (!req) { + break; + } + req->vq =3D vq; + + Coroutine *co =3D + qemu_coroutine_create(vduse_blk_virtio_process_req, req); + qemu_coroutine_enter(co); + } +} + +static void on_vduse_vq_kick(void *opaque) +{ + VduseVirtq *vq =3D opaque; + VduseDev *dev =3D vduse_queue_get_dev(vq); + int fd =3D vduse_queue_get_fd(vq); + eventfd_t kick_data; + + if (eventfd_read(fd, &kick_data) =3D=3D -1) { + error_report("failed to read data from eventfd"); + return; + } + + vduse_blk_vq_handler(dev, vq); +} + +static void vduse_blk_enable_queue(VduseDev *dev, VduseVirtq *vq) +{ + VduseBlkExport *vblk_exp =3D vduse_dev_get_priv(dev); + + aio_set_fd_handler(vblk_exp->export.ctx, vduse_queue_get_fd(vq), + true, on_vduse_vq_kick, NULL, NULL, NULL, vq); +} + +static void vduse_blk_disable_queue(VduseDev *dev, VduseVirtq *vq) +{ + VduseBlkExport *vblk_exp =3D vduse_dev_get_priv(dev); + + aio_set_fd_handler(vblk_exp->export.ctx, vduse_queue_get_fd(vq), + true, NULL, NULL, NULL, NULL, NULL); +} + +static const VduseOps vduse_blk_ops =3D { + .enable_queue =3D vduse_blk_enable_queue, + .disable_queue =3D vduse_blk_disable_queue, +}; + +static void on_vduse_dev_kick(void *opaque) +{ + VduseDev *dev =3D opaque; + + vduse_dev_handler(dev); +} + +static void vduse_blk_attach_ctx(VduseBlkExport *vblk_exp, AioContext *ctx) +{ + int i; + + aio_set_fd_handler(vblk_exp->export.ctx, vduse_dev_get_fd(vblk_exp->de= v), + true, on_vduse_dev_kick, NULL, NULL, NULL, + vblk_exp->dev); + + for (i =3D 0; i < vblk_exp->num_queues; i++) { + VduseVirtq *vq =3D vduse_dev_get_queue(vblk_exp->dev, i); + int fd =3D vduse_queue_get_fd(vq); + + if (fd < 0) { + continue; + } + aio_set_fd_handler(vblk_exp->export.ctx, fd, true, + on_vduse_vq_kick, NULL, NULL, NULL, vq); + } +} + +static void vduse_blk_detach_ctx(VduseBlkExport *vblk_exp) +{ + int i; + + for (i =3D 0; i < vblk_exp->num_queues; i++) { + VduseVirtq *vq =3D vduse_dev_get_queue(vblk_exp->dev, i); + int fd =3D vduse_queue_get_fd(vq); + + if (fd < 0) { + continue; + } + aio_set_fd_handler(vblk_exp->export.ctx, fd, + true, NULL, NULL, NULL, NULL, NULL); + } + aio_set_fd_handler(vblk_exp->export.ctx, vduse_dev_get_fd(vblk_exp->de= v), + true, NULL, NULL, NULL, NULL, NULL); +} + + +static void blk_aio_attached(AioContext *ctx, void *opaque) +{ + VduseBlkExport *vblk_exp =3D opaque; + + vblk_exp->export.ctx =3D ctx; + vduse_blk_attach_ctx(vblk_exp, ctx); +} + +static void blk_aio_detach(void *opaque) +{ + VduseBlkExport *vblk_exp =3D opaque; + + vduse_blk_detach_ctx(vblk_exp); + vblk_exp->export.ctx =3D NULL; +} + +static int vduse_blk_exp_create(BlockExport *exp, BlockExportOptions *opts, + Error **errp) +{ + VduseBlkExport *vblk_exp =3D container_of(exp, VduseBlkExport, export); + BlockExportOptionsVduseBlk *vblk_opts =3D &opts->u.vduse_blk; + uint64_t logical_block_size =3D VIRTIO_BLK_SECTOR_SIZE; + uint16_t num_queues =3D VDUSE_DEFAULT_NUM_QUEUE; + uint16_t queue_size =3D VDUSE_DEFAULT_QUEUE_SIZE; + Error *local_err =3D NULL; + struct virtio_blk_config config =3D { 0 }; + uint64_t features; + int i; + + if (vblk_opts->has_num_queues) { + num_queues =3D vblk_opts->num_queues; + if (num_queues =3D=3D 0) { + error_setg(errp, "num-queues must be greater than 0"); + return -EINVAL; + } + } + + if (vblk_opts->has_queue_size) { + queue_size =3D vblk_opts->queue_size; + if (queue_size <=3D 2 || !is_power_of_2(queue_size) || + queue_size > VIRTQUEUE_MAX_SIZE) { + error_setg(errp, "queue-size is invalid"); + return -EINVAL; + } + } + + if (vblk_opts->has_logical_block_size) { + logical_block_size =3D vblk_opts->logical_block_size; + check_block_size(exp->id, "logical-block-size", logical_block_size, + &local_err); + if (local_err) { + error_propagate(errp, local_err); + return -EINVAL; + } + } + vblk_exp->num_queues =3D num_queues; + vblk_exp->handler.blk =3D exp->blk; + vblk_exp->handler.serial =3D exp->id; + vblk_exp->handler.logical_block_size =3D logical_block_size; + vblk_exp->handler.writable =3D opts->writable; + + config.capacity =3D + cpu_to_le64(blk_getlength(exp->blk) >> VIRTIO_BLK_SECTOR_BITS); + config.seg_max =3D cpu_to_le32(queue_size - 2); + config.min_io_size =3D cpu_to_le16(1); + config.opt_io_size =3D cpu_to_le32(1); + config.num_queues =3D cpu_to_le16(num_queues); + config.blk_size =3D cpu_to_le32(logical_block_size); + config.max_discard_sectors =3D cpu_to_le32(VIRTIO_BLK_MAX_DISCARD_SECT= ORS); + config.max_discard_seg =3D cpu_to_le32(1); + config.discard_sector_alignment =3D + cpu_to_le32(logical_block_size >> VIRTIO_BLK_SECTOR_BITS); + config.max_write_zeroes_sectors =3D + cpu_to_le32(VIRTIO_BLK_MAX_WRITE_ZEROES_SECTORS); + config.max_write_zeroes_seg =3D cpu_to_le32(1); + + features =3D vduse_get_virtio_features() | + (1ULL << VIRTIO_BLK_F_SEG_MAX) | + (1ULL << VIRTIO_BLK_F_TOPOLOGY) | + (1ULL << VIRTIO_BLK_F_BLK_SIZE) | + (1ULL << VIRTIO_BLK_F_FLUSH) | + (1ULL << VIRTIO_BLK_F_DISCARD) | + (1ULL << VIRTIO_BLK_F_WRITE_ZEROES); + + if (num_queues > 1) { + features |=3D 1ULL << VIRTIO_BLK_F_MQ; + } + if (!opts->writable) { + features |=3D 1ULL << VIRTIO_BLK_F_RO; + } + + vblk_exp->dev =3D vduse_dev_create(exp->id, VIRTIO_ID_BLOCK, 0, + features, num_queues, + sizeof(struct virtio_blk_config), + (char *)&config, &vduse_blk_ops, + vblk_exp); + if (!vblk_exp->dev) { + error_setg(errp, "failed to create vduse device"); + return -ENOMEM; + } + + for (i =3D 0; i < num_queues; i++) { + vduse_dev_setup_queue(vblk_exp->dev, i, queue_size); + } + + aio_set_fd_handler(exp->ctx, vduse_dev_get_fd(vblk_exp->dev), true, + on_vduse_dev_kick, NULL, NULL, NULL, vblk_exp->dev); + + blk_add_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detac= h, + vblk_exp); + + return 0; +} + +static void vduse_blk_exp_delete(BlockExport *exp) +{ + VduseBlkExport *vblk_exp =3D container_of(exp, VduseBlkExport, export); + + blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_de= tach, + vblk_exp); + vduse_dev_destroy(vblk_exp->dev); +} + +static void vduse_blk_exp_request_shutdown(BlockExport *exp) +{ + VduseBlkExport *vblk_exp =3D container_of(exp, VduseBlkExport, export); + + vduse_blk_detach_ctx(vblk_exp); +} + +const BlockExportDriver blk_exp_vduse_blk =3D { + .type =3D BLOCK_EXPORT_TYPE_VDUSE_BLK, + .instance_size =3D sizeof(VduseBlkExport), + .create =3D vduse_blk_exp_create, + .delete =3D vduse_blk_exp_delete, + .request_shutdown =3D vduse_blk_exp_request_shutdown, +}; diff --git a/block/export/vduse-blk.h b/block/export/vduse-blk.h new file mode 100644 index 0000000000..c4eeb1b70e --- /dev/null +++ b/block/export/vduse-blk.h @@ -0,0 +1,20 @@ +/* + * Export QEMU block device via VDUSE + * + * Copyright (C) 2022 Bytedance Inc. and/or its affiliates. All rights res= erved. + * + * Author: + * Xie Yongji + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#ifndef VDUSE_BLK_H +#define VDUSE_BLK_H + +#include "block/export.h" + +extern const BlockExportDriver blk_exp_vduse_blk; + +#endif /* VDUSE_BLK_H */ diff --git a/meson.build b/meson.build index 3f70ccec60..49176a3d24 100644 --- a/meson.build +++ b/meson.build @@ -1529,6 +1529,17 @@ if have_libvduse libvduse =3D libvduse_proj.get_variable('libvduse_dep') endif =20 +have_vduse_blk_export =3D (have_libvduse and targetos =3D=3D 'linux') +if get_option('vduse_blk_export').enabled() + if targetos !=3D 'linux' + error('vduse_blk_export requires linux') + elif not have_libvduse + error('vduse_blk_export requires libvduse support') + endif +elif get_option('vduse_blk_export').disabled() + have_vduse_blk_export =3D false +endif + # libbpf libbpf =3D dependency('libbpf', required: get_option('bpf'), method: 'pkg-= config') if libbpf.found() and not cc.links(''' @@ -1770,6 +1781,7 @@ config_host_data.set('CONFIG_VHOST_CRYPTO', have_vhos= t_user_crypto) config_host_data.set('CONFIG_VHOST_VDPA', have_vhost_vdpa) config_host_data.set('CONFIG_VMNET', vmnet.found()) config_host_data.set('CONFIG_VHOST_USER_BLK_SERVER', have_vhost_user_blk_s= erver) +config_host_data.set('CONFIG_VDUSE_BLK_EXPORT', have_vduse_blk_export) config_host_data.set('CONFIG_PNG', png.found()) config_host_data.set('CONFIG_VNC', vnc.found()) config_host_data.set('CONFIG_VNC_JPEG', jpeg.found()) @@ -3799,6 +3811,7 @@ if have_block summary_info +=3D {'qed support': get_option('qed').allowed()} summary_info +=3D {'parallels support': get_option('parallels').allowed(= )} summary_info +=3D {'FUSE exports': fuse} + summary_info +=3D {'VDUSE block exports': have_vduse_blk_export} endif summary(summary_info, bool_yn: true, section: 'Block layer support') =20 diff --git a/meson_options.txt b/meson_options.txt index 50da8dea94..dee5671386 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -255,6 +255,8 @@ option('virtiofsd', type: 'feature', value: 'auto', description: 'build virtiofs daemon (virtiofsd)') option('libvduse', type: 'feature', value: 'auto', description: 'build VDUSE Library') +option('vduse_blk_export', type: 'feature', value: 'auto', + description: 'VDUSE block export support') =20 option('capstone', type: 'feature', value: 'auto', description: 'Whether and how to find the capstone library') diff --git a/qapi/block-export.json b/qapi/block-export.json index 0685cb8b9a..e4bd4de363 100644 --- a/qapi/block-export.json +++ b/qapi/block-export.json @@ -177,6 +177,23 @@ '*allow-other': 'FuseExportAllowOther' }, 'if': 'CONFIG_FUSE' } =20 +## +# @BlockExportOptionsVduseBlk: +# +# A vduse-blk block export. +# +# @num-queues: the number of virtqueues. Defaults to 1. +# @queue-size: the size of virtqueue. Defaults to 256. +# @logical-block-size: Logical block size in bytes. Range [512, PAGE_SIZE] +# and must be power of 2. Defaults to 512 bytes. +# +# Since: 7.1 +## +{ 'struct': 'BlockExportOptionsVduseBlk', + 'data': { '*num-queues': 'uint16', + '*queue-size': 'uint16', + '*logical-block-size': 'size'} } + ## # @NbdServerAddOptions: # @@ -280,6 +297,7 @@ # @nbd: NBD export # @vhost-user-blk: vhost-user-blk export (since 5.2) # @fuse: FUSE export (since: 6.0) +# @vduse-blk: vduse-blk export (since 7.1) # # Since: 4.2 ## @@ -287,7 +305,8 @@ 'data': [ 'nbd', { 'name': 'vhost-user-blk', 'if': 'CONFIG_VHOST_USER_BLK_SERVER' }, - { 'name': 'fuse', 'if': 'CONFIG_FUSE' } ] } + { 'name': 'fuse', 'if': 'CONFIG_FUSE' }, + { 'name': 'vduse-blk', 'if': 'CONFIG_VDUSE_BLK_EXPORT' } ] } =20 ## # @BlockExportOptions: @@ -295,7 +314,8 @@ # Describes a block export, i.e. how single node should be exported on an # external interface. # -# @id: A unique identifier for the block export (across all export types) +# @id: A unique identifier for the block export (across the host for vduse= -blk +# export type or across all export types for other types) # # @node-name: The node name of the block node to be exported (since: 5.2) # @@ -331,7 +351,9 @@ 'vhost-user-blk': { 'type': 'BlockExportOptionsVhostUserBlk', 'if': 'CONFIG_VHOST_USER_BLK_SERVER' }, 'fuse': { 'type': 'BlockExportOptionsFuse', - 'if': 'CONFIG_FUSE' } + 'if': 'CONFIG_FUSE' }, + 'vduse-blk': { 'type': 'BlockExportOptionsVduseBlk', + 'if': 'CONFIG_VDUSE_BLK_EXPORT' } } } =20 ## diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 957a46ec89..21b7f4f620 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -160,6 +160,8 @@ meson_options_help() { printf "%s\n" ' vmnet vmnet.framework network backend support' printf "%s\n" ' vhost-user-blk-server' printf "%s\n" ' build vhost-user-blk server' + printf "%s\n" ' vduse-blk-export' + printf "%s\n" ' VDUSE block export support' printf "%s\n" ' vhost-vdpa vhost-vdpa kernel backend support' printf "%s\n" ' virglrenderer virgl rendering support' printf "%s\n" ' virtfs virtio-9p support' @@ -425,6 +427,8 @@ _meson_option_parse() { --disable-vhost-user) printf "%s" -Dvhost_user=3Ddisabled ;; --enable-vhost-user-blk-server) printf "%s" -Dvhost_user_blk_server=3D= enabled ;; --disable-vhost-user-blk-server) printf "%s" -Dvhost_user_blk_server= =3Ddisabled ;; + --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=3Denabled ;; + --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=3Ddisabled = ;; --enable-vhost-vdpa) printf "%s" -Dvhost_vdpa=3Denabled ;; --disable-vhost-vdpa) printf "%s" -Dvhost_vdpa=3Ddisabled ;; --enable-virglrenderer) printf "%s" -Dvirglrenderer=3Denabled ;; --=20 2.20.1 From nobody Thu Apr 25 06:55:41 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653298678; cv=none; d=zohomail.com; s=zohoarc; b=CwVjG3WWqR2UMfmjruJemEfzCJ4WRP2UbArFxa1EQtHtLsD51/JGEj97bTJrguKCW2AgP85Ejan2kE4hdr6S2r3jebg5giSCJteQvxEoyc/U614+4WavNtEDQ36mpJKScouKkJoG1gDtKT1vOYrDYPzWjmpBPf6IQ1nxUMHvth4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653298678; h=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=g7yHpkgF/dQwMATiZoIMH/6ax09zRT4xrq1qJ60gB8Y=; b=hhb4QAt42ge75ikfp9Nlq0B3UzJC+WR2EvxpIReJZnoRePvvDkew/FqeQxnmMf9QDVwDSH0qVePErIZqhxsp9cTb/UoP6PJMi6wj8LbEeprpgHX6hp+L4ZGmRRjFjVEJfYzmSBDCb7EUxBw4hQ93bIebEcX3E/DNVVAyXO3/SUg= 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=fail 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 1653298678466904.9737363977782; Mon, 23 May 2022 02:37:58 -0700 (PDT) Received: from localhost ([::1]:54458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt4Vg-0000YK-SY for importer2@patchew.org; Mon, 23 May 2022 05:37:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57042) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3n1-0002AN-3a for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:48 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]:45958) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mt-0000cV-D1 for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:42 -0400 Received: by mail-pj1-x102b.google.com with SMTP id w2-20020a17090ac98200b001e0519fe5a8so1518392pjt.4 for ; Mon, 23 May 2022 01:51:31 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id mg18-20020a17090b371200b001d93118827asm6549491pjb.57.2022.05.23.01.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=g7yHpkgF/dQwMATiZoIMH/6ax09zRT4xrq1qJ60gB8Y=; b=kPYEyHBuXH9aQLnNJpcgn4RmMqSMAYSr0Ij4gRavWtjVrrnT8roxlXeUeiNcvC0vHW sfx/NhuTyJ6eYZ1Pa6sxOhCwe3MatnOppE+NoTkCQUq6Rb4eYOc1KomFzPKeYEsdU64W V/D+Cbqu4Y5+cIPq9keIbeJLHkaEKBgNL2DzKrlQHyTDCXQkY5fjzM6ZYi+tm/o58rFY 2meVnJzVuh4jdKw1jl3hKR6Jt162pddAeYfTA0n77cPawzkshjLfLDP//3xBFujEtF4b 7BfuAz4ce01utL8FQAn+AZjns95CoX/w/UIgTKmlBqAI+Hy7E/4raWaLyeRrqOL+LuwQ tTQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g7yHpkgF/dQwMATiZoIMH/6ax09zRT4xrq1qJ60gB8Y=; b=r0Y2pB/jqZvELD+nBsU9LQWHPkQtHNNppIwfLDaMK3BRvP3gwNEmzlVgiGPhr+dCXL AfZNrf+1WAm4Ig86Z8ZAACTQkfiCBndZK3nxiQ9q29WjEsi4UH4OV3TOPrwKruujShdw cut8KstxNcFxMzqcE54ZZ9i5yZ1v9OLFLm2JjtZXdrzVW89vronhPmBYw7ISzMuA0FDq 7KU5dpNfUbaQF1/p2gPhLns+WeSrRANi2Uj9OXLXOo7OU/n/XSzxKMeQBD8AZT9QA3CW 4H9E2yPPsp92T7tw9zVaxEGkldPQN4N8qRZDWXkyCXHxrN/plUW/KGPiYoxmvlN+A0qg Lblg== X-Gm-Message-State: AOAM533O3nD7wi7Eue5RkK5Koeqt7KwMexuWIx14b4F/G53lrMyXozUg /tuE9t8aVrhTqleG4s67KWDW X-Google-Smtp-Source: ABdhPJyS2feWnbVWlY+pi/lTYsKy7Oj3PLOkr5qdarfdyFf9u7dvH8ENkh49XJmb7lOy89QnH5ni8A== X-Received: by 2002:a17:903:1013:b0:162:1ddd:b4d6 with SMTP id a19-20020a170903101300b001621dddb4d6mr5607201plb.162.1653295891010; Mon, 23 May 2022 01:51:31 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 7/8] vduse-blk: Add vduse-blk resize support Date: Mon, 23 May 2022 16:46:10 +0800 Message-Id: <20220523084611.91-8-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::102b; envelope-from=xieyongji@bytedance.com; helo=mail-pj1-x102b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653298680875100001 Content-Type: text/plain; charset="utf-8" To support block resize, this uses vduse_dev_update_config() to update the capacity field in configuration space and inject config interrupt on the block resize callback. Signed-off-by: Xie Yongji Reviewed-by: Stefan Hajnoczi --- block/export/vduse-blk.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/block/export/vduse-blk.c b/block/export/vduse-blk.c index 143d58a3f2..1040130f52 100644 --- a/block/export/vduse-blk.c +++ b/block/export/vduse-blk.c @@ -184,6 +184,23 @@ static void blk_aio_detach(void *opaque) vblk_exp->export.ctx =3D NULL; } =20 +static void vduse_blk_resize(void *opaque) +{ + BlockExport *exp =3D opaque; + VduseBlkExport *vblk_exp =3D container_of(exp, VduseBlkExport, export); + struct virtio_blk_config config; + + config.capacity =3D + cpu_to_le64(blk_getlength(exp->blk) >> VIRTIO_BLK_SECTOR_BITS); + vduse_dev_update_config(vblk_exp->dev, sizeof(config.capacity), + offsetof(struct virtio_blk_config, capacity), + (char *)&config.capacity); +} + +static const BlockDevOps vduse_block_ops =3D { + .resize_cb =3D vduse_blk_resize, +}; + static int vduse_blk_exp_create(BlockExport *exp, BlockExportOptions *opts, Error **errp) { @@ -279,6 +296,8 @@ static int vduse_blk_exp_create(BlockExport *exp, Block= ExportOptions *opts, blk_add_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_detac= h, vblk_exp); =20 + blk_set_dev_ops(exp->blk, &vduse_block_ops, exp); + return 0; } =20 @@ -288,6 +307,7 @@ static void vduse_blk_exp_delete(BlockExport *exp) =20 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached, blk_aio_de= tach, vblk_exp); + blk_set_dev_ops(exp->blk, NULL, NULL); vduse_dev_destroy(vblk_exp->dev); } =20 --=20 2.20.1 From nobody Thu Apr 25 06:55:41 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=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1653297145; cv=none; d=zohomail.com; s=zohoarc; b=USyBy0wyQcB5v/d2rafXITi7C0cx/s/gHr6SO3RxdNoU1rwbkwHd3uDRbe14e/qa4hp0EuNRe5+ZfeyninuSCn6DgI1MeAwFpw/gnfLZSiW/DhnG2rXJyI5h8cLwq2f7NLXBfeThmKYBb/pkSIznUryLuTsh96vrIT8HDO8yJZY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653297145; h=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=/I/k6ZqrAE/vjo+NIfttENtcZEayZcmb1VmhB3q3Lo8=; b=JqFU+MJeshWjFYY/bPrhBmKP9PsMaXidiF/S75T7I15Ld7rWZoPmDfZEKJ2v+KYq4qv0hdK0naQcATsKvzBTOIOHPxBIRQrMk+xCr/xs4s6zDNl+8hRekFHD1t0AsSrS7fILMKgv2iQ69Tj7iy04CNfqUx2LcLl5upr4WXFKZoM= 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=fail 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 1653297145416706.5644620512021; Mon, 23 May 2022 02:12:25 -0700 (PDT) Received: from localhost ([::1]:58936 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nt46y-0008K2-9Y for importer2@patchew.org; Mon, 23 May 2022 05:12:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nt3n1-0002AM-2P for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:48 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]:42681) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nt3mt-0000dd-Cm for qemu-devel@nongnu.org; Mon, 23 May 2022 04:51:43 -0400 Received: by mail-pf1-x42e.google.com with SMTP id y199so13074506pfb.9 for ; Mon, 23 May 2022 01:51:36 -0700 (PDT) Received: from localhost ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id x11-20020a170902b40b00b0015ea9aabd19sm4497380plr.241.2022.05.23.01.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:51:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/I/k6ZqrAE/vjo+NIfttENtcZEayZcmb1VmhB3q3Lo8=; b=8G6gFpVD8/JpxnQArou/TUTizxfp8zL4xbel5Zv4WyVMGuQqRW00H0bTZVYFPeD+Ok wICzsGVm+Zavlv4LnrU3TtLwb908SQrQc03z5IXpvH3N0Ng6qQS62rjsUplesI3dhUrL eyar5n/5R3H6JxEBc30erZRK5n9MJ2l53YGDd4ZeOpQVJllwAeiShzszNY+td2AgQW+A H/B6pvQ2thmv8wjqpo/EY4vtlHcp06Xw5+3sg3wyohmOU+q2OoDngjz0ZW/AjKIg2KPE /nJ0K2cPkH57QrgxL4LOMMq8ChIEvcJI9Ff8Tsj/e9AXdT37PHyb7jjqdgBFNf7MYYPN XlwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/I/k6ZqrAE/vjo+NIfttENtcZEayZcmb1VmhB3q3Lo8=; b=dULFRx77HqI8VeucLQ79ses+W8Xnh5pIZuF3La4qzjzblKoDGRezFSkfJ3uzgX9F1Q IwavcMi83P8IplIuEIUuEc05fgP9VxX4Mq8SuJlrYKt1VPyPn/wI/4qpYu6kgKPtMpPu zMfOm5iCfF1PYYx9wuyPLo+legFEmIoyQM3sMrOZ1ATVPWauwjgGuk1jofU4dGmGmYGr YjJCEoP1yRUwJrxIV5ekCOP5D7fkfKAkiCVsGtR+JWygoTMqV7+CyfEC7Ka7WBFD71pb CStJRQ/n/53NqteWXHMG3EfiYrnD7LBR4rSzYF7jTJD1y+RQdanbT2/Ax1MFegBbe9FN E3AQ== X-Gm-Message-State: AOAM531cT4B70tUqlP6V9Lra1gJJj1R28/IMSx+Km8WORgD9zuVU2r/I fObq5X4toubZgh/uRw5Cod4h X-Google-Smtp-Source: ABdhPJwdBXmDhQbe27Yal8EFoBoHg7mqZSzqlT4eZ6RYcPAhZij4kjRLNlIyJbYCHlC9qLwACqROIg== X-Received: by 2002:a63:842:0:b0:3db:b1a0:ed80 with SMTP id 63-20020a630842000000b003dbb1a0ed80mr19415226pgi.141.1653295895090; Mon, 23 May 2022 01:51:35 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, kwolf@redhat.com, mreitz@redhat.com, mlureau@redhat.com, jsnow@redhat.com, eblake@redhat.com, Coiby.Xu@gmail.com, hreitz@redhat.com Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v6 8/8] libvduse: Add support for reconnecting Date: Mon, 23 May 2022 16:46:11 +0800 Message-Id: <20220523084611.91-9-xieyongji@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523084611.91-1-xieyongji@bytedance.com> References: <20220523084611.91-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42e; envelope-from=xieyongji@bytedance.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1653297145905100001 Content-Type: text/plain; charset="utf-8" To support reconnecting after restart or crash, VDUSE backend might need to resubmit inflight I/Os. This stores the metadata such as the index of inflight I/O's descriptors to a shm file so that VDUSE backend can restore them during reconnecting. Signed-off-by: Xie Yongji --- block/export/vduse-blk.c | 14 ++ subprojects/libvduse/libvduse.c | 235 +++++++++++++++++++++++++++++++- subprojects/libvduse/libvduse.h | 12 ++ 3 files changed, 256 insertions(+), 5 deletions(-) diff --git a/block/export/vduse-blk.c b/block/export/vduse-blk.c index 1040130f52..3b10349173 100644 --- a/block/export/vduse-blk.c +++ b/block/export/vduse-blk.c @@ -30,6 +30,7 @@ typedef struct VduseBlkExport { VirtioBlkHandler handler; VduseDev *dev; uint16_t num_queues; + char *recon_file; } VduseBlkExport; =20 typedef struct VduseBlkReq { @@ -107,6 +108,8 @@ static void vduse_blk_enable_queue(VduseDev *dev, Vduse= Virtq *vq) =20 aio_set_fd_handler(vblk_exp->export.ctx, vduse_queue_get_fd(vq), true, on_vduse_vq_kick, NULL, NULL, NULL, vq); + /* Make sure we don't miss any kick afer reconnecting */ + eventfd_write(vduse_queue_get_fd(vq), 1); } =20 static void vduse_blk_disable_queue(VduseDev *dev, VduseVirtq *vq) @@ -286,6 +289,15 @@ static int vduse_blk_exp_create(BlockExport *exp, Bloc= kExportOptions *opts, return -ENOMEM; } =20 + vblk_exp->recon_file =3D g_strdup_printf("%s/vduse-blk-%s", + g_get_tmp_dir(), exp->id); + if (vduse_set_reconnect_log_file(vblk_exp->dev, vblk_exp->recon_file))= { + error_setg(errp, "failed to set reconnect log file"); + vduse_dev_destroy(vblk_exp->dev); + g_free(vblk_exp->recon_file); + return -EINVAL; + } + for (i =3D 0; i < num_queues; i++) { vduse_dev_setup_queue(vblk_exp->dev, i, queue_size); } @@ -309,6 +321,8 @@ static void vduse_blk_exp_delete(BlockExport *exp) vblk_exp); blk_set_dev_ops(exp->blk, NULL, NULL); vduse_dev_destroy(vblk_exp->dev); + unlink(vblk_exp->recon_file); + g_free(vblk_exp->recon_file); } =20 static void vduse_blk_exp_request_shutdown(BlockExport *exp) diff --git a/subprojects/libvduse/libvduse.c b/subprojects/libvduse/libvdus= e.c index fa4822b9a9..78bb777402 100644 --- a/subprojects/libvduse/libvduse.c +++ b/subprojects/libvduse/libvduse.c @@ -41,6 +41,8 @@ #define VDUSE_VQ_ALIGN 4096 #define MAX_IOVA_REGIONS 256 =20 +#define LOG_ALIGNMENT 64 + /* Round number down to multiple */ #define ALIGN_DOWN(n, m) ((n) / (m) * (m)) =20 @@ -51,6 +53,31 @@ #define unlikely(x) __builtin_expect(!!(x), 0) #endif =20 +typedef struct VduseDescStateSplit { + uint8_t inflight; + uint8_t padding[5]; + uint16_t next; + uint64_t counter; +} VduseDescStateSplit; + +typedef struct VduseVirtqLogInflight { + uint64_t features; + uint16_t version; + uint16_t desc_num; + uint16_t last_batch_head; + uint16_t used_idx; + VduseDescStateSplit desc[]; +} VduseVirtqLogInflight; + +typedef struct VduseVirtqLog { + VduseVirtqLogInflight inflight; +} VduseVirtqLog; + +typedef struct VduseVirtqInflightDesc { + uint16_t index; + uint64_t counter; +} VduseVirtqInflightDesc; + typedef struct VduseRing { unsigned int num; uint64_t desc_addr; @@ -73,6 +100,10 @@ struct VduseVirtq { bool ready; int fd; VduseDev *dev; + VduseVirtqInflightDesc *resubmit_list; + uint16_t resubmit_num; + uint64_t counter; + VduseVirtqLog *log; }; =20 typedef struct VduseIovaRegion { @@ -96,8 +127,36 @@ struct VduseDev { int fd; int ctrl_fd; void *priv; + void *log; }; =20 +static inline size_t vduse_vq_log_size(uint16_t queue_size) +{ + return ALIGN_UP(sizeof(VduseDescStateSplit) * queue_size + + sizeof(VduseVirtqLogInflight), LOG_ALIGNMENT); +} + +static void *vduse_log_get(const char *filename, size_t size) +{ + void *ptr =3D MAP_FAILED; + int fd; + + fd =3D open(filename, O_RDWR | O_CREAT, 0600); + if (fd =3D=3D -1) { + return MAP_FAILED; + } + + if (ftruncate(fd, size) =3D=3D -1) { + goto out; + } + + ptr =3D mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + +out: + close(fd); + return ptr; +} + static inline bool has_feature(uint64_t features, unsigned int fbit) { assert(fbit < 64); @@ -148,6 +207,105 @@ static int vduse_inject_irq(VduseDev *dev, int index) return ioctl(dev->fd, VDUSE_VQ_INJECT_IRQ, &index); } =20 +static int inflight_desc_compare(const void *a, const void *b) +{ + VduseVirtqInflightDesc *desc0 =3D (VduseVirtqInflightDesc *)a, + *desc1 =3D (VduseVirtqInflightDesc *)b; + + if (desc1->counter > desc0->counter && + (desc1->counter - desc0->counter) < VIRTQUEUE_MAX_SIZE * 2) { + return 1; + } + + return -1; +} + +static int vduse_queue_check_inflights(VduseVirtq *vq) +{ + int i =3D 0; + VduseDev *dev =3D vq->dev; + + vq->used_idx =3D le16toh(vq->vring.used->idx); + vq->resubmit_num =3D 0; + vq->resubmit_list =3D NULL; + vq->counter =3D 0; + + if (unlikely(vq->log->inflight.used_idx !=3D vq->used_idx)) { + if (vq->log->inflight.last_batch_head > VIRTQUEUE_MAX_SIZE) { + return -1; + } + + vq->log->inflight.desc[vq->log->inflight.last_batch_head].inflight= =3D 0; + + barrier(); + + vq->log->inflight.used_idx =3D vq->used_idx; + } + + for (i =3D 0; i < vq->log->inflight.desc_num; i++) { + if (vq->log->inflight.desc[i].inflight =3D=3D 1) { + vq->inuse++; + } + } + + vq->shadow_avail_idx =3D vq->last_avail_idx =3D vq->inuse + vq->used_i= dx; + + if (vq->inuse) { + vq->resubmit_list =3D calloc(vq->inuse, sizeof(VduseVirtqInflightD= esc)); + if (!vq->resubmit_list) { + return -1; + } + + for (i =3D 0; i < vq->log->inflight.desc_num; i++) { + if (vq->log->inflight.desc[i].inflight) { + vq->resubmit_list[vq->resubmit_num].index =3D i; + vq->resubmit_list[vq->resubmit_num].counter =3D + vq->log->inflight.desc[i].counter; + vq->resubmit_num++; + } + } + + if (vq->resubmit_num > 1) { + qsort(vq->resubmit_list, vq->resubmit_num, + sizeof(VduseVirtqInflightDesc), inflight_desc_compare); + } + vq->counter =3D vq->resubmit_list[0].counter + 1; + } + + vduse_inject_irq(dev, vq->index); + + return 0; +} + +static int vduse_queue_inflight_get(VduseVirtq *vq, int desc_idx) +{ + vq->log->inflight.desc[desc_idx].counter =3D vq->counter++; + + barrier(); + + vq->log->inflight.desc[desc_idx].inflight =3D 1; + + return 0; +} + +static int vduse_queue_inflight_pre_put(VduseVirtq *vq, int desc_idx) +{ + vq->log->inflight.last_batch_head =3D desc_idx; + + return 0; +} + +static int vduse_queue_inflight_post_put(VduseVirtq *vq, int desc_idx) +{ + vq->log->inflight.desc[desc_idx].inflight =3D 0; + + barrier(); + + vq->log->inflight.used_idx =3D vq->used_idx; + + return 0; +} + static void vduse_iova_remove_region(VduseDev *dev, uint64_t start, uint64_t last) { @@ -596,11 +754,24 @@ void *vduse_queue_pop(VduseVirtq *vq, size_t sz) unsigned int head; VduseVirtqElement *elem; VduseDev *dev =3D vq->dev; + int i; =20 if (unlikely(!vq->vring.avail)) { return NULL; } =20 + if (unlikely(vq->resubmit_list && vq->resubmit_num > 0)) { + i =3D (--vq->resubmit_num); + elem =3D vduse_queue_map_desc(vq, vq->resubmit_list[i].index, sz); + + if (!vq->resubmit_num) { + free(vq->resubmit_list); + vq->resubmit_list =3D NULL; + } + + return elem; + } + if (vduse_queue_empty(vq)) { return NULL; } @@ -628,6 +799,8 @@ void *vduse_queue_pop(VduseVirtq *vq, size_t sz) =20 vq->inuse++; =20 + vduse_queue_inflight_get(vq, head); + return elem; } =20 @@ -685,7 +858,9 @@ void vduse_queue_push(VduseVirtq *vq, const VduseVirtqE= lement *elem, unsigned int len) { vduse_queue_fill(vq, elem, len, 0); + vduse_queue_inflight_pre_put(vq, elem->index); vduse_queue_flush(vq, 1); + vduse_queue_inflight_post_put(vq, elem->index); } =20 static int vduse_queue_update_vring(VduseVirtq *vq, uint64_t desc_addr, @@ -764,12 +939,15 @@ static void vduse_queue_enable(VduseVirtq *vq) } =20 vq->fd =3D fd; - vq->shadow_avail_idx =3D vq->last_avail_idx =3D vq_info.split.avail_in= dex; - vq->inuse =3D 0; - vq->used_idx =3D 0; vq->signalled_used_valid =3D false; vq->ready =3D true; =20 + if (vduse_queue_check_inflights(vq)) { + fprintf(stderr, "Failed to check inflights for vq[%d]\n", vq->inde= x); + close(fd); + return; + } + dev->ops->enable_queue(dev, vq); } =20 @@ -819,11 +997,15 @@ static void vduse_dev_start_dataplane(VduseDev *dev) =20 static void vduse_dev_stop_dataplane(VduseDev *dev) { + size_t log_size =3D dev->num_queues * vduse_vq_log_size(VIRTQUEUE_MAX_= SIZE); int i; =20 for (i =3D 0; i < dev->num_queues; i++) { vduse_queue_disable(&dev->vqs[i]); } + if (dev->log) { + memset(dev->log, 0, log_size); + } dev->features =3D 0; vduse_iova_remove_region(dev, 0, ULONG_MAX); } @@ -932,6 +1114,30 @@ int vduse_dev_setup_queue(VduseDev *dev, int index, i= nt max_size) return -errno; } =20 + vduse_queue_enable(vq); + + return 0; +} + +int vduse_set_reconnect_log_file(VduseDev *dev, const char *filename) +{ + + size_t log_size =3D dev->num_queues * vduse_vq_log_size(VIRTQUEUE_MAX_= SIZE); + void *log; + int i; + + dev->log =3D log =3D vduse_log_get(filename, log_size); + if (log =3D=3D MAP_FAILED) { + fprintf(stderr, "Failed to get vduse log\n"); + return -EINVAL; + } + + for (i =3D 0; i < dev->num_queues; i++) { + dev->vqs[i].log =3D log; + dev->vqs[i].log->inflight.desc_num =3D VIRTQUEUE_MAX_SIZE; + log =3D (void *)((char *)log + vduse_vq_log_size(VIRTQUEUE_MAX_SIZ= E)); + } + return 0; } =20 @@ -976,6 +1182,12 @@ static int vduse_dev_init(VduseDev *dev, const char *= name, return -errno; } =20 + if (ioctl(fd, VDUSE_DEV_GET_FEATURES, &dev->features)) { + fprintf(stderr, "Failed to get features: %s\n", strerror(errno)); + close(fd); + return -errno; + } + dev_name =3D strdup(name); if (!dev_name) { close(fd); @@ -1020,6 +1232,12 @@ VduseDev *vduse_dev_create_by_fd(int fd, uint16_t nu= m_queues, return NULL; } =20 + if (ioctl(fd, VDUSE_DEV_GET_FEATURES, &dev->features)) { + fprintf(stderr, "Failed to get features: %s\n", strerror(errno)); + free(dev); + return NULL; + } + ret =3D vduse_dev_init_vqs(dev, num_queues); if (ret) { fprintf(stderr, "Failed to init vqs\n"); @@ -1119,7 +1337,7 @@ VduseDev *vduse_dev_create(const char *name, uint32_t= device_id, =20 ret =3D ioctl(ctrl_fd, VDUSE_CREATE_DEV, dev_config); free(dev_config); - if (ret < 0) { + if (ret && errno !=3D EEXIST) { fprintf(stderr, "Failed to create vduse device %s: %s\n", name, strerror(errno)); goto err_dev; @@ -1146,8 +1364,15 @@ err_ctrl: =20 int vduse_dev_destroy(VduseDev *dev) { - int ret =3D 0; + size_t log_size =3D dev->num_queues * vduse_vq_log_size(VIRTQUEUE_MAX_= SIZE); + int i, ret =3D 0; =20 + if (dev->log) { + munmap(dev->log, log_size); + } + for (i =3D 0; i < dev->num_queues; i++) { + free(dev->vqs[i].resubmit_list); + } free(dev->vqs); if (dev->fd > 0) { close(dev->fd); diff --git a/subprojects/libvduse/libvduse.h b/subprojects/libvduse/libvdus= e.h index 6c2fe98213..32f19e7b48 100644 --- a/subprojects/libvduse/libvduse.h +++ b/subprojects/libvduse/libvduse.h @@ -173,6 +173,18 @@ int vduse_dev_update_config(VduseDev *dev, uint32_t si= ze, */ int vduse_dev_setup_queue(VduseDev *dev, int index, int max_size); =20 +/** + * vduse_set_reconnect_log_file: + * @dev: VDUSE device + * @file: filename of reconnect log + * + * Specify the file to store log for reconnecting. It should + * be called before vduse_dev_setup_queue(). + * + * Returns: 0 on success, -errno on failure. + */ +int vduse_set_reconnect_log_file(VduseDev *dev, const char *filename); + /** * vduse_dev_create_by_fd: * @fd: passed file descriptor --=20 2.20.1