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], n_free_buffers - 1);
444 _vec_len (tm->tx_buffers[thread_index]) =
445 vlib_buffer_alloc_from_free_list (vlib_get_main (),
446 tm->tx_buffers[thread_index],
448 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
449 /* buffer shortage, report failure */
450 if (vec_len (tm->tx_buffers[thread_index]) == 0)
452 clib_warning ("out of buffers");
459 tcp_get_free_buffer_index (tcp_main_t * tm, u32 * bidx)
462 u32 thread_index = vlib_get_thread_index ();
463 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
465 if (tcp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
468 my_tx_buffers = tm->tx_buffers[thread_index];
469 *bidx = my_tx_buffers[_vec_len (my_tx_buffers) - 1];
470 _vec_len (my_tx_buffers) -= 1;
475 tcp_return_buffer (tcp_main_t * tm)
478 u32 thread_index = vlib_get_thread_index ();
479 my_tx_buffers = tm->tx_buffers[thread_index];
480 _vec_len (my_tx_buffers) += 1;
484 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
486 vlib_buffer_t *it = b;
487 u32 save_free_list = b->flags & VLIB_BUFFER_FREE_LIST_INDEX_MASK;
490 it->current_data = 0;
491 it->current_length = 0;
492 it->total_length_not_including_first_buffer = 0;
494 while ((it->flags & VLIB_BUFFER_NEXT_PRESENT)
495 && (it = vlib_get_buffer (vm, it->next_buffer)));
497 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
498 vlib_buffer_free_one (vm, b->next_buffer);
499 b->flags = save_free_list;
501 /* Leave enough space for headers */
502 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
503 vnet_buffer (b)->tcp.flags = 0;
510 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
513 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
514 u8 tcp_opts_len, tcp_hdr_opts_len;
518 wnd = tcp_window_to_advertise (tc, state);
520 /* Make and write options */
521 tcp_opts_len = tcp_make_established_options (tc, snd_opts);
522 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
524 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
525 tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
527 tcp_options_write ((u8 *) (th + 1), snd_opts);
528 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
532 * Convert buffer to ACK
535 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
537 vlib_main_t *vm = vlib_get_main ();
539 tcp_reuse_buffer (vm, b);
540 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
541 TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc);
542 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
543 tc->rcv_las = tc->rcv_nxt;
547 * Convert buffer to FIN-ACK
550 tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b)
552 vlib_main_t *vm = vlib_get_main ();
555 tcp_reuse_buffer (vm, b);
557 flags = TCP_FLAG_FIN | TCP_FLAG_ACK;
558 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, flags);
560 /* Reset flags, make sure ack is sent */
561 vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
567 * Convert buffer to SYN-ACK
570 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
572 vlib_main_t *vm = vlib_get_main ();
573 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
574 u8 tcp_opts_len, tcp_hdr_opts_len;
579 memset (snd_opts, 0, sizeof (*snd_opts));
581 tcp_reuse_buffer (vm, b);
583 /* Set random initial sequence */
584 time_now = tcp_time_now ();
586 tc->iss = random_u32 (&time_now);
587 tc->snd_una = tc->iss;
588 tc->snd_nxt = tc->iss + 1;
589 tc->snd_una_max = tc->snd_nxt;
591 initial_wnd = tcp_initial_window_to_advertise (tc);
593 /* Make and write options */
594 tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
595 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
597 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
598 tc->rcv_nxt, tcp_hdr_opts_len,
599 TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
601 tcp_options_write ((u8 *) (th + 1), snd_opts);
603 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
604 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
606 /* Init retransmit timer */
607 tcp_retransmit_timer_set (tc);
608 TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc);
612 tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
615 u32 *to_next, next_index;
618 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
621 /* Default FIB for now */
622 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
624 /* Send to IP lookup */
625 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
626 f = vlib_get_frame_to_node (vm, next_index);
628 /* Enqueue the packet */
629 to_next = vlib_frame_vector_args (f);
632 vlib_put_frame_to_node (vm, next_index, f);
636 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0,
637 tcp_state_t state, u8 thread_index, u8 is_ip4)
642 ip4_address_t src_ip40, dst_ip40;
643 ip6_address_t src_ip60, dst_ip60;
644 u16 src_port, dst_port;
649 /* Find IP and TCP headers */
650 th0 = tcp_buffer_hdr (b0);
652 /* Save src and dst ip */
655 ih4 = vlib_buffer_get_current (b0);
656 ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
657 src_ip40.as_u32 = ih4->src_address.as_u32;
658 dst_ip40.as_u32 = ih4->dst_address.as_u32;
662 ih6 = vlib_buffer_get_current (b0);
663 ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
664 clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
665 clib_memcpy (&dst_ip60, &ih6->dst_address, sizeof (ip6_address_t));
668 src_port = th0->src_port;
669 dst_port = th0->dst_port;
671 /* Try to determine what/why we're actually resetting */
672 if (state == TCP_STATE_CLOSED)
677 tmp = clib_net_to_host_u32 (th0->seq_number);
679 /* Got a SYN for no listener. */
680 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
681 ack = clib_host_to_net_u32 (tmp + 1);
686 flags = TCP_FLAG_RST;
687 seq = th0->ack_number;
691 tcp_reuse_buffer (vm, b0);
692 th0 = vlib_buffer_push_tcp_net_order (b0, dst_port, src_port, seq, ack,
693 sizeof (tcp_header_t), flags, 0);
697 ih4 = vlib_buffer_push_ip4 (vm, b0, &dst_ip40, &src_ip40,
699 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
704 ih6 = vlib_buffer_push_ip6 (vm, b0, &dst_ip60, &src_ip60,
706 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
714 * Send reset without reusing existing buffer
717 tcp_send_reset (tcp_connection_t * tc, vlib_buffer_t * pkt, u8 is_ip4)
721 tcp_main_t *tm = vnet_get_tcp_main ();
722 vlib_main_t *vm = vlib_get_main ();
723 u8 tcp_hdr_len, flags = 0;
724 tcp_header_t *th, *pkt_th;
726 ip4_header_t *ih4, *pkt_ih4;
727 ip6_header_t *ih6, *pkt_ih6;
729 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
732 b = vlib_get_buffer (vm, bi);
734 /* Leave enough space for headers */
735 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
737 /* Make and write options */
738 tcp_hdr_len = sizeof (tcp_header_t);
742 pkt_ih4 = vlib_buffer_get_current (pkt);
743 pkt_th = ip4_next_header (pkt_ih4);
747 pkt_ih6 = vlib_buffer_get_current (pkt);
748 pkt_th = ip6_next_header (pkt_ih6);
751 if (tcp_ack (pkt_th))
753 flags = TCP_FLAG_RST;
754 seq = pkt_th->ack_number;
755 ack = (tc && tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0;
759 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
761 ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
764 th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
765 seq, ack, tcp_hdr_len, flags, 0);
767 /* Swap src and dst ip */
770 ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
771 ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
772 &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1);
773 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
778 pkt_ih6 = (ip6_header_t *) (pkt_th - 1);
779 ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
782 vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
783 &pkt_ih6->src_address, IP_PROTOCOL_TCP);
784 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
788 tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4);
789 TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
793 tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b)
795 tcp_header_t *th = vlib_buffer_get_current (b);
796 vlib_main_t *vm = vlib_get_main ();
800 ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4,
801 &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1);
802 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih);
809 ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
810 &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
811 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus);
819 * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
820 * The packet is not forwarded through tcpx_output to avoid doing lookups
821 * in the half_open pool.
824 tcp_send_syn (tcp_connection_t * tc)
828 tcp_main_t *tm = vnet_get_tcp_main ();
829 vlib_main_t *vm = vlib_get_main ();
830 u8 tcp_hdr_opts_len, tcp_opts_len;
834 tcp_options_t snd_opts;
836 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
839 b = vlib_get_buffer (vm, bi);
841 /* Leave enough space for headers */
842 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
844 /* Set random initial sequence */
845 time_now = tcp_time_now ();
847 tc->iss = random_u32 (&time_now);
848 tc->snd_una = tc->iss;
849 tc->snd_una_max = tc->snd_nxt = tc->iss + 1;
851 initial_wnd = tcp_initial_window_to_advertise (tc);
853 /* Make and write options */
854 memset (&snd_opts, 0, sizeof (snd_opts));
855 tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
856 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
858 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
859 tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
862 tcp_options_write ((u8 *) (th + 1), &snd_opts);
864 /* Measure RTT with this */
865 tc->rtt_ts = tcp_time_now ();
866 tc->rtt_seq = tc->snd_nxt;
868 /* Start retransmit trimer */
869 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK);
872 /* Set the connection establishment timer */
873 tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME);
875 tcp_push_ip_hdr (tm, tc, b);
876 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
877 TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc);
881 tcp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
884 tcp_main_t *tm = vnet_get_tcp_main ();
885 u32 thread_index = vlib_get_thread_index ();
886 u32 *to_next, next_index;
889 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
892 /* Decide where to send the packet */
893 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
895 /* Initialize the trajectory trace, if configured */
896 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
899 b->pre_data[1] = next_index;
902 /* Get frame to v4/6 output node */
903 f = tm->tx_frames[!is_ip4][thread_index];
906 f = vlib_get_frame_to_node (vm, next_index);
908 tm->tx_frames[!is_ip4][thread_index] = f;
910 to_next = vlib_frame_vector_args (f);
911 to_next[f->n_vectors] = bi;
913 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
915 vlib_put_frame_to_node (vm, next_index, f);
916 tm->tx_frames[!is_ip4][thread_index] = 0;
921 tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4)
923 tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 0);
927 tcp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
930 tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
934 * Flush tx frame populated by retransmits and timer pops
937 tcp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
939 if (tcp_main.tx_frames[!is_ip4][thread_index])
942 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
943 vlib_put_frame_to_node (vm, next_index,
944 tcp_main.tx_frames[!is_ip4][thread_index]);
945 tcp_main.tx_frames[!is_ip4][thread_index] = 0;
950 * Flush both v4 and v6 tx frames for thread index
953 tcp_flush_frames_to_output (u8 thread_index)
955 vlib_main_t *vm = vlib_get_main ();
956 tcp_flush_frame_to_output (vm, thread_index, 1);
957 tcp_flush_frame_to_output (vm, thread_index, 0);
964 tcp_send_fin (tcp_connection_t * tc)
968 tcp_main_t *tm = vnet_get_tcp_main ();
969 vlib_main_t *vm = vlib_get_main ();
971 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
973 b = vlib_get_buffer (vm, bi);
975 tcp_make_fin (tc, b);
976 tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4);
977 tc->flags |= TCP_CONN_FINSNT;
978 tc->flags &= ~TCP_CONN_FINPNDG;
979 tcp_retransmit_timer_force_update (tc);
980 TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc);
984 tcp_make_state_flags (tcp_connection_t * tc, tcp_state_t next_state)
988 case TCP_STATE_ESTABLISHED:
990 case TCP_STATE_SYN_RCVD:
991 return TCP_FLAG_SYN | TCP_FLAG_ACK;
992 case TCP_STATE_SYN_SENT:
994 case TCP_STATE_LAST_ACK:
995 case TCP_STATE_FIN_WAIT_1:
996 if (tc->snd_nxt + 1 < tc->snd_una_max)
1001 clib_warning ("Shouldn't be here!");
1007 * Push TCP header and update connection variables
1010 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b,
1011 tcp_state_t next_state, u8 compute_opts)
1013 u32 advertise_wnd, data_len;
1014 u8 tcp_hdr_opts_len, opts_write_len, flags;
1017 data_len = b->current_length + b->total_length_not_including_first_buffer;
1018 vnet_buffer (b)->tcp.flags = 0;
1021 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1023 tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
1024 advertise_wnd = tcp_window_to_advertise (tc, next_state);
1025 flags = tcp_make_state_flags (tc, next_state);
1027 /* Push header and options */
1028 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
1029 tc->rcv_nxt, tcp_hdr_opts_len, flags,
1031 opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
1033 ASSERT (opts_write_len == tc->snd_opts_len);
1034 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
1037 * Update connection variables
1040 tc->snd_nxt += data_len;
1041 tc->rcv_las = tc->rcv_nxt;
1043 /* TODO this is updated in output as well ... */
1044 if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1046 tc->snd_una_max = tc->snd_nxt;
1047 tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1050 TCP_EVT_DBG (TCP_EVT_PKTIZE, tc);
1054 tcp_send_ack (tcp_connection_t * tc)
1056 tcp_main_t *tm = vnet_get_tcp_main ();
1057 vlib_main_t *vm = vlib_get_main ();
1063 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1065 b = vlib_get_buffer (vm, bi);
1067 /* Fill in the ACK */
1068 tcp_make_ack (tc, b);
1069 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1073 * Delayed ack timer handler
1075 * Sends delayed ACK when timer expires
1078 tcp_timer_delack_handler (u32 index)
1080 u32 thread_index = vlib_get_thread_index ();
1081 tcp_connection_t *tc;
1083 tc = tcp_connection_get (index, thread_index);
1084 tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
1089 * Build a retransmit segment
1091 * @return the number of bytes in the segment or 0 if there's nothing to
1095 tcp_prepare_retransmit_segment (tcp_connection_t * tc, u32 offset,
1096 u32 max_deq_bytes, vlib_buffer_t ** b)
1098 tcp_main_t *tm = vnet_get_tcp_main ();
1099 vlib_main_t *vm = vlib_get_main ();
1101 u32 start, bi, available_bytes;
1103 ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
1104 ASSERT (max_deq_bytes != 0);
1107 * Make sure we can retransmit something
1109 max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes);
1110 available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1111 if (!available_bytes)
1113 max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1114 start = tc->snd_una + offset;
1116 /* Start is beyond snd_congestion */
1117 if (seq_geq (start, tc->snd_congestion))
1122 /* Don't overshoot snd_congestion */
1123 if (seq_gt (start + max_deq_bytes, tc->snd_congestion))
1125 max_deq_bytes = tc->snd_congestion - start;
1126 if (max_deq_bytes == 0)
1135 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1138 * Allocate and fill in buffer(s)
1141 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1143 *b = vlib_get_buffer (vm, bi);
1145 /* Easy case, buffer size greater than mss */
1146 if (PREDICT_TRUE (max_deq_bytes <= tm->bytes_per_buffer))
1148 n_bytes = stream_session_peek_bytes (&tc->connection,
1149 vlib_buffer_get_current (*b),
1150 offset, max_deq_bytes);
1151 ASSERT (n_bytes == max_deq_bytes);
1152 b[0]->current_length = n_bytes;
1153 tcp_push_hdr_i (tc, *b, tc->state, 0);
1155 /* Split mss into multiple buffers */
1158 u32 chain_bi = ~0, n_bufs_per_seg;
1159 u32 thread_index = vlib_get_thread_index ();
1160 u16 n_peeked, len_to_deq, available_bufs;
1161 vlib_buffer_t *chain_b, *prev_b;
1165 n_bufs_per_seg = ceil ((double) max_deq_bytes / tm->bytes_per_buffer);
1166 ASSERT (available_bytes >= max_deq_bytes);
1168 /* Make sure we have enough buffers */
1169 available_bufs = vec_len (tm->tx_buffers[thread_index]);
1170 if (n_bufs_per_seg > available_bufs)
1172 if (tcp_alloc_tx_buffers (tm, thread_index,
1173 VLIB_FRAME_SIZE - available_bufs))
1175 tcp_return_buffer (tm);
1180 n_bytes = stream_session_peek_bytes (&tc->connection,
1181 vlib_buffer_get_current (*b),
1182 offset, tm->bytes_per_buffer);
1183 b[0]->current_length = n_bytes;
1184 b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1185 b[0]->total_length_not_including_first_buffer = 0;
1187 tcp_push_hdr_i (tc, *b, tc->state, 0);
1188 max_deq_bytes -= n_bytes;
1191 for (i = 1; i < n_bufs_per_seg; i++)
1194 len_to_deq = clib_min (max_deq_bytes, tm->bytes_per_buffer);
1195 tcp_get_free_buffer_index (tm, &chain_bi);
1196 ASSERT (chain_bi != (u32) ~ 0);
1197 chain_b = vlib_get_buffer (vm, chain_bi);
1198 chain_b->current_data = 0;
1199 data0 = vlib_buffer_get_current (chain_b);
1200 n_peeked = stream_session_peek_bytes (&tc->connection, data0,
1201 n_bytes, len_to_deq);
1202 n_bytes += n_peeked;
1203 ASSERT (n_peeked == len_to_deq);
1204 chain_b->current_length = n_peeked;
1205 b[0]->total_length_not_including_first_buffer +=
1206 chain_b->current_length;
1208 /* update previous buffer */
1209 prev_b->next_buffer = chain_bi;
1210 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
1212 /* update current buffer */
1213 chain_b->next_buffer = 0;
1215 max_deq_bytes -= n_peeked;
1219 ASSERT (n_bytes > 0);
1221 if (tcp_in_fastrecovery (tc))
1222 tc->snd_rxt_bytes += n_bytes;
1225 TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes);
1230 * Reset congestion control, switch cwnd to loss window and try again.
1233 tcp_rtx_timeout_cc (tcp_connection_t * tc)
1235 tc->prev_ssthresh = tc->ssthresh;
1236 tc->prev_cwnd = tc->cwnd;
1238 /* Cleanly recover cc (also clears up fast retransmit) */
1239 if (tcp_in_fastrecovery (tc))
1240 tcp_cc_fastrecovery_exit (tc);
1242 /* Start again from the beginning */
1243 tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
1244 tc->cwnd = tcp_loss_wnd (tc);
1245 tc->snd_congestion = tc->snd_una_max;
1247 tcp_recovery_on (tc);
1251 tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
1253 tcp_main_t *tm = vnet_get_tcp_main ();
1254 vlib_main_t *vm = vlib_get_main ();
1255 u32 thread_index = vlib_get_thread_index ();
1256 tcp_connection_t *tc;
1257 vlib_buffer_t *b = 0;
1262 tc = tcp_half_open_connection_get (index);
1263 tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
1267 tc = tcp_connection_get (index, thread_index);
1268 tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
1271 if (!tcp_in_recovery (tc) && tc->rto_boff > 0
1272 && tc->state >= TCP_STATE_ESTABLISHED)
1275 tcp_update_rto (tc);
1278 /* Increment RTO backoff (also equal to number of retries) */
1281 /* Go back to first un-acked byte */
1282 tc->snd_nxt = tc->snd_una;
1284 if (tc->state >= TCP_STATE_ESTABLISHED)
1286 /* Lost FIN, retransmit and return */
1287 if (tcp_is_lost_fin (tc))
1293 /* First retransmit timeout */
1294 if (tc->rto_boff == 1)
1295 tcp_rtx_timeout_cc (tc);
1297 /* Exponential backoff */
1298 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1300 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
1302 /* Send one segment */
1303 n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1305 bi = vlib_get_buffer_index (vm, b);
1306 /* TODO be less aggressive about this */
1307 scoreboard_clear (&tc->sack_sb);
1311 clib_warning ("could not retransmit anything");
1312 clib_warning ("%U", format_tcp_connection, tc, 2);
1314 /* Try again eventually */
1315 tcp_retransmit_timer_set (tc);
1316 ASSERT (0 || (tc->rto_boff > 1
1317 && tc->snd_una == tc->snd_congestion));
1321 /* For first retransmit, record timestamp (Eifel detection RFC3522) */
1322 if (tc->rto_boff == 1)
1323 tc->snd_rxt_ts = tcp_time_now ();
1325 /* Retransmit for SYN/SYNACK */
1326 else if (tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_SYN_SENT)
1328 /* Half-open connection actually moved to established but we were
1329 * waiting for syn retransmit to pop to call cleanup from the right
1331 if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
1333 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1334 if (tcp_half_open_connection_cleanup (tc))
1336 clib_warning ("could not remove half-open connection");
1342 /* Try without increasing RTO a number of times. If this fails,
1343 * start growing RTO exponentially */
1344 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1345 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1347 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1349 b = vlib_get_buffer (vm, bi);
1350 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
1351 tcp_push_hdr_i (tc, b, tc->state, 1);
1353 /* Account for the SYN */
1356 TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc,
1357 (tc->state == TCP_STATE_SYN_SENT ? 0 : 1));
1361 ASSERT (tc->state == TCP_STATE_CLOSED);
1362 clib_warning ("connection closed ...");
1368 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1370 /* Re-enable retransmit timer */
1371 tcp_retransmit_timer_set (tc);
1375 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1377 /* This goes straight to ipx_lookup */
1378 tcp_push_ip_hdr (tm, tc, b);
1379 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
1381 /* Re-enable retransmit timer */
1382 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN,
1383 tc->rto * TCP_TO_TIMER_TICK);
1388 tcp_timer_retransmit_handler (u32 index)
1390 tcp_timer_retransmit_handler_i (index, 0);
1394 tcp_timer_retransmit_syn_handler (u32 index)
1396 tcp_timer_retransmit_handler_i (index, 1);
1400 * Got 0 snd_wnd from peer, try to do something about it.
1404 tcp_timer_persist_handler (u32 index)
1406 tcp_main_t *tm = vnet_get_tcp_main ();
1407 vlib_main_t *vm = vlib_get_main ();
1408 u32 thread_index = vlib_get_thread_index ();
1409 tcp_connection_t *tc;
1411 u32 bi, old_snd_nxt, snd_bytes = 0, available_bytes = 0;
1414 tc = tcp_connection_get_if_valid (index, thread_index);
1419 /* Make sure timer handle is set to invalid */
1420 tc->timers[TCP_TIMER_PERSIST] = TCP_TIMER_HANDLE_INVALID;
1422 /* Problem already solved or worse */
1423 available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1424 if (tc->state == TCP_STATE_CLOSED || tc->state > TCP_STATE_ESTABLISHED
1425 || tc->snd_wnd > tc->snd_mss || tcp_in_recovery (tc)
1426 || !available_bytes)
1429 /* Increment RTO backoff */
1431 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1434 * Try to force the first unsent segment (or buffer)
1436 if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1438 b = vlib_get_buffer (vm, bi);
1440 tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1441 tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1442 snd_bytes = clib_min (tc->snd_mss, tm->bytes_per_buffer);
1443 n_bytes = stream_session_peek_bytes (&tc->connection,
1444 vlib_buffer_get_current (b),
1445 tc->snd_una_max - tc->snd_una,
1447 ASSERT (n_bytes != 0);
1448 b->current_length = n_bytes;
1449 ASSERT (tc->snd_nxt == tc->snd_una_max || tc->rto_boff > 1
1450 || tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT));
1452 /* Allow updating of snd_una_max but don't update snd_nxt */
1453 old_snd_nxt = tc->snd_nxt;
1454 tcp_push_hdr_i (tc, b, tc->state, 0);
1455 tc->snd_nxt = old_snd_nxt;
1456 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1458 /* Re-enable persist timer */
1459 tcp_persist_timer_set (tc);
1463 * Retransmit first unacked segment
1466 tcp_retransmit_first_unacked (tcp_connection_t * tc)
1468 vlib_main_t *vm = vlib_get_main ();
1470 u32 bi, old_snd_nxt, n_bytes;
1472 old_snd_nxt = tc->snd_nxt;
1473 tc->snd_nxt = tc->snd_una;
1475 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1476 n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1479 bi = vlib_get_buffer_index (vm, b);
1480 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1482 tc->snd_nxt = old_snd_nxt;
1486 * Do fast retransmit with SACKs
1489 tcp_fast_retransmit_sack (tcp_connection_t * tc)
1491 vlib_main_t *vm = vlib_get_main ();
1492 u32 n_written = 0, offset = 0, max_bytes;
1493 vlib_buffer_t *b = 0;
1494 sack_scoreboard_hole_t *hole;
1495 sack_scoreboard_t *sb;
1496 u32 bi, old_snd_nxt;
1498 u8 snd_limited = 0, can_rescue = 0;
1500 ASSERT (tcp_in_fastrecovery (tc));
1501 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1503 old_snd_nxt = tc->snd_nxt;
1505 snd_space = tcp_available_snd_space (tc);
1507 hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
1508 while (hole && snd_space > 0)
1510 hole = scoreboard_next_rxt_hole (sb, hole,
1511 tcp_fastrecovery_sent_1_smss (tc),
1512 &can_rescue, &snd_limited);
1515 if (!can_rescue || !(seq_lt (sb->rescue_rxt, tc->snd_una)
1516 || seq_gt (sb->rescue_rxt,
1517 tc->snd_congestion)))
1520 /* If rescue rxt undefined or less than snd_una then one segment of
1521 * up to SMSS octets that MUST include the highest outstanding
1522 * unSACKed sequence number SHOULD be returned, and RescueRxt set to
1523 * RecoveryPoint. HighRxt MUST NOT be updated.
1525 max_bytes = clib_min (tc->snd_mss, snd_space);
1526 offset = tc->snd_congestion - tc->snd_una - max_bytes;
1527 sb->rescue_rxt = tc->snd_congestion;
1528 tc->snd_nxt = tc->snd_una + offset;
1529 n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes,
1532 bi = vlib_get_buffer_index (vm, b);
1533 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1537 max_bytes = snd_limited ? tc->snd_mss : hole->end - sb->high_rxt;
1538 offset = sb->high_rxt - tc->snd_una;
1539 tc->snd_nxt = tc->snd_una + offset;
1540 n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes, &b);
1542 /* Nothing left to retransmit */
1546 bi = vlib_get_buffer_index (vm, b);
1547 sb->high_rxt += n_written;
1548 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1549 snd_space -= n_written;
1552 /* If window allows, send 1 SMSS of new data */
1553 tc->snd_nxt = old_snd_nxt;
1557 * Fast retransmit without SACK info
1560 tcp_fast_retransmit_no_sack (tcp_connection_t * tc)
1562 vlib_main_t *vm = vlib_get_main ();
1563 u32 n_written = 0, offset = 0, bi, old_snd_nxt;
1567 ASSERT (tcp_in_fastrecovery (tc));
1568 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1570 /* Start resending from first un-acked segment */
1571 old_snd_nxt = tc->snd_nxt;
1572 tc->snd_nxt = tc->snd_una;
1573 snd_space = tcp_available_snd_space (tc);
1575 while (snd_space > 0)
1577 offset += n_written;
1578 n_written = tcp_prepare_retransmit_segment (tc, offset, snd_space, &b);
1580 /* Nothing left to retransmit */
1584 bi = vlib_get_buffer_index (vm, b);
1585 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1586 snd_space -= n_written;
1589 /* Restore snd_nxt. If window allows, send 1 SMSS of new data */
1590 tc->snd_nxt = old_snd_nxt;
1594 * Do fast retransmit
1597 tcp_fast_retransmit (tcp_connection_t * tc)
1599 if (tcp_opts_sack_permitted (&tc->rcv_opts)
1600 && scoreboard_first_hole (&tc->sack_sb))
1601 tcp_fast_retransmit_sack (tc);
1603 tcp_fast_retransmit_no_sack (tc);
1607 tcp_session_has_ooo_data (tcp_connection_t * tc)
1609 stream_session_t *s =
1610 stream_session_get (tc->c_s_index, tc->c_thread_index);
1611 return svm_fifo_has_ooo_data (s->server_rx_fifo);
1615 tcp46_output_inline (vlib_main_t * vm,
1616 vlib_node_runtime_t * node,
1617 vlib_frame_t * from_frame, int is_ip4)
1619 u32 n_left_from, next_index, *from, *to_next;
1620 u32 my_thread_index = vm->thread_index;
1622 from = vlib_frame_vector_args (from_frame);
1623 n_left_from = from_frame->n_vectors;
1625 next_index = node->cached_next_index;
1627 while (n_left_from > 0)
1631 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1633 while (n_left_from > 0 && n_left_to_next > 0)
1637 tcp_connection_t *tc0;
1639 tcp_header_t *th0 = 0;
1640 u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
1647 n_left_to_next -= 1;
1649 b0 = vlib_get_buffer (vm, bi0);
1650 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1652 if (PREDICT_FALSE (tc0 == 0 || tc0->state == TCP_STATE_CLOSED))
1654 error0 = TCP_ERROR_INVALID_CONNECTION;
1655 next0 = TCP_OUTPUT_NEXT_DROP;
1659 th0 = vlib_buffer_get_current (b0);
1660 TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
1664 vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
1665 IP_PROTOCOL_TCP, 1);
1666 b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1667 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1673 ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
1674 &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
1675 b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1676 vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data;
1677 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1681 /* Filter out DUPACKs if there are no OOO segments left */
1683 (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK))
1685 if (!tcp_session_has_ooo_data (tc0))
1687 error0 = TCP_ERROR_FILTERED_DUPACKS;
1688 next0 = TCP_OUTPUT_NEXT_DROP;
1693 /* Stop DELACK timer and fix flags */
1694 tc0->flags &= ~(TCP_CONN_SNDACK);
1695 tcp_timer_reset (tc0, TCP_TIMER_DELACK);
1697 /* If not retransmitting
1698 * 1) update snd_una_max (SYN, SYNACK, FIN)
1699 * 2) If we're not tracking an ACK, start tracking */
1700 if (seq_lt (tc0->snd_una_max, tc0->snd_nxt))
1702 tc0->snd_una_max = tc0->snd_nxt;
1703 if (tc0->rtt_ts == 0)
1705 tc0->rtt_ts = tcp_time_now ();
1706 tc0->rtt_seq = tc0->snd_nxt;
1710 /* Set the retransmit timer if not set already and not
1711 * doing a pure ACK */
1712 if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT)
1713 && tc0->snd_nxt != tc0->snd_una)
1715 tcp_retransmit_timer_set (tc0);
1720 /* Make sure we haven't lost route to our peer */
1721 if (PREDICT_FALSE (tc0->last_fib_check
1722 < tc0->snd_opts.tsval + TCP_FIB_RECHECK_PERIOD))
1725 (tc0->c_rmt_fei == tcp_lookup_rmt_in_fib (tc0)))
1727 tc0->last_fib_check = tc0->snd_opts.tsval;
1731 clib_warning ("lost connection to peer");
1732 tcp_connection_reset (tc0);
1737 /* Use pre-computed dpo to set next node */
1738 next0 = tc0->c_rmt_dpo.dpoi_next_node;
1739 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = tc0->c_rmt_dpo.dpoi_index;
1742 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1743 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1745 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1747 b0->error = node->errors[error0];
1748 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1750 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1753 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1757 memset (&t0->tcp_header, 0, sizeof (t0->tcp_header));
1759 clib_memcpy (&t0->tcp_connection, tc0,
1760 sizeof (t0->tcp_connection));
1763 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1764 n_left_to_next, bi0, next0);
1767 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1770 return from_frame->n_vectors;
1774 tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1775 vlib_frame_t * from_frame)
1777 return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1781 tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1782 vlib_frame_t * from_frame)
1784 return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1788 VLIB_REGISTER_NODE (tcp4_output_node) =
1790 .function = tcp4_output,.name = "tcp4-output",
1791 /* Takes a vector of packets. */
1792 .vector_size = sizeof (u32),
1793 .n_errors = TCP_N_ERROR,
1794 .error_strings = tcp_error_strings,
1795 .n_next_nodes = TCP_OUTPUT_N_NEXT,
1797 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1798 foreach_tcp4_output_next
1801 .format_buffer = format_tcp_header,
1802 .format_trace = format_tcp_tx_trace,
1806 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output);
1809 VLIB_REGISTER_NODE (tcp6_output_node) =
1811 .function = tcp6_output,
1812 .name = "tcp6-output",
1813 /* Takes a vector of packets. */
1814 .vector_size = sizeof (u32),
1815 .n_errors = TCP_N_ERROR,
1816 .error_strings = tcp_error_strings,
1817 .n_next_nodes = TCP_OUTPUT_N_NEXT,
1819 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1820 foreach_tcp6_output_next
1823 .format_buffer = format_tcp_header,
1824 .format_trace = format_tcp_tx_trace,
1828 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output);
1831 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1833 tcp_connection_t *tc;
1835 tc = (tcp_connection_t *) tconn;
1836 tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED, 0);
1838 if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc))
1840 tc->rtt_ts = tcp_time_now ();
1841 tc->rtt_seq = tc->snd_nxt;
1846 typedef enum _tcp_reset_next
1848 TCP_RESET_NEXT_DROP,
1849 TCP_RESET_NEXT_IP_LOOKUP,
1853 #define foreach_tcp4_reset_next \
1854 _(DROP, "error-drop") \
1855 _(IP_LOOKUP, "ip4-lookup")
1857 #define foreach_tcp6_reset_next \
1858 _(DROP, "error-drop") \
1859 _(IP_LOOKUP, "ip6-lookup")
1862 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1863 vlib_frame_t * from_frame, u8 is_ip4)
1865 u32 n_left_from, next_index, *from, *to_next;
1866 u32 my_thread_index = vm->thread_index;
1868 from = vlib_frame_vector_args (from_frame);
1869 n_left_from = from_frame->n_vectors;
1871 next_index = node->cached_next_index;
1873 while (n_left_from > 0)
1877 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1879 while (n_left_from > 0 && n_left_to_next > 0)
1885 u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
1892 n_left_to_next -= 1;
1894 b0 = vlib_get_buffer (vm, bi0);
1896 if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
1897 my_thread_index, is_ip4))
1899 error0 = TCP_ERROR_LOOKUP_DROPS;
1900 next0 = TCP_RESET_NEXT_DROP;
1904 /* Prepare to send to IP lookup */
1905 vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
1906 next0 = TCP_RESET_NEXT_IP_LOOKUP;
1909 b0->error = node->errors[error0];
1910 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1911 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1913 th0 = vlib_buffer_get_current (b0);
1915 th0 = ip4_next_header ((ip4_header_t *) th0);
1917 th0 = ip6_next_header ((ip6_header_t *) th0);
1918 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1919 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1922 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1923 n_left_to_next, bi0, next0);
1925 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1927 return from_frame->n_vectors;
1931 tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1932 vlib_frame_t * from_frame)
1934 return tcp46_send_reset_inline (vm, node, from_frame, 1);
1938 tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1939 vlib_frame_t * from_frame)
1941 return tcp46_send_reset_inline (vm, node, from_frame, 0);
1945 VLIB_REGISTER_NODE (tcp4_reset_node) = {
1946 .function = tcp4_send_reset,
1947 .name = "tcp4-reset",
1948 .vector_size = sizeof (u32),
1949 .n_errors = TCP_N_ERROR,
1950 .error_strings = tcp_error_strings,
1951 .n_next_nodes = TCP_RESET_N_NEXT,
1953 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1954 foreach_tcp4_reset_next
1957 .format_trace = format_tcp_tx_trace,
1961 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_reset_node, tcp4_send_reset);
1964 VLIB_REGISTER_NODE (tcp6_reset_node) = {
1965 .function = tcp6_send_reset,
1966 .name = "tcp6-reset",
1967 .vector_size = sizeof (u32),
1968 .n_errors = TCP_N_ERROR,
1969 .error_strings = tcp_error_strings,
1970 .n_next_nodes = TCP_RESET_N_NEXT,
1972 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1973 foreach_tcp6_reset_next
1976 .format_trace = format_tcp_tx_trace,
1980 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_reset_node, tcp6_send_reset);
1983 * fd.io coding-style-patch-verification: ON
1986 * eval: (c-set-style "gnu")