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>
19 vlib_node_registration_t tcp4_output_node;
20 vlib_node_registration_t tcp6_output_node;
22 typedef enum _tcp_output_nect
25 TCP_OUTPUT_NEXT_IP_LOOKUP,
29 #define foreach_tcp4_output_next \
30 _ (DROP, "error-drop") \
31 _ (IP_LOOKUP, "ip4-lookup")
33 #define foreach_tcp6_output_next \
34 _ (DROP, "error-drop") \
35 _ (IP_LOOKUP, "ip6-lookup")
37 static char *tcp_error_strings[] = {
38 #define tcp_error(n,s) s,
39 #include <vnet/tcp/tcp_error.def>
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 *);
58 s = format (s, "TBD\n");
64 tcp_set_snd_mss (tcp_connection_t * tc)
68 /* TODO find our iface MTU */
71 /* TODO cache mss and consider PMTU discovery */
72 snd_mss = tc->opt.mss < snd_mss ? tc->opt.mss : snd_mss;
74 tc->snd_mss = snd_mss;
78 clib_warning ("snd mss is 0");
79 tc->snd_mss = dummy_mtu;
84 tcp_window_compute_scale (u32 available_space)
87 while (wnd_scale < TCP_MAX_WND_SCALE
88 && (available_space >> wnd_scale) > TCP_WND_MAX)
94 * TCP's IW as recommended by RFC6928
97 tcp_initial_wnd_unscaled (tcp_connection_t * tc)
99 return TCP_IW_N_SEGMENTS * dummy_mtu;
103 * Compute initial window and scale factor. As per RFC1323, window field in
104 * SYN and SYN-ACK segments is never scaled.
107 tcp_initial_window_to_advertise (tcp_connection_t * tc)
111 /* Initial wnd for SYN. Fifos are not allocated yet.
112 * Use some predefined value. For SYN-ACK we still want the
113 * scale to be computed in the same way */
114 max_fifo = TCP_MAX_RX_FIFO_SIZE;
116 tc->rcv_wscale = tcp_window_compute_scale (max_fifo);
117 tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
119 return clib_min (tc->rcv_wnd, TCP_WND_MAX);
123 * Compute and return window to advertise, scaled as per RFC1323
126 tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state)
128 u32 available_space, max_fifo, observed_wnd;
130 if (state < TCP_STATE_ESTABLISHED)
131 return tcp_initial_window_to_advertise (tc);
134 * Figure out how much space we have available
136 available_space = stream_session_max_enqueue (&tc->connection);
137 max_fifo = stream_session_fifo_size (&tc->connection);
139 ASSERT (tc->opt.mss < max_fifo);
141 if (available_space < tc->opt.mss && available_space < max_fifo / 8)
145 * Use the above and what we know about what we've previously advertised
146 * to compute the new window
148 observed_wnd = tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
150 /* Bad. Thou shalt not shrink */
151 if (available_space < observed_wnd)
153 if (available_space == 0)
154 clib_warning ("Didn't shrink rcv window despite not having space");
157 tc->rcv_wnd = clib_min (available_space, TCP_WND_MAX << tc->rcv_wscale);
159 if (tc->rcv_wnd == 0)
161 tc->flags |= TCP_CONN_SENT_RCV_WND0;
164 return tc->rcv_wnd >> tc->rcv_wscale;
168 * Write TCP options to segment.
171 tcp_options_write (u8 * data, tcp_options_t * opts)
174 u32 buf, seq_len = 4;
176 if (tcp_opts_mss (opts))
178 *data++ = TCP_OPTION_MSS;
179 *data++ = TCP_OPTION_LEN_MSS;
180 buf = clib_host_to_net_u16 (opts->mss);
181 clib_memcpy (data, &buf, sizeof (opts->mss));
182 data += sizeof (opts->mss);
183 opts_len += TCP_OPTION_LEN_MSS;
186 if (tcp_opts_wscale (opts))
188 *data++ = TCP_OPTION_WINDOW_SCALE;
189 *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
190 *data++ = opts->wscale;
191 opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
194 if (tcp_opts_sack_permitted (opts))
196 *data++ = TCP_OPTION_SACK_PERMITTED;
197 *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
198 opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
201 if (tcp_opts_tstamp (opts))
203 *data++ = TCP_OPTION_TIMESTAMP;
204 *data++ = TCP_OPTION_LEN_TIMESTAMP;
205 buf = clib_host_to_net_u32 (opts->tsval);
206 clib_memcpy (data, &buf, sizeof (opts->tsval));
207 data += sizeof (opts->tsval);
208 buf = clib_host_to_net_u32 (opts->tsecr);
209 clib_memcpy (data, &buf, sizeof (opts->tsecr));
210 data += sizeof (opts->tsecr);
211 opts_len += TCP_OPTION_LEN_TIMESTAMP;
214 if (tcp_opts_sack (opts))
217 u32 n_sack_blocks = clib_min (vec_len (opts->sacks),
218 TCP_OPTS_MAX_SACK_BLOCKS);
220 if (n_sack_blocks != 0)
222 *data++ = TCP_OPTION_SACK_BLOCK;
223 *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
224 for (i = 0; i < n_sack_blocks; i++)
226 buf = clib_host_to_net_u32 (opts->sacks[i].start);
227 clib_memcpy (data, &buf, seq_len);
229 buf = clib_host_to_net_u32 (opts->sacks[i].end);
230 clib_memcpy (data, &buf, seq_len);
233 opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
237 /* Terminate TCP options */
240 *data++ = TCP_OPTION_EOL;
241 opts_len += TCP_OPTION_LEN_EOL;
244 /* Pad with zeroes to a u32 boundary */
247 *data++ = TCP_OPTION_NOOP;
248 opts_len += TCP_OPTION_LEN_NOOP;
254 tcp_make_syn_options (tcp_options_t * opts, u8 wnd_scale)
258 opts->flags |= TCP_OPTS_FLAG_MSS;
259 opts->mss = dummy_mtu; /*XXX discover that */
260 len += TCP_OPTION_LEN_MSS;
262 opts->flags |= TCP_OPTS_FLAG_WSCALE;
263 opts->wscale = wnd_scale;
264 len += TCP_OPTION_LEN_WINDOW_SCALE;
266 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
267 opts->tsval = tcp_time_now ();
269 len += TCP_OPTION_LEN_TIMESTAMP;
271 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
272 len += TCP_OPTION_LEN_SACK_PERMITTED;
274 /* Align to needed boundary */
275 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
280 tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts)
284 opts->flags |= TCP_OPTS_FLAG_MSS;
285 opts->mss = dummy_mtu; /*XXX discover that */
286 len += TCP_OPTION_LEN_MSS;
288 if (tcp_opts_wscale (&tc->opt))
290 opts->flags |= TCP_OPTS_FLAG_WSCALE;
291 opts->wscale = tc->rcv_wscale;
292 len += TCP_OPTION_LEN_WINDOW_SCALE;
295 if (tcp_opts_tstamp (&tc->opt))
297 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
298 opts->tsval = tcp_time_now ();
299 opts->tsecr = tc->tsval_recent;
300 len += TCP_OPTION_LEN_TIMESTAMP;
303 if (tcp_opts_sack_permitted (&tc->opt))
305 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
306 len += TCP_OPTION_LEN_SACK_PERMITTED;
309 /* Align to needed boundary */
310 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
315 tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts)
321 if (tcp_opts_tstamp (&tc->opt))
323 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
324 opts->tsval = tcp_time_now ();
325 opts->tsecr = tc->tsval_recent;
326 len += TCP_OPTION_LEN_TIMESTAMP;
328 if (tcp_opts_sack_permitted (&tc->opt))
330 if (vec_len (tc->snd_sacks))
332 opts->flags |= TCP_OPTS_FLAG_SACK;
333 opts->sacks = tc->snd_sacks;
334 opts->n_sack_blocks = vec_len (tc->snd_sacks);
335 len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
339 /* Align to needed boundary */
340 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
345 tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
350 case TCP_STATE_ESTABLISHED:
351 case TCP_STATE_FIN_WAIT_1:
352 return tcp_make_established_options (tc, opts);
353 case TCP_STATE_SYN_RCVD:
354 return tcp_make_synack_options (tc, opts);
355 case TCP_STATE_SYN_SENT:
356 return tcp_make_syn_options (opts, tc->rcv_wscale);
358 clib_warning ("Not handled!");
363 #define tcp_get_free_buffer_index(tm, bidx) \
365 u32 *my_tx_buffers, n_free_buffers; \
366 u32 cpu_index = tm->vlib_main->cpu_index; \
367 my_tx_buffers = tm->tx_buffers[cpu_index]; \
368 if (PREDICT_FALSE(vec_len (my_tx_buffers) == 0)) \
370 n_free_buffers = 32; /* TODO config or macro */ \
371 vec_validate (my_tx_buffers, n_free_buffers - 1); \
372 _vec_len(my_tx_buffers) = vlib_buffer_alloc_from_free_list ( \
373 tm->vlib_main, my_tx_buffers, n_free_buffers, \
374 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); \
375 tm->tx_buffers[cpu_index] = my_tx_buffers; \
377 /* buffer shortage */ \
378 if (PREDICT_FALSE (vec_len (my_tx_buffers) == 0)) \
380 *bidx = my_tx_buffers[_vec_len (my_tx_buffers)-1]; \
381 _vec_len (my_tx_buffers) -= 1; \
385 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
387 vlib_buffer_t *it = b;
390 it->current_data = 0;
391 it->current_length = 0;
392 it->total_length_not_including_first_buffer = 0;
394 while ((it->flags & VLIB_BUFFER_NEXT_PRESENT)
395 && (it = vlib_get_buffer (vm, it->next_buffer)));
397 /* Leave enough space for headers */
398 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
405 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
408 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
409 u8 tcp_opts_len, tcp_hdr_opts_len;
413 wnd = tcp_window_to_advertise (tc, state);
415 /* Make and write options */
416 tcp_opts_len = tcp_make_established_options (tc, snd_opts);
417 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
419 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
420 tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
422 tcp_options_write ((u8 *) (th + 1), snd_opts);
425 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
429 * Convert buffer to ACK
432 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
434 tcp_main_t *tm = vnet_get_tcp_main ();
435 vlib_main_t *vm = tm->vlib_main;
437 tcp_reuse_buffer (vm, b);
438 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
439 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
443 * Convert buffer to FIN-ACK
446 tcp_make_finack (tcp_connection_t * tc, vlib_buffer_t * b)
448 tcp_main_t *tm = vnet_get_tcp_main ();
449 vlib_main_t *vm = tm->vlib_main;
451 tcp_reuse_buffer (vm, b);
452 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK | TCP_FLAG_FIN);
454 /* Reset flags, make sure ack is sent */
455 tc->flags = TCP_CONN_SNDACK;
456 vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
462 * Convert buffer to SYN-ACK
465 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
467 tcp_main_t *tm = vnet_get_tcp_main ();
468 vlib_main_t *vm = tm->vlib_main;
469 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
470 u8 tcp_opts_len, tcp_hdr_opts_len;
475 memset (snd_opts, 0, sizeof (*snd_opts));
477 tcp_reuse_buffer (vm, b);
479 /* Set random initial sequence */
480 time_now = tcp_time_now ();
482 tc->iss = random_u32 (&time_now);
483 tc->snd_una = tc->iss;
484 tc->snd_nxt = tc->iss + 1;
485 tc->snd_una_max = tc->snd_nxt;
487 initial_wnd = tcp_initial_window_to_advertise (tc);
489 /* Make and write options */
490 tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
491 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
493 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
494 tc->rcv_nxt, tcp_hdr_opts_len,
495 TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
497 tcp_options_write ((u8 *) (th + 1), snd_opts);
499 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
500 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
502 /* Init retransmit timer */
503 tcp_retransmit_timer_set (tm, tc);
507 tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
510 u32 *to_next, next_index;
513 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
516 /* Default FIB for now */
517 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
519 /* Send to IP lookup */
520 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
521 f = vlib_get_frame_to_node (vm, next_index);
523 /* Enqueue the packet */
524 to_next = vlib_frame_vector_args (f);
527 vlib_put_frame_to_node (vm, next_index, f);
531 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0,
532 tcp_state_t state, u32 my_thread_index, u8 is_ip4)
534 u8 tcp_hdr_len = sizeof (tcp_header_t);
538 ip4_address_t src_ip40;
539 ip6_address_t src_ip60;
543 /* Find IP and TCP headers */
546 ih4 = vlib_buffer_get_current (b0);
547 th0 = ip4_next_header (ih4);
551 ih6 = vlib_buffer_get_current (b0);
552 th0 = ip6_next_header (ih6);
555 /* Swap src and dst ip */
558 ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
559 src_ip40.as_u32 = ih4->src_address.as_u32;
560 ih4->src_address.as_u32 = ih4->dst_address.as_u32;
561 ih4->dst_address.as_u32 = src_ip40.as_u32;
563 /* Chop the end of the pkt */
564 b0->current_length += ip4_header_bytes (ih4) + tcp_hdr_len;
568 ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
569 clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
570 clib_memcpy (&ih6->src_address, &ih6->dst_address,
571 sizeof (ip6_address_t));
572 clib_memcpy (&ih6->dst_address, &src_ip60, sizeof (ip6_address_t));
574 /* Chop the end of the pkt */
575 b0->current_length += sizeof (ip6_header_t) + tcp_hdr_len;
578 /* Try to determine what/why we're actually resetting and swap
579 * src and dst ports */
580 if (state == TCP_STATE_CLOSED)
585 tmp = clib_net_to_host_u32 (th0->seq_number);
587 /* Got a SYN for no listener. */
588 th0->flags = TCP_FLAG_RST | TCP_FLAG_ACK;
589 th0->ack_number = clib_host_to_net_u32 (tmp + 1);
593 else if (state >= TCP_STATE_SYN_SENT)
595 th0->flags = TCP_FLAG_RST | TCP_FLAG_ACK;
596 th0->seq_number = th0->ack_number;
600 src_port0 = th0->src_port;
601 th0->src_port = th0->dst_port;
602 th0->dst_port = src_port0;
604 th0->data_offset_and_reserved = (tcp_hdr_len >> 2) << 4;
605 th0->urgent_pointer = 0;
607 /* Compute checksum */
610 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
615 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
623 * Send reset without reusing existing buffer
626 tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4)
630 tcp_main_t *tm = vnet_get_tcp_main ();
631 vlib_main_t *vm = tm->vlib_main;
632 u8 tcp_hdr_len, flags = 0;
633 tcp_header_t *th, *pkt_th;
635 ip4_header_t *ih4, *pkt_ih4;
636 ip6_header_t *ih6, *pkt_ih6;
638 tcp_get_free_buffer_index (tm, &bi);
639 b = vlib_get_buffer (vm, bi);
641 /* Leave enough space for headers */
642 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
644 /* Make and write options */
645 tcp_hdr_len = sizeof (tcp_header_t);
649 pkt_ih4 = vlib_buffer_get_current (pkt);
650 pkt_th = ip4_next_header (pkt_ih4);
654 pkt_ih6 = vlib_buffer_get_current (pkt);
655 pkt_th = ip6_next_header (pkt_ih6);
658 if (tcp_ack (pkt_th))
660 flags = TCP_FLAG_RST;
661 seq = pkt_th->ack_number;
666 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
668 ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
671 th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
672 seq, ack, tcp_hdr_len, flags, 0);
674 /* Swap src and dst ip */
677 ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
678 ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
679 &pkt_ih4->src_address, IP_PROTOCOL_TCP);
680 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
685 pkt_ih6 = (ip6_header_t *) (pkt_th - 1);
686 ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
689 vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
690 &pkt_ih6->src_address, IP_PROTOCOL_TCP);
691 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
695 tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4);
699 tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b)
701 tcp_header_t *th = vlib_buffer_get_current (b);
706 ih = vlib_buffer_push_ip4 (tm->vlib_main, b, &tc->c_lcl_ip4,
707 &tc->c_rmt_ip4, IP_PROTOCOL_TCP);
708 th->checksum = ip4_tcp_udp_compute_checksum (tm->vlib_main, b, ih);
715 ih = vlib_buffer_push_ip6 (tm->vlib_main, b, &tc->c_lcl_ip6,
716 &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
717 th->checksum = ip6_tcp_udp_icmp_compute_checksum (tm->vlib_main, b, ih,
726 * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
727 * The packet is not forwarded through tcpx_output to avoid doing lookups
728 * in the half_open pool.
731 tcp_send_syn (tcp_connection_t * tc)
735 tcp_main_t *tm = vnet_get_tcp_main ();
736 vlib_main_t *vm = tm->vlib_main;
737 u8 tcp_hdr_opts_len, tcp_opts_len;
741 tcp_options_t snd_opts;
743 tcp_get_free_buffer_index (tm, &bi);
744 b = vlib_get_buffer (vm, bi);
746 /* Leave enough space for headers */
747 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
749 /* Set random initial sequence */
750 time_now = tcp_time_now ();
752 tc->iss = random_u32 (&time_now);
753 tc->snd_una = tc->iss;
754 tc->snd_una_max = tc->snd_nxt = tc->iss + 1;
756 initial_wnd = tcp_initial_window_to_advertise (tc);
758 /* Make and write options */
759 memset (&snd_opts, 0, sizeof (snd_opts));
760 tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
761 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
763 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
764 tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
767 tcp_options_write ((u8 *) (th + 1), &snd_opts);
769 /* Measure RTT with this */
770 tc->rtt_ts = tcp_time_now ();
771 tc->rtt_seq = tc->snd_nxt;
773 /* Start retransmit trimer */
774 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK);
777 /* Set the connection establishment timer */
778 tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME);
780 tcp_push_ip_hdr (tm, tc, b);
781 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
785 tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4)
787 u32 *to_next, next_index;
790 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
793 /* Decide where to send the packet */
794 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
795 f = vlib_get_frame_to_node (vm, next_index);
797 /* Enqueue the packet */
798 to_next = vlib_frame_vector_args (f);
801 vlib_put_frame_to_node (vm, next_index, f);
808 tcp_send_fin (tcp_connection_t * tc)
812 tcp_main_t *tm = vnet_get_tcp_main ();
813 vlib_main_t *vm = tm->vlib_main;
815 tcp_get_free_buffer_index (tm, &bi);
816 b = vlib_get_buffer (vm, bi);
818 /* Leave enough space for headers */
819 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
821 tcp_make_finack (tc, b);
823 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
827 tcp_make_state_flags (tcp_state_t next_state)
831 case TCP_STATE_ESTABLISHED:
833 case TCP_STATE_SYN_RCVD:
834 return TCP_FLAG_SYN | TCP_FLAG_ACK;
835 case TCP_STATE_SYN_SENT:
837 case TCP_STATE_LAST_ACK:
838 case TCP_STATE_FIN_WAIT_1:
841 clib_warning ("Shouldn't be here!");
847 * Push TCP header and update connection variables
850 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b,
851 tcp_state_t next_state)
853 u32 advertise_wnd, data_len;
854 u8 tcp_opts_len, tcp_hdr_opts_len, opts_write_len, flags;
855 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
858 data_len = b->current_length;
859 vnet_buffer (b)->tcp.flags = 0;
861 /* Make and write options */
862 memset (snd_opts, 0, sizeof (*snd_opts));
863 tcp_opts_len = tcp_make_options (tc, snd_opts, next_state);
864 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
866 /* Get rcv window to advertise */
867 advertise_wnd = tcp_window_to_advertise (tc, next_state);
868 flags = tcp_make_state_flags (next_state);
870 /* Push header and options */
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,
875 opts_write_len = tcp_options_write ((u8 *) (th + 1), snd_opts);
877 ASSERT (opts_write_len == tcp_opts_len);
879 /* Tag the buffer with the connection index */
880 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
882 tc->snd_nxt += data_len;
885 /* Send delayed ACK when timer expires */
887 tcp_timer_delack_handler (u32 index)
889 tcp_main_t *tm = vnet_get_tcp_main ();
890 vlib_main_t *vm = tm->vlib_main;
891 u32 thread_index = os_get_cpu_number ();
892 tcp_connection_t *tc;
896 tc = tcp_connection_get (index, thread_index);
899 tcp_get_free_buffer_index (tm, &bi);
900 b = vlib_get_buffer (vm, bi);
902 /* Fill in the ACK */
903 tcp_make_ack (tc, b);
905 tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
906 tc->flags &= ~TCP_CONN_DELACK;
908 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
911 /** Build a retransmit segment
913 * @return the number of bytes in the segment or 0 if there's nothing to
917 tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
920 tcp_main_t *tm = vnet_get_tcp_main ();
921 vlib_main_t *vm = tm->vlib_main;
922 u32 n_bytes, offset = 0;
923 sack_scoreboard_hole_t *hole;
926 tcp_reuse_buffer (vm, b);
928 ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
929 ASSERT (max_bytes != 0);
931 if (tcp_opts_sack_permitted (&tc->opt))
933 /* XXX get first hole not retransmitted yet */
934 hole = scoreboard_first_hole (&tc->sack_sb);
938 offset = hole->start - tc->snd_una;
939 hole_size = hole->end - hole->start;
943 if (hole_size < max_bytes)
944 max_bytes = hole_size;
948 if (seq_geq (tc->snd_nxt, tc->snd_una_max))
952 n_bytes = stream_session_peek_bytes (&tc->connection,
953 vlib_buffer_get_current (b), offset,
955 ASSERT (n_bytes != 0);
957 tcp_push_hdr_i (tc, b, tc->state);
963 tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
965 tcp_main_t *tm = vnet_get_tcp_main ();
966 vlib_main_t *vm = tm->vlib_main;
967 u32 thread_index = os_get_cpu_number ();
968 tcp_connection_t *tc;
970 u32 bi, max_bytes, snd_space;
974 tc = tcp_half_open_connection_get (index);
978 tc = tcp_connection_get (index, thread_index);
981 /* Make sure timer handle is set to invalid */
982 tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
984 /* Increment RTO backoff (also equal to number of retries) */
987 /* Go back to first un-acked byte */
988 tc->snd_nxt = tc->snd_una;
991 tcp_get_free_buffer_index (tm, &bi);
992 b = vlib_get_buffer (vm, bi);
994 if (tc->state >= TCP_STATE_ESTABLISHED)
996 tcp_fastrecovery_off (tc);
998 /* Exponential backoff */
999 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1001 /* Figure out what and how many bytes we can send */
1002 snd_space = tcp_available_snd_space (tc);
1003 max_bytes = clib_min (tc->snd_mss, snd_space);
1007 clib_warning ("no wnd to retransmit");
1010 tcp_prepare_retransmit_segment (tc, b, max_bytes);
1012 tc->rtx_bytes += max_bytes;
1014 /* No fancy recovery for now! */
1015 scoreboard_clear (&tc->sack_sb);
1019 /* Retransmit for SYN/SYNACK */
1020 ASSERT (tc->state == TCP_STATE_SYN_RCVD
1021 || tc->state == TCP_STATE_SYN_SENT);
1023 /* Try without increasing RTO a number of times. If this fails,
1024 * start growing RTO exponentially */
1025 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1026 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1028 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
1030 tcp_push_hdr_i (tc, b, tc->state);
1032 /* Account for the SYN */
1038 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1040 /* Re-enable retransmit timer */
1041 tcp_retransmit_timer_set (tm, tc);
1045 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1047 /* This goes straight to ipx_lookup */
1048 tcp_push_ip_hdr (tm, tc, b);
1049 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
1051 /* Re-enable retransmit timer */
1052 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN,
1053 tc->rto * TCP_TO_TIMER_TICK);
1058 tcp_timer_retransmit_handler (u32 index)
1060 tcp_timer_retransmit_handler_i (index, 0);
1064 tcp_timer_retransmit_syn_handler (u32 index)
1066 tcp_timer_retransmit_handler_i (index, 1);
1070 * Retansmit first unacked segment */
1072 tcp_retransmit_first_unacked (tcp_connection_t * tc)
1074 tcp_main_t *tm = vnet_get_tcp_main ();
1075 u32 snd_nxt = tc->snd_nxt;
1079 tc->snd_nxt = tc->snd_una;
1082 tcp_get_free_buffer_index (tm, &bi);
1083 b = vlib_get_buffer (tm->vlib_main, bi);
1085 tcp_prepare_retransmit_segment (tc, b, tc->snd_mss);
1086 tcp_enqueue_to_output (tm->vlib_main, b, bi, tc->c_is_ip4);
1088 tc->snd_nxt = snd_nxt;
1089 tc->rtx_bytes += tc->snd_mss;
1093 tcp_fast_retransmit (tcp_connection_t * tc)
1095 tcp_main_t *tm = vnet_get_tcp_main ();
1096 u32 snd_space, max_bytes, n_bytes, bi;
1099 ASSERT (tcp_in_fastrecovery (tc));
1101 clib_warning ("fast retransmit!");
1103 /* Start resending from first un-acked segment */
1104 tc->snd_nxt = tc->snd_una;
1106 snd_space = tcp_available_snd_space (tc);
1110 tcp_get_free_buffer_index (tm, &bi);
1111 b = vlib_get_buffer (tm->vlib_main, bi);
1113 max_bytes = clib_min (tc->snd_mss, snd_space);
1114 n_bytes = tcp_prepare_retransmit_segment (tc, b, max_bytes);
1116 /* Nothing left to retransmit */
1120 tcp_enqueue_to_output (tm->vlib_main, b, bi, tc->c_is_ip4);
1122 snd_space -= n_bytes;
1125 /* If window allows, send new data */
1126 tc->snd_nxt = tc->snd_una_max;
1130 tcp_session_has_ooo_data (tcp_connection_t * tc)
1132 stream_session_t *s =
1133 stream_session_get (tc->c_s_index, tc->c_thread_index);
1134 return svm_fifo_has_ooo_data (s->server_rx_fifo);
1138 tcp46_output_inline (vlib_main_t * vm,
1139 vlib_node_runtime_t * node,
1140 vlib_frame_t * from_frame, int is_ip4)
1142 tcp_main_t *tm = vnet_get_tcp_main ();
1143 u32 n_left_from, next_index, *from, *to_next;
1144 u32 my_thread_index = vm->cpu_index;
1146 from = vlib_frame_vector_args (from_frame);
1147 n_left_from = from_frame->n_vectors;
1149 next_index = node->cached_next_index;
1151 while (n_left_from > 0)
1155 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1157 while (n_left_from > 0 && n_left_to_next > 0)
1161 tcp_connection_t *tc0;
1163 u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
1170 n_left_to_next -= 1;
1172 b0 = vlib_get_buffer (vm, bi0);
1173 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1175 th0 = vlib_buffer_get_current (b0);
1180 ih0 = vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4,
1181 &tc0->c_rmt_ip4, IP_PROTOCOL_TCP);
1182 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih0);
1189 ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
1190 &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
1191 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih0,
1196 /* Filter out DUPACKs if there are no OOO segments left */
1198 (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK))
1200 ASSERT (tc0->snt_dupacks > 0);
1202 if (!tcp_session_has_ooo_data (tc0))
1204 error0 = TCP_ERROR_FILTERED_DUPACKS;
1205 next0 = TCP_OUTPUT_NEXT_DROP;
1210 /* Retransmitted SYNs do reach this but it should be harmless */
1211 tc0->rcv_las = tc0->rcv_nxt;
1213 /* Stop DELACK timer and fix flags */
1215 ~(TCP_CONN_SNDACK | TCP_CONN_DELACK | TCP_CONN_BURSTACK);
1216 if (tcp_timer_is_active (tc0, TCP_TIMER_DELACK))
1218 tcp_timer_reset (tc0, TCP_TIMER_DELACK);
1221 /* If not retransmitting
1222 * 1) update snd_una_max (SYN, SYNACK, new data, FIN)
1223 * 2) If we're not tracking an ACK, start tracking */
1224 if (seq_lt (tc0->snd_una_max, tc0->snd_nxt))
1226 tc0->snd_una_max = tc0->snd_nxt;
1227 if (tc0->rtt_ts == 0)
1229 tc0->rtt_ts = tcp_time_now ();
1230 tc0->rtt_seq = tc0->snd_nxt;
1234 /* Set the retransmit timer if not set already and not
1235 * doing a pure ACK */
1236 if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT)
1237 && tc0->snd_nxt != tc0->snd_una)
1239 tcp_retransmit_timer_set (tm, tc0);
1243 /* set fib index to default and lookup node */
1244 /* XXX network virtualization (vrf/vni) */
1245 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1246 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1248 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1251 b0->error = error0 != 0 ? node->errors[error0] : 0;
1252 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1257 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1258 n_left_to_next, bi0, next0);
1261 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1264 return from_frame->n_vectors;
1268 tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1269 vlib_frame_t * from_frame)
1271 return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1275 tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1276 vlib_frame_t * from_frame)
1278 return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1281 VLIB_REGISTER_NODE (tcp4_output_node) =
1283 .function = tcp4_output,.name = "tcp4-output",
1284 /* Takes a vector of packets. */
1285 .vector_size = sizeof (u32),.n_errors = TCP_N_ERROR,.error_strings =
1286 tcp_error_strings,.n_next_nodes = TCP_OUTPUT_N_NEXT,.next_nodes =
1288 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1289 foreach_tcp4_output_next
1292 ,.format_buffer = format_tcp_header,.format_trace = format_tcp_tx_trace,};
1294 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output)
1295 VLIB_REGISTER_NODE (tcp6_output_node) =
1297 .function = tcp6_output,.name = "tcp6-output",
1298 /* Takes a vector of packets. */
1299 .vector_size = sizeof (u32),.n_errors = TCP_N_ERROR,.error_strings =
1300 tcp_error_strings,.n_next_nodes = TCP_OUTPUT_N_NEXT,.next_nodes =
1302 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1303 foreach_tcp6_output_next
1306 ,.format_buffer = format_tcp_header,.format_trace = format_tcp_tx_trace,};
1308 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output) u32
1309 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1311 tcp_connection_t *tc;
1313 tc = (tcp_connection_t *) tconn;
1314 tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED);
1318 typedef enum _tcp_reset_next
1320 TCP_RESET_NEXT_DROP,
1321 TCP_RESET_NEXT_IP_LOOKUP,
1325 #define foreach_tcp4_reset_next \
1326 _(DROP, "error-drop") \
1327 _(IP_LOOKUP, "ip4-lookup")
1329 #define foreach_tcp6_reset_next \
1330 _(DROP, "error-drop") \
1331 _(IP_LOOKUP, "ip6-lookup")
1334 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1335 vlib_frame_t * from_frame, u8 is_ip4)
1337 u32 n_left_from, next_index, *from, *to_next;
1338 u32 my_thread_index = vm->cpu_index;
1340 from = vlib_frame_vector_args (from_frame);
1341 n_left_from = from_frame->n_vectors;
1343 next_index = node->cached_next_index;
1345 while (n_left_from > 0)
1349 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1351 while (n_left_from > 0 && n_left_to_next > 0)
1355 u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
1362 n_left_to_next -= 1;
1364 b0 = vlib_get_buffer (vm, bi0);
1366 if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
1367 my_thread_index, is_ip4))
1369 error0 = TCP_ERROR_LOOKUP_DROPS;
1370 next0 = TCP_RESET_NEXT_DROP;
1374 /* Prepare to send to IP lookup */
1375 vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
1376 next0 = TCP_RESET_NEXT_IP_LOOKUP;
1379 b0->error = error0 != 0 ? node->errors[error0] : 0;
1380 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1381 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1386 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1387 n_left_to_next, bi0, next0);
1389 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1391 return from_frame->n_vectors;
1395 tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1396 vlib_frame_t * from_frame)
1398 return tcp46_send_reset_inline (vm, node, from_frame, 1);
1402 tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1403 vlib_frame_t * from_frame)
1405 return tcp46_send_reset_inline (vm, node, from_frame, 0);
1409 VLIB_REGISTER_NODE (tcp4_reset_node) = {
1410 .function = tcp4_send_reset,
1411 .name = "tcp4-reset",
1412 .vector_size = sizeof (u32),
1413 .n_errors = TCP_N_ERROR,
1414 .error_strings = tcp_error_strings,
1415 .n_next_nodes = TCP_RESET_N_NEXT,
1417 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1418 foreach_tcp4_reset_next
1425 VLIB_REGISTER_NODE (tcp6_reset_node) = {
1426 .function = tcp6_send_reset,
1427 .name = "tcp6-reset",
1428 .vector_size = sizeof (u32),
1429 .n_errors = TCP_N_ERROR,
1430 .error_strings = tcp_error_strings,
1431 .n_next_nodes = TCP_RESET_N_NEXT,
1433 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1434 foreach_tcp6_reset_next
1441 * fd.io coding-style-patch-verification: ON
1444 * eval: (c-set-style "gnu")