2 * Copyright (c) 2016 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>
17 #include <vnet/lisp-cp/packets.h>
20 vlib_node_registration_t tcp4_output_node;
21 vlib_node_registration_t tcp6_output_node;
23 typedef enum _tcp_output_next
26 TCP_OUTPUT_NEXT_IP_LOOKUP,
30 #define foreach_tcp4_output_next \
31 _ (DROP, "error-drop") \
32 _ (IP_LOOKUP, "ip4-lookup")
34 #define foreach_tcp6_output_next \
35 _ (DROP, "error-drop") \
36 _ (IP_LOOKUP, "ip6-lookup")
38 static char *tcp_error_strings[] = {
39 #define tcp_error(n,s) s,
40 #include <vnet/tcp/tcp_error.def>
46 tcp_header_t tcp_header;
47 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 uword indent = format_get_indent (s);
60 s = format (s, "%U\n%U%U",
61 format_tcp_header, &t->tcp_header, 128,
62 format_white_space, indent,
63 format_tcp_connection, &t->tcp_connection, 1);
69 tcp_window_compute_scale (u32 available_space)
72 while (wnd_scale < TCP_MAX_WND_SCALE
73 && (available_space >> wnd_scale) > TCP_WND_MAX)
79 * Update max segment size we're able to process.
81 * The value is constrained by our interface's MTU and IP options. It is
82 * also what we advertise to our peer.
85 tcp_update_rcv_mss (tcp_connection_t * tc)
87 /* TODO find our iface MTU */
88 tc->mss = dummy_mtu - sizeof (tcp_header_t);
92 * TCP's initial window
95 tcp_initial_wnd_unscaled (tcp_connection_t * tc)
97 /* RFC 6928 recommends the value lower. However at the time our connections
98 * are initialized, fifos may not be allocated. Therefore, advertise the
99 * smallest possible unscaled window size and update once fifos are
100 * assigned to the session.
103 tcp_update_rcv_mss (tc);
104 TCP_IW_N_SEGMENTS * tc->mss;
106 return TCP_MIN_RX_FIFO_SIZE;
110 * Compute initial window and scale factor. As per RFC1323, window field in
111 * SYN and SYN-ACK segments is never scaled.
114 tcp_initial_window_to_advertise (tcp_connection_t * tc)
118 /* Initial wnd for SYN. Fifos are not allocated yet.
119 * Use some predefined value. For SYN-ACK we still want the
120 * scale to be computed in the same way */
121 max_fifo = TCP_MAX_RX_FIFO_SIZE;
123 tc->rcv_wscale = tcp_window_compute_scale (max_fifo);
124 tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
126 return clib_min (tc->rcv_wnd, TCP_WND_MAX);
130 * Compute and return window to advertise, scaled as per RFC1323
133 tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state)
135 if (state < TCP_STATE_ESTABLISHED)
136 return tcp_initial_window_to_advertise (tc);
138 tcp_update_rcv_wnd (tc);
140 if (tc->rcv_wnd == 0)
142 tc->flags |= TCP_CONN_SENT_RCV_WND0;
146 tc->flags &= ~TCP_CONN_SENT_RCV_WND0;
149 return tc->rcv_wnd >> tc->rcv_wscale;
153 tcp_update_rcv_wnd (tcp_connection_t * tc)
156 u32 available_space, max_fifo, wnd;
159 * Figure out how much space we have available
161 available_space = stream_session_max_rx_enqueue (&tc->connection);
162 max_fifo = stream_session_rx_fifo_size (&tc->connection);
164 ASSERT (tc->rcv_opts.mss < max_fifo);
165 if (available_space < tc->rcv_opts.mss && available_space < max_fifo >> 3)
169 * Use the above and what we know about what we've previously advertised
170 * to compute the new window
172 observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
173 if (observed_wnd < 0)
176 /* Bad. Thou shalt not shrink */
177 if (available_space < observed_wnd)
180 TCP_EVT_DBG (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
184 wnd = available_space;
187 /* Make sure we have a multiple of rcv_wscale */
188 if (wnd && tc->rcv_wscale)
190 wnd &= ~(1 << tc->rcv_wscale);
192 wnd = 1 << tc->rcv_wscale;
195 tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale);
199 * Write TCP options to segment.
202 tcp_options_write (u8 * data, tcp_options_t * opts)
205 u32 buf, seq_len = 4;
207 if (tcp_opts_mss (opts))
209 *data++ = TCP_OPTION_MSS;
210 *data++ = TCP_OPTION_LEN_MSS;
211 buf = clib_host_to_net_u16 (opts->mss);
212 clib_memcpy (data, &buf, sizeof (opts->mss));
213 data += sizeof (opts->mss);
214 opts_len += TCP_OPTION_LEN_MSS;
217 if (tcp_opts_wscale (opts))
219 *data++ = TCP_OPTION_WINDOW_SCALE;
220 *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
221 *data++ = opts->wscale;
222 opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
225 if (tcp_opts_sack_permitted (opts))
227 *data++ = TCP_OPTION_SACK_PERMITTED;
228 *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
229 opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
232 if (tcp_opts_tstamp (opts))
234 *data++ = TCP_OPTION_TIMESTAMP;
235 *data++ = TCP_OPTION_LEN_TIMESTAMP;
236 buf = clib_host_to_net_u32 (opts->tsval);
237 clib_memcpy (data, &buf, sizeof (opts->tsval));
238 data += sizeof (opts->tsval);
239 buf = clib_host_to_net_u32 (opts->tsecr);
240 clib_memcpy (data, &buf, sizeof (opts->tsecr));
241 data += sizeof (opts->tsecr);
242 opts_len += TCP_OPTION_LEN_TIMESTAMP;
245 if (tcp_opts_sack (opts))
248 u32 n_sack_blocks = clib_min (vec_len (opts->sacks),
249 TCP_OPTS_MAX_SACK_BLOCKS);
251 if (n_sack_blocks != 0)
253 *data++ = TCP_OPTION_SACK_BLOCK;
254 *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
255 for (i = 0; i < n_sack_blocks; i++)
257 buf = clib_host_to_net_u32 (opts->sacks[i].start);
258 clib_memcpy (data, &buf, seq_len);
260 buf = clib_host_to_net_u32 (opts->sacks[i].end);
261 clib_memcpy (data, &buf, seq_len);
264 opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
268 /* Terminate TCP options */
271 *data++ = TCP_OPTION_EOL;
272 opts_len += TCP_OPTION_LEN_EOL;
275 /* Pad with zeroes to a u32 boundary */
278 *data++ = TCP_OPTION_NOOP;
279 opts_len += TCP_OPTION_LEN_NOOP;
285 tcp_make_syn_options (tcp_options_t * opts, u8 wnd_scale)
289 opts->flags |= TCP_OPTS_FLAG_MSS;
290 opts->mss = dummy_mtu; /*XXX discover that */
291 len += TCP_OPTION_LEN_MSS;
293 opts->flags |= TCP_OPTS_FLAG_WSCALE;
294 opts->wscale = wnd_scale;
295 len += TCP_OPTION_LEN_WINDOW_SCALE;
297 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
298 opts->tsval = tcp_time_now ();
300 len += TCP_OPTION_LEN_TIMESTAMP;
304 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
305 len += TCP_OPTION_LEN_SACK_PERMITTED;
308 /* Align to needed boundary */
309 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
314 tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts)
318 opts->flags |= TCP_OPTS_FLAG_MSS;
320 len += TCP_OPTION_LEN_MSS;
322 if (tcp_opts_wscale (&tc->rcv_opts))
324 opts->flags |= TCP_OPTS_FLAG_WSCALE;
325 opts->wscale = tc->rcv_wscale;
326 len += TCP_OPTION_LEN_WINDOW_SCALE;
329 if (tcp_opts_tstamp (&tc->rcv_opts))
331 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
332 opts->tsval = tcp_time_now ();
333 opts->tsecr = tc->tsval_recent;
334 len += TCP_OPTION_LEN_TIMESTAMP;
337 if (tcp_opts_sack_permitted (&tc->rcv_opts))
339 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
340 len += TCP_OPTION_LEN_SACK_PERMITTED;
343 /* Align to needed boundary */
344 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
349 tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts)
355 if (tcp_opts_tstamp (&tc->rcv_opts))
357 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
358 opts->tsval = tcp_time_now ();
359 opts->tsecr = tc->tsval_recent;
360 len += TCP_OPTION_LEN_TIMESTAMP;
362 if (tcp_opts_sack_permitted (&tc->rcv_opts))
364 if (vec_len (tc->snd_sacks))
366 opts->flags |= TCP_OPTS_FLAG_SACK;
367 opts->sacks = tc->snd_sacks;
368 opts->n_sack_blocks = clib_min (vec_len (tc->snd_sacks),
369 TCP_OPTS_MAX_SACK_BLOCKS);
370 len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
374 /* Align to needed boundary */
375 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
380 tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
385 case TCP_STATE_ESTABLISHED:
386 case TCP_STATE_FIN_WAIT_1:
387 return tcp_make_established_options (tc, opts);
388 case TCP_STATE_SYN_RCVD:
389 return tcp_make_synack_options (tc, opts);
390 case TCP_STATE_SYN_SENT:
391 return tcp_make_syn_options (opts, tc->rcv_wscale);
393 clib_warning ("Not handled!");
399 * Update snd_mss to reflect the effective segment size that we can send
400 * by taking into account all TCP options, including SACKs
403 tcp_update_snd_mss (tcp_connection_t * tc)
405 /* Compute options to be used for connection. These may be reused when
406 * sending data or to compute the effective mss (snd_mss) */
408 tcp_make_options (tc, &tc->snd_opts, TCP_STATE_ESTABLISHED);
410 /* XXX check if MTU has been updated */
411 tc->snd_mss = clib_min (tc->mss, tc->rcv_opts.mss) - tc->snd_opts_len;
412 ASSERT (tc->snd_mss > 0);
416 tcp_init_mss (tcp_connection_t * tc)
418 u16 default_min_mss = 536;
419 tcp_update_rcv_mss (tc);
421 /* TODO cache mss and consider PMTU discovery */
422 tc->snd_mss = clib_min (tc->rcv_opts.mss, tc->mss);
424 if (tc->snd_mss < 45)
426 clib_warning ("snd mss is 0");
427 /* Assume that at least the min default mss works */
428 tc->snd_mss = default_min_mss;
429 tc->rcv_opts.mss = default_min_mss;
432 /* We should have enough space for 40 bytes of options */
433 ASSERT (tc->snd_mss > 45);
435 /* If we use timestamp option, account for it */
436 if (tcp_opts_tstamp (&tc->rcv_opts))
437 tc->snd_mss -= TCP_OPTION_LEN_TIMESTAMP;
441 tcp_alloc_tx_buffers (tcp_main_t * tm, u8 thread_index, u32 n_free_buffers)
443 vec_validate (tm->tx_buffers[thread_index],
444 vec_len (tm->tx_buffers[thread_index]) + n_free_buffers - 1);
445 _vec_len (tm->tx_buffers[thread_index]) =
446 vlib_buffer_alloc_from_free_list (vlib_get_main (),
447 tm->tx_buffers[thread_index],
449 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
450 /* buffer shortage, report failure */
451 if (vec_len (tm->tx_buffers[thread_index]) == 0)
453 clib_warning ("out of buffers");
460 tcp_get_free_buffer_index (tcp_main_t * tm, u32 * bidx)
463 u32 thread_index = vlib_get_thread_index ();
464 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
466 if (tcp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
469 my_tx_buffers = tm->tx_buffers[thread_index];
470 *bidx = my_tx_buffers[_vec_len (my_tx_buffers) - 1];
471 _vec_len (my_tx_buffers) -= 1;
476 tcp_return_buffer (tcp_main_t * tm)
479 u32 thread_index = vlib_get_thread_index ();
480 my_tx_buffers = tm->tx_buffers[thread_index];
481 _vec_len (my_tx_buffers) += 1;
485 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
487 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
488 vlib_buffer_free_one (vm, b->next_buffer);
491 b->current_length = 0;
492 b->total_length_not_including_first_buffer = 0;
493 vnet_buffer (b)->tcp.flags = 0;
495 /* Leave enough space for headers */
496 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
500 tcp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
502 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
503 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
504 b->total_length_not_including_first_buffer = 0;
505 vnet_buffer (b)->tcp.flags = 0;
507 /* Leave enough space for headers */
508 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
515 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
518 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
519 u8 tcp_opts_len, tcp_hdr_opts_len;
523 wnd = tcp_window_to_advertise (tc, state);
525 /* Make and write options */
526 tcp_opts_len = tcp_make_established_options (tc, snd_opts);
527 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
529 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
530 tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
532 tcp_options_write ((u8 *) (th + 1), snd_opts);
533 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
537 * Convert buffer to ACK
540 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
542 vlib_main_t *vm = vlib_get_main ();
544 tcp_reuse_buffer (vm, b);
545 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
546 TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc);
547 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
548 tc->rcv_las = tc->rcv_nxt;
552 * Convert buffer to FIN-ACK
555 tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b)
557 vlib_main_t *vm = vlib_get_main ();
560 tcp_reuse_buffer (vm, b);
562 flags = TCP_FLAG_FIN | TCP_FLAG_ACK;
563 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, flags);
565 /* Reset flags, make sure ack is sent */
566 vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
572 * Convert buffer to SYN-ACK
575 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
577 vlib_main_t *vm = vlib_get_main ();
578 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
579 u8 tcp_opts_len, tcp_hdr_opts_len;
584 memset (snd_opts, 0, sizeof (*snd_opts));
586 tcp_reuse_buffer (vm, b);
588 /* Set random initial sequence */
589 time_now = tcp_time_now ();
591 tc->iss = random_u32 (&time_now);
592 tc->snd_una = tc->iss;
593 tc->snd_nxt = tc->iss + 1;
594 tc->snd_una_max = tc->snd_nxt;
596 initial_wnd = tcp_initial_window_to_advertise (tc);
598 /* Make and write options */
599 tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
600 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
602 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
603 tc->rcv_nxt, tcp_hdr_opts_len,
604 TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
606 tcp_options_write ((u8 *) (th + 1), snd_opts);
608 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
609 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
611 /* Init retransmit timer */
612 tcp_retransmit_timer_set (tc);
613 TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc);
617 tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
620 u32 *to_next, next_index;
623 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
626 /* Default FIB for now */
627 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
629 /* Send to IP lookup */
630 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
631 f = vlib_get_frame_to_node (vm, next_index);
633 /* Enqueue the packet */
634 to_next = vlib_frame_vector_args (f);
637 vlib_put_frame_to_node (vm, next_index, f);
641 tcp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
644 tcp_main_t *tm = vnet_get_tcp_main ();
645 u32 thread_index = vlib_get_thread_index ();
646 u32 *to_next, next_index;
649 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
652 /* Decide where to send the packet */
653 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
655 /* Initialize the trajectory trace, if configured */
656 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
659 b->pre_data[1] = next_index;
662 /* Get frame to v4/6 output node */
663 f = tm->tx_frames[!is_ip4][thread_index];
666 f = vlib_get_frame_to_node (vm, next_index);
668 tm->tx_frames[!is_ip4][thread_index] = f;
670 to_next = vlib_frame_vector_args (f);
671 to_next[f->n_vectors] = bi;
673 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
675 vlib_put_frame_to_node (vm, next_index, f);
676 tm->tx_frames[!is_ip4][thread_index] = 0;
681 tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4)
683 tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 0);
687 tcp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
690 tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
694 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0,
695 tcp_state_t state, u8 thread_index, u8 is_ip4)
700 ip4_address_t src_ip40, dst_ip40;
701 ip6_address_t src_ip60, dst_ip60;
702 u16 src_port, dst_port;
707 /* Find IP and TCP headers */
708 th0 = tcp_buffer_hdr (b0);
710 /* Save src and dst ip */
713 ih4 = vlib_buffer_get_current (b0);
714 ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
715 src_ip40.as_u32 = ih4->src_address.as_u32;
716 dst_ip40.as_u32 = ih4->dst_address.as_u32;
720 ih6 = vlib_buffer_get_current (b0);
721 ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
722 clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
723 clib_memcpy (&dst_ip60, &ih6->dst_address, sizeof (ip6_address_t));
726 src_port = th0->src_port;
727 dst_port = th0->dst_port;
729 /* Try to determine what/why we're actually resetting */
730 if (state == TCP_STATE_CLOSED)
735 tmp = clib_net_to_host_u32 (th0->seq_number);
737 /* Got a SYN for no listener. */
738 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
739 ack = clib_host_to_net_u32 (tmp + 1);
744 flags = TCP_FLAG_RST;
745 seq = th0->ack_number;
749 tcp_reuse_buffer (vm, b0);
750 th0 = vlib_buffer_push_tcp_net_order (b0, dst_port, src_port, seq, ack,
751 sizeof (tcp_header_t), flags, 0);
755 ih4 = vlib_buffer_push_ip4 (vm, b0, &dst_ip40, &src_ip40,
757 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
762 ih6 = vlib_buffer_push_ip6 (vm, b0, &dst_ip60, &src_ip60,
764 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
772 * Send reset without reusing existing buffer
774 * It extracts connection info out of original packet
777 tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt, u8 is_ip4)
781 tcp_main_t *tm = vnet_get_tcp_main ();
782 vlib_main_t *vm = vlib_get_main ();
783 u8 tcp_hdr_len, flags = 0;
784 tcp_header_t *th, *pkt_th;
786 ip4_header_t *ih4, *pkt_ih4;
787 ip6_header_t *ih6, *pkt_ih6;
789 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
792 b = vlib_get_buffer (vm, bi);
793 tcp_init_buffer (vm, b);
795 /* Make and write options */
796 tcp_hdr_len = sizeof (tcp_header_t);
800 pkt_ih4 = vlib_buffer_get_current (pkt);
801 pkt_th = ip4_next_header (pkt_ih4);
805 pkt_ih6 = vlib_buffer_get_current (pkt);
806 pkt_th = ip6_next_header (pkt_ih6);
809 if (tcp_ack (pkt_th))
811 flags = TCP_FLAG_RST;
812 seq = pkt_th->ack_number;
813 ack = (tc && tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0;
817 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
819 ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
822 th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
823 seq, ack, tcp_hdr_len, flags, 0);
825 /* Swap src and dst ip */
828 ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
829 ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
830 &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1);
831 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
836 ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
838 ih6 = vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
839 &pkt_ih6->src_address, IP_PROTOCOL_TCP);
840 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
844 tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4);
845 TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
849 * Build and set reset packet for connection
852 tcp_send_reset (tcp_connection_t * tc)
854 vlib_main_t *vm = vlib_get_main ();
855 tcp_main_t *tm = vnet_get_tcp_main ();
859 u16 tcp_hdr_opts_len, advertise_wnd, opts_write_len;
862 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
864 b = vlib_get_buffer (vm, bi);
865 tcp_init_buffer (vm, b);
867 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
868 tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
869 advertise_wnd = tcp_window_to_advertise (tc, TCP_STATE_ESTABLISHED);
870 flags = TCP_FLAG_RST;
871 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
872 tc->rcv_nxt, tcp_hdr_opts_len, flags,
874 opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
875 ASSERT (opts_write_len == tc->snd_opts_len);
876 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
877 tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4);
881 tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b)
883 tcp_header_t *th = vlib_buffer_get_current (b);
884 vlib_main_t *vm = vlib_get_main ();
888 ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4,
889 &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1);
890 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih);
897 ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
898 &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
899 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus);
907 * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
908 * The packet is not forwarded through tcpx_output to avoid doing lookups
909 * in the half_open pool.
912 tcp_send_syn (tcp_connection_t * tc)
916 tcp_main_t *tm = vnet_get_tcp_main ();
917 vlib_main_t *vm = vlib_get_main ();
918 u8 tcp_hdr_opts_len, tcp_opts_len;
922 tcp_options_t snd_opts;
924 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
927 b = vlib_get_buffer (vm, bi);
928 tcp_init_buffer (vm, b);
930 /* Set random initial sequence */
931 time_now = tcp_time_now ();
933 tc->iss = random_u32 (&time_now);
934 tc->snd_una = tc->iss;
935 tc->snd_una_max = tc->snd_nxt = tc->iss + 1;
937 initial_wnd = tcp_initial_window_to_advertise (tc);
939 /* Make and write options */
940 memset (&snd_opts, 0, sizeof (snd_opts));
941 tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
942 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
944 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
945 tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
948 tcp_options_write ((u8 *) (th + 1), &snd_opts);
950 /* Measure RTT with this */
951 tc->rtt_ts = tcp_time_now ();
952 tc->rtt_seq = tc->snd_nxt;
954 /* Start retransmit trimer */
955 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK);
958 /* Set the connection establishment timer */
959 tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME);
961 tcp_push_ip_hdr (tm, tc, b);
962 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
963 TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc);
967 * Flush tx frame populated by retransmits and timer pops
970 tcp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
972 if (tcp_main.tx_frames[!is_ip4][thread_index])
975 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
976 vlib_put_frame_to_node (vm, next_index,
977 tcp_main.tx_frames[!is_ip4][thread_index]);
978 tcp_main.tx_frames[!is_ip4][thread_index] = 0;
983 * Flush both v4 and v6 tx frames for thread index
986 tcp_flush_frames_to_output (u8 thread_index)
988 vlib_main_t *vm = vlib_get_main ();
989 tcp_flush_frame_to_output (vm, thread_index, 1);
990 tcp_flush_frame_to_output (vm, thread_index, 0);
997 tcp_send_fin (tcp_connection_t * tc)
1001 tcp_main_t *tm = vnet_get_tcp_main ();
1002 vlib_main_t *vm = vlib_get_main ();
1004 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1006 b = vlib_get_buffer (vm, bi);
1007 /* buffer will be initialized by in tcp_make_fin */
1008 tcp_make_fin (tc, b);
1009 tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4);
1010 tc->flags |= TCP_CONN_FINSNT;
1011 tc->flags &= ~TCP_CONN_FINPNDG;
1012 tcp_retransmit_timer_force_update (tc);
1013 TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc);
1017 tcp_make_state_flags (tcp_connection_t * tc, tcp_state_t next_state)
1021 case TCP_STATE_ESTABLISHED:
1022 return TCP_FLAG_ACK;
1023 case TCP_STATE_SYN_RCVD:
1024 return TCP_FLAG_SYN | TCP_FLAG_ACK;
1025 case TCP_STATE_SYN_SENT:
1026 return TCP_FLAG_SYN;
1027 case TCP_STATE_LAST_ACK:
1028 case TCP_STATE_FIN_WAIT_1:
1029 if (tc->snd_nxt + 1 < tc->snd_una_max)
1030 return TCP_FLAG_ACK;
1032 return TCP_FLAG_FIN;
1034 clib_warning ("Shouldn't be here!");
1040 * Push TCP header and update connection variables
1043 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b,
1044 tcp_state_t next_state, u8 compute_opts)
1046 u32 advertise_wnd, data_len;
1047 u8 tcp_hdr_opts_len, opts_write_len, flags;
1050 data_len = b->current_length + b->total_length_not_including_first_buffer;
1051 ASSERT (!b->total_length_not_including_first_buffer
1052 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1053 vnet_buffer (b)->tcp.flags = 0;
1056 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1058 tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
1059 advertise_wnd = tcp_window_to_advertise (tc, next_state);
1060 flags = tcp_make_state_flags (tc, next_state);
1062 /* Push header and options */
1063 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
1064 tc->rcv_nxt, tcp_hdr_opts_len, flags,
1066 opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
1068 ASSERT (opts_write_len == tc->snd_opts_len);
1069 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
1072 * Update connection variables
1075 tc->snd_nxt += data_len;
1076 tc->rcv_las = tc->rcv_nxt;
1078 /* TODO this is updated in output as well ... */
1079 if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1081 tc->snd_una_max = tc->snd_nxt;
1082 tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1085 TCP_EVT_DBG (TCP_EVT_PKTIZE, tc);
1089 tcp_send_ack (tcp_connection_t * tc)
1091 tcp_main_t *tm = vnet_get_tcp_main ();
1092 vlib_main_t *vm = vlib_get_main ();
1098 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1100 b = vlib_get_buffer (vm, bi);
1102 /* Fill in the ACK */
1103 tcp_make_ack (tc, b);
1104 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1108 * Delayed ack timer handler
1110 * Sends delayed ACK when timer expires
1113 tcp_timer_delack_handler (u32 index)
1115 u32 thread_index = vlib_get_thread_index ();
1116 tcp_connection_t *tc;
1118 tc = tcp_connection_get (index, thread_index);
1119 tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
1124 * Build a retransmit segment
1126 * @return the number of bytes in the segment or 0 if there's nothing to
1130 tcp_prepare_retransmit_segment (tcp_connection_t * tc, u32 offset,
1131 u32 max_deq_bytes, vlib_buffer_t ** b)
1133 tcp_main_t *tm = vnet_get_tcp_main ();
1134 vlib_main_t *vm = vlib_get_main ();
1136 u32 start, bi, available_bytes, seg_size;
1139 ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
1140 ASSERT (max_deq_bytes != 0);
1143 * Make sure we can retransmit something
1145 available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1146 available_bytes -= offset;
1147 if (!available_bytes)
1149 max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes);
1150 max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1152 /* Start is beyond snd_congestion */
1153 start = tc->snd_una + offset;
1154 if (seq_geq (start, tc->snd_congestion))
1157 /* Don't overshoot snd_congestion */
1158 if (seq_gt (start + max_deq_bytes, tc->snd_congestion))
1160 max_deq_bytes = tc->snd_congestion - start;
1161 if (max_deq_bytes == 0)
1165 seg_size = max_deq_bytes + MAX_HDRS_LEN;
1170 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1173 * Allocate and fill in buffer(s)
1176 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1178 *b = vlib_get_buffer (vm, bi);
1179 data = tcp_init_buffer (vm, *b);
1181 /* Easy case, buffer size greater than mss */
1182 if (PREDICT_TRUE (seg_size <= tm->bytes_per_buffer))
1184 n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1186 ASSERT (n_bytes == max_deq_bytes);
1187 b[0]->current_length = n_bytes;
1188 tcp_push_hdr_i (tc, *b, tc->state, 0);
1190 /* Split mss into multiple buffers */
1193 u32 chain_bi = ~0, n_bufs_per_seg;
1194 u32 thread_index = vlib_get_thread_index ();
1195 u16 n_peeked, len_to_deq, available_bufs;
1196 vlib_buffer_t *chain_b, *prev_b;
1199 n_bufs_per_seg = ceil ((double) seg_size / tm->bytes_per_buffer);
1201 /* Make sure we have enough buffers */
1202 available_bufs = vec_len (tm->tx_buffers[thread_index]);
1203 if (n_bufs_per_seg > available_bufs)
1205 if (tcp_alloc_tx_buffers (tm, thread_index,
1206 VLIB_FRAME_SIZE - available_bufs))
1208 tcp_return_buffer (tm);
1213 n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1214 tm->bytes_per_buffer -
1216 b[0]->current_length = n_bytes;
1217 b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1218 b[0]->total_length_not_including_first_buffer = 0;
1219 max_deq_bytes -= n_bytes;
1222 for (i = 1; i < n_bufs_per_seg; i++)
1225 len_to_deq = clib_min (max_deq_bytes, tm->bytes_per_buffer);
1226 tcp_get_free_buffer_index (tm, &chain_bi);
1227 ASSERT (chain_bi != (u32) ~ 0);
1228 chain_b = vlib_get_buffer (vm, chain_bi);
1229 chain_b->current_data = 0;
1230 data = vlib_buffer_get_current (chain_b);
1231 n_peeked = stream_session_peek_bytes (&tc->connection, data,
1232 offset + n_bytes, len_to_deq);
1233 ASSERT (n_peeked == len_to_deq);
1234 n_bytes += n_peeked;
1235 chain_b->current_length = n_peeked;
1237 chain_b->next_buffer = 0;
1239 /* update previous buffer */
1240 prev_b->next_buffer = chain_bi;
1241 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
1243 max_deq_bytes -= n_peeked;
1244 b[0]->total_length_not_including_first_buffer += n_peeked;
1247 tcp_push_hdr_i (tc, *b, tc->state, 0);
1250 ASSERT (n_bytes > 0);
1251 ASSERT (((*b)->current_data + (*b)->current_length) <=
1252 tm->bytes_per_buffer);
1254 if (tcp_in_fastrecovery (tc))
1255 tc->snd_rxt_bytes += n_bytes;
1258 TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes);
1263 * Reset congestion control, switch cwnd to loss window and try again.
1266 tcp_rtx_timeout_cc (tcp_connection_t * tc)
1268 tc->prev_ssthresh = tc->ssthresh;
1269 tc->prev_cwnd = tc->cwnd;
1271 /* Cleanly recover cc (also clears up fast retransmit) */
1272 if (tcp_in_fastrecovery (tc))
1273 tcp_cc_fastrecovery_exit (tc);
1275 /* Start again from the beginning */
1276 tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
1277 tc->cwnd = tcp_loss_wnd (tc);
1278 tc->snd_congestion = tc->snd_una_max;
1280 tcp_recovery_on (tc);
1284 tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
1286 tcp_main_t *tm = vnet_get_tcp_main ();
1287 vlib_main_t *vm = vlib_get_main ();
1288 u32 thread_index = vlib_get_thread_index ();
1289 tcp_connection_t *tc;
1290 vlib_buffer_t *b = 0;
1295 tc = tcp_half_open_connection_get (index);
1296 tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
1300 tc = tcp_connection_get (index, thread_index);
1301 tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
1304 if (!tcp_in_recovery (tc) && tc->rto_boff > 0
1305 && tc->state >= TCP_STATE_ESTABLISHED)
1308 tcp_update_rto (tc);
1311 /* Increment RTO backoff (also equal to number of retries) */
1314 /* Go back to first un-acked byte */
1315 tc->snd_nxt = tc->snd_una;
1317 if (tc->state >= TCP_STATE_ESTABLISHED)
1319 /* Lost FIN, retransmit and return */
1320 if (tcp_is_lost_fin (tc))
1326 /* First retransmit timeout */
1327 if (tc->rto_boff == 1)
1328 tcp_rtx_timeout_cc (tc);
1330 /* Exponential backoff */
1331 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1333 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
1335 /* Send one segment */
1336 n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1338 bi = vlib_get_buffer_index (vm, b);
1339 /* TODO be less aggressive about this */
1340 scoreboard_clear (&tc->sack_sb);
1344 clib_warning ("could not retransmit anything");
1345 clib_warning ("%U", format_tcp_connection, tc, 2);
1347 /* Try again eventually */
1348 tcp_retransmit_timer_set (tc);
1349 ASSERT (0 || (tc->rto_boff > 1
1350 && tc->snd_una == tc->snd_congestion));
1354 /* For first retransmit, record timestamp (Eifel detection RFC3522) */
1355 if (tc->rto_boff == 1)
1356 tc->snd_rxt_ts = tcp_time_now ();
1358 /* Retransmit for SYN/SYNACK */
1359 else if (tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_SYN_SENT)
1361 /* Half-open connection actually moved to established but we were
1362 * waiting for syn retransmit to pop to call cleanup from the right
1364 if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
1366 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1367 if (tcp_half_open_connection_cleanup (tc))
1369 clib_warning ("could not remove half-open connection");
1375 /* Try without increasing RTO a number of times. If this fails,
1376 * start growing RTO exponentially */
1377 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1378 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1380 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1382 b = vlib_get_buffer (vm, bi);
1383 tcp_init_buffer (vm, b);
1384 tcp_push_hdr_i (tc, b, tc->state, 1);
1386 /* Account for the SYN */
1389 TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc,
1390 (tc->state == TCP_STATE_SYN_SENT ? 0 : 1));
1394 ASSERT (tc->state == TCP_STATE_CLOSED);
1395 clib_warning ("connection closed ...");
1401 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1403 /* Re-enable retransmit timer */
1404 tcp_retransmit_timer_set (tc);
1408 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1410 /* This goes straight to ipx_lookup */
1411 tcp_push_ip_hdr (tm, tc, b);
1412 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
1414 /* Re-enable retransmit timer */
1415 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN,
1416 tc->rto * TCP_TO_TIMER_TICK);
1421 tcp_timer_retransmit_handler (u32 index)
1423 tcp_timer_retransmit_handler_i (index, 0);
1427 tcp_timer_retransmit_syn_handler (u32 index)
1429 tcp_timer_retransmit_handler_i (index, 1);
1433 * Got 0 snd_wnd from peer, try to do something about it.
1437 tcp_timer_persist_handler (u32 index)
1439 tcp_main_t *tm = vnet_get_tcp_main ();
1440 vlib_main_t *vm = vlib_get_main ();
1441 u32 thread_index = vlib_get_thread_index ();
1442 tcp_connection_t *tc;
1444 u32 bi, old_snd_nxt, max_snd_bytes, available_bytes, offset;
1448 tc = tcp_connection_get_if_valid (index, thread_index);
1453 /* Make sure timer handle is set to invalid */
1454 tc->timers[TCP_TIMER_PERSIST] = TCP_TIMER_HANDLE_INVALID;
1455 offset = tc->snd_una_max - tc->snd_una;
1457 /* Problem already solved or worse */
1458 available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1459 if (tc->state == TCP_STATE_CLOSED || tc->state > TCP_STATE_ESTABLISHED
1460 || tc->snd_wnd > tc->snd_mss || tcp_in_recovery (tc)
1461 || !available_bytes || available_bytes <= offset)
1464 /* Increment RTO backoff */
1466 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1469 * Try to force the first unsent segment (or buffer)
1471 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1473 b = vlib_get_buffer (vm, bi);
1474 data = tcp_init_buffer (vm, b);
1476 tcp_validate_txf_size (tc, offset);
1477 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1478 max_snd_bytes = clib_min (tc->snd_mss, tm->bytes_per_buffer - MAX_HDRS_LEN);
1479 n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1481 b->current_length = n_bytes;
1482 ASSERT (n_bytes != 0 && (tc->snd_nxt == tc->snd_una_max || tc->rto_boff > 1
1483 || tcp_timer_is_active (tc,
1484 TCP_TIMER_RETRANSMIT)));
1486 /* Allow updating of snd_una_max but don't update snd_nxt */
1487 old_snd_nxt = tc->snd_nxt;
1488 tcp_push_hdr_i (tc, b, tc->state, 0);
1489 tc->snd_nxt = old_snd_nxt;
1490 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1492 /* Just sent new data, enable retransmit */
1493 tcp_retransmit_timer_update (tc);
1497 * Retransmit first unacked segment
1500 tcp_retransmit_first_unacked (tcp_connection_t * tc)
1502 vlib_main_t *vm = vlib_get_main ();
1504 u32 bi, old_snd_nxt, n_bytes;
1506 old_snd_nxt = tc->snd_nxt;
1507 tc->snd_nxt = tc->snd_una;
1509 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1510 n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1513 bi = vlib_get_buffer_index (vm, b);
1514 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1516 tc->snd_nxt = old_snd_nxt;
1520 * Do fast retransmit with SACKs
1523 tcp_fast_retransmit_sack (tcp_connection_t * tc)
1525 vlib_main_t *vm = vlib_get_main ();
1526 u32 n_written = 0, offset, max_bytes;
1527 vlib_buffer_t *b = 0;
1528 sack_scoreboard_hole_t *hole;
1529 sack_scoreboard_t *sb;
1530 u32 bi, old_snd_nxt;
1532 u8 snd_limited = 0, can_rescue = 0;
1534 ASSERT (tcp_in_fastrecovery (tc));
1535 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1537 old_snd_nxt = tc->snd_nxt;
1539 snd_space = tcp_available_snd_space (tc);
1541 hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
1542 while (hole && snd_space > 0)
1544 hole = scoreboard_next_rxt_hole (sb, hole,
1545 tcp_fastrecovery_sent_1_smss (tc),
1546 &can_rescue, &snd_limited);
1549 if (!can_rescue || !(seq_lt (sb->rescue_rxt, tc->snd_una)
1550 || seq_gt (sb->rescue_rxt,
1551 tc->snd_congestion)))
1554 /* If rescue rxt undefined or less than snd_una then one segment of
1555 * up to SMSS octets that MUST include the highest outstanding
1556 * unSACKed sequence number SHOULD be returned, and RescueRxt set to
1557 * RecoveryPoint. HighRxt MUST NOT be updated.
1559 max_bytes = clib_min (tc->snd_mss,
1560 tc->snd_congestion - tc->snd_una);
1561 max_bytes = clib_min (max_bytes, snd_space);
1562 offset = tc->snd_congestion - tc->snd_una - max_bytes;
1563 sb->rescue_rxt = tc->snd_congestion;
1564 tc->snd_nxt = tc->snd_una + offset;
1565 n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes,
1568 bi = vlib_get_buffer_index (vm, b);
1569 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1573 max_bytes = clib_min (hole->end - sb->high_rxt, snd_space);
1574 max_bytes = snd_limited ? clib_min (max_bytes, tc->snd_mss) : max_bytes;
1577 offset = sb->high_rxt - tc->snd_una;
1578 tc->snd_nxt = sb->high_rxt;
1579 n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes, &b);
1581 /* Nothing left to retransmit */
1585 bi = vlib_get_buffer_index (vm, b);
1586 sb->high_rxt += n_written;
1587 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1588 ASSERT (n_written <= snd_space);
1589 snd_space -= n_written;
1592 /* If window allows, send 1 SMSS of new data */
1593 tc->snd_nxt = old_snd_nxt;
1597 * Fast retransmit without SACK info
1600 tcp_fast_retransmit_no_sack (tcp_connection_t * tc)
1602 vlib_main_t *vm = vlib_get_main ();
1603 u32 n_written = 0, offset = 0, bi, old_snd_nxt;
1607 ASSERT (tcp_in_fastrecovery (tc));
1608 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1610 /* Start resending from first un-acked segment */
1611 old_snd_nxt = tc->snd_nxt;
1612 tc->snd_nxt = tc->snd_una;
1613 snd_space = tcp_available_snd_space (tc);
1615 while (snd_space > 0)
1617 offset += n_written;
1618 n_written = tcp_prepare_retransmit_segment (tc, offset, snd_space, &b);
1620 /* Nothing left to retransmit */
1624 bi = vlib_get_buffer_index (vm, b);
1625 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1626 snd_space -= n_written;
1629 /* Restore snd_nxt. If window allows, send 1 SMSS of new data */
1630 tc->snd_nxt = old_snd_nxt;
1634 * Do fast retransmit
1637 tcp_fast_retransmit (tcp_connection_t * tc)
1639 if (tcp_opts_sack_permitted (&tc->rcv_opts)
1640 && scoreboard_first_hole (&tc->sack_sb))
1641 tcp_fast_retransmit_sack (tc);
1643 tcp_fast_retransmit_no_sack (tc);
1647 tcp_session_has_ooo_data (tcp_connection_t * tc)
1649 stream_session_t *s =
1650 stream_session_get (tc->c_s_index, tc->c_thread_index);
1651 return svm_fifo_has_ooo_data (s->server_rx_fifo);
1655 tcp46_output_inline (vlib_main_t * vm,
1656 vlib_node_runtime_t * node,
1657 vlib_frame_t * from_frame, int is_ip4)
1659 u32 n_left_from, next_index, *from, *to_next;
1660 u32 my_thread_index = vm->thread_index;
1662 from = vlib_frame_vector_args (from_frame);
1663 n_left_from = from_frame->n_vectors;
1664 next_index = node->cached_next_index;
1665 tcp_set_time_now (my_thread_index);
1667 while (n_left_from > 0)
1671 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1673 while (n_left_from > 0 && n_left_to_next > 0)
1677 tcp_connection_t *tc0;
1679 tcp_header_t *th0 = 0;
1680 u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
1687 n_left_to_next -= 1;
1689 b0 = vlib_get_buffer (vm, bi0);
1690 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1692 if (PREDICT_FALSE (tc0 == 0 || tc0->state == TCP_STATE_CLOSED))
1694 error0 = TCP_ERROR_INVALID_CONNECTION;
1695 next0 = TCP_OUTPUT_NEXT_DROP;
1699 th0 = vlib_buffer_get_current (b0);
1700 TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
1704 vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
1705 IP_PROTOCOL_TCP, 1);
1706 b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1707 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1713 ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
1714 &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
1715 b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1716 vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data;
1717 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1721 /* Filter out DUPACKs if there are no OOO segments left */
1723 (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK))
1725 if (!tcp_session_has_ooo_data (tc0))
1727 error0 = TCP_ERROR_FILTERED_DUPACKS;
1728 next0 = TCP_OUTPUT_NEXT_DROP;
1733 /* Stop DELACK timer and fix flags */
1734 tc0->flags &= ~(TCP_CONN_SNDACK);
1735 tcp_timer_reset (tc0, TCP_TIMER_DELACK);
1737 /* If not retransmitting
1738 * 1) update snd_una_max (SYN, SYNACK, FIN)
1739 * 2) If we're not tracking an ACK, start tracking */
1740 if (seq_lt (tc0->snd_una_max, tc0->snd_nxt))
1742 tc0->snd_una_max = tc0->snd_nxt;
1743 if (tc0->rtt_ts == 0)
1745 tc0->rtt_ts = tcp_time_now ();
1746 tc0->rtt_seq = tc0->snd_nxt;
1750 /* Set the retransmit timer if not set already and not
1751 * doing a pure ACK */
1752 if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT)
1753 && tc0->snd_nxt != tc0->snd_una)
1755 tcp_retransmit_timer_set (tc0);
1760 /* Make sure we haven't lost route to our peer */
1761 if (PREDICT_FALSE (tc0->last_fib_check
1762 < tc0->snd_opts.tsval + TCP_FIB_RECHECK_PERIOD))
1765 (tc0->c_rmt_fei == tcp_lookup_rmt_in_fib (tc0)))
1767 tc0->last_fib_check = tc0->snd_opts.tsval;
1771 clib_warning ("lost connection to peer");
1772 tcp_connection_reset (tc0);
1777 /* Use pre-computed dpo to set next node */
1778 next0 = tc0->c_rmt_dpo.dpoi_next_node;
1779 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = tc0->c_rmt_dpo.dpoi_index;
1782 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1783 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1785 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1787 b0->error = node->errors[error0];
1788 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1790 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1793 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1797 memset (&t0->tcp_header, 0, sizeof (t0->tcp_header));
1799 clib_memcpy (&t0->tcp_connection, tc0,
1800 sizeof (t0->tcp_connection));
1803 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1804 n_left_to_next, bi0, next0);
1807 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1810 return from_frame->n_vectors;
1814 tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1815 vlib_frame_t * from_frame)
1817 return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1821 tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1822 vlib_frame_t * from_frame)
1824 return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1828 VLIB_REGISTER_NODE (tcp4_output_node) =
1830 .function = tcp4_output,.name = "tcp4-output",
1831 /* Takes a vector of packets. */
1832 .vector_size = sizeof (u32),
1833 .n_errors = TCP_N_ERROR,
1834 .error_strings = tcp_error_strings,
1835 .n_next_nodes = TCP_OUTPUT_N_NEXT,
1837 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1838 foreach_tcp4_output_next
1841 .format_buffer = format_tcp_header,
1842 .format_trace = format_tcp_tx_trace,
1846 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output);
1849 VLIB_REGISTER_NODE (tcp6_output_node) =
1851 .function = tcp6_output,
1852 .name = "tcp6-output",
1853 /* Takes a vector of packets. */
1854 .vector_size = sizeof (u32),
1855 .n_errors = TCP_N_ERROR,
1856 .error_strings = tcp_error_strings,
1857 .n_next_nodes = TCP_OUTPUT_N_NEXT,
1859 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1860 foreach_tcp6_output_next
1863 .format_buffer = format_tcp_header,
1864 .format_trace = format_tcp_tx_trace,
1868 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output);
1871 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1873 tcp_connection_t *tc;
1875 tc = (tcp_connection_t *) tconn;
1876 tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED, 0);
1877 ASSERT (seq_leq (tc->snd_una_max, tc->snd_una + tc->snd_wnd));
1879 if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc))
1881 tc->rtt_ts = tcp_time_now ();
1882 tc->rtt_seq = tc->snd_nxt;
1887 typedef enum _tcp_reset_next
1889 TCP_RESET_NEXT_DROP,
1890 TCP_RESET_NEXT_IP_LOOKUP,
1894 #define foreach_tcp4_reset_next \
1895 _(DROP, "error-drop") \
1896 _(IP_LOOKUP, "ip4-lookup")
1898 #define foreach_tcp6_reset_next \
1899 _(DROP, "error-drop") \
1900 _(IP_LOOKUP, "ip6-lookup")
1903 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1904 vlib_frame_t * from_frame, u8 is_ip4)
1906 u32 n_left_from, next_index, *from, *to_next;
1907 u32 my_thread_index = vm->thread_index;
1909 from = vlib_frame_vector_args (from_frame);
1910 n_left_from = from_frame->n_vectors;
1912 next_index = node->cached_next_index;
1914 while (n_left_from > 0)
1918 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1920 while (n_left_from > 0 && n_left_to_next > 0)
1926 u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
1933 n_left_to_next -= 1;
1935 b0 = vlib_get_buffer (vm, bi0);
1937 if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
1938 my_thread_index, is_ip4))
1940 error0 = TCP_ERROR_LOOKUP_DROPS;
1941 next0 = TCP_RESET_NEXT_DROP;
1945 /* Prepare to send to IP lookup */
1946 vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
1947 next0 = TCP_RESET_NEXT_IP_LOOKUP;
1950 b0->error = node->errors[error0];
1951 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1952 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1954 th0 = vlib_buffer_get_current (b0);
1956 th0 = ip4_next_header ((ip4_header_t *) th0);
1958 th0 = ip6_next_header ((ip6_header_t *) th0);
1959 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1960 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1963 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1964 n_left_to_next, bi0, next0);
1966 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1968 return from_frame->n_vectors;
1972 tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1973 vlib_frame_t * from_frame)
1975 return tcp46_send_reset_inline (vm, node, from_frame, 1);
1979 tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1980 vlib_frame_t * from_frame)
1982 return tcp46_send_reset_inline (vm, node, from_frame, 0);
1986 VLIB_REGISTER_NODE (tcp4_reset_node) = {
1987 .function = tcp4_send_reset,
1988 .name = "tcp4-reset",
1989 .vector_size = sizeof (u32),
1990 .n_errors = TCP_N_ERROR,
1991 .error_strings = tcp_error_strings,
1992 .n_next_nodes = TCP_RESET_N_NEXT,
1994 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1995 foreach_tcp4_reset_next
1998 .format_trace = format_tcp_tx_trace,
2002 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_reset_node, tcp4_send_reset);
2005 VLIB_REGISTER_NODE (tcp6_reset_node) = {
2006 .function = tcp6_send_reset,
2007 .name = "tcp6-reset",
2008 .vector_size = sizeof (u32),
2009 .n_errors = TCP_N_ERROR,
2010 .error_strings = tcp_error_strings,
2011 .n_next_nodes = TCP_RESET_N_NEXT,
2013 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2014 foreach_tcp6_reset_next
2017 .format_trace = format_tcp_tx_trace,
2021 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_reset_node, tcp6_send_reset);
2024 * fd.io coding-style-patch-verification: ON
2027 * eval: (c-set-style "gnu")