From patchwork Mon Jun 28 11:46:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xuan Zhuo X-Patchwork-Id: 12347749 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DFC2C2B9F4 for ; Mon, 28 Jun 2021 11:46:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 74B5E61C69 for ; Mon, 28 Jun 2021 11:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232526AbhF1LtR (ORCPT ); Mon, 28 Jun 2021 07:49:17 -0400 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:35602 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232353AbhF1LtQ (ORCPT ); Mon, 28 Jun 2021 07:49:16 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=alimailimapcm10staff010182156082;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=24;SR=0;TI=SMTPD_---0UdwTCe8_1624880807; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0UdwTCe8_1624880807) by smtp.aliyun-inc.com(127.0.0.1); Mon, 28 Jun 2021 19:46:47 +0800 From: Xuan Zhuo To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Jesse Brandeburg , Tony Nguyen , "David S. Miller" , Jakub Kicinski , Giuseppe Cavallaro , Alexandre Torgue , Jose Abreu , Maxime Coquelin , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Magnus Karlsson , Jonathan Lemon , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Jeff Kirsher , Krzysztof Kazimierczak , Maciej Fijalkowski , Ong Boon Leong , intel-wired-lan@lists.osuosl.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH net v3] xdp, net: fix for construct skb by xdp inside xsk zc rx Date: Mon, 28 Jun 2021 19:46:47 +0800 Message-Id: <20210628114647.75449-1-xuanzhuo@linux.alibaba.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org When each driver supports xsk rx, if the received buff returns XDP_PASS after run xdp prog, it must construct skb based on xdp. This patch extracts this logic into a public function xdp_construct_skb(). There is a bug in the original logic. When constructing skb, we should copy the meta information to skb and then use __skb_pull() to correct the data. Fixes: 0a714186d3c0f ("i40e: add AF_XDP zero-copy Rx support") Fixes: 2d4238f556972 ("ice: Add support for AF_XDP") Fixes: bba2556efad66 ("net: stmmac: Enable RX via AF_XDP zero-copy") Fixes: d0bcacd0a1309 ("ixgbe: add AF_XDP zero-copy Rx support") Signed-off-by: Xuan Zhuo --- v3: move xdp_construct_skb to xdp_sock_drv.h drivers/net/ethernet/intel/i40e/i40e_xsk.c | 16 +--------- drivers/net/ethernet/intel/ice/ice_xsk.c | 12 +------- drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c | 12 +------- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 23 +------------- include/net/xdp_sock_drv.h | 30 +++++++++++++++++++ 5 files changed, 34 insertions(+), 59 deletions(-) -- 2.31.0 diff --git a/drivers/net/ethernet/intel/i40e/i40e_xsk.c b/drivers/net/ethernet/intel/i40e/i40e_xsk.c index 68f177a86403..81b0f44eedda 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_xsk.c +++ b/drivers/net/ethernet/intel/i40e/i40e_xsk.c @@ -246,23 +246,9 @@ bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count) static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) { - unsigned int metasize = xdp->data - xdp->data_meta; - unsigned int datasize = xdp->data_end - xdp->data; struct sk_buff *skb; - /* allocate a skb to store the frags */ - skb = __napi_alloc_skb(&rx_ring->q_vector->napi, - xdp->data_end - xdp->data_hard_start, - GFP_ATOMIC | __GFP_NOWARN); - if (unlikely(!skb)) - goto out; - - skb_reserve(skb, xdp->data - xdp->data_hard_start); - memcpy(__skb_put(skb, datasize), xdp->data, datasize); - if (metasize) - skb_metadata_set(skb, metasize); - -out: + skb = xdp_construct_skb(xdp, &rx_ring->q_vector->napi); xsk_buff_free(xdp); return skb; } diff --git a/drivers/net/ethernet/intel/ice/ice_xsk.c b/drivers/net/ethernet/intel/ice/ice_xsk.c index a1f89ea3c2bd..f95e1adcebda 100644 --- a/drivers/net/ethernet/intel/ice/ice_xsk.c +++ b/drivers/net/ethernet/intel/ice/ice_xsk.c @@ -430,22 +430,12 @@ static void ice_bump_ntc(struct ice_ring *rx_ring) static struct sk_buff * ice_construct_skb_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf) { - unsigned int metasize = rx_buf->xdp->data - rx_buf->xdp->data_meta; - unsigned int datasize = rx_buf->xdp->data_end - rx_buf->xdp->data; - unsigned int datasize_hard = rx_buf->xdp->data_end - - rx_buf->xdp->data_hard_start; struct sk_buff *skb; - skb = __napi_alloc_skb(&rx_ring->q_vector->napi, datasize_hard, - GFP_ATOMIC | __GFP_NOWARN); + skb = xdp_construct_skb(rx_buf->xdp, &rx_ring->q_vector->napi); if (unlikely(!skb)) return NULL; - skb_reserve(skb, rx_buf->xdp->data - rx_buf->xdp->data_hard_start); - memcpy(__skb_put(skb, datasize), rx_buf->xdp->data, datasize); - if (metasize) - skb_metadata_set(skb, metasize); - xsk_buff_free(rx_buf->xdp); rx_buf->xdp = NULL; return skb; diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c b/drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c index f72d2978263b..123945832c96 100644 --- a/drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c +++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c @@ -203,22 +203,12 @@ bool ixgbe_alloc_rx_buffers_zc(struct ixgbe_ring *rx_ring, u16 count) static struct sk_buff *ixgbe_construct_skb_zc(struct ixgbe_ring *rx_ring, struct ixgbe_rx_buffer *bi) { - unsigned int metasize = bi->xdp->data - bi->xdp->data_meta; - unsigned int datasize = bi->xdp->data_end - bi->xdp->data; struct sk_buff *skb; - /* allocate a skb to store the frags */ - skb = __napi_alloc_skb(&rx_ring->q_vector->napi, - bi->xdp->data_end - bi->xdp->data_hard_start, - GFP_ATOMIC | __GFP_NOWARN); + skb = xdp_construct_skb(bi->xdp, &rx_ring->q_vector->napi); if (unlikely(!skb)) return NULL; - skb_reserve(skb, bi->xdp->data - bi->xdp->data_hard_start); - memcpy(__skb_put(skb, datasize), bi->xdp->data, datasize); - if (metasize) - skb_metadata_set(skb, metasize); - xsk_buff_free(bi->xdp); bi->xdp = NULL; return skb; diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index c87202cbd3d6..143ac1edb876 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -4729,27 +4729,6 @@ static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv, xdp_do_flush(); } -static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch, - struct xdp_buff *xdp) -{ - unsigned int metasize = xdp->data - xdp->data_meta; - unsigned int datasize = xdp->data_end - xdp->data; - struct sk_buff *skb; - - skb = __napi_alloc_skb(&ch->rxtx_napi, - xdp->data_end - xdp->data_hard_start, - GFP_ATOMIC | __GFP_NOWARN); - if (unlikely(!skb)) - return NULL; - - skb_reserve(skb, xdp->data - xdp->data_hard_start); - memcpy(__skb_put(skb, datasize), xdp->data, datasize); - if (metasize) - skb_metadata_set(skb, metasize); - - return skb; -} - static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue, struct dma_desc *p, struct dma_desc *np, struct xdp_buff *xdp) @@ -4761,7 +4740,7 @@ static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue, struct sk_buff *skb; u32 hash; - skb = stmmac_construct_skb_zc(ch, xdp); + skb = xdp_construct_skb(xdp, &ch->rxtx_napi); if (!skb) { priv->dev->stats.rx_dropped++; return; diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h index 4e295541e396..988665cc2981 100644 --- a/include/net/xdp_sock_drv.h +++ b/include/net/xdp_sock_drv.h @@ -244,4 +244,34 @@ static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool, #endif /* CONFIG_XDP_SOCKETS */ +static __always_inline struct sk_buff *xdp_construct_skb(struct xdp_buff *xdp, + struct napi_struct *napi) +{ + unsigned int metasize; + unsigned int datasize; + unsigned int headroom; + struct sk_buff *skb; + unsigned int len; + + /* this include metasize */ + datasize = xdp->data_end - xdp->data_meta; + metasize = xdp->data - xdp->data_meta; + headroom = xdp->data_meta - xdp->data_hard_start; + len = xdp->data_end - xdp->data_hard_start; + + /* allocate a skb to store the frags */ + skb = __napi_alloc_skb(napi, len, GFP_ATOMIC | __GFP_NOWARN); + if (unlikely(!skb)) + return NULL; + + skb_reserve(skb, headroom); + memcpy(__skb_put(skb, datasize), xdp->data_meta, datasize); + if (metasize) { + __skb_pull(skb, metasize); + skb_metadata_set(skb, metasize); + } + + return skb; +} + #endif /* _LINUX_XDP_SOCK_DRV_H */