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 * Compute initial window and scale factor. As per RFC1323, window field in
95 * SYN and SYN-ACK segments is never scaled.
98 tcp_initial_window_to_advertise (tcp_connection_t * tc)
102 /* Initial wnd for SYN. Fifos are not allocated yet.
103 * Use some predefined value */
104 if (tc->state != TCP_STATE_SYN_RCVD)
106 return TCP_DEFAULT_RX_FIFO_SIZE;
109 available_space = stream_session_max_enqueue (&tc->connection);
110 tc->rcv_wscale = tcp_window_compute_scale (available_space);
111 tc->rcv_wnd = clib_min (available_space, TCP_WND_MAX << tc->rcv_wscale);
113 return clib_min (tc->rcv_wnd, TCP_WND_MAX);
117 * Compute and return window to advertise, scaled as per RFC1323
120 tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state)
122 u32 available_space, wnd, scaled_space;
124 if (state != TCP_STATE_ESTABLISHED)
125 return tcp_initial_window_to_advertise (tc);
127 available_space = stream_session_max_enqueue (&tc->connection);
128 scaled_space = available_space >> tc->rcv_wscale;
130 /* Need to update scale */
131 if (PREDICT_FALSE ((scaled_space == 0 && available_space != 0))
132 || (scaled_space >= TCP_WND_MAX))
133 tc->rcv_wscale = tcp_window_compute_scale (available_space);
135 wnd = clib_min (available_space, TCP_WND_MAX << tc->rcv_wscale);
138 return wnd >> tc->rcv_wscale;
142 * Write TCP options to segment.
145 tcp_options_write (u8 * data, tcp_options_t * opts)
148 u32 buf, seq_len = 4;
150 if (tcp_opts_mss (opts))
152 *data++ = TCP_OPTION_MSS;
153 *data++ = TCP_OPTION_LEN_MSS;
154 buf = clib_host_to_net_u16 (opts->mss);
155 clib_memcpy (data, &buf, sizeof (opts->mss));
156 data += sizeof (opts->mss);
157 opts_len += TCP_OPTION_LEN_MSS;
160 if (tcp_opts_wscale (opts))
162 *data++ = TCP_OPTION_WINDOW_SCALE;
163 *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
164 *data++ = opts->wscale;
165 opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
168 if (tcp_opts_sack_permitted (opts))
170 *data++ = TCP_OPTION_SACK_PERMITTED;
171 *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
172 opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
175 if (tcp_opts_tstamp (opts))
177 *data++ = TCP_OPTION_TIMESTAMP;
178 *data++ = TCP_OPTION_LEN_TIMESTAMP;
179 buf = clib_host_to_net_u32 (opts->tsval);
180 clib_memcpy (data, &buf, sizeof (opts->tsval));
181 data += sizeof (opts->tsval);
182 buf = clib_host_to_net_u32 (opts->tsecr);
183 clib_memcpy (data, &buf, sizeof (opts->tsecr));
184 data += sizeof (opts->tsecr);
185 opts_len += TCP_OPTION_LEN_TIMESTAMP;
188 if (tcp_opts_sack (opts))
191 u32 n_sack_blocks = clib_min (vec_len (opts->sacks),
192 TCP_OPTS_MAX_SACK_BLOCKS);
194 if (n_sack_blocks != 0)
196 *data++ = TCP_OPTION_SACK_BLOCK;
197 *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
198 for (i = 0; i < n_sack_blocks; i++)
200 buf = clib_host_to_net_u32 (opts->sacks[i].start);
201 clib_memcpy (data, &buf, seq_len);
203 buf = clib_host_to_net_u32 (opts->sacks[i].end);
204 clib_memcpy (data, &buf, seq_len);
207 opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
211 /* Terminate TCP options */
214 *data++ = TCP_OPTION_EOL;
215 opts_len += TCP_OPTION_LEN_EOL;
218 /* Pad with zeroes to a u32 boundary */
221 *data++ = TCP_OPTION_NOOP;
222 opts_len += TCP_OPTION_LEN_NOOP;
228 tcp_make_syn_options (tcp_options_t * opts, u32 initial_wnd)
232 opts->flags |= TCP_OPTS_FLAG_MSS;
233 opts->mss = dummy_mtu; /*XXX discover that */
234 len += TCP_OPTION_LEN_MSS;
236 opts->flags |= TCP_OPTS_FLAG_WSCALE;
237 opts->wscale = tcp_window_compute_scale (initial_wnd);
238 len += TCP_OPTION_LEN_WINDOW_SCALE;
240 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
241 opts->tsval = tcp_time_now ();
243 len += TCP_OPTION_LEN_TIMESTAMP;
245 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
246 len += TCP_OPTION_LEN_SACK_PERMITTED;
248 /* Align to needed boundary */
249 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
254 tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts)
258 opts->flags |= TCP_OPTS_FLAG_MSS;
259 opts->mss = dummy_mtu; /*XXX discover that */
260 len += TCP_OPTION_LEN_MSS;
262 if (tcp_opts_wscale (&tc->opt))
264 opts->flags |= TCP_OPTS_FLAG_WSCALE;
265 opts->wscale = tc->rcv_wscale;
266 len += TCP_OPTION_LEN_WINDOW_SCALE;
269 if (tcp_opts_tstamp (&tc->opt))
271 opts->flags |= TCP_OPTS_FLAG_TSTAMP;
272 opts->tsval = tcp_time_now ();
273 opts->tsecr = tc->tsval_recent;
274 len += TCP_OPTION_LEN_TIMESTAMP;
277 if (tcp_opts_sack_permitted (&tc->opt))
279 opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
280 len += TCP_OPTION_LEN_SACK_PERMITTED;
283 /* Align to needed boundary */
284 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
289 tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts)
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;
302 if (tcp_opts_sack_permitted (&tc->opt))
304 if (vec_len (tc->snd_sacks))
306 opts->flags |= TCP_OPTS_FLAG_SACK;
307 opts->sacks = tc->snd_sacks;
308 opts->n_sack_blocks = vec_len (tc->snd_sacks);
309 len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
313 /* Align to needed boundary */
314 len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
319 tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
324 case TCP_STATE_ESTABLISHED:
325 case TCP_STATE_FIN_WAIT_1:
326 return tcp_make_established_options (tc, opts);
327 case TCP_STATE_SYN_RCVD:
328 return tcp_make_synack_options (tc, opts);
329 case TCP_STATE_SYN_SENT:
330 return tcp_make_syn_options (opts,
331 tcp_initial_window_to_advertise (tc));
333 clib_warning ("Not handled!");
338 #define tcp_get_free_buffer_index(tm, bidx) \
340 u32 *my_tx_buffers, n_free_buffers; \
341 u32 cpu_index = tm->vlib_main->cpu_index; \
342 my_tx_buffers = tm->tx_buffers[cpu_index]; \
343 if (PREDICT_FALSE(vec_len (my_tx_buffers) == 0)) \
345 n_free_buffers = 32; /* TODO config or macro */ \
346 vec_validate (my_tx_buffers, n_free_buffers - 1); \
347 _vec_len(my_tx_buffers) = vlib_buffer_alloc_from_free_list ( \
348 tm->vlib_main, my_tx_buffers, n_free_buffers, \
349 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); \
350 tm->tx_buffers[cpu_index] = my_tx_buffers; \
352 /* buffer shortage */ \
353 if (PREDICT_FALSE (vec_len (my_tx_buffers) == 0)) \
355 *bidx = my_tx_buffers[_vec_len (my_tx_buffers)-1]; \
356 _vec_len (my_tx_buffers) -= 1; \
360 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
362 vlib_buffer_t *it = b;
365 it->current_data = 0;
366 it->current_length = 0;
367 it->total_length_not_including_first_buffer = 0;
369 while ((it->flags & VLIB_BUFFER_NEXT_PRESENT)
370 && (it = vlib_get_buffer (vm, it->next_buffer)));
372 /* Leave enough space for headers */
373 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
380 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
383 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
384 u8 tcp_opts_len, tcp_hdr_opts_len;
388 wnd = tcp_window_to_advertise (tc, state);
390 /* Make and write options */
391 tcp_opts_len = tcp_make_established_options (tc, snd_opts);
392 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
394 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
395 tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
397 tcp_options_write ((u8 *) (th + 1), snd_opts);
400 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
404 * Convert buffer to ACK
407 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
409 tcp_main_t *tm = vnet_get_tcp_main ();
410 vlib_main_t *vm = tm->vlib_main;
412 tcp_reuse_buffer (vm, b);
413 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
414 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
418 * Convert buffer to FIN-ACK
421 tcp_make_finack (tcp_connection_t * tc, vlib_buffer_t * b)
423 tcp_main_t *tm = vnet_get_tcp_main ();
424 vlib_main_t *vm = tm->vlib_main;
426 tcp_reuse_buffer (vm, b);
427 tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK | TCP_FLAG_FIN);
429 /* Reset flags, make sure ack is sent */
430 tc->flags = TCP_CONN_SNDACK;
431 vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
437 * Convert buffer to SYN-ACK
440 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
442 tcp_main_t *tm = vnet_get_tcp_main ();
443 vlib_main_t *vm = tm->vlib_main;
444 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
445 u8 tcp_opts_len, tcp_hdr_opts_len;
450 memset (snd_opts, 0, sizeof (*snd_opts));
452 tcp_reuse_buffer (vm, b);
454 /* Set random initial sequence */
455 time_now = tcp_time_now ();
457 tc->iss = random_u32 (&time_now);
458 tc->snd_una = tc->iss;
459 tc->snd_nxt = tc->iss + 1;
460 tc->snd_una_max = tc->snd_nxt;
462 initial_wnd = tcp_initial_window_to_advertise (tc);
464 /* Make and write options */
465 tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
466 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
468 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
469 tc->rcv_nxt, tcp_hdr_opts_len,
470 TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
472 tcp_options_write ((u8 *) (th + 1), snd_opts);
474 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
475 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
477 /* Init retransmit timer */
478 tcp_retransmit_timer_set (tm, tc);
482 tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
485 u32 *to_next, next_index;
488 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
491 /* Default FIB for now */
492 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
494 /* Send to IP lookup */
495 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
496 f = vlib_get_frame_to_node (vm, next_index);
498 /* Enqueue the packet */
499 to_next = vlib_frame_vector_args (f);
502 vlib_put_frame_to_node (vm, next_index, f);
506 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0,
507 tcp_state_t state, u32 my_thread_index, u8 is_ip4)
509 u8 tcp_hdr_len = sizeof (tcp_header_t);
513 ip4_address_t src_ip40;
514 ip6_address_t src_ip60;
518 /* Find IP and TCP headers */
521 ih4 = vlib_buffer_get_current (b0);
522 th0 = ip4_next_header (ih4);
526 ih6 = vlib_buffer_get_current (b0);
527 th0 = ip6_next_header (ih6);
530 /* Swap src and dst ip */
533 ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
534 src_ip40.as_u32 = ih4->src_address.as_u32;
535 ih4->src_address.as_u32 = ih4->dst_address.as_u32;
536 ih4->dst_address.as_u32 = src_ip40.as_u32;
538 /* Chop the end of the pkt */
539 b0->current_length += ip4_header_bytes (ih4) + tcp_hdr_len;
543 ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
544 clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
545 clib_memcpy (&ih6->src_address, &ih6->dst_address,
546 sizeof (ip6_address_t));
547 clib_memcpy (&ih6->dst_address, &src_ip60, sizeof (ip6_address_t));
549 /* Chop the end of the pkt */
550 b0->current_length += sizeof (ip6_header_t) + tcp_hdr_len;
553 /* Try to determine what/why we're actually resetting and swap
554 * src and dst ports */
555 if (state == TCP_STATE_CLOSED)
560 tmp = clib_net_to_host_u32 (th0->seq_number);
562 /* Got a SYN for no listener. */
563 th0->flags = TCP_FLAG_RST | TCP_FLAG_ACK;
564 th0->ack_number = clib_host_to_net_u32 (tmp + 1);
568 else if (state >= TCP_STATE_SYN_SENT)
570 th0->flags = TCP_FLAG_RST | TCP_FLAG_ACK;
571 th0->seq_number = th0->ack_number;
575 src_port0 = th0->src_port;
576 th0->src_port = th0->dst_port;
577 th0->dst_port = src_port0;
579 th0->data_offset_and_reserved = (tcp_hdr_len >> 2) << 4;
580 th0->urgent_pointer = 0;
582 /* Compute checksum */
585 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
590 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
598 * Send reset without reusing existing buffer
601 tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4)
605 tcp_main_t *tm = vnet_get_tcp_main ();
606 vlib_main_t *vm = tm->vlib_main;
607 u8 tcp_hdr_len, flags = 0;
608 tcp_header_t *th, *pkt_th;
610 ip4_header_t *ih4, *pkt_ih4;
611 ip6_header_t *ih6, *pkt_ih6;
613 tcp_get_free_buffer_index (tm, &bi);
614 b = vlib_get_buffer (vm, bi);
616 /* Leave enough space for headers */
617 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
619 /* Make and write options */
620 tcp_hdr_len = sizeof (tcp_header_t);
624 pkt_ih4 = vlib_buffer_get_current (pkt);
625 pkt_th = ip4_next_header (pkt_ih4);
629 pkt_ih6 = vlib_buffer_get_current (pkt);
630 pkt_th = ip6_next_header (pkt_ih6);
633 if (tcp_ack (pkt_th))
635 flags = TCP_FLAG_RST;
636 seq = pkt_th->ack_number;
641 flags = TCP_FLAG_RST | TCP_FLAG_ACK;
643 ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
646 th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
647 seq, ack, tcp_hdr_len, flags, 0);
649 /* Swap src and dst ip */
652 ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
653 ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
654 &pkt_ih4->src_address, IP_PROTOCOL_TCP);
655 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
660 pkt_ih6 = (ip6_header_t *) (pkt_th - 1);
661 ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
664 vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
665 &pkt_ih6->src_address, IP_PROTOCOL_TCP);
666 th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
670 tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4);
674 tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b)
676 tcp_header_t *th = vlib_buffer_get_current (b);
681 ih = vlib_buffer_push_ip4 (tm->vlib_main, b, &tc->c_lcl_ip4,
682 &tc->c_rmt_ip4, IP_PROTOCOL_TCP);
683 th->checksum = ip4_tcp_udp_compute_checksum (tm->vlib_main, b, ih);
690 ih = vlib_buffer_push_ip6 (tm->vlib_main, b, &tc->c_lcl_ip6,
691 &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
692 th->checksum = ip6_tcp_udp_icmp_compute_checksum (tm->vlib_main, b, ih,
701 * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
702 * The packet is not forwarded through tcpx_output to avoid doing lookups
703 * in the half_open pool.
706 tcp_send_syn (tcp_connection_t * tc)
710 tcp_main_t *tm = vnet_get_tcp_main ();
711 vlib_main_t *vm = tm->vlib_main;
712 u8 tcp_hdr_opts_len, tcp_opts_len;
716 tcp_options_t snd_opts;
718 tcp_get_free_buffer_index (tm, &bi);
719 b = vlib_get_buffer (vm, bi);
721 /* Leave enough space for headers */
722 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
724 /* Set random initial sequence */
725 time_now = tcp_time_now ();
727 tc->iss = random_u32 (&time_now);
728 tc->snd_una = tc->iss;
729 tc->snd_una_max = tc->snd_nxt = tc->iss + 1;
731 initial_wnd = tcp_initial_window_to_advertise (tc);
733 /* Make and write options */
734 memset (&snd_opts, 0, sizeof (snd_opts));
735 tcp_opts_len = tcp_make_syn_options (&snd_opts, initial_wnd);
736 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
738 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
739 tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
742 tcp_options_write ((u8 *) (th + 1), &snd_opts);
744 /* Measure RTT with this */
745 tc->rtt_ts = tcp_time_now ();
746 tc->rtt_seq = tc->snd_nxt;
748 /* Start retransmit trimer */
749 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK);
752 /* Set the connection establishment timer */
753 tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME);
755 tcp_push_ip_hdr (tm, tc, b);
756 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
760 tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4)
762 u32 *to_next, next_index;
765 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
768 /* Decide where to send the packet */
769 next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
770 f = vlib_get_frame_to_node (vm, next_index);
772 /* Enqueue the packet */
773 to_next = vlib_frame_vector_args (f);
776 vlib_put_frame_to_node (vm, next_index, f);
783 tcp_send_fin (tcp_connection_t * tc)
787 tcp_main_t *tm = vnet_get_tcp_main ();
788 vlib_main_t *vm = tm->vlib_main;
790 tcp_get_free_buffer_index (tm, &bi);
791 b = vlib_get_buffer (vm, bi);
793 /* Leave enough space for headers */
794 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
796 tcp_make_finack (tc, b);
798 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
802 tcp_make_state_flags (tcp_state_t next_state)
806 case TCP_STATE_ESTABLISHED:
808 case TCP_STATE_SYN_RCVD:
809 return TCP_FLAG_SYN | TCP_FLAG_ACK;
810 case TCP_STATE_SYN_SENT:
812 case TCP_STATE_LAST_ACK:
813 case TCP_STATE_FIN_WAIT_1:
816 clib_warning ("Shouldn't be here!");
822 * Push TCP header and update connection variables
825 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b,
826 tcp_state_t next_state)
828 u32 advertise_wnd, data_len;
829 u8 tcp_opts_len, tcp_hdr_opts_len, opts_write_len, flags;
830 tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
833 data_len = b->current_length;
834 vnet_buffer (b)->tcp.flags = 0;
836 /* Make and write options */
837 memset (snd_opts, 0, sizeof (*snd_opts));
838 tcp_opts_len = tcp_make_options (tc, snd_opts, next_state);
839 tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
841 /* Get rcv window to advertise */
842 advertise_wnd = tcp_window_to_advertise (tc, next_state);
843 flags = tcp_make_state_flags (next_state);
845 /* Push header and options */
846 th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
847 tc->rcv_nxt, tcp_hdr_opts_len, flags,
850 opts_write_len = tcp_options_write ((u8 *) (th + 1), snd_opts);
852 ASSERT (opts_write_len == tcp_opts_len);
854 /* Tag the buffer with the connection index */
855 vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
857 tc->snd_nxt += data_len;
860 /* Send delayed ACK when timer expires */
862 tcp_timer_delack_handler (u32 index)
864 tcp_main_t *tm = vnet_get_tcp_main ();
865 vlib_main_t *vm = tm->vlib_main;
866 u32 thread_index = os_get_cpu_number ();
867 tcp_connection_t *tc;
871 tc = tcp_connection_get (index, thread_index);
874 tcp_get_free_buffer_index (tm, &bi);
875 b = vlib_get_buffer (vm, bi);
877 /* Fill in the ACK */
878 tcp_make_ack (tc, b);
880 tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
881 tc->flags &= ~TCP_CONN_DELACK;
883 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
886 /** Build a retransmit segment
888 * @return the number of bytes in the segment or 0 if there's nothing to
892 tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
895 tcp_main_t *tm = vnet_get_tcp_main ();
896 vlib_main_t *vm = tm->vlib_main;
897 u32 n_bytes, offset = 0;
898 sack_scoreboard_hole_t *hole;
901 tcp_reuse_buffer (vm, b);
903 ASSERT (tc->state == TCP_STATE_ESTABLISHED);
904 ASSERT (max_bytes != 0);
906 if (tcp_opts_sack_permitted (&tc->opt))
908 /* XXX get first hole not retransmitted yet */
909 hole = scoreboard_first_hole (&tc->sack_sb);
913 offset = hole->start - tc->snd_una;
914 hole_size = hole->end - hole->start;
918 if (hole_size < max_bytes)
919 max_bytes = hole_size;
923 if (seq_geq (tc->snd_nxt, tc->snd_una_max))
927 n_bytes = stream_session_peek_bytes (&tc->connection,
928 vlib_buffer_get_current (b), offset,
930 ASSERT (n_bytes != 0);
932 tc->snd_nxt += n_bytes;
933 tcp_push_hdr_i (tc, b, tc->state);
939 tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
941 tcp_main_t *tm = vnet_get_tcp_main ();
942 vlib_main_t *vm = tm->vlib_main;
943 u32 thread_index = os_get_cpu_number ();
944 tcp_connection_t *tc;
946 u32 bi, max_bytes, snd_space;
950 tc = tcp_half_open_connection_get (index);
954 tc = tcp_connection_get (index, thread_index);
957 /* Make sure timer handle is set to invalid */
958 tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
960 /* Increment RTO backoff (also equal to number of retries) */
963 /* Go back to first un-acked byte */
964 tc->snd_nxt = tc->snd_una;
967 tcp_get_free_buffer_index (tm, &bi);
968 b = vlib_get_buffer (vm, bi);
970 if (tc->state == TCP_STATE_ESTABLISHED)
972 tcp_fastrecovery_off (tc);
974 /* Exponential backoff */
975 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
977 /* Figure out what and how many bytes we can send */
978 snd_space = tcp_available_snd_space (tc);
979 max_bytes = clib_min (tc->snd_mss, snd_space);
980 tcp_prepare_retransmit_segment (tc, b, max_bytes);
982 tc->rtx_bytes += max_bytes;
984 /* No fancy recovery for now! */
985 scoreboard_clear (&tc->sack_sb);
989 /* Retransmit for SYN/SYNACK */
990 ASSERT (tc->state == TCP_STATE_SYN_RCVD
991 || tc->state == TCP_STATE_SYN_SENT);
993 /* Try without increasing RTO a number of times. If this fails,
994 * start growing RTO exponentially */
995 if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
996 tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
998 vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
999 tcp_push_hdr_i (tc, b, tc->state);
1004 tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1006 /* Re-enable retransmit timer */
1007 tcp_retransmit_timer_set (tm, tc);
1011 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1013 /* This goes straight to ipx_lookup */
1014 tcp_push_ip_hdr (tm, tc, b);
1015 tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
1017 /* Re-enable retransmit timer */
1018 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN,
1019 tc->rto * TCP_TO_TIMER_TICK);
1024 tcp_timer_retransmit_handler (u32 index)
1026 tcp_timer_retransmit_handler_i (index, 0);
1030 tcp_timer_retransmit_syn_handler (u32 index)
1032 tcp_timer_retransmit_handler_i (index, 1);
1036 * Retansmit first unacked segment */
1038 tcp_retransmit_first_unacked (tcp_connection_t * tc)
1040 tcp_main_t *tm = vnet_get_tcp_main ();
1041 u32 snd_nxt = tc->snd_nxt;
1045 tc->snd_nxt = tc->snd_una;
1048 tcp_get_free_buffer_index (tm, &bi);
1049 b = vlib_get_buffer (tm->vlib_main, bi);
1051 tcp_prepare_retransmit_segment (tc, b, tc->snd_mss);
1052 tcp_enqueue_to_output (tm->vlib_main, b, bi, tc->c_is_ip4);
1054 tc->snd_nxt = snd_nxt;
1055 tc->rtx_bytes += tc->snd_mss;
1059 tcp_fast_retransmit (tcp_connection_t * tc)
1061 tcp_main_t *tm = vnet_get_tcp_main ();
1062 u32 snd_space, max_bytes, n_bytes, bi;
1065 ASSERT (tcp_in_fastrecovery (tc));
1067 clib_warning ("fast retransmit!");
1069 /* Start resending from first un-acked segment */
1070 tc->snd_nxt = tc->snd_una;
1072 snd_space = tcp_available_snd_space (tc);
1076 tcp_get_free_buffer_index (tm, &bi);
1077 b = vlib_get_buffer (tm->vlib_main, bi);
1079 max_bytes = clib_min (tc->snd_mss, snd_space);
1080 n_bytes = tcp_prepare_retransmit_segment (tc, b, max_bytes);
1082 /* Nothing left to retransmit */
1086 tcp_enqueue_to_output (tm->vlib_main, b, bi, tc->c_is_ip4);
1088 snd_space -= n_bytes;
1091 /* If window allows, send new data */
1092 tc->snd_nxt = tc->snd_una_max;
1096 tcp_session_has_ooo_data (tcp_connection_t * tc)
1098 stream_session_t *s =
1099 stream_session_get (tc->c_s_index, tc->c_thread_index);
1100 return svm_fifo_has_ooo_data (s->server_rx_fifo);
1104 tcp46_output_inline (vlib_main_t * vm,
1105 vlib_node_runtime_t * node,
1106 vlib_frame_t * from_frame, int is_ip4)
1108 tcp_main_t *tm = vnet_get_tcp_main ();
1109 u32 n_left_from, next_index, *from, *to_next;
1110 u32 my_thread_index = vm->cpu_index;
1112 from = vlib_frame_vector_args (from_frame);
1113 n_left_from = from_frame->n_vectors;
1115 next_index = node->cached_next_index;
1117 while (n_left_from > 0)
1121 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1123 while (n_left_from > 0 && n_left_to_next > 0)
1127 tcp_connection_t *tc0;
1129 u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
1136 n_left_to_next -= 1;
1138 b0 = vlib_get_buffer (vm, bi0);
1139 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1141 th0 = vlib_buffer_get_current (b0);
1146 ih0 = vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4,
1147 &tc0->c_rmt_ip4, IP_PROTOCOL_TCP);
1148 th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih0);
1155 ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
1156 &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
1157 th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih0,
1162 /* Filter out DUPACKs if there are no OOO segments left */
1164 (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK))
1167 ASSERT (tc0->snt_dupacks >= 0);
1168 if (!tcp_session_has_ooo_data (tc0))
1170 error0 = TCP_ERROR_FILTERED_DUPACKS;
1171 next0 = TCP_OUTPUT_NEXT_DROP;
1176 /* Retransmitted SYNs do reach this but it should be harmless */
1177 tc0->rcv_las = tc0->rcv_nxt;
1179 /* Stop DELACK timer and fix flags */
1181 ~(TCP_CONN_SNDACK | TCP_CONN_DELACK | TCP_CONN_BURSTACK);
1182 if (tcp_timer_is_active (tc0, TCP_TIMER_DELACK))
1184 tcp_timer_reset (tc0, TCP_TIMER_DELACK);
1187 /* If not retransmitting
1188 * 1) update snd_una_max (SYN, SYNACK, new data, FIN)
1189 * 2) If we're not tracking an ACK, start tracking */
1190 if (seq_lt (tc0->snd_una_max, tc0->snd_nxt))
1192 tc0->snd_una_max = tc0->snd_nxt;
1193 if (tc0->rtt_ts == 0)
1195 tc0->rtt_ts = tcp_time_now ();
1196 tc0->rtt_seq = tc0->snd_nxt;
1200 /* Set the retransmit timer if not set already and not
1201 * doing a pure ACK */
1202 if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT)
1203 && tc0->snd_nxt != tc0->snd_una)
1205 tcp_retransmit_timer_set (tm, tc0);
1209 /* set fib index to default and lookup node */
1210 /* XXX network virtualization (vrf/vni) */
1211 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1212 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1214 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1217 b0->error = error0 != 0 ? node->errors[error0] : 0;
1218 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1223 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1224 n_left_to_next, bi0, next0);
1227 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1230 return from_frame->n_vectors;
1234 tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1235 vlib_frame_t * from_frame)
1237 return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1241 tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1242 vlib_frame_t * from_frame)
1244 return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1247 VLIB_REGISTER_NODE (tcp4_output_node) =
1249 .function = tcp4_output,.name = "tcp4-output",
1250 /* Takes a vector of packets. */
1251 .vector_size = sizeof (u32),.n_errors = TCP_N_ERROR,.error_strings =
1252 tcp_error_strings,.n_next_nodes = TCP_OUTPUT_N_NEXT,.next_nodes =
1254 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1255 foreach_tcp4_output_next
1258 ,.format_buffer = format_tcp_header,.format_trace = format_tcp_tx_trace,};
1260 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output)
1261 VLIB_REGISTER_NODE (tcp6_output_node) =
1263 .function = tcp6_output,.name = "tcp6-output",
1264 /* Takes a vector of packets. */
1265 .vector_size = sizeof (u32),.n_errors = TCP_N_ERROR,.error_strings =
1266 tcp_error_strings,.n_next_nodes = TCP_OUTPUT_N_NEXT,.next_nodes =
1268 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1269 foreach_tcp6_output_next
1272 ,.format_buffer = format_tcp_header,.format_trace = format_tcp_tx_trace,};
1274 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output) u32
1275 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1277 tcp_connection_t *tc;
1279 tc = (tcp_connection_t *) tconn;
1280 tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED);
1284 typedef enum _tcp_reset_next
1286 TCP_RESET_NEXT_DROP,
1287 TCP_RESET_NEXT_IP_LOOKUP,
1291 #define foreach_tcp4_reset_next \
1292 _(DROP, "error-drop") \
1293 _(IP_LOOKUP, "ip4-lookup")
1295 #define foreach_tcp6_reset_next \
1296 _(DROP, "error-drop") \
1297 _(IP_LOOKUP, "ip6-lookup")
1300 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1301 vlib_frame_t * from_frame, u8 is_ip4)
1303 u32 n_left_from, next_index, *from, *to_next;
1304 u32 my_thread_index = vm->cpu_index;
1306 from = vlib_frame_vector_args (from_frame);
1307 n_left_from = from_frame->n_vectors;
1309 next_index = node->cached_next_index;
1311 while (n_left_from > 0)
1315 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1317 while (n_left_from > 0 && n_left_to_next > 0)
1321 u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
1328 n_left_to_next -= 1;
1330 b0 = vlib_get_buffer (vm, bi0);
1332 if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
1333 my_thread_index, is_ip4))
1335 error0 = TCP_ERROR_LOOKUP_DROPS;
1336 next0 = TCP_RESET_NEXT_DROP;
1340 /* Prepare to send to IP lookup */
1341 vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
1342 next0 = TCP_RESET_NEXT_IP_LOOKUP;
1345 b0->error = error0 != 0 ? node->errors[error0] : 0;
1346 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1347 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1352 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1353 n_left_to_next, bi0, next0);
1355 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1357 return from_frame->n_vectors;
1361 tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1362 vlib_frame_t * from_frame)
1364 return tcp46_send_reset_inline (vm, node, from_frame, 1);
1368 tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
1369 vlib_frame_t * from_frame)
1371 return tcp46_send_reset_inline (vm, node, from_frame, 0);
1375 VLIB_REGISTER_NODE (tcp4_reset_node) = {
1376 .function = tcp4_send_reset,
1377 .name = "tcp4-reset",
1378 .vector_size = sizeof (u32),
1379 .n_errors = TCP_N_ERROR,
1380 .error_strings = tcp_error_strings,
1381 .n_next_nodes = TCP_RESET_N_NEXT,
1383 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1384 foreach_tcp4_reset_next
1391 VLIB_REGISTER_NODE (tcp6_reset_node) = {
1392 .function = tcp6_send_reset,
1393 .name = "tcp6-reset",
1394 .vector_size = sizeof (u32),
1395 .n_errors = TCP_N_ERROR,
1396 .error_strings = tcp_error_strings,
1397 .n_next_nodes = TCP_RESET_N_NEXT,
1399 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
1400 foreach_tcp6_reset_next
1407 * fd.io coding-style-patch-verification: ON
1410 * eval: (c-set-style "gnu")