From 4ea38ff354eb4dad54be3c056b884ff7920135da Mon Sep 17 00:00:00 2001
From: kircher <majun65@huawei.com>
Date: Tue, 27 Jun 2023 11:18:58 +0800
Subject: [PATCH] add CHECKSUM_UDP when not support OFFLOAD_UDP_CHECKSUM

---
 src/core/tcp_in.c          |  2 +-
 src/core/udp.c             | 26 +++++++++++++++++++++++++-
 src/include/dpdk_cksum.h   | 16 ++++++++++------
 src/include/dpdk_version.h |  1 +
 src/include/lwipopts.h     |  8 ++++++--
 5 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c
index 62a6511..5014a21 100644
--- a/src/core/tcp_in.c
+++ b/src/core/tcp_in.c
@@ -210,7 +210,7 @@ tcp_input(struct pbuf *p, struct netif *inp)
 #if CHECKSUM_CHECK_TCP_HW
   u64_t ret;
   if (get_eth_params_rx_ol() & DEV_RX_OFFLOAD_TCP_CKSUM) {
-    ret = is_cksum_tcpbad(p);
+    ret = is_cksum_bad(p);
   } else {
     ret = (u64_t)ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
                                   ip_current_src_addr(), ip_current_dest_addr());
diff --git a/src/core/udp.c b/src/core/udp.c
index 0b1fa65..d9db535 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -412,9 +412,21 @@ udp_input(struct pbuf *p, struct netif *inp)
 #endif /* LWIP_UDPLITE */
       {
         if (udphdr->chksum != 0) {
+#if CHECKSUM_CHECK_UDP_HW
+          u64_t ret = 0;
+          if (get_eth_params_rx_ol() & DEV_RX_OFFLOAD_UDP_CKSUM) {
+            ret = is_cksum_bad(p);
+          } else {
+            ret = ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
+                               ip_current_src_addr(),
+                               ip_current_dest_addr());
+          }
+          if (ret != 0) {
+#else /* CHECKSUM_CHECK_UDP_HW */
           if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
                                ip_current_src_addr(),
                                ip_current_dest_addr()) != 0) {
+#endif /* CHECKSUM_CHECK_UDP_HW */
             goto chkerr;
           }
         }
@@ -970,8 +982,18 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d
         } else
 #endif /* LWIP_CHECKSUM_ON_COPY */
         {
+#if CHECKSUM_GEN_UDP_HW
+          if (get_eth_params_tx_ol() & DEV_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);
+          } else {
+            udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
+                                         src_ip, dst_ip);
+          }
+#else /* CHECKSUM_GEN_UDP_HW */
           udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
                                        src_ip, dst_ip);
+#endif /* CHECKSUM_GEN_UDP_HW */
         }
 
         /* chksum zero must become 0xffff, as zero means 'no checksum' */
@@ -996,7 +1018,9 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d
   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
   /* output to IP */
   NETIF_SET_HINTS(netif, &(pcb->netif_hints));
-  udph_cksum_set(q, UDP_HLEN);
+#if GAZELLE_UDP_ENABLE
+  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 2c5b31e..d092a1d 100644
--- a/src/include/dpdk_cksum.h
+++ b/src/include/dpdk_cksum.h
@@ -54,12 +54,12 @@ static inline u64_t is_cksum_ipbad(struct pbuf *p) {
 }
 #endif /* CHECKSUM_CHECK_IP_HW */
 
-#if CHECKSUM_CHECK_TCP_HW
-// for tcp_input
-static inline u64_t is_cksum_tcpbad(struct pbuf *p) {
+#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);
 }
-#endif /* CHECKSUM_CHECK_TCP_HW */
+#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) {
@@ -77,18 +77,22 @@ static inline void iph_cksum_set(struct pbuf *p, u16_t len, bool do_ipcksum) {
 #endif /* CHECKSUM_GEN_IP_HW */
 
 // replace ip_chksum_pseudo
-#if CHECKSUM_GEN_TCP_HW
+#if (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW)
 #include <rte_ip.h>
 
+#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;
 }
+#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;
 }
+#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)
@@ -109,7 +113,7 @@ static inline u16_t ip_chksum_pseudo_offload(u8_t proto, u16_t proto_len,
 
     return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
 }
-#endif /* CHECKSUM_GEN_TCP_HW */
+#endif /* (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW) */
 
 #endif /* GAZELLE_ENABLE */
 #endif /* __DPDK_CKSUM_H__ */
diff --git a/src/include/dpdk_version.h b/src/include/dpdk_version.h
index 43b254a..c90ddb8 100644
--- a/src/include/dpdk_version.h
+++ b/src/include/dpdk_version.h
@@ -46,6 +46,7 @@
 #define RTE_MBUF_F_TX_IP_CKSUM      PKT_TX_IP_CKSUM 
 #define RTE_MBUF_F_TX_TCP_CKSUM     PKT_TX_TCP_CKSUM
 #define RTE_MBUF_F_TX_TCP_SEG       PKT_TX_TCP_SEG
+#define RTE_MBUF_F_TX_UDP_CKSUM     PKT_TX_UDP_CKSUM
 
 #endif /* DPDK_VERSION_1911 */
 
diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
index 2ba1e4c..5ba123f 100644
--- a/src/include/lwipopts.h
+++ b/src/include/lwipopts.h
@@ -71,18 +71,22 @@
 // rx cksum
 #define CHECKSUM_CHECK_IP     1 /*  master switch */
 #define CHECKSUM_CHECK_TCP    1 /*  master switch */
+#define CHECKSUM_CHECK_UDP    1 /*  master switch */
 // tx cksum
 #define CHECKSUM_GEN_IP       1 /*  master switch */
-#define CHECKSUM_GEN_TCP      1 /* master switch */
+#define CHECKSUM_GEN_TCP      1 /*  master switch */
+#define CHECKSUM_GEN_UDP      1 /*  master switch */
 
 // rx offload cksum
 #define CHECKSUM_CHECK_IP_HW  (1 && CHECKSUM_CHECK_IP) /*  hardware switch */
 #define CHECKSUM_CHECK_TCP_HW (1 && CHECKSUM_CHECK_TCP) /*  hardware switch */
+#define CHECKSUM_CHECK_UDP_HW (1 && CHECKSUM_CHECK_UDP) /*  hardware switch */
 // tx offload cksum
 #define CHECKSUM_GEN_IP_HW    (1 && CHECKSUM_GEN_IP) /* hardware switch */
 #define CHECKSUM_GEN_TCP_HW   (1 && CHECKSUM_GEN_TCP) /*  hardware switch */
+#define CHECKSUM_GEN_UDP_HW   (1 && CHECKSUM_GEN_UDP) /*  hardware switch */
 
-#define CHECKSUM_OFFLOAD_ALL (CHECKSUM_GEN_IP_HW || CHECKSUM_GEN_TCP_HW || CHECKSUM_CHECK_IP_HW || CHECKSUM_CHECK_TCP_HW)
+#define CHECKSUM_OFFLOAD_ALL (CHECKSUM_GEN_IP_HW || CHECKSUM_GEN_TCP_HW || CHECKSUM_CHECK_IP_HW || CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW || CHECKSUM_GEN_UDP_HW)
 
 
 /*
-- 
2.28.0.windows.1