diff mbox

Use odp_chksum to compute checksum of udp header and data

Message ID 1399895263-20162-1-git-send-email-weilong.chen@linaro.org
State Superseded
Headers show

Commit Message

Weilong Chen May 12, 2014, 11:47 a.m. UTC
Signed-off-by: Weilong Chen <weilong.chen@linaro.org>
---
 include/helper/odp_udp.h |   32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

Comments

Santosh Shukla May 12, 2014, 1:20 p.m. UTC | #1
+1.

Thanks.

On 12 May 2014 17:17, Weilong Chen <weilong.chen@linaro.org> wrote:
>
> Signed-off-by: Weilong Chen <weilong.chen@linaro.org>
> ---
>  include/helper/odp_udp.h |   32 +++++++++++++++++++++-----------
>  1 file changed, 21 insertions(+), 11 deletions(-)
>
> diff --git a/include/helper/odp_udp.h b/include/helper/odp_udp.h
> index 28313ab..c984dc0 100644
> --- a/include/helper/odp_udp.h
> +++ b/include/helper/odp_udp.h
> @@ -33,6 +33,14 @@ typedef struct ODP_PACKED {
>         uint16be_t chksum;   /**< UDP header and data checksum (0 if not used)*/
>  } odp_udphdr_t;
>
> +/** UDP pseudo header */
> +typedef struct ODP_PACKET {
> +       uint32be_t src_addr;
> +       uint32be_t dst_addr;
> +       uint8_t pad;
> +       uint8_t proto;
> +       uint16be_t length;
> +} odp_udpphdr_t;
>  /**
>   * UDP checksum
>   *
> @@ -43,7 +51,8 @@ typedef struct ODP_PACKED {
>   */
>  static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
>  {
> -       uint32_t sum = 0;
> +       uint32_t sum;
> +       odp_udpphdr_t phdr;
>         odp_udphdr_t *udph;
>         odp_ipv4hdr_t *iph;
>         uint16_t udplen;
> @@ -59,23 +68,24 @@ static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
>         udplen = odp_be_to_cpu_16(udph->length);
>
>         /* the source ip */
> -       sum += (iph->src_addr >> 16) & 0xFFFF;
> -       sum += (iph->src_addr) & 0xFFFF;
> +       phdr.src_addr = iph->src_addr;
>         /* the dest ip */
> -       sum += (iph->dst_addr >> 16) & 0xFFFF;
> -       sum += (iph->dst_addr) & 0xFFFF;
> +       phdr.dst_addr = iph->dst_addr;
>         /* proto */
> -       sum += odp_cpu_to_be_16(ODP_IPPROTO_UDP);
> +       phdr.pad = 0;
> +       phdr.proto = ODP_IPPROTO_UDP;
>         /* the length */
> -       sum += udph->length;
> +       phdr.length = udph->length;
> +
> +       sum = odp_chksum(&phdr, sizeof(odp_udpphdr_t));
> +       /* calc udp header and data chksum */
> +       sum += odp_chksum(udph, udplen);
> +
>         /* Fold sum to 16 bits: add carrier to result */
>         while ((sum >> 16) != 0)
>                 sum = (sum & 0xFFFF) + (sum >> 16);
>
> -       /* calc udp header and data chksum */
> -       sum += ~(odp_chksum(udph, udplen));
> -
> -       return (uint16_t)(~sum);
> +       return (uint16_t)sum;
>  }
>
>  /** @internal Compile time assert */
> --
> 1.7.9.5
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/lng-odp
Maxim Uvarov May 14, 2014, 11:28 a.m. UTC | #2
sha1 is broken:

Applying: Use odp_chksum to compute checksum of udp header and data
fatal: sha1 information is lacking or useless (include/helper/odp_udp.h).

did you change patch manually after creation?

Best regards,
Maxim.

On 05/12/2014 03:47 PM, Weilong Chen wrote:
> Signed-off-by: Weilong Chen <weilong.chen@linaro.org>
> ---
>   include/helper/odp_udp.h |   32 +++++++++++++++++++++-----------
>   1 file changed, 21 insertions(+), 11 deletions(-)
>
> diff --git a/include/helper/odp_udp.h b/include/helper/odp_udp.h
> index 28313ab..c984dc0 100644
> --- a/include/helper/odp_udp.h
> +++ b/include/helper/odp_udp.h
> @@ -33,6 +33,14 @@ typedef struct ODP_PACKED {
>   	uint16be_t chksum;   /**< UDP header and data checksum (0 if not used)*/
>   } odp_udphdr_t;
>   
> +/** UDP pseudo header */
> +typedef struct ODP_PACKET {
> +	uint32be_t src_addr;
> +	uint32be_t dst_addr;
> +	uint8_t pad;
> +	uint8_t proto;
> +	uint16be_t length;
> +} odp_udpphdr_t;
>   /**
>    * UDP checksum
>    *
> @@ -43,7 +51,8 @@ typedef struct ODP_PACKED {
>    */
>   static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
>   {
> -	uint32_t sum = 0;
> +	uint32_t sum;
> +	odp_udpphdr_t phdr;
>   	odp_udphdr_t *udph;
>   	odp_ipv4hdr_t *iph;
>   	uint16_t udplen;
> @@ -59,23 +68,24 @@ static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
>   	udplen = odp_be_to_cpu_16(udph->length);
>   
>   	/* the source ip */
> -	sum += (iph->src_addr >> 16) & 0xFFFF;
> -	sum += (iph->src_addr) & 0xFFFF;
> +	phdr.src_addr = iph->src_addr;
>   	/* the dest ip */
> -	sum += (iph->dst_addr >> 16) & 0xFFFF;
> -	sum += (iph->dst_addr) & 0xFFFF;
> +	phdr.dst_addr = iph->dst_addr;
>   	/* proto */
> -	sum += odp_cpu_to_be_16(ODP_IPPROTO_UDP);
> +	phdr.pad = 0;
> +	phdr.proto = ODP_IPPROTO_UDP;
>   	/* the length */
> -	sum += udph->length;
> +	phdr.length = udph->length;
> +
> +	sum = odp_chksum(&phdr, sizeof(odp_udpphdr_t));
> +	/* calc udp header and data chksum */
> +	sum += odp_chksum(udph, udplen);
> +
>   	/* Fold sum to 16 bits: add carrier to result */
>   	while ((sum >> 16) != 0)
>   		sum = (sum & 0xFFFF) + (sum >> 16);
>   
> -	/* calc udp header and data chksum */
> -	sum += ~(odp_chksum(udph, udplen));
> -
> -	return (uint16_t)(~sum);
> +	return (uint16_t)sum;
>   }
>   
>   /** @internal Compile time assert */
diff mbox

Patch

diff --git a/include/helper/odp_udp.h b/include/helper/odp_udp.h
index 28313ab..c984dc0 100644
--- a/include/helper/odp_udp.h
+++ b/include/helper/odp_udp.h
@@ -33,6 +33,14 @@  typedef struct ODP_PACKED {
 	uint16be_t chksum;   /**< UDP header and data checksum (0 if not used)*/
 } odp_udphdr_t;
 
+/** UDP pseudo header */
+typedef struct ODP_PACKET {
+	uint32be_t src_addr;
+	uint32be_t dst_addr;
+	uint8_t pad;
+	uint8_t proto;
+	uint16be_t length;
+} odp_udpphdr_t;
 /**
  * UDP checksum
  *
@@ -43,7 +51,8 @@  typedef struct ODP_PACKED {
  */
 static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
 {
-	uint32_t sum = 0;
+	uint32_t sum;
+	odp_udpphdr_t phdr;
 	odp_udphdr_t *udph;
 	odp_ipv4hdr_t *iph;
 	uint16_t udplen;
@@ -59,23 +68,24 @@  static inline uint16_t odp_ipv4_udp_chksum(odp_packet_t pkt)
 	udplen = odp_be_to_cpu_16(udph->length);
 
 	/* the source ip */
-	sum += (iph->src_addr >> 16) & 0xFFFF;
-	sum += (iph->src_addr) & 0xFFFF;
+	phdr.src_addr = iph->src_addr;
 	/* the dest ip */
-	sum += (iph->dst_addr >> 16) & 0xFFFF;
-	sum += (iph->dst_addr) & 0xFFFF;
+	phdr.dst_addr = iph->dst_addr;
 	/* proto */
-	sum += odp_cpu_to_be_16(ODP_IPPROTO_UDP);
+	phdr.pad = 0;
+	phdr.proto = ODP_IPPROTO_UDP;
 	/* the length */
-	sum += udph->length;
+	phdr.length = udph->length;
+
+	sum = odp_chksum(&phdr, sizeof(odp_udpphdr_t));
+	/* calc udp header and data chksum */
+	sum += odp_chksum(udph, udplen);
+
 	/* Fold sum to 16 bits: add carrier to result */
 	while ((sum >> 16) != 0)
 		sum = (sum & 0xFFFF) + (sum >> 16);
 
-	/* calc udp header and data chksum */
-	sum += ~(odp_chksum(udph, udplen));
-
-	return (uint16_t)(~sum);
+	return (uint16_t)sum;
 }
 
 /** @internal Compile time assert */