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 <vppinfra/sparse_vec.h>
17 #include <vnet/tcp/tcp_packet.h>
18 #include <vnet/tcp/tcp.h>
19 #include <vnet/session/session.h>
22 static char *tcp_error_strings[] = {
23 #define tcp_error(n,s) s,
24 #include <vnet/tcp/tcp_error.def>
28 /* All TCP nodes have the same outgoing arcs */
29 #define foreach_tcp_state_next \
30 _ (DROP, "error-drop") \
31 _ (TCP4_OUTPUT, "tcp4-output") \
32 _ (TCP6_OUTPUT, "tcp6-output")
34 typedef enum _tcp_established_next
36 #define _(s,n) TCP_ESTABLISHED_NEXT_##s,
37 foreach_tcp_state_next
39 TCP_ESTABLISHED_N_NEXT,
40 } tcp_established_next_t;
42 typedef enum _tcp_rcv_process_next
44 #define _(s,n) TCP_RCV_PROCESS_NEXT_##s,
45 foreach_tcp_state_next
47 TCP_RCV_PROCESS_N_NEXT,
48 } tcp_rcv_process_next_t;
50 typedef enum _tcp_syn_sent_next
52 #define _(s,n) TCP_SYN_SENT_NEXT_##s,
53 foreach_tcp_state_next
56 } tcp_syn_sent_next_t;
58 typedef enum _tcp_listen_next
60 #define _(s,n) TCP_LISTEN_NEXT_##s,
61 foreach_tcp_state_next
66 /* Generic, state independent indices */
67 typedef enum _tcp_state_next
69 #define _(s,n) TCP_NEXT_##s,
70 foreach_tcp_state_next
75 #define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \
76 : TCP_NEXT_TCP6_OUTPUT)
78 vlib_node_registration_t tcp4_established_node;
79 vlib_node_registration_t tcp6_established_node;
82 * Validate segment sequence number. As per RFC793:
84 * Segment Receive Test
86 * ------- ------- -------------------------------------------
87 * 0 0 SEG.SEQ = RCV.NXT
88 * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
90 * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
91 * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
93 * This ultimately consists in checking if segment falls within the window.
94 * The one important difference compared to RFC793 is that we use rcv_las,
95 * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the
96 * peer's reference when computing our receive window.
98 * This accepts only segments within the window.
101 tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
103 return seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd)
104 && seq_geq (seq, tc->rcv_nxt);
108 tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
111 u8 opt_len, opts_len, kind;
115 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
116 data = (const u8 *) (th + 1);
118 /* Zero out all flags but those set in SYN */
119 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
121 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
125 /* Get options length */
126 if (kind == TCP_OPTION_EOL)
128 else if (kind == TCP_OPTION_NOOP)
137 /* weird option length */
138 if (opt_len < 2 || opt_len > opts_len)
146 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
148 to->flags |= TCP_OPTS_FLAG_MSS;
149 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
152 case TCP_OPTION_WINDOW_SCALE:
153 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
155 to->flags |= TCP_OPTS_FLAG_WSCALE;
156 to->wscale = data[2];
157 if (to->wscale > TCP_MAX_WND_SCALE)
159 clib_warning ("Illegal window scaling value: %d",
161 to->wscale = TCP_MAX_WND_SCALE;
165 case TCP_OPTION_TIMESTAMP:
166 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
168 to->flags |= TCP_OPTS_FLAG_TSTAMP;
169 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
170 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
173 case TCP_OPTION_SACK_PERMITTED:
174 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
175 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
177 case TCP_OPTION_SACK_BLOCK:
178 /* If SACK permitted was not advertised or a SYN, break */
179 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
182 /* If too short or not correctly formatted, break */
183 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
186 to->flags |= TCP_OPTS_FLAG_SACK;
187 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
188 vec_reset_length (to->sacks);
189 for (j = 0; j < to->n_sack_blocks; j++)
191 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
192 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
193 vec_add1 (to->sacks, b);
197 /* Nothing to see here */
204 tcp_segment_check_paws (tcp_connection_t * tc)
206 /* XXX normally test for timestamp should be lt instead of leq, but for
207 * local testing this is not enough */
208 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
209 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
213 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
215 * It first verifies if segment has a wrapped sequence number (PAWS) and then
216 * does the processing associated to the first four steps (ignoring security
217 * and precedence): sequence number, rst bit and syn bit checks.
219 * @return 0 if segments passes validation.
222 tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
223 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
227 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
230 tcp_options_parse (th0, &tc0->opt);
232 /* RFC1323: Check against wrapped sequence numbers (PAWS). If we have
233 * timestamp to echo and it's less than tsval_recent, drop segment
234 * but still send an ACK in order to retain TCP's mechanism for detecting
235 * and recovering from half-open connections */
236 paws_failed = tcp_segment_check_paws (tc0);
239 clib_warning ("paws failed");
241 /* If it just so happens that a segment updates tsval_recent for a
242 * segment over 24 days old, invalidate tsval_recent. */
243 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
246 /* Age isn't reset until we get a valid tsval (bsd inspired) */
247 tc0->tsval_recent = 0;
251 /* Drop after ack if not rst */
254 tcp_make_ack (tc0, b0);
255 *next0 = tcp_next_output (tc0->c_is_ip4);
261 /* 1st: check sequence number */
262 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
263 vnet_buffer (b0)->tcp.seq_end))
268 tcp_make_ack (tc0, b0);
269 *next0 = tcp_next_output (tc0->c_is_ip4);
274 /* 2nd: check the RST bit */
277 tcp_connection_reset (tc0);
281 /* 3rd: check security and precedence (skip) */
283 /* 4th: check the SYN bit */
286 tcp_send_reset (b0, tc0->c_is_ip4);
290 /* If PAWS passed and segment in window, save timestamp */
293 tc0->tsval_recent = tc0->opt.tsval;
294 tc0->tsval_recent_age = tcp_time_now ();
301 tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
303 /* SND.UNA =< SEG.ACK =< SND.NXT */
304 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
305 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
309 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
311 * Note that although the original article, srtt and rttvar are scaled
312 * to minimize round-off errors, here we don't. Instead, we rely on
313 * better precision time measurements.
315 * TODO support us rtt resolution
318 tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
324 err = mrtt - tc->srtt;
325 tc->srtt += err >> 3;
327 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
328 * The increase should be bound */
329 tc->rttvar += (clib_abs (err) - tc->rttvar) >> 2;
333 /* First measurement. */
335 tc->rttvar = mrtt << 1;
339 /** Update RTT estimate and RTO timer
341 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
342 * timing. Middle boxes are known to fiddle with TCP options so we
343 * should give higher priority to ACK timing.
345 * return 1 if valid rtt 0 otherwise
348 tcp_update_rtt (tcp_connection_t * tc, u32 ack)
352 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
353 * RTT because they're ambiguous. */
354 if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff)
356 mrtt = tcp_time_now () - tc->rtt_ts;
360 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
361 * snd_una, i.e., the left side of the send window:
362 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
363 * try to update rtt for dupacks */
364 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
366 mrtt = tcp_time_now () - tc->opt.tsecr;
369 /* Ignore dubious measurements */
370 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
373 tcp_estimate_rtt (tc, mrtt);
375 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
381 * Dequeue bytes that have been acked and while at it update RTT estimates.
384 tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
386 /* Dequeue the newly ACKed bytes */
387 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
389 /* Update rtt and rto */
390 if (tcp_update_rtt (tc, ack))
392 /* Good ACK received and valid RTT, make sure retransmit backoff is 0 */
397 /** Check if dupack as per RFC5681 Sec. 2 */
399 tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
401 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
402 && seq_gt (tc->snd_una_max, tc->snd_una)
403 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
404 && (new_snd_wnd == tc->snd_wnd));
408 scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
410 sack_scoreboard_hole_t *next, *prev;
412 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
414 next = pool_elt_at_index (sb->holes, hole->next);
415 next->prev = hole->prev;
418 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
420 prev = pool_elt_at_index (sb->holes, hole->prev);
421 prev->next = hole->next;
425 sb->head = hole->next;
428 pool_put (sb->holes, hole);
431 sack_scoreboard_hole_t *
432 scoreboard_insert_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * prev,
435 sack_scoreboard_hole_t *hole, *next;
438 pool_get (sb->holes, hole);
439 memset (hole, 0, sizeof (*hole));
443 hole_index = hole - sb->holes;
447 hole->prev = prev - sb->holes;
448 hole->next = prev->next;
450 if ((next = scoreboard_next_hole (sb, hole)))
451 next->prev = hole_index;
453 prev->next = hole_index;
457 sb->head = hole_index;
458 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
459 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
466 tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
468 sack_scoreboard_t *sb = &tc->sack_sb;
469 sack_block_t *blk, tmp;
470 sack_scoreboard_hole_t *hole, *next_hole;
474 if (!tcp_opts_sack (tc) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
477 /* Remove invalid blocks */
478 vec_foreach (blk, tc->opt.sacks)
480 if (seq_lt (blk->start, blk->end)
481 && seq_gt (blk->start, tc->snd_una)
482 && seq_gt (blk->start, ack) && seq_lt (blk->end, tc->snd_nxt))
485 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
488 /* Add block for cumulative ack */
489 if (seq_gt (ack, tc->snd_una))
491 tmp.start = tc->snd_una;
493 vec_add1 (tc->opt.sacks, tmp);
496 if (vec_len (tc->opt.sacks) == 0)
499 /* Make sure blocks are ordered */
500 for (i = 0; i < vec_len (tc->opt.sacks); i++)
501 for (j = i; j < vec_len (tc->opt.sacks); j++)
502 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
504 tmp = tc->opt.sacks[i];
505 tc->opt.sacks[i] = tc->opt.sacks[j];
506 tc->opt.sacks[j] = tmp;
509 /* If no holes, insert the first that covers all outstanding bytes */
510 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
512 scoreboard_insert_hole (sb, 0, tc->snd_una, tc->snd_una_max);
515 /* Walk the holes with the SACK blocks */
516 hole = pool_elt_at_index (sb->holes, sb->head);
517 while (hole && blk_index < vec_len (tc->opt.sacks))
519 blk = &tc->opt.sacks[blk_index];
521 if (seq_leq (blk->start, hole->start))
523 /* Block covers hole. Remove hole */
524 if (seq_geq (blk->end, hole->end))
526 next_hole = scoreboard_next_hole (sb, hole);
528 /* Byte accounting */
529 if (seq_lt (hole->end, ack))
531 /* Bytes lost because snd wnd left edge advances */
532 if (seq_lt (next_hole->start, ack))
533 sb->sacked_bytes -= next_hole->start - hole->end;
535 sb->sacked_bytes -= ack - hole->end;
539 sb->sacked_bytes += scoreboard_hole_bytes (hole);
542 scoreboard_remove_hole (sb, hole);
545 /* Partial overlap */
548 sb->sacked_bytes += blk->end - hole->start;
549 hole->start = blk->end;
555 /* Hole must be split */
556 if (seq_leq (blk->end, hole->end))
558 sb->sacked_bytes += blk->end - blk->start;
559 scoreboard_insert_hole (sb, hole, blk->end, hole->end);
560 hole->end = blk->start - 1;
565 sb->sacked_bytes += hole->end - blk->start + 1;
566 hole->end = blk->start - 1;
567 hole = scoreboard_next_hole (sb, hole);
575 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
576 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
578 tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
580 if (tc->snd_wl1 < seq || (tc->snd_wl1 == seq && tc->snd_wl2 <= ack))
582 tc->snd_wnd = snd_wnd;
589 tcp_cc_congestion (tcp_connection_t * tc)
591 tc->cc_algo->congestion (tc);
595 tcp_cc_recover (tcp_connection_t * tc)
597 if (tcp_in_fastrecovery (tc))
599 tc->cc_algo->recovered (tc);
600 tcp_recovery_off (tc);
602 else if (tcp_in_recovery (tc))
604 tcp_recovery_off (tc);
605 tc->cwnd = tcp_loss_wnd (tc);
610 tcp_cc_rcv_ack (tcp_connection_t * tc)
614 if (tcp_in_recovery (tc))
616 partial_ack = seq_lt (tc->snd_una, tc->snd_una_max);
619 /* Clear retransmitted bytes. */
625 /* Clear retransmitted bytes. XXX should we clear all? */
627 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
629 /* Retransmit first unacked segment */
630 tcp_retransmit_first_unacked (tc);
635 tc->cc_algo->rcv_ack (tc);
639 tc->tsecr_last_ack = tc->opt.tsecr;
643 tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
645 ASSERT (tc->snd_una == ack);
648 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
650 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
651 if (tc->opt.tsecr != tc->tsecr_last_ack)
657 tcp_fastrecovery_on (tc);
659 /* Handle congestion and dupack */
660 tcp_cc_congestion (tc);
661 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
663 tcp_fast_retransmit (tc);
665 /* Post retransmit update cwnd to ssthresh and account for the
666 * three segments that have left the network and should've been
667 * buffered at the receiver */
668 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
670 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
672 ASSERT (tcp_in_fastrecovery (tc));
674 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
679 tcp_cc_init (tcp_connection_t * tc)
681 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
682 tc->cc_algo->init (tc);
686 tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
687 tcp_header_t * th, u32 * next, u32 * error)
691 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an
692 * ACK, drop the segment, and return */
693 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
695 tcp_make_ack (tc, b);
696 *next = tcp_next_output (tc->c_is_ip4);
697 *error = TCP_ERROR_ACK_INVALID;
701 /* If old ACK, discard */
702 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
704 *error = TCP_ERROR_ACK_OLD;
708 if (tcp_opts_sack_permitted (&tc->opt))
709 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
711 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
713 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
715 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
716 *error = TCP_ERROR_ACK_DUP;
721 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
722 tc->snd_una = vnet_buffer (b)->tcp.ack_number;
724 /* Dequeue ACKed packet and update RTT */
725 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
727 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
728 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
730 /* Updates congestion control (slow start/congestion avoidance) */
733 /* If everything has been acked, stop retransmit timer
734 * otherwise update */
735 if (tc->snd_una == tc->snd_una_max)
736 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
738 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, tc->rto);
744 * Build SACK list as per RFC2018.
746 * Makes sure the first block contains the segment that generated the current
747 * ACK and the following ones are the ones most recently reported in SACK
750 * @param tc TCP connection for which the SACK list is updated
751 * @param start Start sequence number of the newest SACK block
752 * @param end End sequence of the newest SACK block
755 tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
757 sack_block_t *new_list = 0, block;
762 /* If the first segment is ooo add it to the list. Last write might've moved
763 * rcv_nxt over the first segment. */
764 if (seq_lt (tc->rcv_nxt, start))
768 vec_add1 (new_list, block);
772 /* Find the blocks still worth keeping. */
773 for (i = 0; i < vec_len (tc->snd_sacks); i++)
776 * 1) rcv_nxt advanced beyond current block OR
777 * 2) Segment overlapped by the first segment, i.e., it has been merged
779 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt)
780 || seq_leq (tc->snd_sacks[i].start, end))
783 /* Save subsequent segments to new SACK list. */
784 n_elts = clib_min (vec_len (tc->snd_sacks) - i,
785 TCP_MAX_SACK_BLOCKS - new_head);
786 vec_insert_elts (new_list, &tc->snd_sacks[i], n_elts, new_head);
790 /* Replace old vector with new one */
791 vec_free (tc->snd_sacks);
792 tc->snd_sacks = new_list;
795 /** Enqueue data for delivery to application */
797 tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
802 /* Pure ACK. Update rcv_nxt and be done. */
803 if (PREDICT_FALSE (data_len == 0))
805 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
806 return TCP_ERROR_PURE_ACK;
809 written = stream_session_enqueue_data (&tc->connection,
810 vlib_buffer_get_current (b),
811 data_len, 1 /* queue event */ );
814 if (PREDICT_TRUE (written == data_len))
816 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
818 /* If more data written than expected, account for out-of-order bytes. */
819 else if (written > data_len)
821 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
823 /* Send ACK confirming the update */
824 tc->flags |= TCP_CONN_SNDACK;
826 /* Update SACK list if need be */
827 if (tcp_opts_sack_permitted (&tc->opt))
829 /* Remove SACK blocks that have been delivered */
830 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
836 return TCP_ERROR_FIFO_FULL;
839 return TCP_ERROR_ENQUEUED;
842 /** Enqueue out-of-order data */
844 tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
847 stream_session_t *s0;
850 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
851 seq = vnet_buffer (b)->tcp.seq_number;
852 offset = seq - tc->rcv_nxt;
854 if (svm_fifo_enqueue_with_offset (s0->server_rx_fifo, s0->pid, offset,
855 data_len, vlib_buffer_get_current (b)))
856 return TCP_ERROR_FIFO_FULL;
858 /* Update SACK list if in use */
859 if (tcp_opts_sack_permitted (&tc->opt))
861 ooo_segment_t *newest;
864 /* Get the newest segment from the fifo */
865 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
866 start = tc->rcv_nxt + ooo_segment_offset (s0->server_rx_fifo, newest);
867 end = tc->rcv_nxt + ooo_segment_end_offset (s0->server_rx_fifo, newest);
869 tcp_update_sack_list (tc, start, end);
872 return TCP_ERROR_ENQUEUED;
876 * Check if ACK could be delayed. DELACK timer is set only after frame is
877 * processed so this can return true for a full bursts of packets.
880 tcp_can_delack (tcp_connection_t * tc)
882 /* If there's no DELACK timer set and the last window sent wasn't 0 we
883 * can safely delay. */
884 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK)
885 && (tc->flags & TCP_CONN_SENT_RCV_WND0) == 0
886 && (tc->flags & TCP_CONN_SNDACK) == 0)
893 tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
894 u16 n_data_bytes, u32 * next0)
898 /* Handle out-of-order data */
899 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
901 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
903 /* Don't send more than 3 dupacks per burst
904 * XXX decide if this is good */
905 if (tc->snt_dupacks < 3)
907 /* RFC2581: Send DUPACK for fast retransmit */
908 tcp_make_ack (tc, b);
909 *next0 = tcp_next_output (tc->c_is_ip4);
911 /* Mark as DUPACK. We may filter these in output if
912 * the burst fills the holes. */
913 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
921 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
922 * segments can be enqueued after fifo tail offset changes. */
923 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
925 /* Check if ACK can be delayed */
926 if (tcp_can_delack (tc))
928 /* Nothing to do for pure ACKs */
929 if (n_data_bytes == 0)
932 /* If connection has not been previously marked for delay ack
933 * add it to the list and flag it */
934 if (!tc->flags & TCP_CONN_DELACK)
936 vec_add1 (tm->delack_connections[tc->c_thread_index],
938 tc->flags |= TCP_CONN_DELACK;
943 /* Check if a packet has already been enqueued to output for burst.
944 * If yes, then drop this one, otherwise, let it pass through to
946 if ((tc->flags & TCP_CONN_BURSTACK) == 0)
948 *next0 = tcp_next_output (tc->c_is_ip4);
949 tcp_make_ack (tc, b);
950 error = TCP_ERROR_ENQUEUED;
952 /* TODO: maybe add counter to ensure N acks will be sent/burst */
953 tc->flags |= TCP_CONN_BURSTACK;
962 delack_timers_init (tcp_main_t * tm, u32 thread_index)
964 tcp_connection_t *tc;
966 tw_timer_wheel_16t_2w_512sl_t *tw;
968 tw = &tm->timer_wheels[thread_index];
969 conns = tm->delack_connections[thread_index];
970 for (i = 0; i < vec_len (conns); i++)
972 tc = pool_elt_at_index (tm->connections[thread_index], conns[i]);
975 tc->timers[TCP_TIMER_DELACK]
976 = tw_timer_start_16t_2w_512sl (tw, conns[i],
977 TCP_TIMER_DELACK, TCP_DELACK_TIME);
979 vec_reset_length (tm->delack_connections[thread_index]);
983 tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
984 vlib_frame_t * from_frame, int is_ip4)
986 u32 n_left_from, next_index, *from, *to_next;
987 u32 my_thread_index = vm->cpu_index, errors = 0;
988 tcp_main_t *tm = vnet_get_tcp_main ();
990 from = vlib_frame_vector_args (from_frame);
991 n_left_from = from_frame->n_vectors;
993 next_index = node->cached_next_index;
995 while (n_left_from > 0)
999 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1001 while (n_left_from > 0 && n_left_to_next > 0)
1005 tcp_header_t *th0 = 0;
1006 tcp_connection_t *tc0;
1009 u32 n_advance_bytes0, n_data_bytes0;
1010 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1017 n_left_to_next -= 1;
1019 b0 = vlib_get_buffer (vm, bi0);
1020 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1023 if (PREDICT_FALSE (tc0 == 0))
1025 error0 = TCP_ERROR_INVALID_CONNECTION;
1029 /* Checksum computed by ipx_local no need to compute again */
1033 ip40 = vlib_buffer_get_current (b0);
1034 th0 = ip4_next_header (ip40);
1035 n_advance_bytes0 = (ip4_header_bytes (ip40)
1036 + tcp_header_bytes (th0));
1037 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1042 ip60 = vlib_buffer_get_current (b0);
1043 th0 = ip6_next_header (ip60);
1044 n_advance_bytes0 = tcp_header_bytes (th0);
1045 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1047 n_advance_bytes0 += sizeof (ip60[0]);
1050 /* SYNs, FINs and data consume sequence numbers */
1051 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1052 + tcp_is_syn (th0) + tcp_is_fin (th0) + n_data_bytes0;
1054 /* TODO header prediction fast path */
1056 /* 1-4: check SEQ, RST, SYN */
1057 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1059 error0 = TCP_ERROR_SEGMENT_INVALID;
1063 /* 5: check the ACK field */
1064 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1069 /* 6: check the URG bit TODO */
1071 /* 7: process the segment text */
1072 vlib_buffer_advance (b0, n_advance_bytes0);
1073 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1075 /* 8: check the FIN bit */
1078 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1079 * wait for session to call close. To avoid lingering
1080 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
1081 tc0->state = TCP_STATE_CLOSE_WAIT;
1082 stream_session_disconnect_notify (&tc0->connection);
1083 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
1087 b0->error = node->errors[error0];
1088 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1093 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1094 n_left_to_next, bi0, next0);
1097 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1100 errors = session_manager_flush_enqueue_events (my_thread_index);
1104 vlib_node_increment_counter (vm, tcp4_established_node.index,
1105 TCP_ERROR_EVENT_FIFO_FULL, errors);
1107 vlib_node_increment_counter (vm, tcp6_established_node.index,
1108 TCP_ERROR_EVENT_FIFO_FULL, errors);
1111 delack_timers_init (tm, my_thread_index);
1113 return from_frame->n_vectors;
1117 tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1118 vlib_frame_t * from_frame)
1120 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1124 tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1125 vlib_frame_t * from_frame)
1127 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1131 VLIB_REGISTER_NODE (tcp4_established_node) =
1133 .function = tcp4_established,
1134 .name = "tcp4-established",
1135 /* Takes a vector of packets. */
1136 .vector_size = sizeof (u32),
1137 .n_errors = TCP_N_ERROR,.error_strings = tcp_error_strings,
1138 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1141 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1142 foreach_tcp_state_next
1148 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1151 VLIB_REGISTER_NODE (tcp6_established_node) =
1153 .function = tcp6_established,
1154 .name = "tcp6-established",
1155 /* Takes a vector of packets. */
1156 .vector_size = sizeof (u32),
1157 .n_errors = TCP_N_ERROR,
1158 .error_strings = tcp_error_strings,
1159 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1162 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1163 foreach_tcp_state_next
1170 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1172 vlib_node_registration_t tcp4_syn_sent_node;
1173 vlib_node_registration_t tcp6_syn_sent_node;
1176 tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1177 vlib_frame_t * from_frame, int is_ip4)
1179 tcp_main_t *tm = vnet_get_tcp_main ();
1180 u32 n_left_from, next_index, *from, *to_next;
1181 u32 my_thread_index = vm->cpu_index, errors = 0;
1182 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1184 from = vlib_frame_vector_args (from_frame);
1185 n_left_from = from_frame->n_vectors;
1187 next_index = node->cached_next_index;
1189 while (n_left_from > 0)
1193 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1195 while (n_left_from > 0 && n_left_to_next > 0)
1197 u32 bi0, ack0, seq0;
1199 tcp_header_t *tcp0 = 0;
1200 tcp_connection_t *tc0;
1203 u32 n_advance_bytes0, n_data_bytes0;
1204 tcp_connection_t *new_tc0;
1205 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1212 n_left_to_next -= 1;
1214 b0 = vlib_get_buffer (vm, bi0);
1216 tcp_half_open_connection_get (vnet_buffer (b0)->
1217 tcp.connection_index);
1219 ack0 = vnet_buffer (b0)->tcp.ack_number;
1220 seq0 = vnet_buffer (b0)->tcp.seq_number;
1222 /* Checksum computed by ipx_local no need to compute again */
1226 ip40 = vlib_buffer_get_current (b0);
1227 tcp0 = ip4_next_header (ip40);
1228 n_advance_bytes0 = (ip4_header_bytes (ip40)
1229 + tcp_header_bytes (tcp0));
1230 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1235 ip60 = vlib_buffer_get_current (b0);
1236 tcp0 = ip6_next_header (ip60);
1237 n_advance_bytes0 = tcp_header_bytes (tcp0);
1238 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1240 n_advance_bytes0 += sizeof (ip60[0]);
1244 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1247 /* SYNs, FINs and data consume sequence numbers */
1248 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
1249 + tcp_is_fin (tcp0) + n_data_bytes0;
1252 * 1. check the ACK bit
1256 * If the ACK bit is set
1257 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1258 * the RST bit is set, if so drop the segment and return)
1259 * <SEQ=SEG.ACK><CTL=RST>
1260 * and discard the segment. Return.
1261 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1265 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1267 if (!tcp_rst (tcp0))
1268 tcp_send_reset (b0, is_ip4);
1273 /* Make sure ACK is valid */
1274 if (tc0->snd_una > ack0)
1279 * 2. check the RST bit
1284 /* If ACK is acceptable, signal client that peer is not
1285 * willing to accept connection and drop connection*/
1288 stream_session_connect_notify (&tc0->connection, sst,
1290 tcp_connection_cleanup (tc0);
1296 * 3. check the security and precedence (skipped)
1300 * 4. check the SYN bit
1303 /* No SYN flag. Drop. */
1304 if (!tcp_syn (tcp0))
1307 /* Stop connection establishment and retransmit timers */
1308 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1309 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1311 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1312 * current thread pool. */
1313 pool_get (tm->connections[my_thread_index], new_tc0);
1314 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1316 new_tc0->c_thread_index = my_thread_index;
1318 /* Cleanup half-open connection XXX lock */
1319 pool_put (tm->half_open_connections, tc0);
1321 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1322 new_tc0->irs = seq0;
1325 tcp_options_parse (tcp0, &new_tc0->opt);
1327 if (tcp_opts_tstamp (&new_tc0->opt))
1329 new_tc0->tsval_recent = new_tc0->opt.tsval;
1330 new_tc0->tsval_recent_age = tcp_time_now ();
1333 if (tcp_opts_wscale (&new_tc0->opt))
1334 new_tc0->snd_wscale = new_tc0->opt.wscale;
1337 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
1338 new_tc0->snd_wl1 = seq0;
1339 new_tc0->snd_wl2 = ack0;
1341 tcp_connection_init_vars (new_tc0);
1343 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1346 /* Our SYN is ACKed: we have iss < ack = snd_una */
1348 /* TODO Dequeue acknowledged segments if we support Fast Open */
1349 new_tc0->snd_una = ack0;
1350 new_tc0->state = TCP_STATE_ESTABLISHED;
1352 /* Make sure las is initialized for the wnd computation */
1353 new_tc0->rcv_las = new_tc0->rcv_nxt;
1355 /* Notify app that we have connection */
1356 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1358 /* Make sure after data segment processing ACK is sent */
1359 new_tc0->flags |= TCP_CONN_SNDACK;
1361 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1364 new_tc0->state = TCP_STATE_SYN_RCVD;
1366 /* Notify app that we have connection XXX */
1367 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1369 tcp_make_synack (new_tc0, b0);
1370 next0 = tcp_next_output (is_ip4);
1375 /* Read data, if any */
1379 tcp_segment_rcv (tm, new_tc0, b0, n_data_bytes0, &next0);
1380 if (error0 == TCP_ERROR_PURE_ACK)
1381 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1385 tcp_make_ack (new_tc0, b0);
1386 next0 = tcp_next_output (new_tc0->c_is_ip4);
1391 b0->error = error0 ? node->errors[error0] : 0;
1392 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1397 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1398 n_left_to_next, bi0, next0);
1401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1404 errors = session_manager_flush_enqueue_events (my_thread_index);
1408 vlib_node_increment_counter (vm, tcp4_established_node.index,
1409 TCP_ERROR_EVENT_FIFO_FULL, errors);
1411 vlib_node_increment_counter (vm, tcp6_established_node.index,
1412 TCP_ERROR_EVENT_FIFO_FULL, errors);
1415 return from_frame->n_vectors;
1419 tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1420 vlib_frame_t * from_frame)
1422 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1426 tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1427 vlib_frame_t * from_frame)
1429 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1433 VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1435 .function = tcp4_syn_sent,
1436 .name = "tcp4-syn-sent",
1437 /* Takes a vector of packets. */
1438 .vector_size = sizeof (u32),
1439 .n_errors = TCP_N_ERROR,
1440 .error_strings = tcp_error_strings,
1441 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1444 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1445 foreach_tcp_state_next
1451 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1454 VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1456 .function = tcp6_syn_sent_rcv,
1457 .name = "tcp6-syn-sent",
1458 /* Takes a vector of packets. */
1459 .vector_size = sizeof (u32),
1460 .n_errors = TCP_N_ERROR,
1461 .error_strings = tcp_error_strings,
1462 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1465 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1466 foreach_tcp_state_next
1472 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1474 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
1475 * as per RFC793 p. 64
1478 tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1479 vlib_frame_t * from_frame, int is_ip4)
1481 tcp_main_t *tm = vnet_get_tcp_main ();
1482 u32 n_left_from, next_index, *from, *to_next;
1483 u32 my_thread_index = vm->cpu_index, errors = 0;
1485 from = vlib_frame_vector_args (from_frame);
1486 n_left_from = from_frame->n_vectors;
1488 next_index = node->cached_next_index;
1490 while (n_left_from > 0)
1494 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1496 while (n_left_from > 0 && n_left_to_next > 0)
1500 tcp_header_t *tcp0 = 0;
1501 tcp_connection_t *tc0;
1504 u32 n_advance_bytes0, n_data_bytes0;
1505 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1512 n_left_to_next -= 1;
1514 b0 = vlib_get_buffer (vm, bi0);
1515 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1517 if (PREDICT_FALSE (tc0 == 0))
1519 error0 = TCP_ERROR_INVALID_CONNECTION;
1523 /* Checksum computed by ipx_local no need to compute again */
1527 ip40 = vlib_buffer_get_current (b0);
1528 tcp0 = ip4_next_header (ip40);
1529 n_advance_bytes0 = (ip4_header_bytes (ip40)
1530 + tcp_header_bytes (tcp0));
1531 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1536 ip60 = vlib_buffer_get_current (b0);
1537 tcp0 = ip6_next_header (ip60);
1538 n_advance_bytes0 = tcp_header_bytes (tcp0);
1539 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1541 n_advance_bytes0 += sizeof (ip60[0]);
1544 /* SYNs, FINs and data consume sequence numbers */
1545 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1546 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0) + n_data_bytes0;
1549 * Special treatment for CLOSED
1553 case TCP_STATE_CLOSED:
1559 * For all other states (except LISTEN)
1562 /* 1-4: check SEQ, RST, SYN */
1564 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1566 error0 = TCP_ERROR_SEGMENT_INVALID;
1570 /* 5: check the ACK field */
1573 case TCP_STATE_SYN_RCVD:
1575 * If the segment acknowledgment is not acceptable, form a
1577 * <SEQ=SEG.ACK><CTL=RST>
1580 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1582 tcp_send_reset (b0, is_ip4);
1585 /* Switch state to ESTABLISHED */
1586 tc0->state = TCP_STATE_ESTABLISHED;
1588 /* Initialize session variables */
1589 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
1590 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
1592 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1593 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1595 /* Shoulder tap the server */
1596 stream_session_accept_notify (&tc0->connection);
1598 /* Reset SYN-ACK retransmit timer */
1599 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT);
1601 case TCP_STATE_ESTABLISHED:
1602 /* We can get packets in established state here because they
1603 * were enqueued before state change */
1604 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1608 case TCP_STATE_FIN_WAIT_1:
1609 /* In addition to the processing for the ESTABLISHED state, if
1610 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1611 * continue processing in that state. */
1612 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1615 /* If FIN is ACKed */
1616 if (tc0->snd_una == tc0->snd_una_max)
1618 tc0->state = TCP_STATE_FIN_WAIT_2;
1619 /* Stop all timers, 2MSL will be set lower */
1620 tcp_connection_timers_reset (tc0);
1623 case TCP_STATE_FIN_WAIT_2:
1624 /* In addition to the processing for the ESTABLISHED state, if
1625 * the retransmission queue is empty, the user's CLOSE can be
1626 * acknowledged ("ok") but do not delete the TCB. */
1627 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1629 /* check if rtx queue is empty and ack CLOSE TODO */
1631 case TCP_STATE_CLOSE_WAIT:
1632 /* Do the same processing as for the ESTABLISHED state. */
1633 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1636 case TCP_STATE_CLOSING:
1637 /* In addition to the processing for the ESTABLISHED state, if
1638 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1639 * otherwise ignore the segment. */
1640 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1643 /* XXX test that send queue empty */
1644 tc0->state = TCP_STATE_TIME_WAIT;
1648 case TCP_STATE_LAST_ACK:
1649 /* The only thing that can arrive in this state is an
1650 * acknowledgment of our FIN. If our FIN is now acknowledged,
1651 * delete the TCB, enter the CLOSED state, and return. */
1653 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1656 tc0->state = TCP_STATE_CLOSED;
1658 /* Don't delete the connection/session yet. Instead, wait a
1659 * reasonable amount of time until the pipes are cleared. In
1660 * particular, this makes sure that we won't have dead sessions
1661 * when processing events on the tx path */
1662 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1664 /* Stop retransmit */
1665 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT);
1670 case TCP_STATE_TIME_WAIT:
1671 /* The only thing that can arrive in this state is a
1672 * retransmission of the remote FIN. Acknowledge it, and restart
1673 * the 2 MSL timeout. */
1682 /* 6: check the URG bit TODO */
1684 /* 7: process the segment text */
1687 case TCP_STATE_ESTABLISHED:
1688 case TCP_STATE_FIN_WAIT_1:
1689 case TCP_STATE_FIN_WAIT_2:
1690 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1692 case TCP_STATE_CLOSE_WAIT:
1693 case TCP_STATE_CLOSING:
1694 case TCP_STATE_LAST_ACK:
1695 case TCP_STATE_TIME_WAIT:
1696 /* This should not occur, since a FIN has been received from the
1697 * remote side. Ignore the segment text. */
1701 /* 8: check the FIN bit */
1702 if (!tcp_fin (tcp0))
1707 case TCP_STATE_ESTABLISHED:
1708 case TCP_STATE_SYN_RCVD:
1709 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1710 tcp_connection_timers_reset (tc0);
1711 tcp_make_fin (tc0, b0);
1712 next0 = tcp_next_output (tc0->c_is_ip4);
1713 stream_session_disconnect_notify (&tc0->connection);
1714 tc0->state = TCP_STATE_CLOSE_WAIT;
1716 case TCP_STATE_CLOSE_WAIT:
1717 case TCP_STATE_CLOSING:
1718 case TCP_STATE_LAST_ACK:
1721 case TCP_STATE_FIN_WAIT_1:
1722 tc0->state = TCP_STATE_TIME_WAIT;
1723 tcp_connection_timers_reset (tc0);
1724 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1726 case TCP_STATE_FIN_WAIT_2:
1727 /* Got FIN, send ACK! */
1728 tc0->state = TCP_STATE_TIME_WAIT;
1729 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1730 tcp_make_ack (tc0, b0);
1731 next0 = tcp_next_output (is_ip4);
1733 case TCP_STATE_TIME_WAIT:
1734 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1737 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1741 b0->error = error0 ? node->errors[error0] : 0;
1744 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1749 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1750 n_left_to_next, bi0, next0);
1753 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1756 errors = session_manager_flush_enqueue_events (my_thread_index);
1760 vlib_node_increment_counter (vm, tcp4_established_node.index,
1761 TCP_ERROR_EVENT_FIFO_FULL, errors);
1763 vlib_node_increment_counter (vm, tcp6_established_node.index,
1764 TCP_ERROR_EVENT_FIFO_FULL, errors);
1767 return from_frame->n_vectors;
1771 tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1772 vlib_frame_t * from_frame)
1774 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1778 tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1779 vlib_frame_t * from_frame)
1781 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1785 VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
1787 .function = tcp4_rcv_process,
1788 .name = "tcp4-rcv-process",
1789 /* Takes a vector of packets. */
1790 .vector_size = sizeof (u32),
1791 .n_errors = TCP_N_ERROR,
1792 .error_strings = tcp_error_strings,
1793 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1796 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1797 foreach_tcp_state_next
1803 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
1806 VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
1808 .function = tcp6_rcv_process,
1809 .name = "tcp6-rcv-process",
1810 /* Takes a vector of packets. */
1811 .vector_size = sizeof (u32),
1812 .n_errors = TCP_N_ERROR,
1813 .error_strings = tcp_error_strings,
1814 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1817 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1818 foreach_tcp_state_next
1824 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
1826 vlib_node_registration_t tcp4_listen_node;
1827 vlib_node_registration_t tcp6_listen_node;
1830 * LISTEN state processing as per RFC 793 p. 65
1833 tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1834 vlib_frame_t * from_frame, int is_ip4)
1836 u32 n_left_from, next_index, *from, *to_next;
1837 u32 my_thread_index = vm->cpu_index;
1838 tcp_main_t *tm = vnet_get_tcp_main ();
1839 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1841 from = vlib_frame_vector_args (from_frame);
1842 n_left_from = from_frame->n_vectors;
1844 next_index = node->cached_next_index;
1846 while (n_left_from > 0)
1850 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1852 while (n_left_from > 0 && n_left_to_next > 0)
1856 tcp_header_t *th0 = 0;
1857 tcp_connection_t *lc0;
1860 tcp_connection_t *child0;
1861 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
1868 n_left_to_next -= 1;
1870 b0 = vlib_get_buffer (vm, bi0);
1871 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
1875 ip40 = vlib_buffer_get_current (b0);
1876 th0 = ip4_next_header (ip40);
1880 ip60 = vlib_buffer_get_current (b0);
1881 th0 = ip6_next_header (ip60);
1884 /* Create child session. For syn-flood protection use filter */
1886 /* 1. first check for an RST */
1890 /* 2. second check for an ACK */
1893 tcp_send_reset (b0, is_ip4);
1897 /* 3. check for a SYN (did that already) */
1899 /* Create child session and send SYN-ACK */
1900 pool_get (tm->connections[my_thread_index], child0);
1901 memset (child0, 0, sizeof (*child0));
1903 child0->c_c_index = child0 - tm->connections[my_thread_index];
1904 child0->c_lcl_port = lc0->c_lcl_port;
1905 child0->c_rmt_port = th0->src_port;
1906 child0->c_is_ip4 = is_ip4;
1907 child0->c_thread_index = my_thread_index;
1911 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
1912 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
1916 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
1917 sizeof (ip6_address_t));
1918 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
1919 sizeof (ip6_address_t));
1922 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
1925 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
1929 tcp_options_parse (th0, &child0->opt);
1931 child0->irs = vnet_buffer (b0)->tcp.seq_number;
1932 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
1933 child0->state = TCP_STATE_SYN_RCVD;
1935 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
1936 * segments are used to initialize PAWS. */
1937 if (tcp_opts_tstamp (&child0->opt))
1939 child0->tsval_recent = child0->opt.tsval;
1940 child0->tsval_recent_age = tcp_time_now ();
1943 if (tcp_opts_wscale (&child0->opt))
1944 child0->snd_wscale = child0->opt.wscale;
1947 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
1948 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1949 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1951 tcp_connection_init_vars (child0);
1953 /* Reuse buffer to make syn-ack and send */
1954 tcp_make_synack (child0, b0);
1955 next0 = tcp_next_output (is_ip4);
1958 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1963 b0->error = node->errors[error0];
1965 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1966 n_left_to_next, bi0, next0);
1969 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1971 return from_frame->n_vectors;
1975 tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
1976 vlib_frame_t * from_frame)
1978 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1982 tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
1983 vlib_frame_t * from_frame)
1985 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1989 VLIB_REGISTER_NODE (tcp4_listen_node) =
1991 .function = tcp4_listen,
1992 .name = "tcp4-listen",
1993 /* Takes a vector of packets. */
1994 .vector_size = sizeof (u32),
1995 .n_errors = TCP_N_ERROR,
1996 .error_strings = tcp_error_strings,
1997 .n_next_nodes = TCP_LISTEN_N_NEXT,
2000 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2001 foreach_tcp_state_next
2007 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2010 VLIB_REGISTER_NODE (tcp6_listen_node) =
2012 .function = tcp6_listen,
2013 .name = "tcp6-listen",
2014 /* Takes a vector of packets. */
2015 .vector_size = sizeof (u32),
2016 .n_errors = TCP_N_ERROR,
2017 .error_strings = tcp_error_strings,
2018 .n_next_nodes = TCP_LISTEN_N_NEXT,
2021 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2022 foreach_tcp_state_next
2028 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2030 vlib_node_registration_t tcp4_input_node;
2031 vlib_node_registration_t tcp6_input_node;
2033 typedef enum _tcp_input_next
2035 TCP_INPUT_NEXT_DROP,
2036 TCP_INPUT_NEXT_LISTEN,
2037 TCP_INPUT_NEXT_RCV_PROCESS,
2038 TCP_INPUT_NEXT_SYN_SENT,
2039 TCP_INPUT_NEXT_ESTABLISHED,
2040 TCP_INPUT_NEXT_RESET,
2044 #define foreach_tcp4_input_next \
2045 _ (DROP, "error-drop") \
2046 _ (LISTEN, "tcp4-listen") \
2047 _ (RCV_PROCESS, "tcp4-rcv-process") \
2048 _ (SYN_SENT, "tcp4-syn-sent") \
2049 _ (ESTABLISHED, "tcp4-established") \
2050 _ (RESET, "tcp4-reset")
2052 #define foreach_tcp6_input_next \
2053 _ (DROP, "error-drop") \
2054 _ (LISTEN, "tcp6-listen") \
2055 _ (RCV_PROCESS, "tcp6-rcv-process") \
2056 _ (SYN_SENT, "tcp6-syn-sent") \
2057 _ (ESTABLISHED, "tcp6-established") \
2058 _ (RESET, "tcp6-reset")
2067 const char *tcp_fsm_states[] = {
2068 #define _(sym, str) str,
2069 foreach_tcp_fsm_state
2074 format_tcp_state (u8 * s, va_list * args)
2076 tcp_state_t *state = va_arg (*args, tcp_state_t *);
2078 if (state[0] < TCP_N_STATES)
2079 s = format (s, "%s", tcp_fsm_states[state[0]]);
2081 s = format (s, "UNKNOWN");
2087 format_tcp_rx_trace (u8 * s, va_list * args)
2089 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2090 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2091 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
2093 s = format (s, "TCP: src-port %d dst-port %U%s\n",
2094 clib_net_to_host_u16 (t->src_port),
2095 clib_net_to_host_u16 (t->dst_port), format_tcp_state, t->state);
2100 #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2103 tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2104 vlib_frame_t * from_frame, int is_ip4)
2106 u32 n_left_from, next_index, *from, *to_next;
2107 u32 my_thread_index = vm->cpu_index;
2108 tcp_main_t *tm = vnet_get_tcp_main ();
2110 from = vlib_frame_vector_args (from_frame);
2111 n_left_from = from_frame->n_vectors;
2113 next_index = node->cached_next_index;
2115 while (n_left_from > 0)
2119 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2121 while (n_left_from > 0 && n_left_to_next > 0)
2125 tcp_header_t *tcp0 = 0;
2126 tcp_connection_t *tc0;
2129 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2137 n_left_to_next -= 1;
2139 b0 = vlib_get_buffer (vm, bi0);
2140 vnet_buffer (b0)->tcp.flags = 0;
2144 ip40 = vlib_buffer_get_current (b0);
2145 tcp0 = ip4_next_header (ip40);
2147 /* lookup session */
2149 (tcp_connection_t *)
2150 stream_session_lookup_transport4 (&ip40->dst_address,
2154 SESSION_TYPE_IP4_TCP,
2159 ip60 = vlib_buffer_get_current (b0);
2160 tcp0 = ip6_next_header (ip60);
2162 (tcp_connection_t *)
2163 stream_session_lookup_transport6 (&ip60->src_address,
2167 SESSION_TYPE_IP6_TCP,
2171 /* Session exists */
2172 if (PREDICT_TRUE (0 != tc0))
2174 /* Save connection index */
2175 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2176 vnet_buffer (b0)->tcp.seq_number =
2177 clib_net_to_host_u32 (tcp0->seq_number);
2178 vnet_buffer (b0)->tcp.ack_number =
2179 clib_net_to_host_u32 (tcp0->ack_number);
2181 flags0 = tcp0->flags & filter_flags;
2182 next0 = tm->dispatch_table[tc0->state][flags0].next;
2183 error0 = tm->dispatch_table[tc0->state][flags0].error;
2185 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH))
2187 /* Overload tcp flags to store state */
2188 vnet_buffer (b0)->tcp.flags = tc0->state;
2194 next0 = TCP_INPUT_NEXT_RESET;
2195 error0 = TCP_ERROR_NO_LISTENER;
2198 b0->error = error0 ? node->errors[error0] : 0;
2200 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2205 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2206 n_left_to_next, bi0, next0);
2209 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2212 return from_frame->n_vectors;
2216 tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2217 vlib_frame_t * from_frame)
2219 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2223 tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2224 vlib_frame_t * from_frame)
2226 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2230 VLIB_REGISTER_NODE (tcp4_input_node) =
2232 .function = tcp4_input,
2233 .name = "tcp4-input",
2234 /* Takes a vector of packets. */
2235 .vector_size = sizeof (u32),
2236 .n_errors = TCP_N_ERROR,
2237 .error_strings = tcp_error_strings,
2238 .n_next_nodes = TCP_INPUT_N_NEXT,
2241 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2242 foreach_tcp4_input_next
2245 .format_buffer = format_tcp_header,
2246 .format_trace = format_tcp_rx_trace,
2250 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2253 VLIB_REGISTER_NODE (tcp6_input_node) =
2255 .function = tcp6_input,
2256 .name = "tcp6-input",
2257 /* Takes a vector of packets. */
2258 .vector_size = sizeof (u32),
2259 .n_errors = TCP_N_ERROR,
2260 .error_strings = tcp_error_strings,
2261 .n_next_nodes = TCP_INPUT_N_NEXT,
2264 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2265 foreach_tcp6_input_next
2268 .format_buffer = format_tcp_header,
2269 .format_trace = format_tcp_rx_trace,
2273 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
2275 tcp_update_time (f64 now, u32 thread_index)
2277 tcp_main_t *tm = vnet_get_tcp_main ();
2278 tw_timer_expire_timers_16t_2w_512sl (&tm->timer_wheels[thread_index], now);
2282 tcp_dispatch_table_init (tcp_main_t * tm)
2285 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2286 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2288 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2289 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2292 #define _(t,f,n,e) \
2294 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2295 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2298 /* SYNs for new connections -> tcp-listen. */
2299 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
2300 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2301 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2302 /* SYN-ACK for a SYN */
2303 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2305 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2306 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2307 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2309 /* ACK for for established connection -> tcp-established. */
2310 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2311 /* FIN for for established connection -> tcp-established. */
2312 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2313 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2315 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2316 /* ACK or FIN-ACK to our FIN */
2317 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2318 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2320 /* FIN in reply to our FIN from the other side */
2321 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2322 /* FIN confirming that the peer (app) has closed */
2323 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2324 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2326 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2331 tcp_input_init (vlib_main_t * vm)
2333 clib_error_t *error = 0;
2334 tcp_main_t *tm = vnet_get_tcp_main ();
2336 if ((error = vlib_call_init_function (vm, tcp_init)))
2339 /* Initialize dispatch table. */
2340 tcp_dispatch_table_init (tm);
2345 VLIB_INIT_FUNCTION (tcp_input_init);
2348 * fd.io coding-style-patch-verification: ON
2351 * eval: (c-set-style "gnu")