Jason Xing <kern…@gmail.com> writes:
> An XDP_TXMD_FLAGS_CHECKSUM request is turned into an skb carrying
> CHECKSUM_PARTIAL by xsk_skb_metadata() where csum_start/csum_offset/
> ip_summed are set.
>
> However skb->protocol stays 0, the offload never actually reaches
> the hardware and checksum ends up being computed in software:
> 1. harmonize_features() strips the checksum features on devices.
> 2. ice_features_check() rejects the frame on NETIF_F_HW_CSUM capable ice.
>
> The frame layout comes from user space, and request.csum_start is
> already the offset of the L4 header from the start of the frame, so the
> transport header is known exactly.
>
> Only the L2 length has to be found, by parsing the Ethernet header and,
> when present, the VLAN tag stack. skb_header_pointer() is used for that,
> so it works for frames whose data is not in the linear part of the skb.
>
> skb->protocol is set to the outer ethertype, as the rest of the stack does
> for in-band VLAN tags, so skb_vlan_tagged() and vlan_features_check()
> keep seeing the tags. can_checksum_protocol() and the drivers get the L3
> type through vlan_get_protocol(). The mac header is anchored by
> __dev_direct_xmit() in the previous patch.
>
> A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or
> IPv6, or whose csum_start does not leave room for an L3 header is left
> untouched, so validate_xmit_skb() keeps deciding between hardware offload
> and software checksum as before.
>
> The patch is based on Maciej suggestion: 1) do not rely on mac header that
> probe in AF_PACKET uses, 2) use skb_header_pointer and walk vlan tags to
> inspect the accurate location of each field that existing drivers
> possibly use.
>
> [1]: https://lore.kernel.org/all/arKm…@boxer/
>
> Fixes: 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
> Cc: stab…@vger.kernel.org
> Suggested-by: Maciej Fijalkowski <maci…@intel.com>
> Signed-off-by: Jason Xing <kern…@gmail.com>
> ---
> net/xdp/xsk.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 47 insertions(+), 4 deletions(-)
>
> diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> index 52d538160e0a..58f77af6100d 100644
> --- a/net/xdp/xsk.c
> +++ b/net/xdp/xsk.c
> @@ -11,8 +11,12 @@
>
> #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
>
> +#include <linux/if_arp.h>
> +#include <linux/if_vlan.h>
> #include <linux/if_xdp.h>
> #include <linux/init.h>
> +#include <linux/ip.h>
> +#include <linux/ipv6.h>
> #include <linux/sched/mm.h>
> #include <linux/sched/signal.h>
> #include <linux/sched/task.h>
> @@ -1025,12 +1029,51 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
> return 0;
> }
>
> -static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
> +static int xsk_skb_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
> {
> - if (unlikely(pool->tx_sw_csum &&
> - skb->ip_summed == CHECKSUM_PARTIAL))
> + struct ethhdr eth_buf, *eth;
> + int nhoff = ETH_HLEN;
> + unsigned int l3_min;
> + __be16 proto;
> +
> + if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
> + return 0;
> +
> + if (unlikely(pool->tx_sw_csum))
> return skb_checksum_help(skb);
>
> + if (unlikely(skb->dev->type != ARPHRD_ETHER))
> + return 0;
> +
> + eth = skb_header_pointer(skb, 0, sizeof(*eth), ð_buf);
> + if (unlikely(!eth))
> + return 0;
> +
> + proto = eth->h_proto;
> + if (eth_type_vlan(proto)) {
> + proto = __vlan_get_protocol(skb, proto, &nhoff);
> + if (unlikely(!proto))
> + return 0;
> + }
> +
> + switch (proto) {
> + case htons(ETH_P_IP):
> + l3_min = sizeof(struct iphdr);
> + break;
> + case htons(ETH_P_IPV6):
> + l3_min = sizeof(struct ipv6hdr);
> + break;
> + default:
> + return 0;
> + }
> +
> + if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
> + return 0;
> +
> + skb->protocol = eth->h_proto;
> + skb_set_network_header(skb, nhoff);
> + skb_set_transport_header(skb, skb_checksum_start_offset(skb));
I think you can drop skb_set_transport_header(). The core does it in
validate_xmit_skb().
(Sashiko's medium one is IMO correct...)
Björn