From bb07cd2f26a91c4f5396f2e19a409e4014e7a7e8 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Fri, 19 Jan 2024 20:44:54 +0800
Subject: [PATCH] remove unnecessary variables in struct pbuf

---
 src/core/pbuf.c          |  5 ++--
 src/core/tcp_out.c       |  3 +-
 src/core/udp.c           |  4 +--
 src/include/dpdk_cksum.h | 60 ++++++++++++++++++++--------------------
 src/include/lwip/pbuf.h  | 12 --------
 src/netif/ethernet.c     |  3 +-
 6 files changed, 36 insertions(+), 51 deletions(-)

diff --git a/src/core/pbuf.c b/src/core/pbuf.c
index 61690ff..cb5003a 100644
--- a/src/core/pbuf.c
+++ b/src/core/pbuf.c
@@ -86,6 +86,7 @@
 #endif
 #if GAZELLE_ENABLE
 #include <rte_prefetch.h>
+#include "dpdk_cksum.h"
 #endif
 
 #include <string.h>
@@ -1041,9 +1042,7 @@ pbuf_copy_partial_pbuf(struct pbuf *p_to, const struct pbuf *p_from, u16_t copy_
     }
 
 #if GAZELLE_ENABLE && (CHECKSUM_GEN_IP_HW || CHECKSUM_GEN_TCP_HW)
-    p_to->l2_len = p_from->l2_len;
-    p_to->l3_len = p_from->l3_len;
-    p_to->ol_flags = p_from->ol_flags;
+    pbuf_offload_copy(p_to, p_from);
 #endif
 
     len = (u16_t)LWIP_MIN(copy_len, len_calc);
diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c
index bb03aae..f5e1968 100644
--- a/src/core/tcp_out.c
+++ b/src/core/tcp_out.c
@@ -1941,7 +1941,6 @@ tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif
 #if CHECKSUM_GEN_TCP_HW
   if (netif_get_txol_flags(netif) & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
     tcph_cksum_set(seg->p, TCPH_HDRLEN_BYTES(seg->tcphdr));
-    seg->tcphdr->chksum = ip_chksum_pseudo_offload(IP_PROTO_TCP,seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
   } else {
 #if TCP_CHECKSUM_ON_COPY
     u32_t acc;
@@ -2388,10 +2387,10 @@ tcp_output_control_segment(struct tcp_pcb *pcb, struct pbuf *p,
 #if CHECKSUM_GEN_TCP
     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
       struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
+      tcphdr->chksum = 0;
 #if CHECKSUM_GEN_TCP_HW
     if (netif_get_txol_flags(netif) & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
       tcph_cksum_set(p, TCPH_HDRLEN_BYTES(tcphdr));
-      tcphdr->chksum = ip_chksum_pseudo_offload(IP_PROTO_TCP, p->tot_len, src, dst);
     } else {
       tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
                                         src, dst);
diff --git a/src/core/udp.c b/src/core/udp.c
index 718f9f4..99bdac1 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -985,7 +985,7 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d
 #if CHECKSUM_GEN_UDP_HW
           if (netif_get_txol_flags(netif) & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
             udph_cksum_set(q, UDP_HLEN);
-            udpchksum = ip_chksum_pseudo_offload(IP_PROTO_UDP, q->tot_len, &pcb->local_ip, &pcb->remote_ip);
+            udpchksum = 0;
           } else {
             udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
                                          src_ip, dst_ip);
@@ -1019,7 +1019,7 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d
   /* output to IP */
   NETIF_SET_HINTS(netif, &(pcb->netif_hints));
 #if GAZELLE_UDP_ENABLE
-  q->l4_len = UDP_HLEN;
+  PBUF_TO_MBUF(q)->l4_len = UDP_HLEN;
 #endif /* GAZELLE_UDP_ENABLE */
   err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
   NETIF_RESET_HINTS(netif);
diff --git a/src/include/dpdk_cksum.h b/src/include/dpdk_cksum.h
index 38cfb96..115155d 100644
--- a/src/include/dpdk_cksum.h
+++ b/src/include/dpdk_cksum.h
@@ -45,32 +45,48 @@
 #include "lwip/pbuf.h"
 #endif
 
+#define PBUF_TO_MBUF(p)  ((struct rte_mbuf *)RTE_PTR_SUB(p, sizeof(struct rte_mbuf)))
+
+static inline void pbuf_offload_copy(struct pbuf *to, const struct pbuf *from)
+{
+    PBUF_TO_MBUF(to)->l4_len   = PBUF_TO_MBUF(from)->l4_len;
+    PBUF_TO_MBUF(to)->l3_len   = PBUF_TO_MBUF(from)->l3_len;
+    PBUF_TO_MBUF(to)->l2_len   = PBUF_TO_MBUF(from)->l2_len;
+    PBUF_TO_MBUF(to)->ol_flags = PBUF_TO_MBUF(from)->ol_flags;
+}
+
+static inline void pbuf_set_vlan(struct pbuf *p, u16_t vlan_tci)
+{
+    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_VLAN;
+    PBUF_TO_MBUF(p)->vlan_tci = vlan_tci;
+}
+
 #if CHECKSUM_CHECK_IP_HW
 // for ip4_input
 static inline u64_t is_cksum_ipbad(struct pbuf *p) {
-    return p->ol_flags & (RTE_MBUF_F_RX_IP_CKSUM_BAD);
+    return PBUF_TO_MBUF(p)->ol_flags & (RTE_MBUF_F_RX_IP_CKSUM_BAD);
 }
 #endif /* CHECKSUM_CHECK_IP_HW */
 
 #if (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW)
 // for tcp_input and udp_input
 static inline u64_t is_cksum_bad(struct pbuf *p) {
-    return p->ol_flags & (RTE_MBUF_F_RX_L4_CKSUM_BAD);
+    return PBUF_TO_MBUF(p)->ol_flags & (RTE_MBUF_F_RX_L4_CKSUM_BAD);
 }
 #endif /* (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW) */
 
 #if CHECKSUM_GEN_IP_HW
 static inline void ethh_cksum_set(struct pbuf *p, u16_t len) {
-    p->l2_len = len;
+    PBUF_TO_MBUF(p)->l2_len = len;
 }
 
 // replaces IPH_CHKSUM_SET
 static inline void iph_cksum_set(struct pbuf *p, u16_t len, bool do_ipcksum) {
-    p->ol_flags |= ((len == IP_HLEN) ? RTE_MBUF_F_TX_IPV4 : RTE_MBUF_F_TX_IPV6);
+    PBUF_TO_MBUF(p)->ol_flags |= ((len == IP_HLEN) ? RTE_MBUF_F_TX_IPV4 : RTE_MBUF_F_TX_IPV6);
     if (do_ipcksum) {
-        p->ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
+        PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
     }
-    p->l3_len = len;
+    PBUF_TO_MBUF(p)->l3_len = len;
 }
 #endif /* CHECKSUM_GEN_IP_HW */
 
@@ -80,37 +96,17 @@ static inline void iph_cksum_set(struct pbuf *p, u16_t len, bool do_ipcksum) {
 
 #if CHECKSUM_GEN_TCP_HW
 static inline void tcph_cksum_set(struct pbuf *p, u16_t len) {
-    p->l4_len = len;
-    p->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
+    PBUF_TO_MBUF(p)->l4_len = len;
+    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
 }
 #endif /* CHECKSUM_GEN_TCP_HW */
 
 #if CHECKSUM_GEN_UDP_HW
 static inline void udph_cksum_set(struct pbuf *p, u16_t len) {
-    p->l4_len = len;
-    p->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
+    PBUF_TO_MBUF(p)->l4_len = len;
+    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
 }
 #endif /* CHECKSUM_GEN_UDP_HW */
-
-static inline u16_t ip_chksum_pseudo_offload(u8_t proto, u16_t proto_len,
-                                             const ip_addr_t *src, const ip_addr_t *dst)
-{
-    struct ip_psd_header {
-        ip_addr_t src_addr; /* IP address of source host. */
-        ip_addr_t dst_addr; /* IP address of destination host. */
-        uint8_t  zero;     /* zero. */
-        uint8_t  proto;    /* L4 protocol type. */
-        uint16_t len;      /* L4 length. */
-    } psd_hdr;
-
-    ip_addr_copy(psd_hdr.src_addr, *src);
-    ip_addr_copy(psd_hdr.dst_addr, *dst);
-    psd_hdr.proto = proto;
-    psd_hdr.len = lwip_htons(proto_len);
-    psd_hdr.zero = 0;
-
-    return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
-}
 #endif /* (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW) */
 
 #endif /* GAZELLE_ENABLE */
diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h
index ffdc9fe..99a259c 100644
--- a/src/include/lwip/pbuf.h
+++ b/src/include/lwip/pbuf.h
@@ -225,22 +225,10 @@ struct pbuf {
   u8_t if_idx;
 
 #if GAZELLE_ENABLE && CHECKSUM_OFFLOAD_ALL
-  /** checksum offload ol_flags */
-  u64_t ol_flags;
-  /* < L2 (MAC) Header Length for non-tunneling pkt. */
-  u64_t l2_len:7;
-  /* < L3 (IP) Header Length. */
-  u64_t l3_len:9;
-  /* < L4 (TCP/UDP) Header Length. */
-  u64_t l4_len:8;
-  u8_t header_off;
-  u8_t rexmit;
   volatile u8_t allow_in;
-  u8_t head;
   struct pbuf *last;
   pthread_spinlock_t pbuf_lock;
   struct tcp_pcb *pcb;
-  u16_t vlan_tci;
 #if GAZELLE_UDP_ENABLE
   ip_addr_t addr;
   u16_t port;
diff --git a/src/netif/ethernet.c b/src/netif/ethernet.c
index afd13ad..9fb9357 100644
--- a/src/netif/ethernet.c
+++ b/src/netif/ethernet.c
@@ -290,8 +290,7 @@ ethernet_output(struct netif * netif, struct pbuf * p,
 #else
   if (netif->vlan_enable) {
     if (netif->txol_flags & RTE_ETH_TX_OFFLOAD_VLAN_INSERT) {
-      p->ol_flags |= RTE_MBUF_F_TX_VLAN;
-      p->vlan_tci = netif->vlan_tci;
+      pbuf_set_vlan(p, netif->vlan_tci);
     } else {
       vlan_prio_vid = netif->vlan_tci;
     }
-- 
2.33.0

