Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1666840lqg; Sun, 3 Mar 2024 22:56:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXBmIqbHsc+Mf2CwqYXBWV5PIBJI7atId9/3zh4ja8y0LprM2S34/LE/06wtCu28lxPJtsfhnISYj85oAV3mti55Ad9/i3CKExmu27sOg== X-Google-Smtp-Source: AGHT+IHPIahuhK1TOXO4NQcslf1fVGdyOm2xjM/iiniQvv75v21ET5w7OLyU7wFcQzaEy+HOoel7 X-Received: by 2002:a05:6808:220c:b0:3c1:9fde:8ee4 with SMTP id bd12-20020a056808220c00b003c19fde8ee4mr11104220oib.29.1709535386985; Sun, 03 Mar 2024 22:56:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709535386; cv=pass; d=google.com; s=arc-20160816; b=cLRM4diU3+du3KjHx9J/SZHVcf8z/oimswxeWjznNJphtjg4CNefFu1qlaAOv/NmIJ WPb9EUgLvrMJ0ZnTqJoJ2iqY2+Y54qt6Rmq1eBSB/FxwvuIpddb50l9Qq6qE55AQglAB YkdF20kGvvNgJ074HpaG+svEHcCQcpdqkOPBt9HEiTsrIYHfsLZuh9pQuYs3Rd01mD/7 /lHu208uJg6d2RH0M0i9bJa5lNmxS3Gwmj/pEnXrSPhzKyx42T1IN9pbcIQKT1AL7o+p njkEbHgOHT6B+ZWVzH3OCAWvcd96g0x7Ozr/vGSHMmH5aALyaYUWz93/w3d3X6sPT8Ni ZUwA== 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=SnnMKJuI5h57AkdTs6wUac/zEo+KD+16W2yUuRvyBNA=; fh=sa7Bd0zqQGIZYh21oHIJPEXliJr5zfWwaElnrnZzNWE=; b=E2TS37Tdc3p+Eqer5p5Ot9e7e/ZCHBtZQp5EDA2YMkRBgoBCpQUPkri2o/GplMtNpY rV8QiYUKWge/jw7b6nxdVoozxLEZdBnf81udEGm6yXBxvlSd3By46QTsmyfGZDWk0a68 yWc3nbRPJ24Q3Guo6TBuIUQ4PRVm1KlcGwtbU03E3kcmYG5sinbeJponp3VoE2r1rZS3 mvN54Jdqz797nwKZjC4G5hccUxv1jPwFUxOTjDSOwHw/5lIcMXNmRKp0OuBG1+Fekn2C MSflbEukQE9erDCZSoOst1l+oNCnj4q5NAzCMRbZzhZo3tSTsxDghz//UpwTyi2iPVM8 qEug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="H/ORWkey"; 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-90098-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90098-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id bx19-20020a056a02051300b005dc85e8a9e1si8499290pgb.45.2024.03.03.22.56.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 22:56:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-90098-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="H/ORWkey"; 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-90098-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-90098-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 58963B20C17 for ; Mon, 4 Mar 2024 06:56:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 654D2125A3; Mon, 4 Mar 2024 06:56:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="H/ORWkey" 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 35F0111716 for ; Mon, 4 Mar 2024 06:56:08 +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=1709535370; cv=none; b=uH9Z2o9BK+RySgXvZpEVg0O3Yu92L7X1tvVwqrEB5VYhJfFOTif2LzC8cBzDlfXfD6ri6VR+noobhaFMJhLV6FahYjqXgUUIdkG3gh0B2ezSTKkCyBt8O0dx4WXrCXqPxOUHARbs6rMa2/HnTNToyYOztd5S8Bvp8o5C/ZLWVZE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709535370; c=relaxed/simple; bh=ANv7CPYWbCbdTFbaMYIWnQE0qElMaJYeiOgmdNn1AmY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=k54NGZw0JYq5x/xROX/w698EWEs/sBLSwVdOPP6KgHcPyukX8yKpJv0PK3AAEsQEFuV57/eInz1tKkR9Rgd4QAA6rGbSUq3RH0TxWNZqaA2YWfoRa+SVH4J3j92xPOZuflw2ImTyd9Y3Pc1vwnI1u6dIRsVkz7c/+Jjvq66hoKI= 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=H/ORWkey; 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=1709535367; 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=SnnMKJuI5h57AkdTs6wUac/zEo+KD+16W2yUuRvyBNA=; b=H/ORWkeyunEmu0l+KateJcG2a5pOh5ZGKPE5y4ceJUktOEIFJEfF5zIHy7crr5YvKvFJz1 mBizlZ9ZgJffwdKIFRkp5CHYQwWhqYp/qbpipaDPCzAFe65Pg5WmZVFBl7LDQe0Zv+upHj Pvl44SxCzWOVNaxp8v+/Z82x7GS/Qng= Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-164-5e4Ulzn5NmuNsIv162u3fw-1; Mon, 04 Mar 2024 01:56:05 -0500 X-MC-Unique: 5e4Ulzn5NmuNsIv162u3fw-1 Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-5dca5c631ffso3929497a12.2 for ; Sun, 03 Mar 2024 22:56:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709535364; x=1710140164; 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=SnnMKJuI5h57AkdTs6wUac/zEo+KD+16W2yUuRvyBNA=; b=C47Zhe13kKudgHx/t9zxiq4l+dK7eOUtxlW8y7hlajqpr0dmRlENrOTLmMNFiV3DzF UwgSQPkIoLHePLd/uy6zet223GsgCiNUX5YXRKqX+MBO6mN6DYRkmp1a7zVciVx7IfkZ RqpF8iax2IFZX+lhczpT1XXhxQpNSDNgrcaUSyq3vDCneeGbxgPESBn5p2a0aGJP8MmF b5XWDDeEZ+UU+SoBpoy7jhvv2DfzztbtZwNbCpwFT9j1GeyBDr3YaDDV/ugB6g2xB5/5 d/O+EYyYfu6WZUDkR4gPh4t/tTTFkeSCpM1m8xdrlklzbPQsWiXO8LuBwoki7tA7TXbB +ZNA== X-Forwarded-Encrypted: i=1; AJvYcCX0+LXP0H7e95kABJ+vGjqQXMQiv0Zeo+byszSuytsYBwpQnFur99Qu14wWMPEoXlOQ1VZMNsTH6ts5p0GYnf3dKj8n0i+bLJ8BSs7I X-Gm-Message-State: AOJu0YzUdMKxyGp1uS90FGGsR2P6Sdrtny9OoChEoG+ajFT4cXSlfwRm XIRK4AblARcqiRDSo4x2rKUzbNBhY35bijHU48c9gDlRlnJazwz5XLje5PHvWED+nQJlY1iX+N4 uuJnQxzLRTxVw0bYObKqurNdyQ7OAwX/YT21ld+Vlc/l298LmqQQeLVWdpcW7tDxH8p8WBHwIJb /1MU11OB7a6AHaJ/roV46lZuGNd2dGCR4N4H/t X-Received: by 2002:a05:6a20:9586:b0:1a1:4ed3:c088 with SMTP id iu6-20020a056a20958600b001a14ed3c088mr1655522pzb.42.1709535364706; Sun, 03 Mar 2024 22:56:04 -0800 (PST) X-Received: by 2002:a05:6a20:9586:b0:1a1:4ed3:c088 with SMTP id iu6-20020a056a20958600b001a14ed3c088mr1655512pzb.42.1709535364439; Sun, 03 Mar 2024 22:56:04 -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: <1709118356-133960-1-git-send-email-wangyunjian@huawei.com> From: Jason Wang Date: Mon, 4 Mar 2024 14:55:53 +0800 Message-ID: Subject: Re: [PATCH net-next v2 3/3] tun: AF_XDP Tx zero-copy support To: Yunjian Wang 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@huawei.com, liwei395@huawei.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 operations 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) > to 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_devic= e *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 *tfile) > skb_queue_purge(&tfile->sk.sk_error_queue); > } > > +static void tun_set_xsk_pool(struct tun_file *tfile, struct xsk_buff_poo= l *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 N= ULL) > + 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, boo= l clean) > u16 index =3D tfile->queue_index; > BUG_ON(index >=3D tun->numqueues); > > + ntfile =3D rtnl_dereference(tun->tfiles[tun->numqueues - = 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_index= ) > 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 attache= d */ > err =3D xdp_rxq_info_reg(&tfile->xdp_rxq, > @@ -1221,11 +1266,50 @@ static int tun_xdp_set(struct net_device *dev, st= ruct 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_buff_po= ol *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.qu= eue_id); > default: > return -EINVAL; > } > @@ -1330,6 +1414,19 @@ static int tun_xdp_tx(struct net_device *dev, stru= ct 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? Thanks