Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5726254pxb; Tue, 16 Feb 2021 06:13:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyZByhGSAcNnWPvkeTD5ZO93pNoyQce0QBz95aN9EjSBoD4X3+/5hEzS/WPvzrhdsonNnd X-Received: by 2002:a05:6512:104e:: with SMTP id c14mr11998612lfb.481.1613484837676; Tue, 16 Feb 2021 06:13:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613484837; cv=none; d=google.com; s=arc-20160816; b=qnXofnHVOXBivIoyqtSl6lyD1etp7z0Dh9xmbqCNeXZmVxawr9moYLXhbd4yAiTplB T/EZN/ZJJrGif+KYQXGVI9ZzA9PbUI7N1QF7nsAKIhu2sm+0CjE3c9vSxjYtxi93sVcM 4g2cqsQaoX1ml6nhPbS8tybepNYQl4itW5s3AsaArAETeWqlFvPRWdkweDLp3i7hRN/1 nFtA4VDpwOH/NUlDrUK5xIMYMDqFaDv/ECTRK2QMC5gCCed+dnsHVziY6dC5wp+81qSW FlddxD3Esvgx/cRIElG8IaeTTHgeQHfxpjDZ8BDu8ClZrJebsWlTAx7YrdAXFpLC7VKs nLzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=OgRH2QvT55resuzZcjaVbYIuOjjhvKh/QAEvJ9R6nGQ=; b=zpOw+IIylCtW3E+oh2I4bf7DeRInGGd+S1tWcvN8s0BNwtNgAy7pKREHmKKTcqXfFO apnB+Q3CI/7oQTv40cnLG7p8ev49IvK0y+yyO9N/8AHSd1EGKgCLSjTK1JGddVCGcK1H Q++yLWsjX4CcQOS6u7nWscWMzZQ83QEXoHsa2Tk5cXNtqe2mLp6rukloSJ+wfQJio0JY 9ZTyNCOXKHqQ8uSP2F3AijoCUfurp8VzHtJAOfwzchkX6kyCoKpIuC8jJ1Q24Cyk44vg peBVhVJ/nwKiC0qw6qnwaIecXUW5kqVOxXa0ZwRZTST9vzvQ/gfHV6gWFlCJzTRfWYFR 6oRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="gL/5jjOb"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l7si587675ejq.16.2021.02.16.06.13.33; Tue, 16 Feb 2021 06:13:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="gL/5jjOb"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230020AbhBPOJV (ORCPT + 99 others); Tue, 16 Feb 2021 09:09:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229913AbhBPOJT (ORCPT ); Tue, 16 Feb 2021 09:09:19 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5224DC061574; Tue, 16 Feb 2021 06:08:38 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id d2so5996858pjs.4; Tue, 16 Feb 2021 06:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OgRH2QvT55resuzZcjaVbYIuOjjhvKh/QAEvJ9R6nGQ=; b=gL/5jjOb8YOhYJ0bd/DdgdvoFPDkLLVLOjPEwrBrH1qe6vR5cWMDfnNE4MLYrGsfTt n0WXxRTyioW7yNRv9RI4vt4F18xZOgOK391zZvBeBRZgPbTALmYDLSBLBdtEve8ePQPZ qjTbUCLuso+BhK5kCKDH6CqPeHqyRB+mtoE1itjOWOJKCVqxsHTJ0fPgcKktX3SNIIw2 czoredUeEEJvxFjTgBXUreotGd75lf9zgnq0JBGD6BrOlCgPiNViykS/DDo+VSoEOTFT DDvmFHgtSqiP/8rMaDxuEwtD1AyRdb5yrwMVq5CPpUsw/DmJ5m+wpFVdjA300aGOfszY h7JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OgRH2QvT55resuzZcjaVbYIuOjjhvKh/QAEvJ9R6nGQ=; b=DkSxrmhF+tmMo2LY+CCxT2lHnSKcMTvf0i8x+XJQhNjCugV9Ykz5zzUlhxqeuzC6Lu ZxVrZWXwn+ABH6Ld+01IySmIGN6vg6HM1N55YbO/4pcnX/tNM2m/obZR1fyRF8EIz5+k mDga8m4CLBW4wb1joH9Jxu0dVljafw7SefXFAOmlxt6mb5mKTpxnyZn4hmRmnkSPTH22 Yb6PsA/9tLHMiJ0bfSe0ox/Eh6gJbOXJQySgGUATWpBnSJjG5uTbPQUAUcYdAJZMldoq kiniStk8IRlO0nSZAQOMSTd3syrQ6yDVBlTED/r93nnXI1dPucon/4aQv+laQzdcXYxI Hfaw== X-Gm-Message-State: AOAM531RW57QpKyxdsj8c16RL6J1hvadN5C/InOJaRO3MLnHsyMmMtjj 5HKQXMe6VGVgADcNojYcK3BE7UODPPmtyRnD1fI= X-Received: by 2002:a17:90a:bf02:: with SMTP id c2mr4515137pjs.117.1613484517788; Tue, 16 Feb 2021 06:08:37 -0800 (PST) MIME-Version: 1.0 References: <20210216113740.62041-1-alobakin@pm.me> <20210216113740.62041-7-alobakin@pm.me> In-Reply-To: <20210216113740.62041-7-alobakin@pm.me> From: Magnus Karlsson Date: Tue, 16 Feb 2021 15:08:26 +0100 Message-ID: Subject: Re: [PATCH v4 bpf-next 6/6] xsk: build skb by page (aka generic zerocopy xmit) To: Alexander Lobakin Cc: Magnus Karlsson , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Jakub Kicinski , Jonathan Lemon , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Paolo Abeni , Eric Dumazet , Xuan Zhuo , Dust Li , virtualization@lists.linux-foundation.org, Network Development , open list , bpf Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 16, 2021 at 12:44 PM Alexander Lobakin wrote: > > From: Xuan Zhuo > > This patch is used to construct skb based on page to save memory copy > overhead. > > This function is implemented based on IFF_TX_SKB_NO_LINEAR. Only the > network card priv_flags supports IFF_TX_SKB_NO_LINEAR will use page to > directly construct skb. If this feature is not supported, it is still > necessary to copy data to construct skb. > > ---------------- Performance Testing ------------ > > The test environment is Aliyun ECS server. > Test cmd: > ``` > xdpsock -i eth0 -t -S -s > ``` > > Test result data: > > size 64 512 1024 1500 > copy 1916747 1775988 1600203 1440054 > page 1974058 1953655 1945463 1904478 > percent 3.0% 10.0% 21.58% 32.3% > > Signed-off-by: Xuan Zhuo > Reviewed-by: Dust Li > [ alobakin: > - expand subject to make it clearer; > - improve skb->truesize calculation; > - reserve some headroom in skb for drivers; > - tailroom is not needed as skb is non-linear ] > Signed-off-by: Alexander Lobakin Thank you Alexander! Acked-by: Magnus Karlsson > --- > net/xdp/xsk.c | 119 ++++++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 95 insertions(+), 24 deletions(-) > > diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c > index 143979ea4165..ff7bd06e1241 100644 > --- a/net/xdp/xsk.c > +++ b/net/xdp/xsk.c > @@ -445,6 +445,96 @@ static void xsk_destruct_skb(struct sk_buff *skb) > sock_wfree(skb); > } > > +static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs, > + struct xdp_desc *desc) > +{ > + struct xsk_buff_pool *pool = xs->pool; > + u32 hr, len, offset, copy, copied; > + struct sk_buff *skb; > + struct page *page; > + void *buffer; > + int err, i; > + u64 addr; > + > + hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom)); > + > + skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err); > + if (unlikely(!skb)) > + return ERR_PTR(err); > + > + skb_reserve(skb, hr); > + > + addr = desc->addr; > + len = desc->len; > + > + buffer = xsk_buff_raw_get_data(pool, addr); > + offset = offset_in_page(buffer); > + addr = buffer - pool->addrs; > + > + for (copied = 0, i = 0; copied < len; i++) { > + page = pool->umem->pgs[addr >> PAGE_SHIFT]; > + get_page(page); > + > + copy = min_t(u32, PAGE_SIZE - offset, len - copied); > + skb_fill_page_desc(skb, i, page, offset, copy); > + > + copied += copy; > + addr += copy; > + offset = 0; > + } > + > + skb->len += len; > + skb->data_len += len; > + skb->truesize += pool->unaligned ? len : pool->chunk_size; > + > + refcount_add(skb->truesize, &xs->sk.sk_wmem_alloc); > + > + return skb; > +} > + > +static struct sk_buff *xsk_build_skb(struct xdp_sock *xs, > + struct xdp_desc *desc) > +{ > + struct net_device *dev = xs->dev; > + struct sk_buff *skb; > + > + if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) { > + skb = xsk_build_skb_zerocopy(xs, desc); > + if (IS_ERR(skb)) > + return skb; > + } else { > + u32 hr, tr, len; > + void *buffer; > + int err; > + > + hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom)); > + tr = dev->needed_tailroom; > + len = desc->len; > + > + skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err); > + if (unlikely(!skb)) > + return ERR_PTR(err); > + > + skb_reserve(skb, hr); > + skb_put(skb, len); > + > + buffer = xsk_buff_raw_get_data(xs->pool, desc->addr); > + err = skb_store_bits(skb, 0, buffer, len); > + if (unlikely(err)) { > + kfree_skb(skb); > + return ERR_PTR(err); > + } > + } > + > + skb->dev = dev; > + skb->priority = xs->sk.sk_priority; > + skb->mark = xs->sk.sk_mark; > + skb_shinfo(skb)->destructor_arg = (void *)(long)desc->addr; > + skb->destructor = xsk_destruct_skb; > + > + return skb; > +} > + > static int xsk_generic_xmit(struct sock *sk) > { > struct xdp_sock *xs = xdp_sk(sk); > @@ -454,56 +544,37 @@ static int xsk_generic_xmit(struct sock *sk) > struct sk_buff *skb; > unsigned long flags; > int err = 0; > - u32 hr, tr; > > mutex_lock(&xs->mutex); > > if (xs->queue_id >= xs->dev->real_num_tx_queues) > goto out; > > - hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom)); > - tr = xs->dev->needed_tailroom; > - > while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) { > - char *buffer; > - u64 addr; > - u32 len; > - > if (max_batch-- == 0) { > err = -EAGAIN; > goto out; > } > > - len = desc.len; > - skb = sock_alloc_send_skb(sk, hr + len + tr, 1, &err); > - if (unlikely(!skb)) > + skb = xsk_build_skb(xs, &desc); > + if (IS_ERR(skb)) { > + err = PTR_ERR(skb); > goto out; > + } > > - skb_reserve(skb, hr); > - skb_put(skb, len); > - > - addr = desc.addr; > - buffer = xsk_buff_raw_get_data(xs->pool, addr); > - err = skb_store_bits(skb, 0, buffer, len); > /* This is the backpressure mechanism for the Tx path. > * Reserve space in the completion queue and only proceed > * if there is space in it. This avoids having to implement > * any buffering in the Tx path. > */ > spin_lock_irqsave(&xs->pool->cq_lock, flags); > - if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) { > + if (xskq_prod_reserve(xs->pool->cq)) { > spin_unlock_irqrestore(&xs->pool->cq_lock, flags); > kfree_skb(skb); > goto out; > } > spin_unlock_irqrestore(&xs->pool->cq_lock, flags); > > - skb->dev = xs->dev; > - skb->priority = sk->sk_priority; > - skb->mark = sk->sk_mark; > - skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr; > - skb->destructor = xsk_destruct_skb; > - > err = __dev_direct_xmit(skb, xs->queue_id); > if (err == NETDEV_TX_BUSY) { > /* Tell user-space to retry the send */ > -- > 2.30.1 > >