aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2020-08-31 19:54:19 +0200
committerBenoƮt Ganne <bganne@cisco.com>2020-09-01 06:56:26 +0000
commit9a79a1ab931c3b5a7ae07d6f0fcfef7c4368a2c4 (patch)
treefc74fb440e5f7aaf64c6c66c0b8c3960c082f360
parentedc816355a999df476074881ae8ed927cad88532 (diff)
ip: improve ip4_header_checksum_is_valid
It is cheaper to include checksum field in calculation and simply compare result with zero. Type: improvement Change-Id: I6f77632c0a4d2f2c632d044d3a5d2fcf2b5bac62 Signed-off-by: Damjan Marion <damarion@cisco.com>
-rw-r--r--src/vnet/ip/ip4_packet.h17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/vnet/ip/ip4_packet.h b/src/vnet/ip/ip4_packet.h
index 6abb6a9ebf0..06530926120 100644
--- a/src/vnet/ip/ip4_packet.h
+++ b/src/vnet/ip/ip4_packet.h
@@ -205,8 +205,8 @@ ip4_next_header (ip4_header_t * i)
WARN_OFF(array-bounds)
/* *INDENT-ON* */
-always_inline u16
-ip4_header_checksum (ip4_header_t * i)
+static_always_inline u16
+ip4_header_checksum_inline (ip4_header_t * i, int with_checksum)
{
int option_len = (i->ip_version_and_header_length & 0xf) - 5;
uword sum = 0;
@@ -215,7 +215,7 @@ ip4_header_checksum (ip4_header_t * i)
sum += iphdr[0];
sum += iphdr[1];
- sum += *(u16 *) (iphdr + 2);
+ sum += with_checksum ? iphdr[2] : *(u16 *) (iphdr + 2);
/* skip checksum */
sum += iphdr[3];
sum += iphdr[4];
@@ -256,7 +256,8 @@ ip4_header_checksum (ip4_header_t * i)
sum += iphdr[2];
sum += iphdr[3];
sum += iphdr[4];
- /* skip checksum */
+ if (with_checksum)
+ sum += iphdr[5];
sum += iphdr[6];
sum += iphdr[7];
sum += iphdr[8];
@@ -309,6 +310,12 @@ ip4_header_checksum (ip4_header_t * i)
WARN_ON(array-bounds)
/* *INDENT-ON* */
+always_inline u16
+ip4_header_checksum (ip4_header_t * i)
+{
+ return ip4_header_checksum_inline (i, /* with_checksum */ 0);
+}
+
always_inline void
ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
{
@@ -376,7 +383,7 @@ ip4_header_get_df (const ip4_header_t * ip4)
static inline uword
ip4_header_checksum_is_valid (ip4_header_t * i)
{
- return i->checksum == ip4_header_checksum (i);
+ return ip4_header_checksum_inline (i, /* with_checksum */ 1) == 0;
}
#define ip4_partial_header_checksum_x1(ip0,sum0) \