preserve MSG_ZEROCOPY with forwarding
Subsystems (4)
cover preserve MSG_ZEROCOPY with forwarding
From: Willem de Bruijn <[email protected]>
Avoid false positive copying of zerocopy skb frags when entering the ingress path if the skb is not queued locally but forwarded.
Patch 1 for more details and feature.
Patch 2 converts the existing selftest to a pass/fail test and adds
coverage for this new feature.
Willem de Bruijn (2):
net: preserve MSG_ZEROCOPY with forwarding
selftest: net: extend msg_zerocopy test with forwarding
net/core/dev.c | 2 - net/ipv4/ip_input.c | 6 ++ net/ipv6/ip6_input.c | 7 ++ tools/testing/selftests/net/msg_zerocopy.c | 24 +++--- tools/testing/selftests/net/msg_zerocopy.sh | 84 +++++++++++++++------ 5 files changed, 90 insertions(+), 33 deletions(-)
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH 1/2 net: preserve MSG_ZEROCOPY with forwarding
From: Willem de Bruijn <[email protected]>
MSG_ZEROCOPY data must be copied before data is queued to local sockets, to avoid indefinite timeout until memory release.
This test is performed by skb_orphan_frags_rx, which is called when looping an egress skb to packet sockets, error queue or ingress path.
To preserve zerocopy for skbs that are looped to ingress but are then forwarded to an egress device rather than delivered locally, defer this last check until an skb enters the local IP receive path.
This is analogous to existing behavior of skb_clear_delivery_time.
--- net/core/dev.c | 2 -- net/ipv4/ip_input.c | 6 ++++++ net/ipv6/ip6_input.c | 7 +++++++ 3 files changed, 13 insertions(+), 2 deletions(-)
net/core/dev.c +0 -2
@@ -5937,8 +5937,6 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, }if (pt_prev) { -if (unlikely (skb_orphan_frags_rx (skb,GFP_ATOMIC ))) - goto drop; *ppt_prev = pt_prev; }else { drop:
net/ipv4/ip_input.c +6 -0
@@ -226,6 +226,12 @@ void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int protocol)static int ip_local_deliver_finish (struct net *net,struct sock *sk,struct sk_buff *skb) { +if (unlikely (skb_orphan_frags_rx (skb,GFP_ATOMIC ))) { +__IP_INC_STATS (net,IPSTATS_MIB_INDISCARDS ); +kfree_skb_reason (skb,SKB_DROP_REASON_NOMEM ); +return 0 ; + } +skb_clear_delivery_time (skb);__skb_pull (skb,skb_network_header_len (skb));
net/ipv6/ip6_input.c +7 -0
@@ -476,6 +476,13 @@ void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,static int ip6_input_finish (struct net *net,struct sock *sk,struct sk_buff *skb) { +if (unlikely (skb_orphan_frags_rx (skb,GFP_ATOMIC ))) { +__IP6_INC_STATS (net,ip6_dst_idev (skb_dst (skb)), +IPSTATS_MIB_INDISCARDS ); +kfree_skb_reason (skb,SKB_DROP_REASON_NOMEM ); +return 0 ; + } +skb_clear_delivery_time (skb);ip6_protocol_deliver_rcu (net, skb,0 , false);
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
PATCH 2/2 selftest: net: extend msg_zerocopy test with forwarding
In reply to Willem de BruijnFrom: Willem de Bruijn <[email protected]>
Zerocopy skbs are converted to regular copy skbs when data is queued to a local socket. This happens in the existing test with a sender and receiver communicating over a veth device.
Zerocopy skbs are sent without copying if egressing a device. Verify that this behavior is maintained even in the common container setup where data is forwarded over a veth to the physical device.
Update msg_zerocopy.sh to
1. Have a dummy network device to simulate a physical device.
2. Have forwarding enabled between veth and dummy.
3. Add a tx-only test that sends out dummy via the forwarding path.
4. Verify the exitcode of the sender, which signals zerocopy success.
As dummy drops all packets, this cannot be a TCP connection. Test the new case with unconnected UDP only.
Update msg_zerocopy.c to
- Accept an argument whether send with zerocopy is expected.
- Return an exitcode whether behavior matched that expectation.
--- tools/testing/selftests/net/msg_zerocopy.c | 24 +++--- tools/testing/selftests/net/msg_zerocopy.sh | 84 +++++++++++++++------ 2 files changed, 77 insertions(+), 31 deletions(-)
tools/testing/selftests/net/msg_zerocopy.c +15 -9
@@ -77,6 +77,7 @@static int cfg_cork;static bool cfg_cork_mixed;static int cfg_cpu =-1 ;/* default: pin to last cpu */ +static int cfg_expect_zerocopy =-1 ;static int cfg_family =PF_UNSPEC ;static int cfg_ifindex =1 ;static int cfg_payload_len;
@@ -92,9 +93,9 @@ static socklen_t cfg_alen;static struct sockaddr_storage cfg_dst_addr;static struct sockaddr_storage cfg_src_addr; +static int exitcode;static char payload[IP_MAXPACKET ];static long packets, bytes, completions, expected_completions; -static int zerocopied =-1 ;static uint32_t next_completion;static uint32_t sends_since_notify;
@@ -444,11 +445,13 @@ static bool do_recv_completion(int fd, int domain) next_completion = hi +1 ; zerocopy = !(serr->ee_code &SO_EE_CODE_ZEROCOPY_COPIED ); -if (zerocopied ==-1 ) - zerocopied = zerocopy; -else if (zerocopied != zerocopy) { -fprintf (stderr,"serr: inconsistent\n" ); - zerocopied = zerocopy; +if (cfg_expect_zerocopy !=-1 && + cfg_expect_zerocopy != zerocopy) { +fprintf (stderr,"serr: ee_code: %u != expected %u\n" , + zerocopy, cfg_expect_zerocopy); + exitcode =1 ; +/* suppress repeated messages */ + cfg_expect_zerocopy = zerocopy; }if (cfg_verbose >=2 )
@@ -571,7 +574,7 @@ static void do_tx(int domain, int type, int protocol)fprintf (stderr,"tx=%lu (%lu MB) txc=%lu zc=%c\n" , packets, bytes >>20 , completions, - zerocopied ==1 ?'y' :'n' ); + cfg_zerocopy && cfg_expect_zerocopy ==1 ?'y' :'n' ); }static int do_setup_rx (int domain,int type,int protocol)
@@ -715,7 +718,7 @@ static void parse_opts(int argc, char **argv) cfg_payload_len = max_payload_len; -while ((c =getopt (argc, argv,"46c:C:D:i:l:mp:rs:S:t:vz" )) !=-1 ) { +while ((c =getopt (argc, argv,"46c:C:D:i:l:mp:rs:S:t:vzZ:" )) !=-1 ) {switch (c) {case '4' :if (cfg_family !=PF_UNSPEC )
@@ -770,6 +773,9 @@ static void parse_opts(int argc, char **argv)case 'z' : cfg_zerocopy = true;break ; +case 'Z' : + cfg_expect_zerocopy = !!atoi (optarg); +break ; } }
@@ -817,5 +823,5 @@ int main(int argc, char **argv)else error (1 ,0 ,"unknown cfg_test %s" , cfg_test); -return 0 ; +return exitcode; }
tools/testing/selftests/net/msg_zerocopy.sh +62 -22
133 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Mon, 30 Jun 2025 15:42:10 -0400 you wrote:
From: Willem de Bruijn <[email protected]>
Avoid false positive copying of zerocopy skb frags when entering the ingress path if the skb is not queued locally but forwarded.
Patch 1 for more details and feature.
[...]
Here is the summary with links:
- [net-next,1/2] net: preserve MSG_ZEROCOPY with forwarding
https://git.kernel.org/netdev/net-next/c/d2527ad3a9e1 - [net-next,2/2] selftest: net: extend msg_zerocopy test with forwarding https://git.kernel.org/netdev/net-next/c/81d572a551f4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html