From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: sun jian <sun.jian.kdev@gmail.com>,
Menglong Dong <menglong.dong@linux.dev>
Cc: Emil Tsalapatis <emil@etsalapatis.com>,
bpf@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, davem@davemloft.net, kuba@kernel.org,
hawk@kernel.org, john.fastabend@gmail.com, sdf@fomichev.me,
shuah@kernel.org, liuhangbin@gmail.com
Subject: Re: [PATCH] bpf: Unshare cloned skb before devmap egress XDP program
Date: Wed, 10 Jun 2026 13:13:22 +0800 [thread overview]
Message-ID: <b35a2395-0e15-4390-ba7a-b48748e7e3f3@linux.dev> (raw)
In-Reply-To: <CABFUUZH9F+tiitstSBy2-M=WC84jg28CVMXU+Lu-va6jm71fDw@mail.gmail.com>
On 6/10/26 9:58 AM, sun jian wrote:
> On Wed, Jun 10, 2026 at 9:21 AM Menglong Dong <menglong.dong@linux.dev> wrote:
>> On 2026/6/10 08:06 Emil Tsalapatis <emil@etsalapatis.com> write:
>>> On Tue Jun 9, 2026 at 7:06 AM EDT, Menglong Dong wrote:
>>>> On 2026/6/9 18:02 Sun Jian <sun.jian.kdev@gmail.com> write:
[...]
>>>> Hi, Jian.
>>>>
>>>> This sounds like a good idea in this case. When I have a look at bpf_clone_redirect(),
>>>> I found that it use skb_clone() too, which means it has the same problem. The
>>>> data can be modified by other xdp prog in the destination NIC if we use
>>>> bpf_clone_redirect().
>>>>
>>>> So maybe this is the default logic, and I'm not sure if this patch can break the
>>>> existing users :/
>>> I think for use cases where we are using bpf_clone_redirect() to use
>>> one clone for inspection this would add an unnecessary copy. Maybe
>>> adding *_copy() variants instead of changing the *_clone() would be
>>> better? That way we wouldn't be changing the behavior for existing
>>> consumers and the naming would be consistent with the skb_* methods.
>> Agree. It's not a good idea to change the logic of the existing API. Or
>> maybe we can add a BPF_F_CLONE flag for the existing API.
>>
>>> But more importantly, is there an actual use case for the kind of API
>>> that the modified selftest requires? Nobody until now has considered
>>> the existing behavior to be a problem.
>> Agree too. Obviously, this is not a bug. For the use case in the commit
>> log, it's something that can be fixed by the user themself. If we need
>> modify the MAC, we'd better attach a BPF program for all the egress
>> device in the devmap.
>>
[...]
>>>>
> Thanks for all the comments.
>
> I agree this should not be treated as a generic skb_clone() issue, and I
> understand the concern about changing existing clone/shared-data semantics.
>
> The case I was trying to address is narrower: generic XDP devmap
> broadcast/multi redirect with per-devmap-entry egress programs. The use case is
> not newly introduced by this patch. The existing xdp_veth_egress test already
> attaches xdp_devmap_prog to all egress devmap entries, and that program rewrites
> eth->h_source based on ctx->egress_ifindex. My change only strengthens the test
> from checking that the observed MAC is not equal to a single magic MAC to
> checking that each destination observes the MAC selected for its own egress
> ifindex.
>
> So attaching an egress program to all devmap entries is already what this test
> does. The SKB_MODE failure happens because the cloned skbs can still share the
> packet data, and a later per-egress rewrite can be observed by another
> destination.
>
> That said, I see the concern that this may need a clearer semantic boundary
> between clone and copy behavior. I will also check the last_dst path pointed out
> by Sashiko before deciding whether this should be handled as a bug fix, or
> whether it needs a separate explicit copy/isolated redirect semantic instead.
I agree there's a real concern here. For context, native XDP already
makes a full copy
of the frame for every broadcast destination:
dev_map_enqueue_multi -> dev_map_enqueue_clone -> xdpf_clone
so on the native path each destination gets its own independent buffer.
The generic path is the odd one out, because skb_clone() shares the
underlying data.
I think this rationale should be spelled out in the commit message.
Regarding performance: generic XDP is already a slow path — the ingress
side already
linearizes / expands the skb (pskb_expand_head() in
netif_receive_generic_xdp())
so the extra copy here is not a concern.
However, there is one problem with the patch as it stands.
The last destination is transmitted using the original skb directly,
without unsharing it.
So with two destinations where the first one has no egress XDP program
and the second (i.e. the last one) does,
the egress modification on the second destination will corrupt the
packet sent to the first.
This could be addressed by doing the skb_unshare() inside
dev_map_generic_redirect() instead ?
For the non-broadcast (existing) path the skb is not cloned, so
skb_unshare() simply returns it unchanged.
prev parent reply other threads:[~2026-06-10 5:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-09 10:02 [PATCH] bpf: Unshare cloned skb before devmap egress XDP program Sun Jian
2026-06-09 11:06 ` Menglong Dong
2026-06-10 0:06 ` Emil Tsalapatis
2026-06-10 1:21 ` Menglong Dong
2026-06-10 1:58 ` sun jian
2026-06-10 5:13 ` Jiayuan Chen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b35a2395-0e15-4390-ba7a-b48748e7e3f3@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=emil@etsalapatis.com \
--cc=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=liuhangbin@gmail.com \
--cc=martin.lau@linux.dev \
--cc=menglong.dong@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=sun.jian.kdev@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox