Skip to contentSkip to conversation

net/mlx5: Correctly set gso_size when LRO is used

PATCH In review [email protected] 4 participants 6 messages Jul 10, 2025 – Jul 15, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mlx5: Correctly set gso_size when LRO is used

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

So, we need to discount the protocol headers from cqe_bcnt so we can actually divide the payload by lro_num_seg to get the real gso_size.

Fixes e586b3b0baee ("net/mlx5: Ethernet Datapath files")
---
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 20 +++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +16 -4
index 84b1ab8233b8..e23bb80b0e0d 100644
@@ -1154,12 +1154,14 @@ static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
 	}
 }
 
-static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
-				 u32 cqe_bcnt)
+static unsigned int mlx5e_lro_update_hdr(struct sk_buff *skb,
+					 struct mlx5_cqe64 *cqe,
+					 u32 cqe_bcnt)
 {
 	struct ethhdr	*eth = (struct ethhdr *)(skb->data);
 	struct tcphdr	*tcp;
 	int network_depth = 0;
+	unsigned int hdrlen;
 	__wsum check;
 	__be16 proto;
 	u16 tot_len;
@@ -1169,11 +1171,14 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
 
 	tot_len = cqe_bcnt - network_depth;
 	ip_p = skb->data + network_depth;
+	hdrlen = network_depth;
 
 	if (proto == htons(ETH_P_IP)) {
 		struct iphdr *ipv4 = ip_p;
 
 		tcp = ip_p + sizeof(struct iphdr);
+		hdrlen += sizeof(struct iphdr);
+
 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
 
 		ipv4->ttl               = cqe->lro.min_ttl;
@@ -1193,6 +1198,8 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
 		struct ipv6hdr *ipv6 = ip_p;
 
 		tcp = ip_p + sizeof(struct ipv6hdr);
+		hdrlen += sizeof(struct ipv6hdr);
+
 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
 
 		ipv6->hop_limit         = cqe->lro.min_ttl;
@@ -1205,6 +1212,10 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
 		tcp->check = tcp_v6_check(payload_len, &ipv6->saddr,
 					  &ipv6->daddr, check);
 	}
+
+	hdrlen += tcp->doff * 4;
+
+	return hdrlen;
 }
 
 static void *mlx5e_shampo_get_packet_hd(struct mlx5e_rq *rq, u16 header_index)
@@ -1561,8 +1572,9 @@ static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
 		mlx5e_macsec_offload_handle_rx_skb(netdev, skb, cqe);
 
 	if (lro_num_seg > 1) {
-		mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
-		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
+		unsigned int hdrlen = mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
+
+		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt - hdrlen, lro_num_seg);
 		/* Subtract one since we already counted this as one
 		 * "regular" packet in mlx5e_complete_rx_cqe()
 		 */
Signature · 1 line
2.49.0
#1 lore raw
Quoted 66 lines · On 10/07/2025 21:26, [email protected] wrote:

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

So, we need to discount the protocol headers from cqe_bcnt so we can actually divide the payload by lro_num_seg to get the real gso_size.

Fixes e586b3b0baee ("net/mlx5: Ethernet Datapath files")
---
  .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 20 +++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +9 -2
index 84b1ab8233b8..e23bb80b0e0d 100644
@@ -1154,12 +1154,14 @@ static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
  	}
  }
 
-static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
-				 u32 cqe_bcnt)
+static unsigned int mlx5e_lro_update_hdr(struct sk_buff *skb,
+					 struct mlx5_cqe64 *cqe,
+					 u32 cqe_bcnt)
  {
  	struct ethhdr	*eth = (struct ethhdr *)(skb->data);
  	struct tcphdr	*tcp;
  	int network_depth = 0;
+	unsigned int hdrlen;
  	__wsum check;
  	__be16 proto;
  	u16 tot_len;
@@ -1169,11 +1171,14 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
 
  	tot_len = cqe_bcnt - network_depth;
  	ip_p = skb->data + network_depth;
+	hdrlen = network_depth;
 
  	if (proto == htons(ETH_P_IP)) {
  		struct iphdr *ipv4 = ip_p;
 
  		tcp = ip_p + sizeof(struct iphdr);
+		hdrlen += sizeof(struct iphdr);
+
  		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
 
  		ipv4->ttl               = cqe->lro.min_ttl;
@@ -1193,6 +1198,8 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
  		struct ipv6hdr *ipv6 = ip_p;
 
  		tcp = ip_p + sizeof(struct ipv6hdr);
+		hdrlen += sizeof(struct ipv6hdr);
+
  		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
 
  		ipv6->hop_limit         = cqe->lro.min_ttl;

drivers/net/ethernet/mellanox/mlx5/core/en_rx.c static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe, · line 1217

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +3 -0
@@ -1205,6 +1212,10 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
  		tcp->check = tcp_v6_check(payload_len, &ipv6->saddr,
  					  &ipv6->daddr, check);
  	}
+
+	hdrlen += tcp->doff * 4;
+

Thanks for your patch!

Calculations seem correct.
Wouldn't it be simpler to just return the below?

(void *)tcp + tcp->doff * 4 - skb->data

Quoted 16 lines
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +4 -2
+	return hdrlen;
  }
 
  static void *mlx5e_shampo_get_packet_hd(struct mlx5e_rq *rq, u16 header_index)
@@ -1561,8 +1572,9 @@ static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
  		mlx5e_macsec_offload_handle_rx_skb(netdev, skb, cqe);
 
  	if (lro_num_seg > 1) {
-		mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
-		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
+		unsigned int hdrlen = mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
+
+		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt - hdrlen, lro_num_seg);
  		/* Subtract one since we already counted this as one
  		 * "regular" packet in mlx5e_complete_rx_cqe()
  		 */
#2 lore raw
Quoted 80 lines · Tariq Toukan
Quoted 66 lines · On 10/07/2025 21:26, [email protected] wrote:

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

So, we need to discount the protocol headers from cqe_bcnt so we can actually divide the payload by lro_num_seg to get the real gso_size.

Fixes e586b3b0baee ("net/mlx5: Ethernet Datapath files")
---
  .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 20 +++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +9 -2
index 84b1ab8233b8..e23bb80b0e0d 100644
@@ -1154,12 +1154,14 @@ static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
      }
  }
 
-static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
-                              u32 cqe_bcnt)
+static unsigned int mlx5e_lro_update_hdr(struct sk_buff *skb,
+                                      struct mlx5_cqe64 *cqe,
+                                      u32 cqe_bcnt)
  {
      struct ethhdr   *eth = (struct ethhdr *)(skb->data);
      struct tcphdr   *tcp;
      int network_depth = 0;
+     unsigned int hdrlen;
      __wsum check;
      __be16 proto;
      u16 tot_len;
@@ -1169,11 +1171,14 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
 
      tot_len = cqe_bcnt - network_depth;
      ip_p = skb->data + network_depth;
+     hdrlen = network_depth;
 
      if (proto == htons(ETH_P_IP)) {
              struct iphdr *ipv4 = ip_p;
 
              tcp = ip_p + sizeof(struct iphdr);
+             hdrlen += sizeof(struct iphdr);
+
              skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
 
              ipv4->ttl               = cqe->lro.min_ttl;
@@ -1193,6 +1198,8 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
              struct ipv6hdr *ipv6 = ip_p;
 
              tcp = ip_p + sizeof(struct ipv6hdr);
+             hdrlen += sizeof(struct ipv6hdr);
+
              skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
 
              ipv6->hop_limit         = cqe->lro.min_ttl;

drivers/net/ethernet/mellanox/mlx5/core/en_rx.c static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe, · line 1217

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +3 -0
@@ -1205,6 +1212,10 @@ static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
              tcp->check = tcp_v6_check(payload_len, &ipv6->saddr,
                                        &ipv6->daddr, check);
      }
+
+     hdrlen += tcp->doff * 4;
+

Thanks for your patch!

Quoted 4 lines

Calculations seem correct.
Wouldn't it be simpler to just return the below?

(void *)tcp + tcp->doff * 4 - skb->data

Absolutely! I can do that!

Christoph

Quoted 19 lines
Quoted 17 lines
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +4 -2
+     return hdrlen;
  }
 
  static void *mlx5e_shampo_get_packet_hd(struct mlx5e_rq *rq, u16 header_index)
@@ -1561,8 +1572,9 @@ static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
              mlx5e_macsec_offload_handle_rx_skb(netdev, skb, cqe);
 
      if (lro_num_seg > 1) {
-             mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
-             skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
+             unsigned int hdrlen = mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
+
+             skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt - hdrlen, lro_num_seg);
              /* Subtract one since we already counted this as one
               * "regular" packet in mlx5e_complete_rx_cqe()
               */
#3 lore raw
In reply to [email protected]

Hi Christoph,

Quoted 10 lines · On 10/07/2025 21:26, [email protected] wrote:

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

Other than introspecting the wrong gso_size value, is there a functional breakage that can be observed?

#4 lore raw
Quoted 11 lines · Gal Pressman

Hi Christoph,

Quoted 7 lines · On 10/07/2025 21:26, [email protected] wrote:

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

Quoted 5 lines
Quoted 3 lines

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

Other than introspecting the wrong gso_size value, is there a functional breakage that can be observed?

I wouldn't call it "functional breakage", but definitely unintended consequences / lower perf :
- In tcp_measure_rcv_mss() len will be for example 1450, but. rcv_mss will be 1448 (because tp->advmss is 1448). Thus, we will always recompute scaling_ratio each time an LRO-packet is received.
- In tcp_gro_receive, it will interfere with the decision whether or not to flush and thus potentially result in less gro'ed packets.

Christoph

#5 lore raw
Quoted 18 lines · Christoph Paasch
Quoted 11 lines · Gal Pressman

Hi Christoph,

Quoted 7 lines · On 10/07/2025 21:26, [email protected] wrote:

From: Christoph Paasch <[email protected]>

gso_size is expected by the networking stack to be the size of the payload (thus, not including ethernet/IP/TCP-headers). However, cqe_bcnt is the full sized frame (including the headers). Dividing cqe_bcnt by lro_num_seg will then give incorrect results.

Quoted 5 lines
Quoted 3 lines

For example, running a bpftrace higher up in the TCP-stack (tcp_event_data_recv), we commonly have gso_size set to 1450 or 1451 even though in reality the payload was only 1448 bytes.

Other than introspecting the wrong gso_size value, is there a functional breakage that can be observed?

Quoted 7 lines

I wouldn't call it "functional breakage", but definitely unintended consequences / lower perf :
- In tcp_measure_rcv_mss() len will be for example 1450, but. rcv_mss will be 1448 (because tp->advmss is 1448). Thus, we will always recompute scaling_ratio each time an LRO-packet is received.
- In tcp_gro_receive, it will interfere with the decision whether or not to flush and thus potentially result in less gro'ed packets.

Thanks!
Please put that in the commit message.