summaryrefslogtreecommitdiffstats
path: root/src/vnet/ip
diff options
context:
space:
mode:
authorSrikanth A <srakula@cisco.com>2019-10-02 17:48:58 -0700
committerJohn Lo <loj@cisco.com>2019-10-10 15:46:18 +0000
commit3642782a2748503f5b5ccf89d1575c1d489948ef (patch)
tree6305f4017d34ef73873d600f8aa0ec083c2bf755 /src/vnet/ip
parent43b36af1fca4298d6c40f139aaf2462dd4747957 (diff)
tcp: custom checksum calculations for Ipv4/Ipv6
Type: feature Based on the configuration, we can disable checksum offload capability and calculate checksum while pushing the TCP & IP header. This saves some cycles when VPP stack is used in legacy hardware devices. Signed-off-by: Srikanth A <srakula@cisco.com> Change-Id: Ic1b3fcf3040917e47ee65263694ebf7437ac5668
Diffstat (limited to 'src/vnet/ip')
-rw-r--r--src/vnet/ip/ip.h79
-rw-r--r--src/vnet/ip/ip4_forward.c46
-rw-r--r--src/vnet/ip/ip6.h3
-rw-r--r--src/vnet/ip/ip6_forward.c69
4 files changed, 97 insertions, 100 deletions
diff --git a/src/vnet/ip/ip.h b/src/vnet/ip/ip.h
index 7a82dcf8f2b..65ccaef40c2 100644
--- a/src/vnet/ip/ip.h
+++ b/src/vnet/ip/ip.h
@@ -195,6 +195,85 @@ ip_incremental_checksum_buffer (vlib_main_t * vm,
return sum;
}
+always_inline u16
+ip_calculate_l4_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
+ ip_csum_t sum0, u32 payload_length,
+ u8 * iph, u32 ip_header_size, u8 * l4h)
+{
+ u16 sum16;
+ u8 *data_this_buffer, length_odd;
+ u32 n_bytes_left, n_this_buffer, n_ip_bytes_this_buffer;
+
+ n_bytes_left = payload_length;
+
+ if (l4h) /* packet l4 header and no buffer chain involved */
+ {
+ ASSERT (p0 == NULL);
+ n_this_buffer = payload_length;
+ data_this_buffer = l4h;
+ }
+ else
+ {
+ ASSERT (p0);
+ if (iph) /* ip header pointer set to packet in buffer */
+ {
+ ASSERT (ip_header_size);
+ n_this_buffer = payload_length;
+ data_this_buffer = iph + ip_header_size; /* at l4 header */
+ n_ip_bytes_this_buffer =
+ p0->current_length - (((u8 *) iph - p0->data) - p0->current_data);
+ if (PREDICT_FALSE (payload_length + ip_header_size >
+ n_ip_bytes_this_buffer))
+ {
+ n_this_buffer = n_ip_bytes_this_buffer - ip_header_size;
+ if (PREDICT_FALSE (n_this_buffer >> 31))
+ { /* error - ip header don't fit this buffer */
+ ASSERT (0);
+ return 0xfefe;
+ }
+ }
+ }
+ else /* packet in buffer with no ip header */
+ { /* buffer current pointer at l4 header */
+ n_this_buffer = p0->current_length;
+ data_this_buffer = vlib_buffer_get_current (p0);
+ }
+ n_this_buffer = clib_min (n_this_buffer, n_bytes_left);
+ }
+
+ while (1)
+ {
+ sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
+ n_bytes_left -= n_this_buffer;
+ if (n_bytes_left == 0)
+ break;
+
+ if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
+ {
+ ASSERT (0); /* error - more buffer expected */
+ return 0xfefe;
+ }
+
+ length_odd = (n_this_buffer & 1);
+
+ p0 = vlib_get_buffer (vm, p0->next_buffer);
+ data_this_buffer = vlib_buffer_get_current (p0);
+ n_this_buffer = clib_min (p0->current_length, n_bytes_left);
+
+ if (PREDICT_FALSE (length_odd))
+ {
+ /* Prepend a 0 byte to maintain 2-byte checksum alignment */
+ data_this_buffer--;
+ n_this_buffer++;
+ n_bytes_left++;
+ data_this_buffer[0] = 0;
+ }
+ }
+
+ sum16 = ~ip_csum_fold (sum0);
+ return sum16;
+}
+
void ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index);
extern vlib_node_registration_t ip4_inacl_node;
diff --git a/src/vnet/ip/ip4_forward.c b/src/vnet/ip/ip4_forward.c
index 2cf84f3114c..dd5f6d031db 100644
--- a/src/vnet/ip/ip4_forward.c
+++ b/src/vnet/ip/ip4_forward.c
@@ -1301,10 +1301,6 @@ ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
{
ip_csum_t sum0;
u32 ip_header_length, payload_length_host_byte_order;
- u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
- u16 sum16;
- u8 *data_this_buffer;
- u8 length_odd;
/* Initialize checksum with ip header. */
ip_header_length = ip4_header_bytes (ip0);
@@ -1327,45 +1323,9 @@ ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
sum0 =
ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u64));
- n_bytes_left = n_this_buffer = payload_length_host_byte_order;
- data_this_buffer = (u8 *) ip0 + ip_header_length;
- n_ip_bytes_this_buffer =
- p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
- if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
- {
- n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
- n_ip_bytes_this_buffer - ip_header_length : 0;
- }
-
- while (1)
- {
- sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
- n_bytes_left -= n_this_buffer;
- if (n_bytes_left == 0)
- break;
-
- ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
- if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
- return 0xfefe;
-
- length_odd = (n_this_buffer & 1);
-
- p0 = vlib_get_buffer (vm, p0->next_buffer);
- data_this_buffer = vlib_buffer_get_current (p0);
- n_this_buffer = clib_min (p0->current_length, n_bytes_left);
-
- if (PREDICT_FALSE (length_odd))
- {
- /* Prepend a 0 or the resulting checksum will be incorrect. */
- data_this_buffer--;
- n_this_buffer++;
- n_bytes_left++;
- data_this_buffer[0] = 0;
- }
- }
-
- sum16 = ~ip_csum_fold (sum0);
- return sum16;
+ return ip_calculate_l4_checksum (vm, p0, sum0,
+ payload_length_host_byte_order, (u8 *) ip0,
+ ip_header_length, NULL);
}
u32
diff --git a/src/vnet/ip/ip6.h b/src/vnet/ip/ip6.h
index 810fd70ff0c..94c5080a0aa 100644
--- a/src/vnet/ip/ip6.h
+++ b/src/vnet/ip/ip6.h
@@ -683,7 +683,8 @@ vlib_buffer_push_ip6 (vlib_main_t * vm, vlib_buffer_t * b,
sizeof (ip6h->src_address));
clib_memcpy_fast (ip6h->dst_address.as_u8, dst->as_u8,
sizeof (ip6h->src_address));
- b->flags |= VNET_BUFFER_F_IS_IP6;
+ vnet_buffer (b)->l3_hdr_offset = (u8 *) ip6h - b->data;
+ b->flags |= VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
return ip6h;
}
diff --git a/src/vnet/ip/ip6_forward.c b/src/vnet/ip/ip6_forward.c
index eb6c89b39d7..a7a41eea419 100644
--- a/src/vnet/ip/ip6_forward.c
+++ b/src/vnet/ip/ip6_forward.c
@@ -1014,11 +1014,10 @@ ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
ip6_header_t * ip0, int *bogus_lengthp)
{
ip_csum_t sum0;
- u16 sum16, payload_length_host_byte_order;
- u32 i, n_this_buffer, n_bytes_left;
+ u16 payload_length_host_byte_order;
+ u32 i;
u32 headers_size = sizeof (ip0[0]);
u8 *data_this_buffer;
- u8 length_odd;
ASSERT (bogus_lengthp);
*bogus_lengthp = 0;
@@ -1030,14 +1029,10 @@ ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
{
- sum0 = ip_csum_with_carry (sum0,
- clib_mem_unaligned (&ip0->
- src_address.as_uword[i],
- uword));
- sum0 =
- ip_csum_with_carry (sum0,
- clib_mem_unaligned (&ip0->dst_address.as_uword[i],
- uword));
+ sum0 = ip_csum_with_carry
+ (sum0, clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
+ sum0 = ip_csum_with_carry
+ (sum0, clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
}
/* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
@@ -1059,52 +1054,14 @@ ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
headers_size += skip_bytes;
}
- n_bytes_left = n_this_buffer = payload_length_host_byte_order;
-
if (p0)
- {
- u32 n_ip_bytes_this_buffer =
- p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
- if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
- {
- n_this_buffer = p0->current_length > headers_size ?
- n_ip_bytes_this_buffer - headers_size : 0;
- }
- }
-
- while (1)
- {
- sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
- n_bytes_left -= n_this_buffer;
- if (n_bytes_left == 0)
- break;
-
- ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
- if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
- {
- *bogus_lengthp = 1;
- return 0xfefe;
- }
-
- length_odd = (n_this_buffer & 1);
-
- p0 = vlib_get_buffer (vm, p0->next_buffer);
- data_this_buffer = vlib_buffer_get_current (p0);
- n_this_buffer = clib_min (p0->current_length, n_bytes_left);
-
- if (PREDICT_FALSE (length_odd))
- {
- /* Prepend a 0 or the resulting checksum will be incorrect. */
- data_this_buffer--;
- n_this_buffer++;
- n_bytes_left++;
- data_this_buffer[0] = 0;
- }
- }
-
- sum16 = ~ip_csum_fold (sum0);
-
- return sum16;
+ return ip_calculate_l4_checksum (vm, p0, sum0,
+ payload_length_host_byte_order,
+ (u8 *) ip0, headers_size, NULL);
+ else
+ return ip_calculate_l4_checksum (vm, 0, sum0,
+ payload_length_host_byte_order, NULL, 0,
+ data_this_buffer);
}
u32