2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/tcp/tcp.h>
19 typedef enum _tcp_output_next
22 TCP_OUTPUT_NEXT_IP_LOOKUP,
23 TCP_OUTPUT_NEXT_IP_REWRITE,
24 TCP_OUTPUT_NEXT_IP_ARP,
28 #define foreach_tcp4_output_next \
29 _ (DROP, "error-drop") \
30 _ (IP_LOOKUP, "ip4-lookup") \
31 _ (IP_REWRITE, "ip4-rewrite") \
34 #define foreach_tcp6_output_next \
35 _ (DROP, "error-drop") \
36 _ (IP_LOOKUP, "ip6-lookup") \
37 _ (IP_REWRITE, "ip6-rewrite") \
38 _ (IP_ARP, "ip6-discover-neighbor")
40 static char *tcp_error_strings[] = {
41 #define tcp_error(n,s) s,
42 #include <vnet/tcp/tcp_error.def>
48 tcp_header_t tcp_header;
49 tcp_connection_t tcp_connection;
53 format_tcp_tx_trace (u8 * s, va_list * args)
55 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57 tcp_tx_trace_t *t = va_arg (*args, tcp_tx_trace_t *);
58 tcp_connection_t *tc = &t->tcp_connection;
59 u32 indent = format_get_indent (s);
61 s = format (s, "%U state %U\n%U%U", format_tcp_connection_id, tc,
62 format_tcp_state, tc->state, format_white_space, indent,
63 format_tcp_header, &t->tcp_header, 128);
68 #ifndef CLIB_MARCH_VARIANT
70 tcp_window_compute_scale (u32 window)
73 while (wnd_scale < TCP_MAX_WND_SCALE && (window >> wnd_scale) > TCP_WND_MAX)
79 * TCP's initial window
82 tcp_initial_wnd_unscaled (tcp_connection_t * tc)
84 /* RFC 6928 recommends the value lower. However at the time our connections
85 * are initialized, fifos may not be allocated. Therefore, advertise the
86 * smallest possible unscaled window size and update once fifos are
87 * assigned to the session.
90 tcp_update_rcv_mss (tc);
91 TCP_IW_N_SEGMENTS * tc->mss;
93 return tcp_cfg.min_rx_fifo;
97 * Compute initial window and scale factor. As per RFC1323, window field in
98 * SYN and SYN-ACK segments is never scaled.
101 tcp_initial_window_to_advertise (tcp_connection_t * tc)
103 /* Compute rcv wscale only if peer advertised support for it */
104 if (tc->state != TCP_STATE_SYN_RCVD || tcp_opts_wscale (&tc->rcv_opts))
105 tc->rcv_wscale = tcp_window_compute_scale (tcp_cfg.max_rx_fifo);
107 tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
109 return clib_min (tc->rcv_wnd, TCP_WND_MAX);
113 tcp_update_rcv_wnd (tcp_connection_t * tc)
115 u32 available_space, wnd;
119 * Figure out how much space we have available
121 available_space = transport_max_rx_enqueue (&tc->connection);
122 if (PREDICT_FALSE (available_space < tc->rcv_opts.mss))
129 * Use the above and what we know about what we've previously advertised
130 * to compute the new window
132 observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
134 /* Bad. Thou shalt not shrink */
135 if (PREDICT_FALSE ((i32) available_space < observed_wnd))
137 wnd = clib_max (observed_wnd, 0);
138 TCP_EVT (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
142 wnd = available_space;
145 /* Make sure we have a multiple of rcv_wscale */
146 if (wnd && tc->rcv_wscale)
148 wnd &= ~((1 << tc->rcv_wscale) - 1);
150 wnd = 1 << tc->rcv_wscale;
153 tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale);
157 * Compute and return window to advertise, scaled as per RFC1323
160 tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state)
162 if (state < TCP_STATE_ESTABLISHED)
163 return tcp_initial_window_to_advertise (tc);
165 tcp_update_rcv_wnd (tc);
166 return tc->rcv_wnd >> tc->rcv_wscale;
170 * Write TCP options to segment.
173 tcp_options_write (u8 * data, tcp_options_t * opts)
176 u32 buf, seq_len = 4;
178 if (tcp_opts_mss (opts))
180 *data++ = TCP_OPTION_MSS;
181 *data++ = TCP_OPTION_LEN_MSS;
182 buf = clib_host_to_net_u16 (opts->mss);
183 clib_memcpy_fast (data, &buf, sizeof (opts->mss));
184 data += sizeof (opts->mss);
185 opts_len += TCP_OPTION_LEN_MSS;
188 if (tcp_opts_wscale (opts))
190 *data++ = TCP_OPTION_WINDOW_SCALE;
191 *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
192 *data++ = opts->wscale;
193 opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
196 if (tcp_opts_sack_permitted (opts))
198 *data++ = TCP_OPTION_SACK_PERMITTED;
199 *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
200 opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
203 if (tcp_opts_tstamp (opts))
205 *data++ = TCP_OPTION_TIMESTAMP;
206 *data++ = TCP_OPTION_LEN_TIMESTAMP;
207 buf = clib_host_to_net_u32 (opts->tsval);
208 clib_memcpy_fast (data, &buf, sizeof (opts->tsval));
209 data += sizeof (opts->tsval);
210 buf = clib_host_to_net_u32 (opts->tsecr);
211 clib_memcpy_fast (data, &buf, sizeof (opts->tsecr));
212 data += sizeof (opts->tsecr);
213 opts_len += TCP_OPTION_LEN_TIMESTAMP;
216 if (tcp_opts_sack (opts))
220 if (opts->n_sack_blocks != 0)
222 *data++ = TCP_OPTION_SACK_BLOCK;
223 *data++ = 2 + opts->n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
224 for (i = 0; i < opts->n_sack_blocks; i++)
226 buf = clib_host_to_net_u32 (opts->sacks[i].start);
227 clib_memcpy_fast (data, &buf, seq_len);
229 buf = clib_host_to_net_u32 (opts->sacks[i].end);
230 clib_memcpy_fast (data, &buf, seq_len);
233 opts_len += 2 + opts->n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
237 /* Terminate TCP options */
240 *data++ = TCP_OPTION_EOL;
241 opts_len += TCP_OPTION_LEN_EOL;
244 /* Pad with zeroes to a u32 boundary */
247 *data++ = TCP_OPTION_NOOP;
248 opts_len += TCP_OPTION_LEN_NOOP;
254 tcp_make_syn_options (tcp_connection_t * tc, tcp_options_t * opts)
258 opts->flags |= TCP_OPTS_FLAG_MSS;
260 len += TCP_OPTION_LEN_MSS;
262 opts->flags |= TCP_OPTS_FLAG_WSCALE;
263 opts->wscale = tc->rcv_wscale;
264 len += TCP_OPTION_LEN_WINDOW_SCALE;
266 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
267 opts->tsval = tcp_time_now ();
269 len += TCP_OPTION_LEN_TIMESTAMP;
273 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
274 len += TCP_OPTION_LEN_SACK_PERMITTED;
277 /* Align to needed boundary */
278 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
283 tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts)
287 opts->flags |= TCP_OPTS_FLAG_MSS;
289 len += TCP_OPTION_LEN_MSS;
291 if (tcp_opts_wscale (&tc->rcv_opts))
293 opts->flags |= TCP_OPTS_FLAG_WSCALE;
294 opts->wscale = tc->rcv_wscale;
295 len += TCP_OPTION_LEN_WINDOW_SCALE;
298 if (tcp_opts_tstamp (&tc->rcv_opts))
300 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
301 opts->tsval = tcp_time_now ();
302 opts->tsecr = tc->tsval_recent;
303 len += TCP_OPTION_LEN_TIMESTAMP;
306 if (tcp_opts_sack_permitted (&tc->rcv_opts))
308 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
309 len += TCP_OPTION_LEN_SACK_PERMITTED;
312 /* Align to needed boundary */
313 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
318 tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts)
324 if (tcp_opts_tstamp (&tc->rcv_opts))
326 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
327 opts->tsval = tcp_tstamp (tc);
328 opts->tsecr = tc->tsval_recent;
329 len += TCP_OPTION_LEN_TIMESTAMP;
331 if (tcp_opts_sack_permitted (&tc->rcv_opts))
333 if (vec_len (tc->snd_sacks))
335 opts->flags |= TCP_OPTS_FLAG_SACK;
336 if (tc->snd_sack_pos >= vec_len (tc->snd_sacks))
337 tc->snd_sack_pos = 0;
338 opts->sacks = &tc->snd_sacks[tc->snd_sack_pos];
339 opts->n_sack_blocks = vec_len (tc->snd_sacks) - tc->snd_sack_pos;
340 opts->n_sack_blocks = clib_min (opts->n_sack_blocks,
341 TCP_OPTS_MAX_SACK_BLOCKS);
342 tc->snd_sack_pos += opts->n_sack_blocks;
343 len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
347 /* Align to needed boundary */
348 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
353 tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
358 case TCP_STATE_ESTABLISHED:
359 case TCP_STATE_CLOSE_WAIT:
360 case TCP_STATE_FIN_WAIT_1:
361 case TCP_STATE_LAST_ACK:
362 case TCP_STATE_CLOSING:
363 case TCP_STATE_FIN_WAIT_2:
364 case TCP_STATE_TIME_WAIT:
365 case TCP_STATE_CLOSED:
366 return tcp_make_established_options (tc, opts);
367 case TCP_STATE_SYN_RCVD:
368 return tcp_make_synack_options (tc, opts);
369 case TCP_STATE_SYN_SENT:
370 return tcp_make_syn_options (tc, opts);
372 clib_warning ("State not handled! %d", state);
378 * Update burst send vars
380 * - Updates snd_mss to reflect the effective segment size that we can send
381 * by taking into account all TCP options, including SACKs.
382 * - Cache 'on the wire' options for reuse
383 * - Updates receive window which can be reused for a burst.
385 * This should *only* be called when doing bursts
388 tcp_update_burst_snd_vars (tcp_connection_t * tc)
390 tcp_main_t *tm = &tcp_main;
392 /* Compute options to be used for connection. These may be reused when
393 * sending data or to compute the effective mss (snd_mss) */
394 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts,
395 TCP_STATE_ESTABLISHED);
397 /* XXX check if MTU has been updated */
398 tc->snd_mss = clib_min (tc->mss, tc->rcv_opts.mss) - tc->snd_opts_len;
399 ASSERT (tc->snd_mss > 0);
401 tcp_options_write (tm->wrk_ctx[tc->c_thread_index].cached_opts,
404 tcp_update_rcv_wnd (tc);
406 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
407 tcp_bt_check_app_limited (tc);
409 if (tc->snd_una == tc->snd_nxt)
411 tcp_cc_event (tc, TCP_CC_EVT_START_TX);
412 tcp_connection_tx_pacer_reset (tc, tc->cwnd, TRANSPORT_PACER_MIN_BURST);
416 #endif /* CLIB_MARCH_VARIANT */
419 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
421 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
422 vlib_buffer_free_one (vm, b->next_buffer);
423 /* Zero all flags but free list index and trace flag */
424 b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
426 b->current_length = 0;
427 b->total_length_not_including_first_buffer = 0;
428 vnet_buffer (b)->tcp.flags = 0;
430 /* Leave enough space for headers */
431 return vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
434 #ifndef CLIB_MARCH_VARIANT
436 tcp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
438 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
439 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
440 b->total_length_not_including_first_buffer = 0;
442 vnet_buffer (b)->tcp.flags = 0;
443 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
444 /* Leave enough space for headers */
445 return vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
449 /* Compute TCP checksum in software when offloading is disabled for a connection */
451 ip6_tcp_compute_checksum_custom (vlib_main_t * vm, vlib_buffer_t * p0,
452 ip46_address_t * src, ip46_address_t * dst)
455 u16 payload_length_host_byte_order;
458 /* Initialize checksum with ip header. */
459 sum0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0)) +
460 clib_host_to_net_u16 (IP_PROTOCOL_TCP);
461 payload_length_host_byte_order = vlib_buffer_length_in_chain (vm, p0);
463 for (i = 0; i < ARRAY_LEN (src->ip6.as_uword); i++)
465 sum0 = ip_csum_with_carry
466 (sum0, clib_mem_unaligned (&src->ip6.as_uword[i], uword));
467 sum0 = ip_csum_with_carry
468 (sum0, clib_mem_unaligned (&dst->ip6.as_uword[i], uword));
471 return ip_calculate_l4_checksum (vm, p0, sum0,
472 payload_length_host_byte_order, NULL, 0,
477 ip4_tcp_compute_checksum_custom (vlib_main_t * vm, vlib_buffer_t * p0,
478 ip46_address_t * src, ip46_address_t * dst)
481 u32 payload_length_host_byte_order;
483 payload_length_host_byte_order = vlib_buffer_length_in_chain (vm, p0);
485 clib_host_to_net_u32 (payload_length_host_byte_order +
486 (IP_PROTOCOL_TCP << 16));
488 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&src->ip4, u32));
489 sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&dst->ip4, u32));
491 return ip_calculate_l4_checksum (vm, p0, sum0,
492 payload_length_host_byte_order, NULL, 0,
497 tcp_compute_checksum (tcp_connection_t * tc, vlib_buffer_t * b)
500 if (PREDICT_FALSE (tc->cfg_flags & TCP_CFG_F_NO_CSUM_OFFLOAD))
502 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
503 vlib_main_t *vm = wrk->vm;
506 checksum = ip4_tcp_compute_checksum_custom
507 (vm, b, &tc->c_lcl_ip, &tc->c_rmt_ip);
509 checksum = ip6_tcp_compute_checksum_custom
510 (vm, b, &tc->c_lcl_ip, &tc->c_rmt_ip);
514 b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
523 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
526 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
527 u8 tcp_opts_len, tcp_hdr_opts_len;
531 wnd = tcp_window_to_advertise (tc, state);
533 /* Make and write options */
534 tcp_opts_len = tcp_make_established_options (tc, snd_opts);
535 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
537 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
538 tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
540 tcp_options_write ((u8 *) (th + 1), snd_opts);
542 th->checksum = tcp_compute_checksum (tc, b);
544 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
547 tcp_zero_rwnd_sent_on (tc);
549 tcp_zero_rwnd_sent_off (tc);
553 * Convert buffer to ACK
556 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
558 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
559 TCP_EVT (TCP_EVT_ACK_SENT, tc);
560 tc->rcv_las = tc->rcv_nxt;
564 * Convert buffer to FIN-ACK
567 tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b)
569 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK);
573 * Convert buffer to SYN
576 tcp_make_syn (tcp_connection_t * tc, vlib_buffer_t * b)
578 u8 tcp_hdr_opts_len, tcp_opts_len;
581 tcp_options_t snd_opts;
583 initial_wnd = tcp_initial_window_to_advertise (tc);
585 /* Make and write options */
586 clib_memset (&snd_opts, 0, sizeof (snd_opts));
587 tcp_opts_len = tcp_make_syn_options (tc, &snd_opts);
588 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
590 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
591 tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
593 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
594 tcp_options_write ((u8 *) (th + 1), &snd_opts);
595 th->checksum = tcp_compute_checksum (tc, b);
599 * Convert buffer to SYN-ACK
602 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
604 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
605 u8 tcp_opts_len, tcp_hdr_opts_len;
609 clib_memset (snd_opts, 0, sizeof (*snd_opts));
610 initial_wnd = tcp_initial_window_to_advertise (tc);
611 tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
612 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
614 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
615 tc->rcv_nxt, tcp_hdr_opts_len,
616 TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
617 tcp_options_write ((u8 *) (th + 1), snd_opts);
619 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
620 th->checksum = tcp_compute_checksum (tc, b);
624 tcp_enqueue_to_ip_lookup_i (tcp_worker_ctx_t * wrk, vlib_buffer_t * b, u32 bi,
625 u8 is_ip4, u32 fib_index, u8 flush)
627 vlib_main_t *vm = wrk->vm;
628 u32 *to_next, next_index;
631 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
634 vnet_buffer (b)->sw_if_index[VLIB_TX] = fib_index;
635 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
637 /* Send to IP lookup */
638 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
639 tcp_trajectory_add_start (b, 1);
641 f = wrk->ip_lookup_tx_frames[!is_ip4];
644 f = vlib_get_frame_to_node (vm, next_index);
646 wrk->ip_lookup_tx_frames[!is_ip4] = f;
649 to_next = vlib_frame_vector_args (f);
650 to_next[f->n_vectors] = bi;
652 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
654 vlib_put_frame_to_node (vm, next_index, f);
655 wrk->ip_lookup_tx_frames[!is_ip4] = 0;
660 tcp_enqueue_to_ip_lookup_now (tcp_worker_ctx_t * wrk, vlib_buffer_t * b,
661 u32 bi, u8 is_ip4, u32 fib_index)
663 tcp_enqueue_to_ip_lookup_i (wrk, b, bi, is_ip4, fib_index, 1);
667 tcp_enqueue_to_ip_lookup (tcp_worker_ctx_t * wrk, vlib_buffer_t * b, u32 bi,
668 u8 is_ip4, u32 fib_index)
670 tcp_enqueue_to_ip_lookup_i (wrk, b, bi, is_ip4, fib_index, 0);
671 if (wrk->vm->thread_index == 0 && vlib_num_workers ())
672 session_flush_frames_main_thread (wrk->vm);
676 tcp_enqueue_to_output (tcp_worker_ctx_t * wrk, vlib_buffer_t * b, u32 bi,
681 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
684 st = session_type_from_proto_and_ip (TRANSPORT_PROTO_TCP, is_ip4);
685 session_add_pending_tx_buffer (st, wrk->vm->thread_index, bi);
688 #endif /* CLIB_MARCH_VARIANT */
691 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b, u8 is_ip4)
696 ip4_address_t src_ip4, dst_ip4;
697 ip6_address_t src_ip6, dst_ip6;
698 u16 src_port, dst_port;
699 u32 tmp, len, seq, ack;
702 /* Find IP and TCP headers */
703 th = tcp_buffer_hdr (b);
705 /* Save src and dst ip */
708 ih4 = vlib_buffer_get_current (b);
709 ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
710 src_ip4.as_u32 = ih4->src_address.as_u32;
711 dst_ip4.as_u32 = ih4->dst_address.as_u32;
715 ih6 = vlib_buffer_get_current (b);
716 ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
717 clib_memcpy_fast (&src_ip6, &ih6->src_address, sizeof (ip6_address_t));
718 clib_memcpy_fast (&dst_ip6, &ih6->dst_address, sizeof (ip6_address_t));
721 src_port = th->src_port;
722 dst_port = th->dst_port;
723 flags = TCP_FLAG_RST;
726 * RFC 793. If the ACK bit is off, sequence number zero is used,
727 * <SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
728 * If the ACK bit is on,
729 * <SEQ=SEG.ACK><CTL=RST>
733 seq = th->ack_number;
738 flags |= TCP_FLAG_ACK;
739 tmp = clib_net_to_host_u32 (th->seq_number);
740 len = vnet_buffer (b)->tcp.data_len + tcp_is_syn (th) + tcp_is_fin (th);
741 ack = clib_host_to_net_u32 (tmp + len);
745 tcp_reuse_buffer (vm, b);
746 tcp_trajectory_add_start (b, 4);
747 th = vlib_buffer_push_tcp_net_order (b, dst_port, src_port, seq, ack,
748 sizeof (tcp_header_t), flags, 0);
752 ih4 = vlib_buffer_push_ip4 (vm, b, &dst_ip4, &src_ip4,
754 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
759 ih6 = vlib_buffer_push_ip6 (vm, b, &dst_ip6, &src_ip6, IP_PROTOCOL_TCP);
760 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
767 #ifndef CLIB_MARCH_VARIANT
769 * Send reset without reusing existing buffer
771 * It extracts connection info out of original packet
774 tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt,
775 u32 thread_index, u8 is_ip4)
777 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
778 vlib_main_t *vm = wrk->vm;
780 u32 bi, sw_if_index, fib_index;
781 u8 tcp_hdr_len, flags = 0;
782 tcp_header_t *th, *pkt_th;
784 ip4_header_t *ih4, *pkt_ih4;
785 ip6_header_t *ih6, *pkt_ih6;
786 fib_protocol_t fib_proto;
788 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
791 b = vlib_get_buffer (vm, bi);
792 sw_if_index = vnet_buffer (pkt)->sw_if_index[VLIB_RX];
793 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
794 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
795 tcp_init_buffer (vm, b);
797 /* Make and write options */
798 tcp_hdr_len = sizeof (tcp_header_t);
802 pkt_ih4 = vlib_buffer_get_current (pkt);
803 pkt_th = ip4_next_header (pkt_ih4);
807 pkt_ih6 = vlib_buffer_get_current (pkt);
808 pkt_th = ip6_next_header (pkt_ih6);
811 if (tcp_ack (pkt_th))
813 flags = TCP_FLAG_RST;
814 seq = pkt_th->ack_number;
815 ack = (tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0;
819 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
821 ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
824 th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
825 seq, ack, tcp_hdr_len, flags, 0);
827 /* Swap src and dst ip */
830 ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
831 ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
832 &pkt_ih4->src_address, IP_PROTOCOL_TCP,
833 tcp_csum_offload (tc));
834 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
839 ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
841 ih6 = vlib_buffer_push_ip6_custom (vm, b, &pkt_ih6->dst_address,
842 &pkt_ih6->src_address,
844 tc->ipv6_flow_label);
845 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
849 tcp_enqueue_to_ip_lookup_now (wrk, b, bi, is_ip4, fib_index);
850 TCP_EVT (TCP_EVT_RST_SENT, tc);
851 vlib_node_increment_counter (vm, tcp_node_index (output, tc->c_is_ip4),
852 TCP_ERROR_RST_SENT, 1);
856 * Build and set reset packet for connection
859 tcp_send_reset (tcp_connection_t * tc)
861 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
862 vlib_main_t *vm = wrk->vm;
866 u16 tcp_hdr_opts_len, advertise_wnd, opts_write_len;
869 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
871 b = vlib_get_buffer (vm, bi);
872 tcp_init_buffer (vm, b);
874 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
875 tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
876 advertise_wnd = tc->rcv_wnd >> tc->rcv_wscale;
877 flags = TCP_FLAG_RST;
878 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
879 tc->rcv_nxt, tcp_hdr_opts_len, flags,
881 opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
882 th->checksum = tcp_compute_checksum (tc, b);
883 ASSERT (opts_write_len == tc->snd_opts_len);
884 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
885 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
886 TCP_EVT (TCP_EVT_RST_SENT, tc);
887 vlib_node_increment_counter (vm, tcp_node_index (output, tc->c_is_ip4),
888 TCP_ERROR_RST_SENT, 1);
892 tcp_push_ip_hdr (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
897 vlib_buffer_push_ip4 (wrk->vm, b, &tc->c_lcl_ip4, &tc->c_rmt_ip4,
898 IP_PROTOCOL_TCP, tcp_csum_offload (tc));
902 vlib_buffer_push_ip6_custom (wrk->vm, b, &tc->c_lcl_ip6, &tc->c_rmt_ip6,
903 IP_PROTOCOL_TCP, tc->ipv6_flow_label);
910 * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
911 * The packet is not forwarded through tcpx_output to avoid doing lookups
912 * in the half_open pool.
915 tcp_send_syn (tcp_connection_t * tc)
917 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
918 vlib_main_t *vm = wrk->vm;
923 * Setup retransmit and establish timers before requesting buffer
924 * such that we can return if we've ran out.
926 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
927 tc->rto * TCP_TO_TIMER_TICK);
929 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
931 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN, 1);
935 b = vlib_get_buffer (vm, bi);
936 tcp_init_buffer (vm, b);
937 tcp_make_syn (tc, b);
939 /* Measure RTT with this */
940 tc->rtt_ts = tcp_time_now_us (vlib_num_workers ()? 1 : 0);
941 tc->rtt_seq = tc->snd_nxt;
944 tcp_push_ip_hdr (wrk, tc, b);
945 tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
946 TCP_EVT (TCP_EVT_SYN_SENT, tc);
950 tcp_send_synack (tcp_connection_t * tc)
952 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
953 vlib_main_t *vm = wrk->vm;
957 tcp_retransmit_timer_force_update (tc);
959 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
961 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
965 tc->rtt_ts = tcp_time_now_us (tc->c_thread_index);
966 b = vlib_get_buffer (vm, bi);
967 tcp_init_buffer (vm, b);
968 tcp_make_synack (tc, b);
969 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
970 TCP_EVT (TCP_EVT_SYNACK_SENT, tc);
977 tcp_send_fin (tcp_connection_t * tc)
979 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
980 vlib_main_t *vm = wrk->vm;
985 fin_snt = tc->flags & TCP_CONN_FINSNT;
989 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
991 /* Out of buffers so program fin retransmit ASAP */
992 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
996 /* Make sure retransmit retries a fin not data */
997 tc->flags |= TCP_CONN_FINSNT;
1001 /* If we have non-dupacks programmed, no need to send them */
1002 if ((tc->flags & TCP_CONN_SNDACK) && !tc->pending_dupacks)
1003 tc->flags &= ~TCP_CONN_SNDACK;
1005 tcp_retransmit_timer_force_update (tc);
1006 b = vlib_get_buffer (vm, bi);
1007 tcp_init_buffer (vm, b);
1008 tcp_make_fin (tc, b);
1009 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1010 TCP_EVT (TCP_EVT_FIN_SENT, tc);
1011 /* Account for the FIN */
1015 tc->flags |= TCP_CONN_FINSNT;
1016 tc->flags &= ~TCP_CONN_FINPNDG;
1017 tc->snd_una_max = seq_max (tc->snd_una_max, tc->snd_nxt);
1022 * Push TCP header and update connection variables. Should only be called
1023 * for segments with data, not for 'control' packets.
1026 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b, u32 snd_nxt,
1027 u8 compute_opts, u8 maybe_burst, u8 update_snd_nxt)
1029 u8 tcp_hdr_opts_len, flags = TCP_FLAG_ACK;
1030 u32 advertise_wnd, data_len;
1031 tcp_main_t *tm = &tcp_main;
1034 data_len = b->current_length;
1035 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
1036 data_len += b->total_length_not_including_first_buffer;
1038 vnet_buffer (b)->tcp.flags = 0;
1039 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
1042 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1044 tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
1047 advertise_wnd = tc->rcv_wnd >> tc->rcv_wscale;
1049 advertise_wnd = tcp_window_to_advertise (tc, TCP_STATE_ESTABLISHED);
1051 if (PREDICT_FALSE (tc->flags & TCP_CONN_PSH_PENDING))
1053 if (seq_geq (tc->psh_seq, snd_nxt)
1054 && seq_lt (tc->psh_seq, snd_nxt + data_len))
1055 flags |= TCP_FLAG_PSH;
1057 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, snd_nxt,
1058 tc->rcv_nxt, tcp_hdr_opts_len, flags,
1063 clib_memcpy_fast ((u8 *) (th + 1),
1064 tm->wrk_ctx[tc->c_thread_index].cached_opts,
1069 u8 len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
1070 ASSERT (len == tc->snd_opts_len);
1074 * Update connection variables
1078 tc->snd_nxt += data_len;
1079 tc->rcv_las = tc->rcv_nxt;
1081 tc->bytes_out += data_len;
1082 tc->data_segs_out += 1;
1084 th->checksum = tcp_compute_checksum (tc, b);
1086 TCP_EVT (TCP_EVT_PKTIZE, tc);
1090 tcp_buffer_len (vlib_buffer_t * b)
1092 u32 data_len = b->current_length;
1093 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
1094 data_len += b->total_length_not_including_first_buffer;
1099 tcp_session_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1101 tcp_connection_t *tc = (tcp_connection_t *) tconn;
1103 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
1104 tcp_bt_track_tx (tc, tcp_buffer_len (b));
1106 tcp_push_hdr_i (tc, b, tc->snd_nxt, /* compute opts */ 0, /* burst */ 1,
1107 /* update_snd_nxt */ 1);
1109 tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
1110 tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1111 /* If not tracking an ACK, start tracking */
1112 if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc))
1114 tc->rtt_ts = tcp_time_now_us (tc->c_thread_index);
1115 tc->rtt_seq = tc->snd_nxt;
1117 if (PREDICT_FALSE (!tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT)))
1119 tcp_retransmit_timer_set (tc);
1122 tcp_trajectory_add_start (b, 3);
1127 tcp_send_ack (tcp_connection_t * tc)
1129 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1130 vlib_main_t *vm = wrk->vm;
1134 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
1136 tcp_update_rcv_wnd (tc);
1139 b = vlib_get_buffer (vm, bi);
1140 tcp_init_buffer (vm, b);
1141 tcp_make_ack (tc, b);
1142 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1146 tcp_program_ack (tcp_connection_t * tc)
1148 if (!(tc->flags & TCP_CONN_SNDACK))
1150 session_add_self_custom_tx_evt (&tc->connection, 1);
1151 tc->flags |= TCP_CONN_SNDACK;
1156 tcp_program_dupack (tcp_connection_t * tc)
1158 if (!(tc->flags & TCP_CONN_SNDACK))
1160 session_add_self_custom_tx_evt (&tc->connection, 1);
1161 tc->flags |= TCP_CONN_SNDACK;
1163 if (tc->pending_dupacks < 255)
1164 tc->pending_dupacks += 1;
1168 tcp_program_retransmit (tcp_connection_t * tc)
1170 if (!(tc->flags & TCP_CONN_RXT_PENDING))
1172 session_add_self_custom_tx_evt (&tc->connection, 0);
1173 tc->flags |= TCP_CONN_RXT_PENDING;
1178 * Delayed ack timer handler
1180 * Sends delayed ACK when timer expires
1183 tcp_timer_delack_handler (tcp_connection_t * tc)
1189 * Send window update ack
1191 * Ensures that it will be sent only once, after a zero rwnd has been
1192 * advertised in a previous ack, and only if rwnd has grown beyond a
1193 * configurable value.
1196 tcp_send_window_update_ack (tcp_connection_t * tc)
1198 if (tcp_zero_rwnd_sent (tc))
1200 tcp_update_rcv_wnd (tc);
1201 if (tc->rcv_wnd >= tcp_cfg.rwnd_min_update_ack * tc->snd_mss)
1203 tcp_zero_rwnd_sent_off (tc);
1204 tcp_program_ack (tc);
1210 * Allocate a new buffer and build a new tcp segment
1212 * @param wrk tcp worker
1213 * @param tc connection for which the segment will be allocated
1214 * @param offset offset of the first byte in the tx fifo
1215 * @param max_deq_byte segment size
1216 * @param[out] b pointer to buffer allocated
1218 * @return the number of bytes in the segment or 0 if buffer cannot be
1219 * allocated or no data available
1222 tcp_prepare_segment (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
1223 u32 offset, u32 max_deq_bytes, vlib_buffer_t ** b)
1225 u32 bytes_per_buffer = vnet_get_tcp_main ()->bytes_per_buffer;
1226 vlib_main_t *vm = wrk->vm;
1231 seg_size = max_deq_bytes + TRANSPORT_MAX_HDRS_LEN;
1236 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1239 * Allocate and fill in buffer(s)
1242 /* Easy case, buffer size greater than mss */
1243 if (PREDICT_TRUE (seg_size <= bytes_per_buffer))
1245 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
1247 *b = vlib_get_buffer (vm, bi);
1248 data = tcp_init_buffer (vm, *b);
1249 n_bytes = session_tx_fifo_peek_bytes (&tc->connection, data, offset,
1251 ASSERT (n_bytes == max_deq_bytes);
1252 b[0]->current_length = n_bytes;
1253 tcp_push_hdr_i (tc, *b, tc->snd_una + offset, /* compute opts */ 0,
1254 /* burst */ 0, /* update_snd_nxt */ 0);
1256 /* Split mss into multiple buffers */
1259 u32 chain_bi = ~0, n_bufs_per_seg, n_bufs;
1260 u16 n_peeked, len_to_deq;
1261 vlib_buffer_t *chain_b, *prev_b;
1264 /* Make sure we have enough buffers */
1265 n_bufs_per_seg = ceil ((double) seg_size / bytes_per_buffer);
1266 vec_validate_aligned (wrk->tx_buffers, n_bufs_per_seg - 1,
1267 CLIB_CACHE_LINE_BYTES);
1268 n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_per_seg);
1269 if (PREDICT_FALSE (n_bufs != n_bufs_per_seg))
1272 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
1276 *b = vlib_get_buffer (vm, wrk->tx_buffers[--n_bufs]);
1277 data = tcp_init_buffer (vm, *b);
1278 n_bytes = session_tx_fifo_peek_bytes (&tc->connection, data, offset,
1280 TRANSPORT_MAX_HDRS_LEN);
1281 b[0]->current_length = n_bytes;
1282 b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1283 b[0]->total_length_not_including_first_buffer = 0;
1284 max_deq_bytes -= n_bytes;
1287 for (i = 1; i < n_bufs_per_seg; i++)
1290 len_to_deq = clib_min (max_deq_bytes, bytes_per_buffer);
1291 chain_bi = wrk->tx_buffers[--n_bufs];
1292 chain_b = vlib_get_buffer (vm, chain_bi);
1293 chain_b->current_data = 0;
1294 data = vlib_buffer_get_current (chain_b);
1295 n_peeked = session_tx_fifo_peek_bytes (&tc->connection, data,
1298 ASSERT (n_peeked == len_to_deq);
1299 n_bytes += n_peeked;
1300 chain_b->current_length = n_peeked;
1301 chain_b->next_buffer = 0;
1303 /* update previous buffer */
1304 prev_b->next_buffer = chain_bi;
1305 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
1307 max_deq_bytes -= n_peeked;
1308 b[0]->total_length_not_including_first_buffer += n_peeked;
1311 tcp_push_hdr_i (tc, *b, tc->snd_una + offset, /* compute opts */ 0,
1312 /* burst */ 0, /* update_snd_nxt */ 0);
1314 if (PREDICT_FALSE (n_bufs))
1316 clib_warning ("not all buffers consumed");
1317 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
1321 ASSERT (n_bytes > 0);
1322 ASSERT (((*b)->current_data + (*b)->current_length) <= bytes_per_buffer);
1328 * Build a retransmit segment
1330 * @return the number of bytes in the segment or 0 if there's nothing to
1334 tcp_prepare_retransmit_segment (tcp_worker_ctx_t * wrk,
1335 tcp_connection_t * tc, u32 offset,
1336 u32 max_deq_bytes, vlib_buffer_t ** b)
1338 u32 start, available_bytes;
1341 ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
1342 ASSERT (max_deq_bytes != 0);
1345 * Make sure we can retransmit something
1347 available_bytes = transport_max_tx_dequeue (&tc->connection);
1348 ASSERT (available_bytes >= offset);
1349 available_bytes -= offset;
1350 if (!available_bytes)
1353 max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes);
1354 max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1356 start = tc->snd_una + offset;
1357 ASSERT (seq_leq (start + max_deq_bytes, tc->snd_nxt));
1359 n_bytes = tcp_prepare_segment (wrk, tc, offset, max_deq_bytes, b);
1363 tc->snd_rxt_bytes += n_bytes;
1365 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
1366 tcp_bt_track_rxt (tc, start, start + n_bytes);
1368 tc->bytes_retrans += n_bytes;
1369 tc->segs_retrans += 1;
1370 tcp_workerp_stats_inc (wrk, rxt_segs, 1);
1371 TCP_EVT (TCP_EVT_CC_RTX, tc, offset, n_bytes);
1377 tcp_check_sack_reneging (tcp_connection_t * tc)
1379 sack_scoreboard_t *sb = &tc->sack_sb;
1380 sack_scoreboard_hole_t *hole;
1382 hole = scoreboard_first_hole (sb);
1383 if (!sb->is_reneging && (!hole || hole->start == tc->snd_una))
1386 scoreboard_clear_reneging (sb, tc->snd_una, tc->snd_nxt);
1390 * Reset congestion control, switch cwnd to loss window and try again.
1393 tcp_cc_init_rxt_timeout (tcp_connection_t * tc)
1395 TCP_EVT (TCP_EVT_CC_EVT, tc, 6);
1397 tc->prev_ssthresh = tc->ssthresh;
1398 tc->prev_cwnd = tc->cwnd;
1400 /* If we entrered loss without fast recovery, notify cc algo of the
1401 * congestion event such that it can update ssthresh and its state */
1402 if (!tcp_in_fastrecovery (tc))
1403 tcp_cc_congestion (tc);
1405 /* Let cc algo decide loss cwnd and ssthresh post unrecovered loss */
1409 tc->cwnd_acc_bytes = 0;
1410 tc->tr_occurences += 1;
1411 tcp_recovery_on (tc);
1415 tcp_timer_retransmit_handler (tcp_connection_t * tc)
1417 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1418 vlib_main_t *vm = wrk->vm;
1419 vlib_buffer_t *b = 0;
1422 tcp_workerp_stats_inc (wrk, tr_events, 1);
1424 /* Should be handled by a different handler */
1425 if (PREDICT_FALSE (tc->state == TCP_STATE_SYN_SENT))
1428 /* Wait-close and retransmit could pop at the same time */
1429 if (tc->state == TCP_STATE_CLOSED)
1432 if (tc->state >= TCP_STATE_ESTABLISHED)
1434 TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
1436 /* Lost FIN, retransmit and return */
1437 if (tc->flags & TCP_CONN_FINSNT)
1441 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1445 /* Shouldn't be here. This condition is tricky because it has to take
1446 * into account boff > 0 due to persist timeout. */
1447 if ((tc->rto_boff == 0 && tc->snd_una == tc->snd_nxt)
1448 || (tc->rto_boff > 0 && seq_geq (tc->snd_una, tc->snd_congestion)
1449 && !tcp_flight_size (tc)))
1451 ASSERT (!tcp_in_recovery (tc));
1456 /* We're not in recovery so make sure rto_boff is 0. Can be non 0 due
1457 * to persist timer timeout */
1458 if (!tcp_in_recovery (tc) && tc->rto_boff > 0)
1461 tcp_update_rto (tc);
1464 /* Peer is dead or network connectivity is lost. Close connection.
1465 * RFC 1122 section 4.2.3.5 recommends a value of at least 100s. For
1466 * a min rto of 0.2s we need to retry about 8 times. */
1467 if (tc->rto_boff >= TCP_RTO_BOFF_MAX)
1469 tcp_send_reset (tc);
1470 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
1471 session_transport_closing_notify (&tc->connection);
1472 session_transport_closed_notify (&tc->connection);
1473 tcp_connection_timers_reset (tc);
1474 tcp_program_cleanup (wrk, tc);
1475 tcp_workerp_stats_inc (wrk, tr_abort, 1);
1479 if (tcp_opts_sack_permitted (&tc->rcv_opts))
1480 tcp_check_sack_reneging (tc);
1482 /* Update send congestion to make sure that rxt has data to send */
1483 tc->snd_congestion = tc->snd_nxt;
1485 /* Send the first unacked segment. If we're short on buffers, return
1486 * as soon as possible */
1487 n_bytes = clib_min (tc->snd_mss, tc->snd_nxt - tc->snd_una);
1488 n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, n_bytes, &b);
1491 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
1495 bi = vlib_get_buffer_index (vm, b);
1496 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1498 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1499 tcp_retransmit_timer_force_update (tc);
1502 if (tc->rto_boff == 1)
1504 tcp_cc_init_rxt_timeout (tc);
1505 /* Record timestamp. Eifel detection algorithm RFC3522 */
1506 tc->snd_rxt_ts = tcp_tstamp (tc);
1509 if (tcp_opts_sack_permitted (&tc->rcv_opts))
1510 scoreboard_init_rxt (&tc->sack_sb, tc->snd_una + n_bytes);
1512 tcp_program_retransmit (tc);
1514 /* Retransmit SYN-ACK */
1515 else if (tc->state == TCP_STATE_SYN_RCVD)
1517 TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
1521 /* Passive open establish timeout */
1522 if (tc->rto > TCP_ESTABLISH_TIME >> 1)
1524 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
1525 tcp_connection_timers_reset (tc);
1526 tcp_program_cleanup (wrk, tc);
1527 tcp_workerp_stats_inc (wrk, tr_abort, 1);
1531 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
1533 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
1538 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1539 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1541 tcp_retransmit_timer_force_update (tc);
1543 b = vlib_get_buffer (vm, bi);
1544 tcp_init_buffer (vm, b);
1545 tcp_make_synack (tc, b);
1546 TCP_EVT (TCP_EVT_SYN_RXT, tc, 1);
1548 /* Retransmit timer already updated, just enqueue to output */
1549 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1553 ASSERT (tc->state == TCP_STATE_CLOSED);
1559 * SYN retransmit timer handler. Active open only.
1562 tcp_timer_retransmit_syn_handler (tcp_connection_t * tc)
1564 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1565 vlib_main_t *vm = wrk->vm;
1566 vlib_buffer_t *b = 0;
1569 /* Note: the connection may have transitioned to ESTABLISHED... */
1570 if (PREDICT_FALSE (tc->state != TCP_STATE_SYN_SENT))
1573 /* Half-open connection actually moved to established but we were
1574 * waiting for syn retransmit to pop to call cleanup from the right
1576 if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
1578 if (tcp_half_open_connection_cleanup (tc))
1579 TCP_DBG ("could not remove half-open connection");
1583 TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
1586 /* Active open establish timeout */
1587 if (tc->rto >= TCP_ESTABLISH_TIME >> 1)
1589 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
1590 tcp_connection_cleanup (tc);
1594 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
1596 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN, 1);
1600 /* Try without increasing RTO a number of times. If this fails,
1601 * start growing RTO exponentially */
1603 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1604 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1606 b = vlib_get_buffer (vm, bi);
1607 tcp_init_buffer (vm, b);
1608 tcp_make_syn (tc, b);
1610 TCP_EVT (TCP_EVT_SYN_RXT, tc, 0);
1612 /* This goes straight to ipx_lookup */
1613 tcp_push_ip_hdr (wrk, tc, b);
1614 tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
1616 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
1617 tc->rto * TCP_TO_TIMER_TICK);
1621 * Got 0 snd_wnd from peer, try to do something about it.
1625 tcp_timer_persist_handler (tcp_connection_t * tc)
1627 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1628 u32 bi, max_snd_bytes, available_bytes, offset;
1629 tcp_main_t *tm = vnet_get_tcp_main ();
1630 vlib_main_t *vm = wrk->vm;
1635 /* Problem already solved or worse */
1636 if (tc->state == TCP_STATE_CLOSED || tc->snd_wnd > tc->snd_mss
1637 || (tc->flags & TCP_CONN_FINSNT))
1638 goto update_scheduler;
1640 available_bytes = transport_max_tx_dequeue (&tc->connection);
1641 offset = tc->snd_nxt - tc->snd_una;
1643 /* Reprogram persist if no new bytes available to send. We may have data
1645 if (!available_bytes)
1647 tcp_persist_timer_set (tc);
1651 if (available_bytes <= offset)
1652 goto update_scheduler;
1654 /* Increment RTO backoff */
1656 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1659 * Try to force the first unsent segment (or buffer)
1661 if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
1663 tcp_persist_timer_set (tc);
1667 b = vlib_get_buffer (vm, bi);
1668 data = tcp_init_buffer (vm, b);
1670 tcp_validate_txf_size (tc, offset);
1671 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1672 max_snd_bytes = clib_min (tc->snd_mss,
1673 tm->bytes_per_buffer - TRANSPORT_MAX_HDRS_LEN);
1674 n_bytes = session_tx_fifo_peek_bytes (&tc->connection, data, offset,
1676 b->current_length = n_bytes;
1677 ASSERT (n_bytes != 0 && (tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT)
1678 || tc->snd_nxt == tc->snd_una_max
1679 || tc->rto_boff > 1));
1681 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
1683 tcp_bt_check_app_limited (tc);
1684 tcp_bt_track_tx (tc, n_bytes);
1687 tcp_push_hdr_i (tc, b, tc->snd_nxt, /* compute opts */ 0,
1688 /* burst */ 0, /* update_snd_nxt */ 1);
1689 tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
1690 tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1691 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1693 /* Just sent new data, enable retransmit */
1694 tcp_retransmit_timer_update (tc);
1700 if (tcp_is_descheduled (tc))
1701 transport_connection_reschedule (&tc->connection);
1705 * Retransmit first unacked segment
1708 tcp_retransmit_first_unacked (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
1710 vlib_main_t *vm = wrk->vm;
1714 TCP_EVT (TCP_EVT_CC_EVT, tc, 1);
1716 n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
1720 bi = vlib_get_buffer_index (vm, b);
1721 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1727 tcp_transmit_unsent (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
1730 u32 offset, n_segs = 0, n_written, bi, available_wnd;
1731 vlib_main_t *vm = wrk->vm;
1732 vlib_buffer_t *b = 0;
1734 offset = tc->snd_nxt - tc->snd_una;
1735 available_wnd = tc->snd_wnd - offset;
1736 burst_size = clib_min (burst_size, available_wnd / tc->snd_mss);
1738 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
1739 tcp_bt_check_app_limited (tc);
1741 while (n_segs < burst_size)
1743 n_written = tcp_prepare_segment (wrk, tc, offset, tc->snd_mss, &b);
1747 bi = vlib_get_buffer_index (vm, b);
1748 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1749 offset += n_written;
1752 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
1753 tcp_bt_track_tx (tc, n_written);
1755 tc->snd_nxt += n_written;
1756 tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
1764 * Estimate send space using proportional rate reduction (RFC6937)
1767 tcp_fastrecovery_prr_snd_space (tcp_connection_t * tc)
1772 pipe = tcp_flight_size (tc);
1773 prr_out = tc->snd_rxt_bytes + (tc->snd_nxt - tc->snd_congestion);
1775 if (pipe > tc->ssthresh)
1777 space = ((int) tc->prr_delivered * ((f64) tc->ssthresh / tc->prev_cwnd))
1783 limit = clib_max ((int) (tc->prr_delivered - prr_out), 0) + tc->snd_mss;
1784 space = clib_min (tc->ssthresh - pipe, limit);
1786 space = clib_max (space, prr_out ? 0 : tc->snd_mss);
1791 tcp_retransmit_should_retry_head (tcp_connection_t * tc,
1792 sack_scoreboard_t * sb)
1794 u32 tx_adv_sack = sb->high_sacked - tc->snd_congestion;
1795 f64 rr = (f64) tc->ssthresh / tc->prev_cwnd;
1797 if (tcp_fastrecovery_first (tc))
1800 return (tx_adv_sack > (tc->snd_una - tc->prr_start) * rr);
1804 tcp_max_tx_deq (tcp_connection_t * tc)
1806 return (transport_max_tx_dequeue (&tc->connection)
1807 - (tc->snd_nxt - tc->snd_una));
1810 #define scoreboard_rescue_rxt_valid(_sb, _tc) \
1811 (seq_geq (_sb->rescue_rxt, _tc->snd_una) \
1812 && seq_leq (_sb->rescue_rxt, _tc->snd_congestion))
1815 * Do retransmit with SACKs
1818 tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
1821 u32 n_written = 0, offset, max_bytes, n_segs = 0;
1822 u8 snd_limited = 0, can_rescue = 0;
1823 u32 bi, max_deq, burst_bytes;
1824 sack_scoreboard_hole_t *hole;
1825 vlib_main_t *vm = wrk->vm;
1826 vlib_buffer_t *b = 0;
1827 sack_scoreboard_t *sb;
1830 ASSERT (tcp_in_cong_recovery (tc));
1832 burst_bytes = transport_connection_tx_pacer_burst (&tc->connection);
1833 burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
1836 tcp_program_retransmit (tc);
1840 if (tcp_in_recovery (tc))
1841 snd_space = tcp_available_cc_snd_space (tc);
1843 snd_space = tcp_fastrecovery_prr_snd_space (tc);
1845 if (snd_space < tc->snd_mss)
1850 /* Check if snd_una is a lost retransmit */
1851 if (pool_elts (sb->holes)
1852 && seq_gt (sb->high_sacked, tc->snd_congestion)
1853 && tc->rxt_head != tc->snd_una
1854 && tcp_retransmit_should_retry_head (tc, sb))
1856 max_bytes = clib_min (tc->snd_mss, tc->snd_congestion - tc->snd_una);
1857 n_written = tcp_prepare_retransmit_segment (wrk, tc, 0, max_bytes, &b);
1860 tcp_program_retransmit (tc);
1863 bi = vlib_get_buffer_index (vm, b);
1864 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1867 tc->rxt_head = tc->snd_una;
1868 tc->rxt_delivered += n_written;
1869 tc->prr_delivered += n_written;
1870 ASSERT (tc->rxt_delivered <= tc->snd_rxt_bytes);
1873 tcp_fastrecovery_first_off (tc);
1875 TCP_EVT (TCP_EVT_CC_EVT, tc, 0);
1876 hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
1878 max_deq = transport_max_tx_dequeue (&tc->connection);
1879 max_deq -= tc->snd_nxt - tc->snd_una;
1881 while (snd_space > 0 && n_segs < burst_size)
1883 hole = scoreboard_next_rxt_hole (sb, hole, max_deq != 0, &can_rescue,
1887 /* We are out of lost holes to retransmit so send some new data. */
1888 if (max_deq > tc->snd_mss)
1893 /* Make sure we don't exceed available window and leave space
1894 * for one more packet, to avoid zero window acks */
1895 av_wnd = (int) tc->snd_wnd - (tc->snd_nxt - tc->snd_una);
1896 av_wnd = clib_max (av_wnd - tc->snd_mss, 0);
1897 snd_space = clib_min (snd_space, av_wnd);
1898 snd_space = clib_min (max_deq, snd_space);
1899 burst_size = clib_min (burst_size - n_segs,
1900 snd_space / tc->snd_mss);
1901 burst_size = clib_min (burst_size, TCP_RXT_MAX_BURST);
1902 n_segs_new = tcp_transmit_unsent (wrk, tc, burst_size);
1903 if (max_deq > n_segs_new * tc->snd_mss)
1904 tcp_program_retransmit (tc);
1906 n_segs += n_segs_new;
1910 if (tcp_in_recovery (tc) || !can_rescue
1911 || scoreboard_rescue_rxt_valid (sb, tc))
1914 /* If rescue rxt undefined or less than snd_una then one segment of
1915 * up to SMSS octets that MUST include the highest outstanding
1916 * unSACKed sequence number SHOULD be returned, and RescueRxt set to
1917 * RecoveryPoint. HighRxt MUST NOT be updated.
1919 hole = scoreboard_last_hole (sb);
1920 max_bytes = clib_min (tc->snd_mss, hole->end - hole->start);
1921 max_bytes = clib_min (max_bytes, snd_space);
1922 offset = hole->end - tc->snd_una - max_bytes;
1923 n_written = tcp_prepare_retransmit_segment (wrk, tc, offset,
1928 sb->rescue_rxt = tc->snd_congestion;
1929 bi = vlib_get_buffer_index (vm, b);
1930 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1935 max_bytes = clib_min (hole->end - sb->high_rxt, snd_space);
1936 max_bytes = snd_limited ? clib_min (max_bytes, tc->snd_mss) : max_bytes;
1940 offset = sb->high_rxt - tc->snd_una;
1941 n_written = tcp_prepare_retransmit_segment (wrk, tc, offset, max_bytes,
1943 ASSERT (n_written <= snd_space);
1945 /* Nothing left to retransmit */
1949 bi = vlib_get_buffer_index (vm, b);
1950 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1952 sb->high_rxt += n_written;
1953 ASSERT (seq_leq (sb->high_rxt, tc->snd_nxt));
1955 snd_space -= n_written;
1960 tcp_program_retransmit (tc);
1964 transport_connection_tx_pacer_reset_bucket (&tc->connection, 0);
1969 * Fast retransmit without SACK info
1972 tcp_retransmit_no_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
1975 u32 n_written = 0, offset = 0, bi, max_deq, n_segs_now, max_bytes;
1976 u32 burst_bytes, sent_bytes;
1977 vlib_main_t *vm = wrk->vm;
1978 int snd_space, n_segs = 0;
1982 ASSERT (tcp_in_cong_recovery (tc));
1983 TCP_EVT (TCP_EVT_CC_EVT, tc, 0);
1985 burst_bytes = transport_connection_tx_pacer_burst (&tc->connection);
1986 burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
1989 tcp_program_retransmit (tc);
1993 snd_space = tcp_available_cc_snd_space (tc);
1994 cc_limited = snd_space < burst_bytes;
1996 if (!tcp_fastrecovery_first (tc))
1999 /* RFC 6582: [If a partial ack], retransmit the first unacknowledged
2001 while (snd_space > 0 && n_segs < burst_size)
2003 max_bytes = clib_min (tc->snd_mss,
2004 tc->snd_congestion - tc->snd_una - offset);
2007 n_written = tcp_prepare_retransmit_segment (wrk, tc, offset, max_bytes,
2010 /* Nothing left to retransmit */
2014 bi = vlib_get_buffer_index (vm, b);
2015 tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
2016 snd_space -= n_written;
2017 offset += n_written;
2021 if (n_segs == burst_size)
2026 /* RFC 6582: Send a new segment if permitted by the new value of cwnd. */
2027 if (snd_space < tc->snd_mss || tc->snd_mss == 0)
2030 max_deq = transport_max_tx_dequeue (&tc->connection);
2031 max_deq -= tc->snd_nxt - tc->snd_una;
2034 snd_space = clib_min (max_deq, snd_space);
2035 burst_size = clib_min (burst_size - n_segs, snd_space / tc->snd_mss);
2036 n_segs_now = tcp_transmit_unsent (wrk, tc, burst_size);
2037 if (n_segs_now && max_deq > n_segs_now * tc->snd_mss)
2038 tcp_program_retransmit (tc);
2039 n_segs += n_segs_now;
2043 tcp_fastrecovery_first_off (tc);
2045 sent_bytes = clib_min (n_segs * tc->snd_mss, burst_bytes);
2046 sent_bytes = cc_limited ? burst_bytes : sent_bytes;
2047 transport_connection_tx_pacer_update_bytes (&tc->connection, sent_bytes);
2053 tcp_send_acks (tcp_connection_t * tc, u32 max_burst_size)
2057 if (!tc->pending_dupacks)
2059 if (tcp_in_cong_recovery (tc) || !tcp_max_tx_deq (tc)
2060 || tc->state != TCP_STATE_ESTABLISHED)
2068 /* If we're supposed to send dupacks but have no ooo data
2069 * send only one ack */
2070 if (!vec_len (tc->snd_sacks))
2073 tc->dupacks_out += 1;
2074 tc->pending_dupacks = 0;
2078 /* Start with first sack block */
2079 tc->snd_sack_pos = 0;
2081 /* Generate enough dupacks to cover all sack blocks. Do not generate
2082 * more sacks than the number of packets received. But do generate at
2083 * least 3, i.e., the number needed to signal congestion, if needed. */
2084 n_acks = vec_len (tc->snd_sacks) / TCP_OPTS_MAX_SACK_BLOCKS;
2085 n_acks = clib_min (n_acks, tc->pending_dupacks);
2086 n_acks = clib_max (n_acks, clib_min (tc->pending_dupacks, 3));
2087 for (j = 0; j < clib_min (n_acks, max_burst_size); j++)
2090 if (n_acks < max_burst_size)
2092 tc->pending_dupacks = 0;
2093 tc->snd_sack_pos = 0;
2094 tc->dupacks_out += n_acks;
2099 TCP_DBG ("constrained by burst size");
2100 tc->pending_dupacks = n_acks - max_burst_size;
2101 tc->dupacks_out += max_burst_size;
2102 tcp_program_dupack (tc);
2103 return max_burst_size;
2108 tcp_do_retransmit (tcp_connection_t * tc, u32 max_burst_size)
2110 tcp_worker_ctx_t *wrk;
2113 if (PREDICT_FALSE (tc->state == TCP_STATE_CLOSED))
2116 wrk = tcp_get_worker (tc->c_thread_index);
2118 if (tcp_opts_sack_permitted (&tc->rcv_opts))
2119 n_segs = tcp_retransmit_sack (wrk, tc, max_burst_size);
2121 n_segs = tcp_retransmit_no_sack (wrk, tc, max_burst_size);
2127 tcp_session_custom_tx (void *conn, u32 max_burst_size)
2129 tcp_connection_t *tc = (tcp_connection_t *) conn;
2132 if (tcp_in_cong_recovery (tc) && (tc->flags & TCP_CONN_RXT_PENDING))
2134 tc->flags &= ~TCP_CONN_RXT_PENDING;
2135 n_segs = tcp_do_retransmit (tc, max_burst_size);
2136 max_burst_size -= n_segs;
2139 if (!(tc->flags & TCP_CONN_SNDACK))
2142 tc->flags &= ~TCP_CONN_SNDACK;
2144 /* We have retransmitted packets and no dupack */
2145 if (n_segs && !tc->pending_dupacks)
2148 if (!max_burst_size)
2150 tcp_program_ack (tc);
2151 return max_burst_size;
2154 n_segs += tcp_send_acks (tc, max_burst_size);
2158 #endif /* CLIB_MARCH_VARIANT */
2161 tcp_output_handle_link_local (tcp_connection_t * tc0, vlib_buffer_t * b0,
2162 u16 * next0, u32 * error0)
2164 ip_adjacency_t *adj;
2167 /* Not thread safe but as long as the connection exists the adj should
2169 ai = adj_nbr_find (FIB_PROTOCOL_IP6, VNET_LINK_IP6, &tc0->c_rmt_ip,
2171 if (ai == ADJ_INDEX_INVALID)
2173 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
2174 *next0 = TCP_OUTPUT_NEXT_DROP;
2175 *error0 = TCP_ERROR_LINK_LOCAL_RW;
2180 if (PREDICT_TRUE (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE))
2181 *next0 = TCP_OUTPUT_NEXT_IP_REWRITE;
2182 else if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
2183 *next0 = TCP_OUTPUT_NEXT_IP_ARP;
2186 *next0 = TCP_OUTPUT_NEXT_DROP;
2187 *error0 = TCP_ERROR_LINK_LOCAL_RW;
2189 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ai;
2193 tcp46_output_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
2194 u32 * to_next, u32 n_bufs)
2196 tcp_connection_t *tc;
2202 for (i = 0; i < n_bufs; i++)
2204 b = vlib_get_buffer (vm, to_next[i]);
2205 if (!(b->flags & VLIB_BUFFER_IS_TRACED))
2207 th = vlib_buffer_get_current (b);
2208 tc = tcp_connection_get (vnet_buffer (b)->tcp.connection_index,
2210 t = vlib_add_trace (vm, node, b, sizeof (*t));
2211 clib_memcpy_fast (&t->tcp_header, th, sizeof (t->tcp_header));
2212 clib_memcpy_fast (&t->tcp_connection, tc, sizeof (t->tcp_connection));
2217 tcp_output_push_ip (vlib_main_t * vm, vlib_buffer_t * b0,
2218 tcp_connection_t * tc0, u8 is_ip4)
2220 TCP_EVT (TCP_EVT_OUTPUT, tc0,
2221 ((tcp_header_t *) vlib_buffer_get_current (b0))->flags,
2222 b0->current_length);
2225 vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
2226 IP_PROTOCOL_TCP, tcp_csum_offload (tc0));
2228 vlib_buffer_push_ip6_custom (vm, b0, &tc0->c_lcl_ip6, &tc0->c_rmt_ip6,
2229 IP_PROTOCOL_TCP, tc0->ipv6_flow_label);
2233 tcp_check_if_gso (tcp_connection_t * tc, vlib_buffer_t * b)
2235 if (PREDICT_TRUE (!(tc->cfg_flags & TCP_CFG_F_TSO)))
2238 u16 data_len = b->current_length - sizeof (tcp_header_t) - tc->snd_opts_len;
2240 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
2241 data_len += b->total_length_not_including_first_buffer;
2243 if (PREDICT_TRUE (data_len <= tc->snd_mss))
2247 ASSERT ((b->flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID) != 0);
2248 ASSERT ((b->flags & VNET_BUFFER_F_L4_HDR_OFFSET_VALID) != 0);
2249 b->flags |= VNET_BUFFER_F_GSO;
2250 vnet_buffer2 (b)->gso_l4_hdr_sz =
2251 sizeof (tcp_header_t) + tc->snd_opts_len;
2252 vnet_buffer2 (b)->gso_size = tc->snd_mss;
2257 tcp_output_handle_packet (tcp_connection_t * tc0, vlib_buffer_t * b0,
2258 vlib_node_runtime_t * error_node, u16 * next0,
2261 /* If next_index is not drop use it */
2262 if (tc0->next_node_index)
2264 *next0 = tc0->next_node_index;
2265 vnet_buffer (b0)->tcp.next_node_opaque = tc0->next_node_opaque;
2269 *next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
2272 vnet_buffer (b0)->sw_if_index[VLIB_TX] = tc0->c_fib_index;
2273 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
2279 if (PREDICT_FALSE (ip6_address_is_link_local_unicast (&tc0->c_rmt_ip6)))
2280 tcp_output_handle_link_local (tc0, b0, next0, &error0);
2282 if (PREDICT_FALSE (error0))
2284 b0->error = error_node->errors[error0];
2289 if (!TCP_ALWAYS_ACK)
2290 tcp_timer_reset (tc0, TCP_TIMER_DELACK);
2296 tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2297 vlib_frame_t * frame, int is_ip4)
2299 u32 n_left_from, *from, thread_index = vm->thread_index;
2300 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
2301 u16 nexts[VLIB_FRAME_SIZE], *next;
2302 vlib_node_runtime_t *error_node;
2304 error_node = vlib_node_get_runtime (vm, tcp_node_index (output, is_ip4));
2306 from = vlib_frame_vector_args (frame);
2307 n_left_from = frame->n_vectors;
2308 tcp_set_time_now (tcp_get_worker (thread_index));
2310 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
2311 tcp46_output_trace_frame (vm, node, from, n_left_from);
2313 vlib_get_buffers (vm, from, bufs, n_left_from);
2317 while (n_left_from >= 4)
2319 tcp_connection_t *tc0, *tc1;
2322 vlib_prefetch_buffer_header (b[2], STORE);
2323 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2325 vlib_prefetch_buffer_header (b[3], STORE);
2326 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2329 tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
2331 tc1 = tcp_connection_get (vnet_buffer (b[1])->tcp.connection_index,
2334 if (PREDICT_TRUE (!tc0 + !tc1 == 0))
2336 tcp_output_push_ip (vm, b[0], tc0, is_ip4);
2337 tcp_output_push_ip (vm, b[1], tc1, is_ip4);
2339 tcp_check_if_gso (tc0, b[0]);
2340 tcp_check_if_gso (tc1, b[1]);
2342 tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
2343 tcp_output_handle_packet (tc1, b[1], error_node, &next[1], is_ip4);
2349 tcp_output_push_ip (vm, b[0], tc0, is_ip4);
2350 tcp_check_if_gso (tc0, b[0]);
2351 tcp_output_handle_packet (tc0, b[0], error_node, &next[0],
2356 b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
2357 next[0] = TCP_OUTPUT_NEXT_DROP;
2361 tcp_output_push_ip (vm, b[1], tc1, is_ip4);
2362 tcp_check_if_gso (tc1, b[1]);
2363 tcp_output_handle_packet (tc1, b[1], error_node, &next[1],
2368 b[1]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
2369 next[1] = TCP_OUTPUT_NEXT_DROP;
2377 while (n_left_from > 0)
2379 tcp_connection_t *tc0;
2381 if (n_left_from > 1)
2383 vlib_prefetch_buffer_header (b[1], STORE);
2384 CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2387 tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
2390 if (PREDICT_TRUE (tc0 != 0))
2392 tcp_output_push_ip (vm, b[0], tc0, is_ip4);
2393 tcp_check_if_gso (tc0, b[0]);
2394 tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
2398 b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
2399 next[0] = TCP_OUTPUT_NEXT_DROP;
2407 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
2408 vlib_node_increment_counter (vm, tcp_node_index (output, is_ip4),
2409 TCP_ERROR_PKTS_SENT, frame->n_vectors);
2410 return frame->n_vectors;
2413 VLIB_NODE_FN (tcp4_output_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2414 vlib_frame_t * from_frame)
2416 return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2419 VLIB_NODE_FN (tcp6_output_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2420 vlib_frame_t * from_frame)
2422 return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2426 VLIB_REGISTER_NODE (tcp4_output_node) =
2428 .name = "tcp4-output",
2429 /* Takes a vector of packets. */
2430 .vector_size = sizeof (u32),
2431 .n_errors = TCP_N_ERROR,
2432 .protocol_hint = VLIB_NODE_PROTO_HINT_TCP,
2433 .error_strings = tcp_error_strings,
2434 .n_next_nodes = TCP_OUTPUT_N_NEXT,
2436 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
2437 foreach_tcp4_output_next
2440 .format_buffer = format_tcp_header,
2441 .format_trace = format_tcp_tx_trace,
2446 VLIB_REGISTER_NODE (tcp6_output_node) =
2448 .name = "tcp6-output",
2449 /* Takes a vector of packets. */
2450 .vector_size = sizeof (u32),
2451 .n_errors = TCP_N_ERROR,
2452 .protocol_hint = VLIB_NODE_PROTO_HINT_TCP,
2453 .error_strings = tcp_error_strings,
2454 .n_next_nodes = TCP_OUTPUT_N_NEXT,
2456 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
2457 foreach_tcp6_output_next
2460 .format_buffer = format_tcp_header,
2461 .format_trace = format_tcp_tx_trace,
2465 typedef enum _tcp_reset_next
2467 TCP_RESET_NEXT_DROP,
2468 TCP_RESET_NEXT_IP_LOOKUP,
2472 #define foreach_tcp4_reset_next \
2473 _(DROP, "error-drop") \
2474 _(IP_LOOKUP, "ip4-lookup")
2476 #define foreach_tcp6_reset_next \
2477 _(DROP, "error-drop") \
2478 _(IP_LOOKUP, "ip6-lookup")
2481 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2482 vlib_frame_t * from_frame, u8 is_ip4)
2484 u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
2485 u32 n_left_from, next_index, *from, *to_next;
2487 from = vlib_frame_vector_args (from_frame);
2488 n_left_from = from_frame->n_vectors;
2490 next_index = node->cached_next_index;
2492 while (n_left_from > 0)
2496 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2498 while (n_left_from > 0 && n_left_to_next > 0)
2510 n_left_to_next -= 1;
2512 b0 = vlib_get_buffer (vm, bi0);
2513 tcp_make_reset_in_place (vm, b0, is_ip4);
2515 /* Prepare to send to IP lookup */
2516 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
2518 b0->error = node->errors[error0];
2519 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2520 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2522 th0 = vlib_buffer_get_current (b0);
2524 th0 = ip4_next_header ((ip4_header_t *) th0);
2526 th0 = ip6_next_header ((ip6_header_t *) th0);
2527 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2528 clib_memcpy_fast (&t0->tcp_header, th0,
2529 sizeof (t0->tcp_header));
2532 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2533 n_left_to_next, bi0, next0);
2535 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2537 return from_frame->n_vectors;
2540 VLIB_NODE_FN (tcp4_reset_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2541 vlib_frame_t * from_frame)
2543 return tcp46_send_reset_inline (vm, node, from_frame, 1);
2546 VLIB_NODE_FN (tcp6_reset_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2547 vlib_frame_t * from_frame)
2549 return tcp46_send_reset_inline (vm, node, from_frame, 0);
2553 VLIB_REGISTER_NODE (tcp4_reset_node) = {
2554 .name = "tcp4-reset",
2555 .vector_size = sizeof (u32),
2556 .n_errors = TCP_N_ERROR,
2557 .error_strings = tcp_error_strings,
2558 .n_next_nodes = TCP_RESET_N_NEXT,
2560 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2561 foreach_tcp4_reset_next
2564 .format_trace = format_tcp_tx_trace,
2569 VLIB_REGISTER_NODE (tcp6_reset_node) = {
2570 .name = "tcp6-reset",
2571 .vector_size = sizeof (u32),
2572 .n_errors = TCP_N_ERROR,
2573 .error_strings = tcp_error_strings,
2574 .n_next_nodes = TCP_RESET_N_NEXT,
2576 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2577 foreach_tcp6_reset_next
2580 .format_trace = format_tcp_tx_trace,
2585 * fd.io coding-style-patch-verification: ON
2588 * eval: (c-set-style "gnu")