Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp804418lqs; Tue, 5 Mar 2024 18:12:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiR3m5DnEFldc+GaqTQ2BbPse2Nnh4LBvy7EMkEzclOawXOiLPqlSSNJhtEp3qR/npkEjSqfXvR5Xi1aBiYOlNfkbmKyM15idTn62zdw== X-Google-Smtp-Source: AGHT+IGit+w/Ub6KF3GXQd8UM8hhpVCtP4ZgC63xXNZrQeHZe1Q0mMU6Rm4HKWeI2sG4s9IYjzbB X-Received: by 2002:a50:f61c:0:b0:565:f51:3b26 with SMTP id c28-20020a50f61c000000b005650f513b26mr9067736edn.30.1709691158508; Tue, 05 Mar 2024 18:12:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709691158; cv=pass; d=google.com; s=arc-20160816; b=kKM2X3yz1QD8ybkcMC8qfV+3LG1x0VZs6WNFOqOeuUrvIE/psyuxp8RPwfEtNzWsK/ dO2zWDOnfQkzuR2v8OM6CIzmUV0YWPmoMUA+RjT7WLuepjm0s3ovo8vTcTE8dDBE2tIR J73BdtDbydNaI01rNdsCBRcuvSDW51pm/RsTNVP3+E2V9HCWA4lKRpLWURwo77TMMBB4 /BCl/4BaAfXspCUqZZjb1WC5/pTJMTqAlVlWTQoXnakyAcmzpcOQ2o/0gOeskyLtNprz B/GUCA3uyz1pxJXAz09M5WUeb35WLcbgW11X5ASTDrXnB1Uaeg492OXYQLHuMMrHVWXO 5KOQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=9sbZgd4q8MukS3NmUkwfErLL+fIzH7IaTlMNRuUTYw8=; fh=T3Q62WJ7F++CJZEvKTkREvPBkiOSSnW4AwrGM8b2nB4=; b=kJ0Z68wN1fMF3uBkuAKWbhO+4HdezL15G4Rzt+tscbJzfBjTIjgDZ68JOnUeX76u2s Vbx5EsPZSnpDfIERFGDpXQp8bJ6fbxHq34h/pvVwitcVn0shKfNMis0na/+dtvig0kSw ewMGdVFximeoDYfSSjGLZFZnEPKcj0c2yNgBlmErCNMaXvU2L1P7DolkitQ/qJ0WNX6K AF4mQSmQCnuUmdDuULsWCrpHRzXGi5Bs+cDIJFVcT/YvMMfryv5h7tK/AAzuJvzT+uFz Yn5fja70ok08C0LWsLQvFFtDnYNuFp7+3m1lyuHkWn4kER5dvhAwWHz6bXLUtVGmRhWW cQ5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XSPzk3RS; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-93231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93231-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y8-20020a056402270800b00566a6071ec7si5531469edd.403.2024.03.05.18.12.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 18:12:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XSPzk3RS; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-93231-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93231-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 13E1F1F2174E for ; Wed, 6 Mar 2024 02:12:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AF3F17FE; Wed, 6 Mar 2024 02:12:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XSPzk3RS" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 577EA7460 for ; Wed, 6 Mar 2024 02:12:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709691137; cv=none; b=i6THy2DlisEE9cmqB+6Qji1uI+IxWeVWN2tIsECEynVrJ79wnTSV+i3MrIzgi7TuDsVrQBugFStfLo25/GIch5ploVq2v3YIgeZc507yTsIf38OonIgAD9cPuHafOX8xawutFdpnnji9UfF0DxNaCtQCcdeHirGnxgLO3wYYiJs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709691137; c=relaxed/simple; bh=GlmCKD+xlQJlfcx2lNeOwkFvc6sYU8wmBbQJeeN5+yw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=N4Sl8Zsj4KZkZcIa2FxeIp6cMzpkcmNj13mhCX4uFJFS6qZQr+WxiD+OfEl8ru++oXT8lFzWJ8UiNVD8+PQQFHbewYDfSR30Ei6nWEWuZJtsC+6kiZqjTPQLLxv6LJjoPW0aWWSw2PBmhH/YsefHRf8dVyRw6HVwmwB+ZrPEt+4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XSPzk3RS; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709691134; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9sbZgd4q8MukS3NmUkwfErLL+fIzH7IaTlMNRuUTYw8=; b=XSPzk3RSqwq67Pkz3e0DEwxFnDB1Fyvyf6kc8Q8pqBojGHfMnss876wj32Efo1VLW4C4/L s/XcXU1FewN9tTbgmsC2Jj8C1/T/wB0ILVjm3QAeM+e9ClQPrChltbThGhzsjMf4nZsZUp nCzlJj8a0gf33aaNmaC2PtETV+oDO6k= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-P_HyVIXdOJqVXRcWwESadw-1; Tue, 05 Mar 2024 21:12:12 -0500 X-MC-Unique: P_HyVIXdOJqVXRcWwESadw-1 Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-6e5d234fb2bso4340614b3a.0 for ; Tue, 05 Mar 2024 18:12:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709691131; x=1710295931; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9sbZgd4q8MukS3NmUkwfErLL+fIzH7IaTlMNRuUTYw8=; b=MWaK6l+6chVqGmCFYAjfnIY5qQwX5/+qI3/uuPVfb4ORbAWTmVWaNoOJEpZCNm2Ikh pe9U/+yAn6CqwIQ+LURXoA0wWv79qdj50Zl+uGSTq9rsKiJggGwAHwcnpYGUk2lcgbm5 EU/imooY5ZtSk7NwsztLpUTEiFpG3BZaecdSPnrvx/nkZ9Px+9aGfaxaMCB9+kecOqWR K65SsKy8fWG2Q3ZG+dNzCjMvu6MRYcqJim3u1d2ARofGPb4xGWTlHJFgaIQkY95OWoZi gF76yUh8TlErTrtgSmfvy7I3NP9mwwswFW4XSnjDZZIWLZmAwb8kxxNjkc/RMtcPfDOY nRXA== X-Forwarded-Encrypted: i=1; AJvYcCVUhviaB0LIr5MovB3ssLwMPf1szmtcVSTG7dEScIp6NbisLSc8WoyAM29VZmKRmsygQvZFpwXXdCBeHObV1lCsneYDpw5gIS7muBaQ X-Gm-Message-State: AOJu0Yw7JOdAyfVe/yF6NnXe019vlRvpy3G0kIlkb2J6FDpKU+3pU2BE lOz6jvBgJAsBCE08NUB/hFRlEleYzPeaUlckRYXZTReQuV3IayBJYTiT7cRX9EtbG1kL5mJSJwg 0Ei092mXSHNs0VVS2RXN8TcOT6kpbnmmKlkWn7JKP+CQo0c9Tr+3FLkdsSmDuulFAgDB/sG1ulv Xy7vXIpUU1fG8kbLBg9eA95ZRo6/L48FGBOL41 X-Received: by 2002:a05:6a00:2da8:b0:6e3:caa7:3038 with SMTP id fb40-20020a056a002da800b006e3caa73038mr17578683pfb.0.1709691131217; Tue, 05 Mar 2024 18:12:11 -0800 (PST) X-Received: by 2002:a05:6a00:2da8:b0:6e3:caa7:3038 with SMTP id fb40-20020a056a002da800b006e3caa73038mr17578657pfb.0.1709691130621; Tue, 05 Mar 2024 18:12:10 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <1709118356-133960-1-git-send-email-wangyunjian@huawei.com> In-Reply-To: From: Jason Wang Date: Wed, 6 Mar 2024 10:11:59 +0800 Message-ID: Subject: Re: [PATCH net-next v2 3/3] tun: AF_XDP Tx zero-copy support To: wangyunjian Cc: "mst@redhat.com" , "willemdebruijn.kernel@gmail.com" , "kuba@kernel.org" , "bjorn@kernel.org" , "magnus.karlsson@intel.com" , "maciej.fijalkowski@intel.com" , "jonathan.lemon@gmail.com" , "davem@davemloft.net" , "bpf@vger.kernel.org" , "netdev@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "virtualization@lists.linux.dev" , xudingke , "liwei (DT)" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 4, 2024 at 7:24=E2=80=AFPM wangyunjian = wrote: > > > > > -----Original Message----- > > From: Jason Wang [mailto:jasowang@redhat.com] > > Sent: Monday, March 4, 2024 2:56 PM > > To: wangyunjian > > Cc: mst@redhat.com; willemdebruijn.kernel@gmail.com; kuba@kernel.org; > > bjorn@kernel.org; magnus.karlsson@intel.com; maciej.fijalkowski@intel.c= om; > > jonathan.lemon@gmail.com; davem@davemloft.net; bpf@vger.kernel.org; > > netdev@vger.kernel.org; linux-kernel@vger.kernel.org; kvm@vger.kernel.o= rg; > > virtualization@lists.linux.dev; xudingke ; liwei (= DT) > > > > Subject: Re: [PATCH net-next v2 3/3] tun: AF_XDP Tx zero-copy support > > > > On Wed, Feb 28, 2024 at 7:06=E2=80=AFPM Yunjian Wang > > wrote: > > > > > > This patch set allows TUN to support the AF_XDP Tx zero-copy feature, > > > which can significantly reduce CPU utilization for XDP programs. > > > > > > Since commit fc72d1d54dd9 ("tuntap: XDP transmission"), the pointer > > > ring has been utilized to queue different types of pointers by > > > encoding the type into the lower bits. Therefore, we introduce a new > > > flag, TUN_XDP_DESC_FLAG(0x2UL), which allows us to enqueue XDP > > > descriptors and differentiate them from XDP buffers and sk_buffs. > > > Additionally, a spin lock is added for enabling and disabling operati= ons on the > > xsk pool. > > > > > > The performance testing was performed on a Intel E5-2620 2.40GHz > > machine. > > > Traffic were generated/send through TUN(testpmd txonly with AF_XDP) t= o > > > VM (testpmd rxonly in guest). > > > > > > +------+---------+---------+---------+ > > > | | copy |zero-copy| speedup | > > > +------+---------+---------+---------+ > > > | UDP | Mpps | Mpps | % | > > > | 64 | 2.5 | 4.0 | 60% | > > > | 512 | 2.1 | 3.6 | 71% | > > > | 1024 | 1.9 | 3.3 | 73% | > > > +------+---------+---------+---------+ > > > > > > Signed-off-by: Yunjian Wang > > > --- > > > drivers/net/tun.c | 177 > > +++++++++++++++++++++++++++++++++++++++-- > > > drivers/vhost/net.c | 4 + > > > include/linux/if_tun.h | 32 ++++++++ > > > 3 files changed, 208 insertions(+), 5 deletions(-) > > > > > > diff --git a/drivers/net/tun.c b/drivers/net/tun.c index > > > bc80fc1d576e..7f4ff50b532c 100644 > > > --- a/drivers/net/tun.c > > > +++ b/drivers/net/tun.c > > > @@ -63,6 +63,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include > > > #include > > > #include > > > @@ -86,6 +87,7 @@ static void tun_default_link_ksettings(struct > > net_device *dev, > > > struct > > ethtool_link_ksettings > > > *cmd); > > > > > > #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) > > > +#define TUN_XDP_BATCH 64 > > > > > > /* TUN device flags */ > > > > > > @@ -146,6 +148,9 @@ struct tun_file { > > > struct tun_struct *detached; > > > struct ptr_ring tx_ring; > > > struct xdp_rxq_info xdp_rxq; > > > + struct xsk_buff_pool *xsk_pool; > > > + spinlock_t pool_lock; /* Protects xsk pool enable/disable *= / > > > + u32 nb_descs; > > > }; > > > > > > struct tun_page { > > > @@ -614,6 +619,8 @@ void tun_ptr_free(void *ptr) > > > struct xdp_frame *xdpf =3D tun_ptr_to_xdp(ptr); > > > > > > xdp_return_frame(xdpf); > > > + } else if (tun_is_xdp_desc_frame(ptr)) { > > > + return; > > > } else { > > > __skb_array_destroy_skb(ptr); > > > } > > > @@ -631,6 +638,37 @@ static void tun_queue_purge(struct tun_file *tfi= le) > > > skb_queue_purge(&tfile->sk.sk_error_queue); > > > } > > > > > > +static void tun_set_xsk_pool(struct tun_file *tfile, struct > > > +xsk_buff_pool *pool) { > > > + if (!pool) > > > + return; > > > + > > > + spin_lock(&tfile->pool_lock); > > > + xsk_pool_set_rxq_info(pool, &tfile->xdp_rxq); > > > + tfile->xsk_pool =3D pool; > > > + spin_unlock(&tfile->pool_lock); } > > > + > > > +static void tun_clean_xsk_pool(struct tun_file *tfile) { > > > + spin_lock(&tfile->pool_lock); > > > + if (tfile->xsk_pool) { > > > + void *ptr; > > > + > > > + while ((ptr =3D ptr_ring_consume(&tfile->tx_ring)) != =3D NULL) > > > + tun_ptr_free(ptr); > > > + > > > + if (tfile->nb_descs) { > > > + xsk_tx_completed(tfile->xsk_pool, > > tfile->nb_descs); > > > + if (xsk_uses_need_wakeup(tfile->xsk_pool)) > > > + > > xsk_set_tx_need_wakeup(tfile->xsk_pool); > > > + tfile->nb_descs =3D 0; > > > + } > > > + tfile->xsk_pool =3D NULL; > > > + } > > > + spin_unlock(&tfile->pool_lock); } > > > + > > > static void __tun_detach(struct tun_file *tfile, bool clean) { > > > struct tun_file *ntfile; > > > @@ -648,6 +686,11 @@ static void __tun_detach(struct tun_file *tfile,= bool > > clean) > > > u16 index =3D tfile->queue_index; > > > BUG_ON(index >=3D tun->numqueues); > > > > > > + ntfile =3D rtnl_dereference(tun->tfiles[tun->numqueue= s - > > 1]); > > > + /* Stop xsk zc xmit */ > > > + tun_clean_xsk_pool(tfile); > > > + tun_clean_xsk_pool(ntfile); > > > + > > > rcu_assign_pointer(tun->tfiles[index], > > > tun->tfiles[tun->numqueues - 1]); > > > ntfile =3D rtnl_dereference(tun->tfiles[index]); > > > @@ -668,6 +711,7 @@ static void __tun_detach(struct tun_file *tfile, = bool > > clean) > > > tun_flow_delete_by_queue(tun, tun->numqueues + 1); > > > /* Drop read queue */ > > > tun_queue_purge(tfile); > > > + tun_set_xsk_pool(ntfile, > > > + xsk_get_pool_from_qid(tun->dev, index)); > > > tun_set_real_num_queues(tun); > > > } else if (tfile->detached && clean) { > > > tun =3D tun_enable_queue(tfile); @@ -801,6 +845,7 @@ > > > static int tun_attach(struct tun_struct *tun, struct file *file, > > > > > > if (tfile->xdp_rxq.queue_index !=3D tfile->queue_i= ndex) > > > tfile->xdp_rxq.queue_index =3D > > > tfile->queue_index; > > > + tun_set_xsk_pool(tfile, xsk_get_pool_from_qid(dev, > > > + tfile->queue_index)); > > > } else { > > > /* Setup XDP RX-queue info, for new tfile getting > > attached */ > > > err =3D xdp_rxq_info_reg(&tfile->xdp_rxq, @@ -1221,11 > > > +1266,50 @@ static int tun_xdp_set(struct net_device *dev, struct bpf= _prog > > *prog, > > > return 0; > > > } > > > > > > +static int tun_xsk_pool_enable(struct net_device *netdev, > > > + struct xsk_buff_pool *pool, > > > + u16 qid) { > > > + struct tun_struct *tun =3D netdev_priv(netdev); > > > + struct tun_file *tfile; > > > + > > > + if (qid >=3D tun->numqueues) > > > + return -EINVAL; > > > + > > > + tfile =3D rtnl_dereference(tun->tfiles[qid]); > > > + tun_set_xsk_pool(tfile, pool); > > > + > > > + return 0; > > > +} > > > + > > > +static int tun_xsk_pool_disable(struct net_device *netdev, u16 qid) = { > > > + struct tun_struct *tun =3D netdev_priv(netdev); > > > + struct tun_file *tfile; > > > + > > > + if (qid >=3D MAX_TAP_QUEUES) > > > + return -EINVAL; > > > + > > > + tfile =3D rtnl_dereference(tun->tfiles[qid]); > > > + if (tfile) > > > + tun_clean_xsk_pool(tfile); > > > + return 0; > > > +} > > > + > > > +static int tun_xsk_pool_setup(struct net_device *dev, struct xsk_buf= f_pool > > *pool, > > > + u16 qid) { > > > + return pool ? tun_xsk_pool_enable(dev, pool, qid) : > > > + tun_xsk_pool_disable(dev, qid); } > > > + > > > static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) = { > > > switch (xdp->command) { > > > case XDP_SETUP_PROG: > > > return tun_xdp_set(dev, xdp->prog, xdp->extack); > > > + case XDP_SETUP_XSK_POOL: > > > + return tun_xsk_pool_setup(dev, xdp->xsk.pool, > > > + xdp->xsk.queue_id); > > > default: > > > return -EINVAL; > > > } > > > @@ -1330,6 +1414,19 @@ static int tun_xdp_tx(struct net_device *dev, > > struct xdp_buff *xdp) > > > return nxmit; > > > } > > > > > > +static int tun_xsk_wakeup(struct net_device *dev, u32 qid, u32 flags= ) > > > +{ > > > + struct tun_struct *tun =3D netdev_priv(dev); > > > + struct tun_file *tfile; > > > + > > > + rcu_read_lock(); > > > + tfile =3D rcu_dereference(tun->tfiles[qid]); > > > + if (tfile) > > > + __tun_xdp_flush_tfile(tfile); > > > + rcu_read_unlock(); > > > + return 0; > > > +} > > > > I may miss something but why not simply queue xdp frames into ptr ring = then > > we don't need tricks for peek? > > The current implementation is implemented with reference to other NIC's d= rivers > (ixgbe, ice, dpaa2, mlx5), which use XDP descriptors directly. Well, they all do the same thing which is translate XDP descriptors to the vendor specific descriptor format. For TUN, the ptr_ring is the "vendor specific" format. > Converting XDP > descriptors to XDP frames does not seem to be very beneficial. Get rid of the trick for peek like what is done in this patch? Thanks > > Thanks > > > > Thanks >