aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorsantosh shukla <santosh.shukla@linaro.org>2014-03-19 11:21:17 -0700
committerMaxim Uvarov <maxim.uvarov@linaro.org>2014-03-21 16:21:49 +0400
commit14ce31ed6863142b86616f067bd4534ab2a15b01 (patch)
tree9ccb2ea02ecf45e4572fdb4d17985a52eeefd6f4 /include
parentb594f9a2183ff9422bd62deaa10a6708daa6ed00 (diff)
chksum: move chksum calculation from ipv4 header file
created new chksum file in include/helper directory and added chksum function there. Signed-off-by: santosh shukla <santosh.shukla@linaro.org>
Diffstat (limited to 'include')
-rw-r--r--include/helper/odp_chksum.h53
-rw-r--r--include/helper/odp_ip.h23
2 files changed, 56 insertions, 20 deletions
diff --git a/include/helper/odp_chksum.h b/include/helper/odp_chksum.h
new file mode 100644
index 00000000..12ef61f5
--- /dev/null
+++ b/include/helper/odp_chksum.h
@@ -0,0 +1,53 @@
+/* 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 b13262c4..af6d23ba 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;
}