From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795543134; b=EIIDrpsnUYUVcNex5hQ4B058pZkQs5ue/5MR1VDddv0dfDEWtA18lsu1TfNaM48w/pPx v2+Q5Yz044FvXuaqi8chTVHnSMPaJQxoJlqYu0zxmym+X7q3HmV1mhhsp3ytp6gW7jZpH EAaEHa1FUbi3BpzmOwUHakogo9f+EkCeLdjX82e0regrFTap6VeuJfnpd5Ww+HJ/lOGOb jEYj77qdN/bobwyNrPT5cPqCAFujmxLMOb6wdoxwlrpFXgg0bTbnM516EAV7tR1RL0QQP thhYviyDib7Pq/fiC+BMgG6c1gWBVY9ImFYh0W9IndVtcobdTHxJk1LQ+e10wDXAnig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795543134; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=QVKzXmezLyb1NzGGicKlncvxhs46uSTwixCg7Ak8Quw=; b=Kl8i4mHV/3Z2RKHot14KZmt+bXz7bSggaYFyAz5uaDx5MhYogi+4CMHUYKP7XWhH2oJ4 JAMtPwOElFh2bE2qR0Ynx+YP6wQK5q5PdK03v6AYYMBCMqD7ZxO2oHYfpifLkEdkUQ/vF iWQJ9UEcd/yYHr62aS+ZqgVhq6Y9rGhJ0GDkxsKjN77xiILSUZNGnHKzTLWHq5bw8hatT TotVECJgJSmq7Kt75w/q1864iaQwIz6BuWFv7AyWA5UPz8phCZbIjCvPOPwTWRsWauHH4 bBHeZ3/Ns6ASjQw1vxSGkv38sLA+mUXorBmIJsz+10VVx0/+lLHtVFhgVyBwCDaSHtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=cloudflare.com; spf=pass smtp.mailfrom=cloudflare.com; dkim=pass header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google09082023 header.b=QtXjcB4o; arc=none smtp.remote-ip=209.85.208.50 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E925606A327 for ; Tue, 24 Nov 2026 17:58:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E925606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.a=rsa-sha256 header.s=google09082023 header.b=QtXjcB4o Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-6b86bbce3d1so6516248a12.0 for ; Tue, 24 Nov 2026 09:58:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1795543132; x=1796147932; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QVKzXmezLyb1NzGGicKlncvxhs46uSTwixCg7Ak8Quw=; b=QtXjcB4oQ5HtEFGw561LNSn69+Ml0BSi25Bv5mpPJnz/b7yezCgL9QUbRGZoN054zV RKtYuB8YcYIOAVmoueHSaDTuvibpcufl4GNBRM4O8ZXkWsA0kCFFXv/1j7ypByJR/3i4 cP3jlgLRvgQaTeHcafIwcYzOZFUPLw8tgGVSXJDl9r4kIwbiK05xZ1/rA+6LyCzn4HhY bl81uoif9lyjmkHr6iie0mA5Yn7F8EhJugrPFVSWCAYsPPc6YCfhlA3BWlZFvD/KoGTw RFNJ/ITyx5YqpHZNdCYtmy92mxSHjGI21uW56CsZC9OXS+v2tlIbTZVDucJfTvgMvh2/ s3Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795543132; x=1796147932; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QVKzXmezLyb1NzGGicKlncvxhs46uSTwixCg7Ak8Quw=; b=X5p+usgYt38w1mXaH1ktckb1c+N9qB1dzYqXrUw7iQyYOvdeLffBcngZVxgOTugQFu ObF58Qi5rT+0nNTQ4Fv310w9/efXrvRdcPopMn/L+Kbxl0+qp64b2Gc7lFWXTt8JPVyH jQLpF47YgLCruA0LZBDDzKgQaJ4eYVW46DYliwhoKV78U77SMyZLzij21vbzN/6vr7O6 ktTa03jjSxKSmdeerjUOBRYZD140o94CuQOyMB8H3F2t2x5Z0bvdBt+FLm9RPOoIxxoo XwCFFnw5FC25DGRpM5UdwPNTeA6R4wE5m4LxT8EX+onsa8Ib+6CH3pTTJgzfhzrFHT56 tpbg== X-Gm-Message-State: APuU4Z8v9fNM9RCJFsANkt8mqGJAq694QbrjHYQDX5t/cy6Wcl5jstXE K4URZUeg/M1eIQqK5DQSixXbOOsl82czVFT92bN4wJ1pwq0CMoJUQ3CDT8DKKMMAQZD1IBtuZxr YDUzsKRspFw== X-Gm-Gg: AZFdTZftc9uAfMVbG57oSHNIyXeih3nCn8mx5n8GA/p38RSavomLkg5ri2GEwbNQmi0 4S/654kIhKJj8hdV8ejAEB0hv7Xq6lLs3c9GsWpLuysWEHwB7bFUU6DGLdP+oZudOBnCD4iAnXQ PrCQLO16ucdSM+qqOvRMSNFXYYNSKueG60saen3dnl9+wNeSS+MWG1Kls+wEDNhKIRpr99+FjTY vQUZSWtP8nE3SygVlemKRQQoXrE9WCRC0igbIV8cnmKnbAqEQ5Ub+nv2vT6AOx/FO6yRIDebNEg g2z5zr694A4UE7phY3DmoVa6xmeQ7gdec0r3J0ZJVhNtv7F4IIIKZ8lhIxjmr/W1ig9VK8etbdx 0+7H4Y+jrMmR04jl32FtKzN5Pc8iGAtDyvvnc5LWF02a75WKErYXHO3Rjx3bbjPga+PuoW1G5bw bIkaaw2Fls7Jj0UGWe2NsV/X8hMB18WVry/sV0n2cDaal8RjHdyMI8SZTXs/AKrR1WXntmx66f5 AgtoLRZ0Vuq+SVMelw407eXFH6lhK8mxycn2CixxSc7ih63sqGdWmYsfyrVQSMX0Rtyw+JZUlxU u4xhzd0+pMfwKnQWgBTIyLBw3IOHIrCV5XnicBw1Nc+v224= X-Received: by 2002:a05:6402:400d:b0:6b5:9849:e328 with SMTP id 4fb4d7f45d1cf-6ba37ebdda1mr10870750a12.39.1795543131550; Tue, 24 Nov 2026 09:58:51 -0800 (PST) Received: from L9JK9NPFH9 ([2a09:bac6:37ad:1e5a::306:5]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-6ba3711ad52sm7205762a12.35.2026.11.24.09.58.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Nov 2026 09:58:51 -0800 (PST) From: Tianyi Gao To: netdev@vger.kernel.org, Jakub Kicinski , Paolo Abeni , Eric Dumazet Cc: "David S. Miller" , Simon Horman , David Keogh , Jesper Dangaard Brouer , Daniel Borkmann , David Woodhouse , Dragos Tatulea , Nimrod Oren , Alasdair McWilliam , Willem de Bruijn , Maciej Fijalkowski , Stanislav Fomichev , Magnus Karlsson , David Ahern , Ido Schimmel , Alexei Starovoitov , John Fastabend , Donald Hunter , Andrew Lunn , Jonathan Corbet , Shuah Khan , Shuah Khan , Randy Dunlap , Breno Leitao , =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= , Danielle Ratson , Kuniyuki Iwashima , bpf@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@cloudflare.com, nh-open-source@amazon.com, Stephen Hemminger , "Gustavo A. R. Silva" , "Remy D. Farley" , Amery Hung , Andrii Nakryiko , Andy Roulin , Anil Kaushik , Demos Ra , Eduard Zingerman , Emil Tsalapatis , Hangbin Liu , Ihor Solodrai , Jiayuan Chen , Jiri Olsa , Joe Damato , Kumar Kartikeya Dwivedi , Li RongQing , Louis Scalbert , Marc Kleine-Budde , Martin KaFai Lau , Martin Karsten , Matt Bobrowski , Michael Bommarito , Nikolay Aleksandrov , Runyu Xiao , Song Liu , Thorsten Leemhuis , Wang Zhan , Yonghong Song , Zhou Qiuyue , Sebastian Andrzej Siewior , Jeff Layton , Tianyi Gao , Tianyi Gao Subject: [PATCH net-next v3 03/11] net: set tx_extra_headroom of the peer on XDP attach Date: Tue, 24 Nov 2026 17:58:16 +0000 Message-ID: <20261124175824.60470-4-tianyi@cloudflare.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124175824.60470-1-tianyi@cloudflare.com> References: <20261124175824.60470-1-tianyi@cloudflare.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit XDP needs XDP_PACKET_HEADROOM (256 bytes), but locally generated packets only get needed_headroom. XDP on the peer of a veth or netkit device then copies every packet. Generic XDP copies it in netif_skb_check_for_xdp(), and native XDP on veth in veth_convert_skb_to_xdp_buff(). While the peer has an XDP program, set tx_extra_headroom of the device to XDP_PACKET_HEADROOM minus needed_headroom. netdev_update_tx_extra_headroom() recalculates it when an XDP program is attached or detached, also when an upper device such as a bond propagates its program to the peer with netif_xdp_propagate(). It does not recalculate it: - for IFLA_NETKIT_HEADROOM, which cannot change after the netkit device is created, - in dev_xdp_uninstall(), because veth and netkit unregister both ends together, - when bridge or openvswitch change needed_headroom later. openvswitch changes needed_headroom under ovs_mutex, without RTNL. There are three ways to handle a later change of needed_headroom: - Recalculate tx_extra_headroom in netdev_set_rx_headroom(). This races with an XDP attach or detach under RTNL. The last write can leave tx_extra_headroom at 0 while XDP is attached. - Do not subtract needed_headroom. This always reserves needed_headroom bytes more than XDP needs. - Subtract needed_headroom only when tx_extra_headroom is recalculated. This patch uses the last one, because it does not race and reserves only what XDP needs. If needed_headroom shrinks later, XDP on the peer copies packets again until the next recalculation. This removes the copy for generic XDP. Native XDP on veth also copies a packet whose head is not a page fragment, and a locally generated packet has a kmalloc head. A follow-up veth patch changes this for XDP_PASS and XDP_DROP. Until then, native XDP does not use the extra headroom, and a packet close to the MTU gets a larger head. tx_extra_headroom is used rather than needed_headroom, because raising needed_headroom can regress the performance of other paths. Signed-off-by: Tianyi Gao --- v3: - Mention in the commit message and the documentation that native XDP on veth still copies locally generated packets (Sashiko). v2: - New patch, replaces IFLA_TX_EXTRA_HEADROOM for XDP on the peer (Stephen). --- Documentation/networking/netdevices.rst | 12 ++++++++ include/linux/netdevice.h | 1 + net/core/dev.c | 38 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/Documentation/networking/netdevices.rst b/Documentation/networking/netdevices.rst index 1f4ff67eb0fc..a1c12ca704d6 100644 --- a/Documentation/networking/netdevices.rst +++ b/Documentation/networking/netdevices.rst @@ -458,6 +458,18 @@ Packets built by ip_append_data() or ip6_append_data(), raw packets with IP_HDRINCL, AF_PACKET and AF_XDP copy-mode packets reserve ``tx_extra_headroom``. +The kernel sets ``tx_extra_headroom`` of a device to the sum of these +values: + +* ``XDP_PACKET_HEADROOM`` (256 bytes) minus ``needed_headroom``, when the + peer of a veth or netkit device has an XDP program + +The kernel updates ``tx_extra_headroom`` when an XDP program is attached +to or detached from the peer, to reserve the headroom that XDP needs. +Native XDP on veth still copies locally generated packets, because +their head is not a page fragment. ``tx_extra_headroom`` is not updated +when a bridge or openvswitch changes ``needed_headroom`` later. + NETDEV_INTERNAL symbol namespace ================================ diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index c55a1cf42498..2486b0d10474 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4485,6 +4485,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf); int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf); u8 dev_xdp_sb_prog_count(struct net_device *dev); u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode); +void netdev_update_tx_extra_headroom(struct net_device *dev); u32 dev_get_min_mp_channel_count(const struct net_device *dev); diff --git a/net/core/dev.c b/net/core/dev.c index 4fedd5765867..c1788a18f0fd 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10671,6 +10671,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf) if (old_prog) bpf_prog_put(old_prog); + netdev_update_tx_extra_headroom(dev); return 0; } EXPORT_SYMBOL_GPL(netif_xdp_propagate); @@ -10682,6 +10683,41 @@ u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) return prog ? prog->aux->id : 0; } +static void __netdev_update_tx_extra_headroom(struct net_device *dev, + struct net_device *peer) +{ + unsigned int needed_headroom = READ_ONCE(dev->needed_headroom); + unsigned int tx_extra_headroom = 0; + + if (peer && dev_xdp_prog_count(peer) && + needed_headroom < XDP_PACKET_HEADROOM) + tx_extra_headroom = XDP_PACKET_HEADROOM - needed_headroom; + + WRITE_ONCE(dev->tx_extra_headroom, tx_extra_headroom); +} + +/** + * netdev_update_tx_extra_headroom - recalculate tx_extra_headroom + * @dev: the device to check + * + * Recalculate tx_extra_headroom of @dev and its peer. Should be called + * after conditions that influence tx_extra_headroom might have changed. + * The caller must hold the RTNL lock. + */ +void netdev_update_tx_extra_headroom(struct net_device *dev) +{ + struct net_device *peer = NULL; + + rcu_read_lock(); + if (dev->netdev_ops->ndo_get_peer_dev) + peer = dev->netdev_ops->ndo_get_peer_dev(dev); + __netdev_update_tx_extra_headroom(dev, peer); + if (peer) + __netdev_update_tx_extra_headroom(peer, dev); + rcu_read_unlock(); +} +EXPORT_SYMBOL_GPL(netdev_update_tx_extra_headroom); + static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, bpf_op_t bpf_op, struct netlink_ext_ack *extack, u32 flags, struct bpf_prog *prog) @@ -10892,6 +10928,7 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack if (cur_prog) bpf_prog_put(cur_prog); + netdev_update_tx_extra_headroom(dev); return 0; } @@ -10918,6 +10955,7 @@ static int dev_xdp_detach_link(struct net_device *dev, bpf_op = dev_xdp_bpf_op(dev, mode); WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); dev_xdp_set_link(dev, mode, NULL); + netdev_update_tx_extra_headroom(dev); return 0; } -- 2.56.0