net/tap: fix Rx cksum
Subsystems (2)
cover net/tap: fix Rx cksum
This patchset fixes the Rx checksum flags in net/tap driver. The first two patches are the effective fixes.
The last 2 patches introduce a new checksum API to verify a L4 checksum and its unt test, in order to simplify the net/tap code, or any other code that has the same needs.
The last 2 patches may be postponed to 20.08 if required.
Olivier Matz (4):
net/tap: fix Rx cksum flags on IP options packets net/tap: fix Rx cksum flags on TCP packets
net: introduce functions to verify L4 checksums test/cksum: new test for L3/L4 checksum API
MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++ drivers/net/tap/rte_eth_tap.c | 17 ++- lib/net/rte_ip.h | 124 +++++++++++++--- 6 files changed, 390 insertions(+), 31 deletions(-) create mode 100644 app/test/test_cksum.c
Signature · 1 line
2.29.2
PATCH 1/4 net/tap: fix Rx cksum flags on IP options packets
When packet type is IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.
Since we know the header lengths, we can easily call rte_ipv4_udptcp_cksum() in this case too.
--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/tap/rte_eth_tap.c +2 -2
@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {
@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Signature · 1 line
2.29.2
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 19 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/tap/rte_eth_tap.c +0
-0
drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 5 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {
Should we take 'RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?
Quoted 13 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 15 lines · Ferruh Yigit
Quoted 14 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines
drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 10 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?
I think we should.
Quoted 14 lines
Quoted 14 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Hi Ferruh, Andrew,
Quoted 29 lines · Andrew Rybchenko
Quoted 15 lines · Ferruh Yigit
Quoted 14 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 12 lines
drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 10 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?
Quoted 1 line
I think we should.
I think 'RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' cannot happen here:
- mbuf->packet_type is generated by rte_net_get_ptype(), which cannot return 'RTE_PTYPE_L3_IPV4_EXT_UNKNOWN'
- right above this code, we already returned if l3 is not in (RTE_PTYPE_L3_IPV4, RTE_PTYPE_L3_IPV4_EXT, RTE_PTYPE_L3_IPV6)
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 32 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 29 lines · Andrew Rybchenko
Quoted 27 lines · Ferruh Yigit
Quoted 19 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/tap/rte_eth_tap.c +0
-0drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 5 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?
Quoted 5 lines
Quoted 1 line
I think we should.
I think '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' cannot happen here:-
mbuf->packet_typeis generated by
(), which cannot
Quoted 1 line
return '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN'
My question if it is guaranteed and the only possible branch. Can application set packet_type itself and do not call rte_net_get_ptype(). Yes, typically application knows if it has IPv4 options in the header or not, but theoretically could be unaware as well.
Quoted 2 lines
- right above this code, we already returned if l3 is not in (
RTE_PTYPE_L3_IPV4,RTE_PTYPE_L3_IPV4_EXT,RTE_PTYPE_L3_IPV6)
If so, it sounds like it should be allowed above as well.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 39 lines · Andrew Rybchenko
Quoted 32 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 29 lines · Andrew Rybchenko
Quoted 27 lines · Ferruh Yigit
Quoted 19 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/tap/rte_eth_tap.c +0
-0drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 5 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?Quoted 5 lines
Quoted 1 line
I think we should.
I think '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' cannot happen here:-
mbuf->packet_typeis generated by
Quoted 8 lines
(), which cannot
Quoted 1 line
return '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN'My question if it is guaranteed and the only possible branch. Can application set
packet_typeitself and do not callrte_net_get_ptype(). Yes, typically application knows if it has IPv4 options in the header or not, but theoretically could be unaware as well.
This function is called on the Rx path from pmd_rx_burst(), so the application does not have access to the mbuf.
The software parser that sets the packet type returns either RTE_PTYPE_L3_IPV4 if there is no option, or RTE_PTYPE_L3_IPV4_EXT else. The value RTE_PTYPE_L3_IPV4_EXT_UNKNOWN is used by PMDs that don't know if there are options.
Quoted 22 lines
Quoted 2 lines
- right above this code, we already returned if l3 is not in (
RTE_PTYPE_L3_IPV4,RTE_PTYPE_L3_IPV4_EXT,RTE_PTYPE_L3_IPV6)If so, it sounds like it should be allowed above as well.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 49 lines · Olivier Matz
Quoted 39 lines · Andrew Rybchenko
Quoted 32 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 29 lines · Andrew Rybchenko
Quoted 27 lines · Ferruh Yigit
Quoted 19 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/tap/rte_eth_tap.c +0
-0drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 5 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?Quoted 5 lines
Quoted 1 line
I think we should.
I think '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' cannot happen here:-
mbuf->packet_typeis generated byQuoted 8 lines
(), which cannot
Quoted 1 line
return '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN'My question if it is guaranteed and the only possible branch. Can application set
packet_typeitself and do not callrte_net_get_ptype(). Yes, typically application knows if it has IPv4 options in the header or not, but theoretically could be unaware as well.
Quoted 7 lines
This function is called on the Rx path from
pmd_rx_burst(), so the application does not have access to the mbuf.The software parser that sets the packet type returns either
RTE_PTYPE_L3_IPV4if there is no option, orRTE_PTYPE_L3_IPV4_EXTelse. The valueRTE_PTYPE_L3_IPV4_EXT_UNKNOWNis used by PMDs that don't know if there are options.
I see. What I'm trying to say that there are non obvious assumptions here on rte_net_get_ptype() behaviour which can be changed. May be it makes sense to add comments here to highlight it.
Quoted 23 lines
Quoted 23 lines
Quoted 3 lines
- right above this code, we already returned if l3 is not in (
RTE_PTYPE_L3_IPV4,RTE_PTYPE_L3_IPV4_EXT,RTE_PTYPE_L3_IPV6)If so, it sounds like it should be allowed above as well.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Re: [dpdk-dev] [dpdk-stable] [PATCH 1/4] net/tap: fix Rx cksum flags on IP options packets
Quoted 58 lines · Andrew Rybchenko
Quoted 49 lines · Olivier Matz
Quoted 39 lines · Andrew Rybchenko
Quoted 32 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 29 lines · Andrew Rybchenko
Quoted 27 lines · Ferruh Yigit
Quoted 19 lines · Olivier Matz
When packet type is
IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.Since we know the header lengths, we can easily call
rte_ipv4_udptcp_cksum()in this case too.Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/tap/rte_eth_tap.c +0
-0drivers/net/tap/rte_eth_tap.c tap_verify_csum(struct rte_mbuf *mbuf) · line 353
Quoted 5 lines
drivers/net/tap/rte_eth_tap.c +1
-1@@ -350,7 +350,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {Should we take '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' into account?Quoted 5 lines
Quoted 1 line
I think we should.
I think '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN' cannot happen here:-
mbuf->packet_typeis generated byQuoted 8 lines
(), which cannot
Quoted 1 line
return '
RTE_PTYPE_L3_IPV4_EXT_UNKNOWN'My question if it is guaranteed and the only possible branch. Can application set
packet_typeitself and do not callrte_net_get_ptype(). Yes, typically application knows if it has IPv4 options in the header or not, but theoretically could be unaware as well.Quoted 7 lines
This function is called on the Rx path from
pmd_rx_burst(), so the application does not have access to the mbuf.The software parser that sets the packet type returns either
RTE_PTYPE_L3_IPV4if there is no option, orRTE_PTYPE_L3_IPV4_EXTelse. The valueRTE_PTYPE_L3_IPV4_EXT_UNKNOWNis used by PMDs that don't know if there are options.
Quoted 4 lines
I see. What I'm trying to say that there are non obvious assumptions here on
rte_net_get_ptype()behaviour which can be changed. May be it makes sense to add comments here to highlight it.
Ok, I'll add some words about it.
Thanks!
Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Quoted 4 lines
- right above this code, we already returned if l3 is not in (
RTE_PTYPE_L3_IPV4,RTE_PTYPE_L3_IPV4_EXT,RTE_PTYPE_L3_IPV6)If so, it sounds like it should be allowed above as well.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
drivers/net/tap/rte_eth_tap.c +1
-1if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {@@ -364,7 +364,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
PATCH 2/4 net/tap: fix Rx cksum flags on TCP packets
In reply to Olivier MatzSince commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() or rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum.
This new behavior broke the checksum verification in tap driver for TCP
packets: these packets are marked with PKT_RX_L4_CKSUM_BAD.
Fix this by checking the 2 possible values. A next commit will introduce a checksum verification helper to simplify this a bit.
--- drivers/net/tap/rte_eth_tap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/tap/rte_eth_tap.c +7 -5
@@ -346,6 +346,8 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; }if (l4 ==RTE_PTYPE_L4_UDP || l4 ==RTE_PTYPE_L4_TCP ) { +int cksum_ok; + l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )
@@ -363,13 +365,13 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } - mbuf->ol_flags |= cksum ? -PKT_RX_L4_CKSUM_BAD : -PKT_RX_L4_CKSUM_GOOD ; + cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); + mbuf->ol_flags |= cksum_ok ? +PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; } }
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH 2/4] net/tap: fix Rx cksum flags on TCP packets
Quoted 8 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()orrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.This new behavior broke the checksum verification in tap driver for TCP
packets: these packets are marked withPKT_RX_L4_CKSUM_BAD.
Quoted 7 lines
Fix this by checking the 2 possible values. A next commit will introduce a checksum verification helper to simplify this a bit.
Fixes d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0")Signed-off-by Olivier Matz <[email protected]>
PATCH 3/4 net: introduce functions to verify L4 checksums
In reply to Olivier MatzSince commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() and rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum.
Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)
drivers/net/tap/rte_eth_tap.c +4 -3
@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }
lib/net/rte_ip.h +100 -24
165 lines not on this page · raw message
Signature · 1 line
2.29.2
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Quoted 106 lines
From: Olivier Matz [mailto:[email protected]]
Sent: Tuesday, April 27, 2021 3:58 PMSince commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in
a packet, introduce 2 new helpers for checksum verification. They return
0 if the checksum is valid in the packet.Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +2
-12@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr){
lib/net/rte_ip.h +24
-2uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP,
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr · line 402
Quoted 10 lines
lib/net/rte_ip.h +3
-1+* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +}
The GCC static branch predictor treats the above comparison as likely. Playing around with Godbolt, I came up with this alternative:
if (likely(cksum != 0)) return cksum; if (ipv4_hdr->next_proto_id == IPPROTO_UDP) return 0xffff; return cksum;
Quoted 10 lines
lib/net/rte_ip.h +10
-0+ +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1.
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 420
Quoted 10 lines
lib/net/rte_ip.h +10
-0+*/ +__rte_experimental +static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ;
The GCC static branch predictor treats the above comparison as likely, so I would prefer unlikely() around it.
Quoted 55 lines
lib/net/rte_ip.h +2
-0+ +return 0 ; }/**
lib/net/rte_ip.h +19
-0@@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum.
*lib/net/rte_ip.h +2
-8@@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* lib/net/rte_ip.h +0
-0* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones
lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr · line 533
Quoted 3 lines
lib/net/rte_ip.h +0
-0@@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ;
Same comment about GCC static branch prediction as above.
Quoted 19 lines
lib/net/rte_ip.h +7
-1-return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1
+ * (Upper-Layer Checksums) in RFC 8200.
+ *
+ * @paramipv6_hdr
+ * The pointer to the contiguous IPv6 header.
+ * @paraml4_hdr
+ * The pointer to the beginning of the L4 header.
+ * @return
+ * Return 0 if the checksum is correct, else -1.
+ */
lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 559
Quoted 10 lines
lib/net/rte_ip.h +10
-0+__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify (const struct rte_ipv6_hdr *ipv6_hdr, +const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ;
Same comment about GCC static branch prediction as above.
Quoted 8 lines
lib/net/rte_ip.h +2
-1+ +return 0 ; }/** IPv6 fragment extension header. */ --2.29.2
With or without my suggested modifications:
Without my suggested modifications:
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
In reply to Olivier MatzQuoted 106 lines
From: Olivier Matz [mailto:[email protected]]
Sent: Tuesday, April 27, 2021 3:58 PMSince commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in
a packet, introduce 2 new helpers for checksum verification. They return
0 if the checksum is valid in the packet.Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +2
-12@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr){
lib/net/rte_ip.h +24
-2uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP,
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr · line 402
Quoted 10 lines
lib/net/rte_ip.h +3
-1+* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +}
The GCC static branch predictor treats the above comparison as likely. Playing around with Godbolt, I came up with this alternative:
if (likely(cksum != 0)) return cksum; if (ipv4_hdr->next_proto_id == IPPROTO_UDP) return 0xffff; return 0;
Quoted 10 lines
lib/net/rte_ip.h +10
-0+ +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1.
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 420
Quoted 10 lines
lib/net/rte_ip.h +10
-0+*/ +__rte_experimental +static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ;
The GCC static branch predictor treats the above comparison as likely, so I would prefer unlikely() around it.
Quoted 55 lines
lib/net/rte_ip.h +2
-0+ +return 0 ; }/**
lib/net/rte_ip.h +19
-0@@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum.
*lib/net/rte_ip.h +2
-8@@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* lib/net/rte_ip.h +0
-0* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones
lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr · line 533
Quoted 3 lines
lib/net/rte_ip.h +0
-0@@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ;
Same comment about GCC static branch prediction as above.
Quoted 19 lines
lib/net/rte_ip.h +7
-1-return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1
+ * (Upper-Layer Checksums) in RFC 8200.
+ *
+ * @paramipv6_hdr
+ * The pointer to the contiguous IPv6 header.
+ * @paraml4_hdr
+ * The pointer to the beginning of the L4 header.
+ * @return
+ * Return 0 if the checksum is correct, else -1.
+ */
lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 559
Quoted 10 lines
lib/net/rte_ip.h +10
-0+__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify (const struct rte_ipv6_hdr *ipv6_hdr, +const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ;
Same comment about GCC static branch prediction as above.
Quoted 8 lines
lib/net/rte_ip.h +2
-1+ +return 0 ; }/** IPv6 fragment extension header. */ --2.29.2
With or without my suggested modifications:
Without my suggested modifications:
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Hi Morten,
Thank you for the review.
<...>
Quoted 23 lines · Morten Brørup
Quoted 10 lines
(quoted diff) +2
-0+static inline uint16_t +rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void
*l4_hdr)lib/net/rte_ip.h +5
-1+{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 400
Quoted 10 lines
lib/net/rte_ip.h +3
-1+* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +}The GCC static branch predictor treats the above comparison as likely. Playing around with Godbolt, I came up with this alternative:
Quoted 3 lines
if (likely(cksum != 0)) return cksum; if (ipv4_hdr->next_proto_id == IPPROTO_UDP) return 0xffff; return 0;
Good idea, this is indeed an unlikely branch. However this code was already present before this patch, so I suggest to add it as a specific optimization patch.
Quoted 14 lines
Quoted 14 lines
lib/net/rte_ip.h +14
-0+ +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
Quoted 8 lines
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 420
Quoted 6 lines
lib/net/rte_ip.h +6
-0+ const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ;The GCC static branch predictor treats the above comparison as likely, so I would prefer
unlikely()around it.
For this one, I'm less convinced: should we decide here whether the good or the bad checksum is more likely than the other?
Given it's a static inline function, wouldn't it be better to let the application call it this way:
if (likely(rte_ipv4_udptcp_cksum_verify(...) == 0)) ?
Regards,
Olivier
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Quoted 35 lines
From: dev [mailto:[email protected]] On Behalf Of Olivier Matz
Sent: Wednesday, April 28, 2021 2:22 PMHi Morten,
Thank you for the review.
<...>
Quoted 2 lines · Morten Brørup
Review
Quoted 2 lines
(quoted diff) +2
-0+static inline uint16_t +rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, constvoid
Quoted 15 lines
Quoted 15 lines
*l4_hdr)lib/net/rte_ip.h +6
-1+{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ;Quoted 5 lines
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 400
Quoted 3 lines
lib/net/rte_ip.h +2
-1-return (uint16_t )cksum; +return cksum; +}The GCC static branch predictor treats the above comparison as
likely. Playing around with Godbolt, I came up with this alternative:
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
if (likely(cksum != 0)) return cksum; if (ipv4_hdr->next_proto_id == IPPROTO_UDP) return 0xffff; return 0;Good idea, this is indeed an unlikely branch. However this code was already present before this patch, so I suggest to add it as a specific optimization patch.
Please do.
Quoted 23 lines
Quoted 15 lines
Quoted 15 lines
lib/net/rte_ip.h +14
-0+ +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,Quoted 8 lines
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 420
Quoted 6 lines
lib/net/rte_ip.h +6
-0+ const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ;The GCC static branch predictor treats the above comparison as
Quoted 4 lines
likely, so I would prefer
unlikely()around it.For this one, I'm less convinced: should we decide here whether the good or the bad checksum is more likely than the other?
You are right... this may be a question of personal preference - or application specific preference.
Quoted 5 lines
Given it's a static inline function, wouldn't it be better to let the application call it this way:
if (likely(rte_ipv4_udptcp_cksum_verify(...) == 0)) ?
Good point. Double checking on Godbolt confirms the validity of your suggestion.
Quoted 3 lines
Regards,
Olivier
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
In reply to Olivier MatzQuoted 117 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +41
-15@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) {uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental
lib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 422
Quoted 10 lines
lib/net/rte_ip.h +10
-0+static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ; + +return 0 ;
There is behavior change in tap verify, I am asking just to verify if expected,
Previously for UDP, if calculated checksum is '0', the 'rte_ipv4_udptcp_cksum()' returns 0xFFFF.
And 0xFFFF is taken as good checksum by tap PMD.
With new 'rte_ipv4_udptcp_cksum_verify()', if calculated checksum is '0' it will be taken as bad checksum.
I don't know if calculated checksum with valid checksum in place can return 0.
Also for TCP, 'rte_ipv4_udptcp_cksum_verify()' doesn't have inversion (cksum = ~cksum;) seems changing pass/fail status of the checksum, unless I am not missing anything here.
Quoted 72 lines
lib/net/rte_ip.h +40
-9}/** @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum. * @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1 +* (Upper-Layer Checksums) in RFC 8200. +* +* @param ipv6_hdr +* The pointer to the contiguous IPv6 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 562
Quoted 10 lines
lib/net/rte_ip.h +9
-0+ const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ; + +return 0 ; }
Nitpicking but, 'rte_ipv4_udptcp_cksum_verify()' is almost same with this function ('rte_ipv6_udptcp_cksum_verify()') but they have different line spacing, can be good to have similar syntax for both.
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Quoted 138 lines · Ferruh Yigit
Quoted 117 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +41
-15@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) {uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimentallib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 422
Quoted 10 lines
lib/net/rte_ip.h +10
-0+static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ; + +return 0 ;There is behavior change in tap verify, I am asking just to verify if expected,
Previously for UDP, if calculated checksum is '0', the '
rte_ipv4_udptcp_cksum()' returns0xFFFF.
And0xFFFFis taken as good checksum by tap PMD.With new '
rte_ipv4_udptcp_cksum_verify()', if calculated checksum is '0' it will be taken as bad checksum.
Quoted 6 lines
I don't know if calculated checksum with valid checksum in place can return 0.
Also for TCP, '
rte_ipv4_udptcp_cksum_verify()' doesn't have inversion (cksum = ~cksum;) seems changing pass/fail status of the checksum, unless I am not missing anything here.
Yes, it looks suspicious to me as well.
Olivier, could you clarify, please.
Quoted 87 lines
Quoted 72 lines
lib/net/rte_ip.h +40
-9}/** @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum. * @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1 +* (Upper-Layer Checksums) in RFC 8200. +* +* @param ipv6_hdr +* The pointer to the contiguous IPv6 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 562
Quoted 10 lines
lib/net/rte_ip.h +9
-0+ const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ; + +return 0 ; }Nitpicking but, '
rte_ipv4_udptcp_cksum_verify()' is almost same with this function ('rte_ipv6_udptcp_cksum_verify()') but they have different line spacing, can be good to have similar syntax for both.
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Hi Ferruh, Andrew,
Quoted 132 lines · Andrew Rybchenko
Quoted 131 lines · Ferruh Yigit
Quoted 117 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +41
-15@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) {uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimentallib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 422
Quoted 10 lines
lib/net/rte_ip.h +10
-0+static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ; + +return 0 ;There is behavior change in tap verify, I am asking just to verify if expected,
Quoted 3 lines
Quoted 3 lines
Previously for UDP, if calculated checksum is '0', the '
rte_ipv4_udptcp_cksum()' returns0xFFFF.
And0xFFFFis taken as good checksum by tap PMD.
rte_ipv4_udptcp_cksum() cannot return 0xFFFF: this is only possible if all data is 0. Before verifying a udp packet, the user must check that it is not 0 (which means no checksum). In tcp, "Data offset" is never
0. Moreover, port=0 is a reserved value for both udp and tcp.
Quoted 6 lines
Quoted 6 lines
With new '
rte_ipv4_udptcp_cksum_verify()', if calculated checksum is '0' it will be taken as bad checksum.I don't know if calculated checksum with valid checksum in place can return 0.
Quoted 7 lines
Quoted 3 lines
Also for TCP, '
rte_ipv4_udptcp_cksum_verify()' doesn't have inversion (cksum = ~cksum;) seems changing pass/fail status of the checksum, unless I am not missing anything here.Yes, it looks suspicious to me as well.
Olivier, could you clarify, please.
Before commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the behavior was:
// rte_ipv4_udptcp_cksum() is 0xffff if checksum is valid // so cksum is 0 if checksum is valid
cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
// ol_flags is set to PKT_RX_L4_CKSUM_GOOD if checksum is valid mbuf->ol_flags |= cksum ? PKT_RX_L4_CKSUM_BAD : PKT_RX_L4_CKSUM_GOOD;
After commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), it is broken:
// rte_ipv4_udptcp_cksum() is 0 (tcp) or 0xffff (udp) if checksum is valid // so cksum is 0xffff (tcp) or 0 (udp) if checksum is valid cksum = ~rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
// ol_flags is set to BAD (tcp) or GOOD (udp) if checksum is valid mbuf->ol_flags |= cksum ? PKT_RX_L4_CKSUM_BAD : PKT_RX_L4_CKSUM_GOOD;
After patch 2/4 ("net/tap: fix Rx cksum flags on TCP packets"), the correct behavior is restored:
// cksum is 0 (tcp) or 0xffff (udp) if checksum is valid
// note: 0xffff for tcp cannot happen (there is at least 1 bit set in the header) // note: 0 for udp cannot happen (it is replaced by in rte_ipv4_udptcp_cksum()) cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
// cksum_ok is 1 if checksum is valid
cksum_ok = (cksum == 0) || (cksum == 0xffff);
// ol_flags is set to GOOD if checksum is valid
mbuf->ol_flags |= cksum_ok ? PKT_RX_L4_CKSUM_GOOD : PKT_RX_L4_CKSUM_BAD;
After this patch [3/4] ("net: introduce functions to verify L4 checksums"), it is simplified by using rte_ipv4_udptcp_cksum_verify():
// cksum_ok is 1 if checksum is valid
cksum_ok = !rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr);
// ol_flags is set to GOOD if checksum is valid mbuf->ol_flags |= cksum_ok ? PKT_RX_L4_CKSUM_GOOD : PKT_RX_L4_CKSUM_BAD;
Olivier
Quoted 89 lines
Quoted 88 lines
Quoted 73 lines
lib/net/rte_ip.h +40
-9}/** @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum. * @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1 +* (Upper-Layer Checksums) in RFC 8200. +* +* @param ipv6_hdr +* The pointer to the contiguous IPv6 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 562
Quoted 10 lines
lib/net/rte_ip.h +9
-0+ const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ; + +return 0 ; }Nitpicking but, '
rte_ipv4_udptcp_cksum_verify()' is almost same with this function ('rte_ipv6_udptcp_cksum_verify()') but they have different line spacing, can be good to have similar syntax for both.
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Quoted 188 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 132 lines · Andrew Rybchenko
Quoted 131 lines · Ferruh Yigit
Quoted 117 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +41
-15@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) {uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimentallib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 422
Quoted 10 lines
lib/net/rte_ip.h +10
-0+static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ; + +return 0 ;There is behavior change in tap verify, I am asking just to verify if expected,
Quoted 3 lines
Quoted 3 lines
Previously for UDP, if calculated checksum is '0', the '
rte_ipv4_udptcp_cksum()' returns0xFFFF.
And0xFFFFis taken as good checksum by tap PMD.
rte_ipv4_udptcp_cksum()cannot return0xFFFF: this is only possible if all data is 0. Before verifying a udp packet, the user must check that it is not 0 (which means no checksum). In tcp, "Data offset" is never
0. Moreover, port=0 is a reserved value for both udp and tcp.Quoted 6 lines
Quoted 6 lines
With new '
rte_ipv4_udptcp_cksum_verify()', if calculated checksum is '0' it will be taken as bad checksum.I don't know if calculated checksum with valid checksum in place can return 0.
Quoted 7 lines
Quoted 3 lines
Also for TCP, '
rte_ipv4_udptcp_cksum_verify()' doesn't have inversion (cksum = ~cksum;) seems changing pass/fail status of the checksum, unless I am not missing anything here.Yes, it looks suspicious to me as well.
Olivier, could you clarify, please.
Before commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the behavior was://
rte_ipv4_udptcp_cksum()is0xffffif checksum is valid // so cksum is 0 if checksum is valid
cksum = ~rte_ipv4_udptcp_cksum(l3_hdr,l4_hdr);
//ol_flagsis set toPKT_RX_L4_CKSUM_GOODif checksum is validmbuf->ol_flags|= cksum ?PKT_RX_L4_CKSUM_BAD:PKT_RX_L4_CKSUM_GOOD;After commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), it is broken://
rte_ipv4_udptcp_cksum()is 0 (tcp) or0xffff(udp) if checksum is valid // so cksum is0xffff(tcp) or 0 (udp) if checksum is valid cksum = ~rte_ipv4_udptcp_cksum(l3_hdr,l4_hdr);
//ol_flagsis set to BAD (tcp) or GOOD (udp) if checksum is validmbuf->ol_flags|= cksum ?PKT_RX_L4_CKSUM_BAD:PKT_RX_L4_CKSUM_GOOD;After patch 2/4 ("net/tap: fix Rx cksum flags on TCP packets"), the correct behavior is restored:
// cksum is 0 (tcp) or
0xffff(udp) if checksum is valid
// note:0xfffffor tcp cannot happen (there is at least 1 bit set in the header) // note: 0 for udp cannot happen (it is replaced by inrte_ipv4_udptcp_cksum()) cksum =rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
//cksum_okis 1 if checksum is valid
cksum_ok= (cksum == 0) || (cksum ==0xffff);
//ol_flagsis set to GOOD if checksum is valid
mbuf->ol_flags|=cksum_ok?PKT_RX_L4_CKSUM_GOOD:PKT_RX_L4_CKSUM_BAD;
Quoted 8 lines
After this patch [3/4] ("net: introduce functions to verify L4 checksums"), it is simplified by using
rte_ipv4_udptcp_cksum_verify()://
cksum_okis 1 if checksum is valid
cksum_ok= !rte_ipv4_udptcp_cksum_verify(l3_hdr,l4_hdr);
//ol_flagsis set to GOOD if checksum is validmbuf->ol_flags|=cksum_ok?PKT_RX_L4_CKSUM_GOOD:PKT_RX_L4_CKSUM_BAD;
Many thanks for the detailed explanations. It replies to all my questions (even not asked, but kept in my head).
Andrew.
Quoted 91 lines
Olivier
Quoted 89 lines
Quoted 88 lines
Quoted 73 lines
lib/net/rte_ip.h +40
-9}/** @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)return __rte_raw_cksum_reduce (sum); } +/** +* @internal Calculate the non-complemented IPv4 L4 checksum +*/ +static inline uint16_t +__rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) +{ +uint32_t cksum; +uint32_t l4_len; + + l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); + + cksum =rte_raw_cksum (l4_hdr, l4_len); + cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + + cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); + +return (uint16_t )cksum; +} +/** * Process the IPv6 UDP or TCP checksum. * @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)static inline uint16_t rte_ipv6_udptcp_cksum (const struct rte_ipv6_hdr *ipv6_hdr,const void *l4_hdr) { -uint32_t cksum; -uint32_t l4_len; - - l4_len =rte_be_to_cpu_16 (ipv6_hdr->payload_len); +uint16_t cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); - cksum =rte_raw_cksum (l4_hdr, l4_len); - cksum +=rte_ipv6_phdr_cksum (ipv6_hdr,0 ); + cksum = ~cksum; - cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ;/* * Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)if (cksum ==0 && ipv6_hdr->proto ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv6 UDP or TCP checksum. +* +* The function accepts a 0 checksum, since it can exceptionally happen. See 8.1 +* (Upper-Layer Checksums) in RFC 8200. +* +* @param ipv6_hdr +* The pointer to the contiguous IPv6 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimental +static inline int +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,lib/net/rte_ip.h rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) · line 562
Quoted 10 lines
lib/net/rte_ip.h +9
-0+ const void *l4_hdr) +{ +uint16_t cksum; + + cksum =__rte_ipv6_udptcp_cksum (ipv6_hdr, l4_hdr); +if (cksum !=0xffff ) +return -1 ; + +return 0 ; }Nitpicking but, '
rte_ipv4_udptcp_cksum_verify()' is almost same with this function ('rte_ipv6_udptcp_cksum_verify()') but they have different line spacing, can be good to have similar syntax for both.
Re: [dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Quoted 192 lines · Andrew Rybchenko
Quoted 191 lines · Olivier Matz
Hi Ferruh, Andrew,
Quoted 132 lines · Andrew Rybchenko
Quoted 131 lines · Ferruh Yigit
Quoted 117 lines · Olivier Matz
Since commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the functionsrte_ipv4_udptcp_cksum()andrte_ipv6_udptcp_cksum()can return either0x0000or0xffffwhen used to verify a packet containing a valid checksum.Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
Signed-off-by Olivier Matz <[email protected]>--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 27 deletions(-)drivers/net/tap/rte_eth_tap.c +4
-3@@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }lib/net/rte_ip.h +41
-15@@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) }/** -* Process the IPv4 UDP or TCP checksum. -* -* The IP and layer 4 checksum must be set to 0 in the packet by -* the caller. -* -* @param ipv4_hdr -* The pointer to the contiguous IPv4 header. -* @param l4_hdr -* The pointer to the beginning of the L4 header. -* @return -* The complemented checksum to set in the IP packet. +* @internal Calculate the non-complemented IPv4 L4 checksum */ static inline uint16_t -rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +__rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) {uint32_t cksum;uint32_t l3_len, l4_len;@@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) cksum +=rte_ipv4_phdr_cksum (ipv4_hdr,0 ); cksum = ((cksum &0xffff0000 ) >>16 ) + (cksum &0xffff ); - cksum = (~cksum) &0xffff ; + +return (uint16_t )cksum; +} + +/** +* Process the IPv4 UDP or TCP checksum. +* +* The IP and layer 4 checksum must be set to 0 in the packet by +* the caller. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* The complemented checksum to set in the IP packet. +*/ +static inline uint16_t +rte_ipv4_udptcp_cksum (const struct rte_ipv4_hdr *ipv4_hdr,const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + + cksum = ~cksum; +/* -* Per RFC 768:If the computed checksum is zero for UDP, +* Per RFC 768: If the computed checksum is zero for UDP, * it is transmitted as all ones * (the equivalent in one's complement arithmetic). */ if (cksum ==0 && ipv4_hdr->next_proto_id ==IPPROTO_UDP ) cksum =0xffff ; -return (uint16_t )cksum; +return cksum; +} + +/** +* Validate the IPv4 UDP or TCP checksum. +* +* @param ipv4_hdr +* The pointer to the contiguous IPv4 header. +* @param l4_hdr +* The pointer to the beginning of the L4 header. +* @return +* Return 0 if the checksum is correct, else -1. +*/ +__rte_experimentallib/net/rte_ip.h rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) · line 422
Quoted 10 lines
lib/net/rte_ip.h +10
-0+static inline int +rte_ipv4_udptcp_cksum_verify (const struct rte_ipv4_hdr *ipv4_hdr, +const void *l4_hdr) +{ +uint16_t cksum =__rte_ipv4_udptcp_cksum (ipv4_hdr, l4_hdr); + +if (cksum !=0xffff ) +return -1 ; + +return 0 ;There is behavior change in tap verify, I am asking just to verify if expected,
Quoted 3 lines
Quoted 3 lines
Previously for UDP, if calculated checksum is '0', the '
rte_ipv4_udptcp_cksum()' returns0xFFFF.
And0xFFFFis taken as good checksum by tap PMD.
rte_ipv4_udptcp_cksum()cannot return0xFFFF: this is only possible if all data is 0. Before verifying a udp packet, the user must check that it is not 0 (which means no checksum). In tcp, "Data offset" is never
0. Moreover, port=0 is a reserved value for both udp and tcp.Quoted 6 lines
Quoted 6 lines
With new '
rte_ipv4_udptcp_cksum_verify()', if calculated checksum is '0' it will be taken as bad checksum.I don't know if calculated checksum with valid checksum in place can return 0.
Quoted 7 lines
Quoted 3 lines
Also for TCP, '
rte_ipv4_udptcp_cksum_verify()' doesn't have inversion (cksum = ~cksum;) seems changing pass/fail status of the checksum, unless I am not missing anything here.Yes, it looks suspicious to me as well.
Olivier, could you clarify, please.
Before commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), the behavior was://
rte_ipv4_udptcp_cksum()is0xffffif checksum is valid // so cksum is 0 if checksum is valid
cksum = ~rte_ipv4_udptcp_cksum(l3_hdr,l4_hdr);
//ol_flagsis set toPKT_RX_L4_CKSUM_GOODif checksum is validmbuf->ol_flags|= cksum ?PKT_RX_L4_CKSUM_BAD:PKT_RX_L4_CKSUM_GOOD;After commit
d5df2ae0428a("net: fix unneeded replacement of TCP checksum 0"), it is broken://
rte_ipv4_udptcp_cksum()is 0 (tcp) or0xffff(udp) if checksum is valid // so cksum is0xffff(tcp) or 0 (udp) if checksum is valid cksum = ~rte_ipv4_udptcp_cksum(l3_hdr,l4_hdr);
//ol_flagsis set to BAD (tcp) or GOOD (udp) if checksum is validmbuf->ol_flags|= cksum ?PKT_RX_L4_CKSUM_BAD:PKT_RX_L4_CKSUM_GOOD;After patch 2/4 ("net/tap: fix Rx cksum flags on TCP packets"), the correct behavior is restored:
// cksum is 0 (tcp) or
0xffff(udp) if checksum is valid
// note:0xfffffor tcp cannot happen (there is at least 1 bit set in the header) // note: 0 for udp cannot happen (it is replaced by inrte_ipv4_udptcp_cksum()) cksum =rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
//cksum_okis 1 if checksum is valid
cksum_ok= (cksum == 0) || (cksum ==0xffff);
//ol_flagsis set to GOOD if checksum is valid
mbuf->ol_flags|=cksum_ok?PKT_RX_L4_CKSUM_GOOD:PKT_RX_L4_CKSUM_BAD;After this patch [3/4] ("net: introduce functions to verify L4 checksums"), it is simplified by using
rte_ipv4_udptcp_cksum_verify():
Quoted 9 lines
Quoted 5 lines
//
cksum_okis 1 if checksum is valid
cksum_ok= !rte_ipv4_udptcp_cksum_verify(l3_hdr,l4_hdr);
//ol_flagsis set to GOOD if checksum is validmbuf->ol_flags|=cksum_ok?PKT_RX_L4_CKSUM_GOOD:PKT_RX_L4_CKSUM_BAD;Many thanks for the detailed explanations. It replies to all my questions (even not asked, but kept in my head).
Thanks for clarification, after checking again with help of description above, it looks good to me.
PATCH 4/4 test/cksum: new test for L3/L4 checksum API
In reply to Olivier MatzAdd a simple unit test for checksum API.
--- MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 app/test/test_cksum.c
MAINTAINERS +1 -0
@@ -1309,6 +1309,7 @@ Packet processing Network headers M: Olivier Matz <[email protected]> F: lib/net/ +F: app/test/test_cksum.c Packet CRC M: Jasvinder Singh <[email protected]>
app/test/autotest_data.py +6 -0
@@ -585,6 +585,12 @@ "Func": default_autotest, "Report": None, }, + { + "Name": "Checksum autotest", + "Command": "cksum_autotest", + "Func": default_autotest, + "Report": None, + }, # #Please always keep all dump tests at the end and together! #
app/test/meson.build +2 -0
@@ -17,6 +17,7 @@ test_sources = files( 'test_bitmap.c', 'test_bpf.c', 'test_byteorder.c', + 'test_cksum.c', 'test_cmdline.c', 'test_cmdline_cirbuf.c', 'test_cmdline_etheraddr.c',
@@ -189,6 +190,7 @@ fast_tests = [ ['atomic_autotest', false], ['bitops_autotest', true], ['byteorder_autotest', true], + ['cksum_autotest', true], ['cmdline_autotest', true], ['common_autotest', true], ['cpuflags_autotest', true],
app/test/test_cksum.c new file +271 -0
271 lines not on this page · raw message
Signature · 1 line
2.29.2
v2 cover net/tap: fix Rx cksum
In reply to Olivier MatzThis patchset fixes the Rx checksum flags in net/tap driver. The first two patches are the effective fixes.
The last 2 patches introduce a new checksum API to verify a L4 checksum and its unt test, in order to simplify the net/tap code, or any other code that has the same needs.
v2:
* clarify why RTE_PTYPE_L3_IPV4_EXT_UNKNOWN cannot happen in tap_verify_csum() (patch 1)
* align style of rte_ipv6_udptcp_cksum_verify() to rte_ipv4_udptcp_cksum_verify() (patch 3)
* clarify comment above rte_ipv4_udptcp_cksum_verify() and rte_ipv6_udptcp_cksum_verify() (patch 3)
Olivier Matz (4):
net/tap: fix Rx cksum flags on IP options packets net/tap: fix Rx cksum flags on TCP packets
net: introduce functions to verify L4 checksums test/cksum: new test for L3/L4 checksum API
MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++ drivers/net/tap/rte_eth_tap.c | 23 ++- lib/net/rte_ip.h | 127 +++++++++++++--- 6 files changed, 398 insertions(+), 32 deletions(-) create mode 100644 app/test/test_cksum.c
Signature · 1 line
2.29.2
PATCH v2 1/4 net/tap: fix Rx cksum flags on IP options packets
When packet type is IPV4_EXT, the checksum is always marked as good in the mbuf offload flags.
Since we know the header lengths, we can easily call rte_ipv4_udptcp_cksum() in this case too.
--- drivers/net/tap/rte_eth_tap.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/tap/rte_eth_tap.c +7 -3
@@ -342,7 +342,11 @@ tap_verify_csum(struct rte_mbuf *mbuf) rte_pktmbuf_data_len(mbuf))return ; }else { -/* IPv6 extensions are not supported */ +/* - RTE_PTYPE_L3_IPV4_EXT_UNKNOWN cannot happen because +* mbuf->packet_type is filled by rte_net_get_ptype() which +* never returns this value. +* - IPv6 extensions are not supported. +*/ return ; }if (l4 ==RTE_PTYPE_L4_UDP || l4 ==RTE_PTYPE_L4_TCP ) {
@@ -350,7 +354,7 @@ tap_verify_csum(struct rte_mbuf *mbuf)/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )return ; -if (l3 ==RTE_PTYPE_L3_IPV4 ) { +if (l3 ==RTE_PTYPE_L3_IPV4 || l3 ==RTE_PTYPE_L3_IPV4_EXT ) {if (l4 ==RTE_PTYPE_L4_UDP ) { udp_hdr = (struct rte_udp_hdr *)l4_hdr;if (udp_hdr->dgram_cksum ==0 ) {
@@ -364,7 +368,7 @@ tap_verify_csum(struct rte_mbuf *mbuf) } } cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); - }else if (l3 ==RTE_PTYPE_L3_IPV6 ) { + }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } mbuf->ol_flags |= cksum ?
Signature · 1 line
2.29.2
PATCH v2 2/4 net/tap: fix Rx cksum flags on TCP packets
In reply to Olivier MatzSince commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() or rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum.
This new behavior broke the checksum verification in tap driver for TCP
packets: these packets are marked with PKT_RX_L4_CKSUM_BAD.
Fix this by checking the 2 possible values. A next commit will introduce a checksum verification helper to simplify this a bit.
--- drivers/net/tap/rte_eth_tap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/tap/rte_eth_tap.c +7 -5
@@ -350,6 +350,8 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; }if (l4 ==RTE_PTYPE_L4_UDP || l4 ==RTE_PTYPE_L4_TCP ) { +int cksum_ok; + l4_hdr = rte_pktmbuf_mtod_offset(mbuf, void *, l2_len + l3_len);/* Don't verify checksum for multi-segment packets. */ if (mbuf->nb_segs >1 )
@@ -367,13 +369,13 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum = ~rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum = ~rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); } - mbuf->ol_flags |= cksum ? -PKT_RX_L4_CKSUM_BAD : -PKT_RX_L4_CKSUM_GOOD ; + cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); + mbuf->ol_flags |= cksum_ok ? +PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; } }
Signature · 1 line
2.29.2
PATCH v2 3/4 net: introduce functions to verify L4 checksums
In reply to Olivier MatzSince commit d5df2ae0428a ("net: fix unneeded replacement of TCP checksum 0"), the functions rte_ipv4_udptcp_cksum() and rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to verify a packet containing a valid checksum.
Since these functions should be used to calculate the checksum to set in a packet, introduce 2 new helpers for checksum verification. They return 0 if the checksum is valid in the packet.
Use this new helper in net/tap driver.
--- drivers/net/tap/rte_eth_tap.c | 7 +- lib/net/rte_ip.h | 127 +++++++++++++++++++++++++++------- 2 files changed, 107 insertions(+), 27 deletions(-)
drivers/net/tap/rte_eth_tap.c +4 -3
@@ -369,11 +369,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)return ; } } - cksum =rte_ipv4_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv4_udptcp_cksum_verify (l3_hdr, + l4_hdr); }else {/* l3 == RTE_PTYPE_L3_IPV6, checked above */ - cksum =rte_ipv6_udptcp_cksum (l3_hdr, l4_hdr); + cksum_ok = !rte_ipv6_udptcp_cksum_verify (l3_hdr, + l4_hdr); } - cksum_ok = (cksum ==0 ) || (cksum ==0xffff ); mbuf->ol_flags |= cksum_ok ?PKT_RX_L4_CKSUM_GOOD :PKT_RX_L4_CKSUM_BAD ; }
lib/net/rte_ip.h +103 -24
168 lines not on this page · raw message
Signature · 1 line
2.29.2
PATCH v2 4/4 test/cksum: new test for L3/L4 checksum API
In reply to Olivier MatzAdd a simple unit test for checksum API.
--- MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 app/test/test_cksum.c
MAINTAINERS +1 -0
@@ -1314,6 +1314,7 @@ Packet processing Network headers M: Olivier Matz <[email protected]> F: lib/net/ +F: app/test/test_cksum.c Packet CRC M: Jasvinder Singh <[email protected]>
app/test/autotest_data.py +6 -0
@@ -567,6 +567,12 @@ "Func": default_autotest, "Report": None, }, + { + "Name": "Checksum autotest", + "Command": "cksum_autotest", + "Func": default_autotest, + "Report": None, + }, # #Please always keep all dump tests at the end and together! #
app/test/meson.build +2 -0
@@ -17,6 +17,7 @@ test_sources = files( 'test_bitmap.c', 'test_bpf.c', 'test_byteorder.c', + 'test_cksum.c', 'test_cmdline.c', 'test_cmdline_cirbuf.c', 'test_cmdline_etheraddr.c',
@@ -188,6 +189,7 @@ fast_tests = [ ['atomic_autotest', false], ['bitops_autotest', true], ['byteorder_autotest', true], + ['cksum_autotest', true], ['cmdline_autotest', true], ['common_autotest', true], ['cpuflags_autotest', true],
app/test/test_cksum.c new file +271 -0
271 lines not on this page · raw message
Signature · 1 line
2.29.2
Andrew Rybchenko
AppliedQuoted 24 lines · Olivier Matz
This patchset fixes the Rx checksum flags in net/tap driver. The first two patches are the effective fixes.
The last 2 patches introduce a new checksum API to verify a L4 checksum and its unt test, in order to simplify the net/tap code, or any other code that has the same needs.
v2:
* clarify why
RTE_PTYPE_L3_IPV4_EXT_UNKNOWNcannot happen intap_verify_csum()(patch 1)
* align style ofrte_ipv6_udptcp_cksum_verify()torte_ipv4_udptcp_cksum_verify()(patch 3)
* clarify comment aboverte_ipv4_udptcp_cksum_verify()andrte_ipv6_udptcp_cksum_verify()(patch 3)Olivier Matz (4):
net/tap: fix Rx cksum flags on IP options packets net/tap: fix Rx cksum flags on TCP packets
net: introduce functions to verify L4 checksums test/cksum: new test for L3/L4 checksum API
Quoted 9 lines
MAINTAINERS | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 2 + app/test/test_cksum.c | 271 ++++++++++++++++++++++++++++++++++ drivers/net/tap/rte_eth_tap.c | 23 ++- lib/net/rte_ip.h | 127 +++++++++++++--- 6 files changed, 398 insertions(+), 32 deletions(-) create mode 100644 app/test/test_cksum.c
Applied, thanks.