From 02833ff3294f4abbd8e3d52b38446e0f8f533ffc Mon Sep 17 00:00:00 2001 From: Srikanth A Date: Wed, 2 Oct 2019 17:48:58 -0700 Subject: 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 Change-Id: Ic1b3fcf3040917e47ee65263694ebf7437ac5668 (cherry picked from commit 3642782a2748503f5b5ccf89d1575c1d489948ef) --- src/vnet/tcp/tcp.h | 3 ++ src/vnet/tcp/tcp_output.c | 116 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 22 deletions(-) (limited to 'src/vnet/tcp') diff --git a/src/vnet/tcp/tcp.h b/src/vnet/tcp/tcp.h index a1b7d4cbd0d..1bf32818171 100644 --- a/src/vnet/tcp/tcp.h +++ b/src/vnet/tcp/tcp.h @@ -120,6 +120,7 @@ extern timer_expiration_handler tcp_timer_retransmit_syn_handler; _(RATE_SAMPLE, "Conn does rate sampling") \ _(TRACK_BURST, "Track burst") \ _(ZERO_RWND_SENT, "Zero RWND sent") \ + _(NO_CSUM_OFFLOAD, "No Checksum Offload") \ typedef enum _tcp_connection_flag_bits { @@ -1233,6 +1234,8 @@ vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq, th->window = wnd; th->checksum = 0; th->urgent_pointer = 0; + vnet_buffer (b)->l4_hdr_offset = (u8 *) th - b->data; + b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID; return th; } diff --git a/src/vnet/tcp/tcp_output.c b/src/vnet/tcp/tcp_output.c index 7ebea37fc43..a7b0e398d36 100644 --- a/src/vnet/tcp/tcp_output.c +++ b/src/vnet/tcp/tcp_output.c @@ -444,6 +444,78 @@ tcp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b) return vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN); } + +/* Compute TCP checksum in software when offloading is disabled for a connection */ +u16 +ip6_tcp_compute_checksum_custom (vlib_main_t * vm, vlib_buffer_t * p0, + ip46_address_t * src, ip46_address_t * dst) +{ + ip_csum_t sum0; + u16 payload_length_host_byte_order; + u32 i; + + /* Initialize checksum with ip header. */ + sum0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0)) + + clib_host_to_net_u16 (IP_PROTOCOL_TCP); + payload_length_host_byte_order = vlib_buffer_length_in_chain (vm, p0); + + for (i = 0; i < ARRAY_LEN (src->ip6.as_uword); i++) + { + sum0 = ip_csum_with_carry + (sum0, clib_mem_unaligned (&src->ip6.as_uword[i], uword)); + sum0 = ip_csum_with_carry + (sum0, clib_mem_unaligned (&dst->ip6.as_uword[i], uword)); + } + + return ip_calculate_l4_checksum (vm, p0, sum0, + payload_length_host_byte_order, NULL, 0, + NULL); +} + +u16 +ip4_tcp_compute_checksum_custom (vlib_main_t * vm, vlib_buffer_t * p0, + ip46_address_t * src, ip46_address_t * dst) +{ + ip_csum_t sum0; + u32 payload_length_host_byte_order; + + payload_length_host_byte_order = vlib_buffer_length_in_chain (vm, p0); + sum0 = + clib_host_to_net_u32 (payload_length_host_byte_order + + (IP_PROTOCOL_TCP << 16)); + + sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&src->ip4, u32)); + sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&dst->ip4, u32)); + + return ip_calculate_l4_checksum (vm, p0, sum0, + payload_length_host_byte_order, NULL, 0, + NULL); +} + +static inline u16 +tcp_compute_checksum (tcp_connection_t * tc, vlib_buffer_t * b) +{ + u16 checksum = 0; + if (PREDICT_FALSE (tc->flags & TCP_CONN_NO_CSUM_OFFLOAD)) + { + tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index); + vlib_main_t *vm = wrk->vm; + + if (tc->c_is_ip4) + checksum = ip4_tcp_compute_checksum_custom + (vm, b, &tc->c_lcl_ip, &tc->c_rmt_ip); + else + checksum = ip6_tcp_compute_checksum_custom + (vm, b, &tc->c_lcl_ip, &tc->c_rmt_ip); + } + else + { + b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; + } + return checksum; +} + + /** * Prepare ACK */ @@ -466,6 +538,9 @@ tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state, tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd); tcp_options_write ((u8 *) (th + 1), snd_opts); + + th->checksum = tcp_compute_checksum (tc, b); + vnet_buffer (b)->tcp.connection_index = tc->c_c_index; if (wnd == 0) @@ -517,6 +592,7 @@ tcp_make_syn (tcp_connection_t * tc, vlib_buffer_t * b) initial_wnd); vnet_buffer (b)->tcp.connection_index = tc->c_c_index; tcp_options_write ((u8 *) (th + 1), &snd_opts); + th->checksum = tcp_compute_checksum (tc, b); } /** @@ -541,6 +617,7 @@ tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b) tcp_options_write ((u8 *) (th + 1), snd_opts); vnet_buffer (b)->tcp.connection_index = tc->c_c_index; + th->checksum = tcp_compute_checksum (tc, b); } always_inline void @@ -786,7 +863,8 @@ tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt, { ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40); ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address, - &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1); + &pkt_ih4->src_address, IP_PROTOCOL_TCP, + (!(tc->flags & TCP_CONN_NO_CSUM_OFFLOAD))); th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4); } else @@ -833,6 +911,7 @@ tcp_send_reset (tcp_connection_t * tc) tc->rcv_nxt, tcp_hdr_opts_len, flags, advertise_wnd); opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts); + th->checksum = tcp_compute_checksum (tc, b); ASSERT (opts_write_len == tc->snd_opts_len); vnet_buffer (b)->tcp.connection_index = tc->c_c_index; tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4); @@ -851,7 +930,8 @@ tcp_push_ip_hdr (tcp_worker_ctx_t * wrk, tcp_connection_t * tc, { ip4_header_t *ih; ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4, - &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1); + &tc->c_rmt_ip4, IP_PROTOCOL_TCP, + (!(tc->flags & TCP_CONN_NO_CSUM_OFFLOAD))); th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih); } else @@ -1082,6 +1162,9 @@ tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b, u32 snd_nxt, tc->bytes_out += data_len; tc->data_segs_out += 1; + + th->checksum = tcp_compute_checksum (tc, b); + TCP_EVT (TCP_EVT_PKTIZE, tc); } @@ -2160,30 +2243,19 @@ always_inline void tcp_output_push_ip (vlib_main_t * vm, vlib_buffer_t * b0, tcp_connection_t * tc0, u8 is_ip4) { - tcp_header_t *th0 = 0; + u8 __clib_unused *ih0; + tcp_header_t __clib_unused *th0 = vlib_buffer_get_current (b0); - th0 = vlib_buffer_get_current (b0); TCP_EVT (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length); + if (is_ip4) - { - vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4, - IP_PROTOCOL_TCP, 1); - b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; - vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data; - th0->checksum = 0; - } + ih0 = vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4, + IP_PROTOCOL_TCP, + (!(tc0->flags & TCP_CONN_NO_CSUM_OFFLOAD))); else - { - ip6_header_t *ih0; - ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6, - &tc0->c_rmt_ip6, IP_PROTOCOL_TCP); - b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; - vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data; - vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data; - b0->flags |= - VNET_BUFFER_F_L3_HDR_OFFSET_VALID | VNET_BUFFER_F_L4_HDR_OFFSET_VALID; - th0->checksum = 0; - } + ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6, &tc0->c_rmt_ip6, + IP_PROTOCOL_TCP); + } always_inline void -- cgit 1.2.3-korg