diff mbox

[ODP/PATCH] chksum: move chksum calculation from ipv4 header file

Message ID 1395253277-9177-1-git-send-email-santosh.shukla@linaro.org
State Accepted, archived
Headers show

Commit Message

Santosh Shukla March 19, 2014, 6:21 p.m. UTC
From: santosh shukla <santosh.shukla@linaro.org>

created new chksum file in include/helper directory and
added chksum function there.

Signed-off-by: santosh shukla <santosh.shukla@linaro.org>
---
 include/helper/odp_chksum.h |   54 +++++++++++++++++++++++++++++++++++++++++++
 include/helper/odp_ip.h     |   23 +++---------------
 2 files changed, 57 insertions(+), 20 deletions(-)
 create mode 100644 include/helper/odp_chksum.h

Comments

Maxim Uvarov March 21, 2014, 12:25 p.m. UTC | #1
Merged patch + fixed empty line warn by git am.

Maxim.

On 03/19/2014 10:21 PM, Santosh Shukla wrote:
> From: santosh shukla <santosh.shukla@linaro.org>
>
> created new chksum file in include/helper directory and
> added chksum function there.
>
> Signed-off-by: santosh shukla <santosh.shukla@linaro.org>
> ---
>   include/helper/odp_chksum.h |   54 +++++++++++++++++++++++++++++++++++++++++++
>   include/helper/odp_ip.h     |   23 +++---------------
>   2 files changed, 57 insertions(+), 20 deletions(-)
>   create mode 100644 include/helper/odp_chksum.h
>
> diff --git a/include/helper/odp_chksum.h b/include/helper/odp_chksum.h
> new file mode 100644
> index 0000000..89ba8a2
> --- /dev/null
> +++ b/include/helper/odp_chksum.h
> @@ -0,0 +1,54 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:     BSD-3-Clause
> + */
> +
> +
> +/**
> + * @file
> + *
> + * ODP Checksum
> + */
> +#ifndef ODP_CHKSUM_H_
> +#define ODP_CHKSUM_H_
> +
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +#include <odp_std_types.h>
> +
> +/**
> + * Checksum
> + *
> + * @param buffer calculate chksum for buffer
> + * @param len    buffer length
> + *
> + * @return checksum value
> + */
> +static inline uint16_t odp_chksum(void *buffer, int len)
> +{
> +	uint16_t *buf = buffer;
> +	unsigned int sum = 0;
> +	uint16_t result;
> +
> +	for (sum = 0; len > 1; len -= 2)
> +		sum += *buf++;
> +
> +	if (len == 1)
> +		sum += *(unsigned char *)buf;
> +
> +	sum = (sum >> 16) + (sum & 0xFFFF);
> +	sum += (sum >> 16);
> +	result = ~sum;
> +
> +	return result;
> +}
> +
> +#ifdef __cplusplus
> +}
> +#endif
> +
> +#endif
> +
> diff --git a/include/helper/odp_ip.h b/include/helper/odp_ip.h
> index b13262c..af6d23b 100644
> --- a/include/helper/odp_ip.h
> +++ b/include/helper/odp_ip.h
> @@ -21,6 +21,7 @@ extern "C" {
>   #include <odp_align.h>
>   #include <odp_debug.h>
>   #include <odp_byteorder.h>
> +#include "odp_chksum.h"
>   
>   #include <string.h>
>   
> @@ -53,7 +54,6 @@ ODP_ASSERT(sizeof(odp_ipv4hdr_t) == ODP_IPV4HDR_LEN, ODP_IPV4HDR_T__SIZE_ERROR);
>   
>   static inline int odp_ipv4_csum_valid(odp_packet_t pkt)
>   {
> -	int sum = 0;
>   	uint16be_t res = 0;
>   	uint16_t *w;
>   	int nleft = sizeof(odp_ipv4hdr_t);
> @@ -68,21 +68,13 @@ static inline int odp_ipv4_csum_valid(odp_packet_t pkt)
>   	chksum = ip.chksum;
>   	ip.chksum = 0x0;
>   
> -	while (nleft > 1) {
> -		sum += *w++;
> -		nleft -= 2;
> -	}
> -
> -	sum = (sum >> 16) + (sum & 0xFFFF);
> -	sum += sum >> 16;
> -	res = ~sum;
> +	res = odp_chksum(w, nleft);
>   	return (res == chksum) ? 1 : 0;
>   }
>   
>   
>   static inline uint16be_t odp_ipv4_csum_update(odp_packet_t pkt)
>   {
> -	int sum = 0;
>   	uint16be_t res = 0;
>   	uint16_t *w;
>   	odp_ipv4hdr_t *ip;
> @@ -93,16 +85,7 @@ static inline uint16be_t odp_ipv4_csum_update(odp_packet_t pkt)
>   
>   	ip = (odp_ipv4hdr_t *)odp_packet_l3(pkt);
>   	w = (uint16_t *)(void *)ip;
> -	ip->chksum = 0x0;
> -
> -	while (nleft > 1) {
> -		sum += *w++;
> -		nleft -= 2;
> -	}
> -
> -	sum = (sum >> 16) + (sum & 0xFFFF);
> -	sum += sum >> 16;
> -	res = ~sum;
> +	res = odp_chksum(w, nleft);
>   	ip->chksum = res;
>   	return res;
>   }
diff mbox

Patch

diff --git a/include/helper/odp_chksum.h b/include/helper/odp_chksum.h
new file mode 100644
index 0000000..89ba8a2
--- /dev/null
+++ b/include/helper/odp_chksum.h
@@ -0,0 +1,54 @@ 
+/* Copyright (c) 2014, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:     BSD-3-Clause
+ */
+
+
+/**
+ * @file
+ *
+ * ODP Checksum
+ */
+#ifndef ODP_CHKSUM_H_
+#define ODP_CHKSUM_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <odp_std_types.h>
+
+/**
+ * Checksum
+ *
+ * @param buffer calculate chksum for buffer
+ * @param len    buffer length
+ *
+ * @return checksum value
+ */
+static inline uint16_t odp_chksum(void *buffer, int len)
+{
+	uint16_t *buf = buffer;
+	unsigned int sum = 0;
+	uint16_t result;
+
+	for (sum = 0; len > 1; len -= 2)
+		sum += *buf++;
+
+	if (len == 1)
+		sum += *(unsigned char *)buf;
+
+	sum = (sum >> 16) + (sum & 0xFFFF);
+	sum += (sum >> 16);
+	result = ~sum;
+
+	return result;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
diff --git a/include/helper/odp_ip.h b/include/helper/odp_ip.h
index b13262c..af6d23b 100644
--- a/include/helper/odp_ip.h
+++ b/include/helper/odp_ip.h
@@ -21,6 +21,7 @@  extern "C" {
 #include <odp_align.h>
 #include <odp_debug.h>
 #include <odp_byteorder.h>
+#include "odp_chksum.h"
 
 #include <string.h>
 
@@ -53,7 +54,6 @@  ODP_ASSERT(sizeof(odp_ipv4hdr_t) == ODP_IPV4HDR_LEN, ODP_IPV4HDR_T__SIZE_ERROR);
 
 static inline int odp_ipv4_csum_valid(odp_packet_t pkt)
 {
-	int sum = 0;
 	uint16be_t res = 0;
 	uint16_t *w;
 	int nleft = sizeof(odp_ipv4hdr_t);
@@ -68,21 +68,13 @@  static inline int odp_ipv4_csum_valid(odp_packet_t pkt)
 	chksum = ip.chksum;
 	ip.chksum = 0x0;
 
-	while (nleft > 1) {
-		sum += *w++;
-		nleft -= 2;
-	}
-
-	sum = (sum >> 16) + (sum & 0xFFFF);
-	sum += sum >> 16;
-	res = ~sum;
+	res = odp_chksum(w, nleft);
 	return (res == chksum) ? 1 : 0;
 }
 
 
 static inline uint16be_t odp_ipv4_csum_update(odp_packet_t pkt)
 {
-	int sum = 0;
 	uint16be_t res = 0;
 	uint16_t *w;
 	odp_ipv4hdr_t *ip;
@@ -93,16 +85,7 @@  static inline uint16be_t odp_ipv4_csum_update(odp_packet_t pkt)
 
 	ip = (odp_ipv4hdr_t *)odp_packet_l3(pkt);
 	w = (uint16_t *)(void *)ip;
-	ip->chksum = 0x0;
-
-	while (nleft > 1) {
-		sum += *w++;
-		nleft -= 2;
-	}
-
-	sum = (sum >> 16) + (sum & 0xFFFF);
-	sum += sum >> 16;
-	res = ~sum;
+	res = odp_chksum(w, nleft);
 	ip->chksum = res;
 	return res;
 }