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.
99 * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las)
100 * however, is too strict when we have retransmits. Instead we just check that
101 * the seq is not beyond the right edge and that the end of the segment is not
102 * less than the left edge.
104 * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so
105 * use rcv_nxt in the right edge window test instead of rcv_las.
109 tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
111 return (seq_geq (end_seq, tc->rcv_las)
112 && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd));
116 tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
119 u8 opt_len, opts_len, kind;
123 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
124 data = (const u8 *) (th + 1);
126 /* Zero out all flags but those set in SYN */
127 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
129 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
133 /* Get options length */
134 if (kind == TCP_OPTION_EOL)
136 else if (kind == TCP_OPTION_NOOP)
145 /* weird option length */
146 if (opt_len < 2 || opt_len > opts_len)
154 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
156 to->flags |= TCP_OPTS_FLAG_MSS;
157 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
160 case TCP_OPTION_WINDOW_SCALE:
161 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
163 to->flags |= TCP_OPTS_FLAG_WSCALE;
164 to->wscale = data[2];
165 if (to->wscale > TCP_MAX_WND_SCALE)
167 clib_warning ("Illegal window scaling value: %d",
169 to->wscale = TCP_MAX_WND_SCALE;
173 case TCP_OPTION_TIMESTAMP:
174 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
176 to->flags |= TCP_OPTS_FLAG_TSTAMP;
177 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
178 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
181 case TCP_OPTION_SACK_PERMITTED:
182 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
183 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
185 case TCP_OPTION_SACK_BLOCK:
186 /* If SACK permitted was not advertised or a SYN, break */
187 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
190 /* If too short or not correctly formatted, break */
191 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
194 to->flags |= TCP_OPTS_FLAG_SACK;
195 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
196 vec_reset_length (to->sacks);
197 for (j = 0; j < to->n_sack_blocks; j++)
199 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
200 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
201 vec_add1 (to->sacks, b);
205 /* Nothing to see here */
212 tcp_segment_check_paws (tcp_connection_t * tc)
214 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
215 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
219 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
221 * It first verifies if segment has a wrapped sequence number (PAWS) and then
222 * does the processing associated to the first four steps (ignoring security
223 * and precedence): sequence number, rst bit and syn bit checks.
225 * @return 0 if segments passes validation.
228 tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
229 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
233 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
236 tcp_options_parse (th0, &tc0->opt);
238 /* RFC1323: Check against wrapped sequence numbers (PAWS). If we have
239 * timestamp to echo and it's less than tsval_recent, drop segment
240 * but still send an ACK in order to retain TCP's mechanism for detecting
241 * and recovering from half-open connections */
242 paws_failed = tcp_segment_check_paws (tc0);
245 clib_warning ("paws failed");
247 /* If it just so happens that a segment updates tsval_recent for a
248 * segment over 24 days old, invalidate tsval_recent. */
249 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
252 /* Age isn't reset until we get a valid tsval (bsd inspired) */
253 tc0->tsval_recent = 0;
257 /* Drop after ack if not rst */
260 tcp_make_ack (tc0, b0);
261 *next0 = tcp_next_output (tc0->c_is_ip4);
262 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
268 /* 1st: check sequence number */
269 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
270 vnet_buffer (b0)->tcp.seq_end))
272 /* If our window is 0 and the packet is in sequence, let it pass
273 * through for ack processing. It should be dropped later.*/
274 if (tc0->rcv_wnd == 0
275 && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number)
277 /* TODO Should segment be tagged? */
281 /* If not RST, send dup ack */
284 tcp_make_ack (tc0, b0);
285 *next0 = tcp_next_output (tc0->c_is_ip4);
286 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
292 /* 2nd: check the RST bit */
295 tcp_connection_reset (tc0);
299 /* 3rd: check security and precedence (skip) */
301 /* 4th: check the SYN bit */
304 tcp_send_reset (b0, tc0->c_is_ip4);
308 /* If PAWS passed and segment in window, save timestamp */
311 tc0->tsval_recent = tc0->opt.tsval;
312 tc0->tsval_recent_age = tcp_time_now ();
319 tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
321 /* SND.UNA =< SEG.ACK =< SND.NXT */
322 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
323 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
327 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
329 * Note that although the original article, srtt and rttvar are scaled
330 * to minimize round-off errors, here we don't. Instead, we rely on
331 * better precision time measurements.
333 * TODO support us rtt resolution
336 tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
342 err = mrtt - tc->srtt;
343 tc->srtt += err >> 3;
345 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
346 * The increase should be bound */
347 tc->rttvar += ((int) clib_abs (err) - (int) tc->rttvar) >> 2;
351 /* First measurement. */
353 tc->rttvar = mrtt >> 1;
357 /** Update RTT estimate and RTO timer
359 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
360 * timing. Middle boxes are known to fiddle with TCP options so we
361 * should give higher priority to ACK timing.
363 * return 1 if valid rtt 0 otherwise
366 tcp_update_rtt (tcp_connection_t * tc, u32 ack)
370 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
371 * RTT because they're ambiguous. */
372 if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff)
374 mrtt = tcp_time_now () - tc->rtt_ts;
377 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
378 * snd_una, i.e., the left side of the send window:
379 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
380 * try to update rtt for dupacks */
381 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
383 mrtt = tcp_time_now () - tc->opt.tsecr;
386 /* Ignore dubious measurements */
387 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
390 tcp_estimate_rtt (tc, mrtt);
392 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
394 /* Allow measuring of RTT and make sure boff is 0 */
402 * Dequeue bytes that have been acked and while at it update RTT estimates.
405 tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
407 /* Dequeue the newly ACKed bytes */
408 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
410 /* Update rtt and rto */
411 tcp_update_rtt (tc, ack);
415 * Check if dupack as per RFC5681 Sec. 2
417 * This works only if called before updating snd_wnd.
420 tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
422 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
423 && seq_gt (tc->snd_una_max, tc->snd_una)
424 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
425 && (new_snd_wnd == tc->snd_wnd));
429 scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
431 sack_scoreboard_hole_t *next, *prev;
433 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
435 next = pool_elt_at_index (sb->holes, hole->next);
436 next->prev = hole->prev;
439 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
441 prev = pool_elt_at_index (sb->holes, hole->prev);
442 prev->next = hole->next;
446 sb->head = hole->next;
449 pool_put (sb->holes, hole);
452 sack_scoreboard_hole_t *
453 scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
456 sack_scoreboard_hole_t *hole, *next, *prev;
459 pool_get (sb->holes, hole);
460 memset (hole, 0, sizeof (*hole));
464 hole_index = hole - sb->holes;
466 prev = scoreboard_get_hole (sb, prev_index);
469 hole->prev = prev - sb->holes;
470 hole->next = prev->next;
472 if ((next = scoreboard_next_hole (sb, hole)))
473 next->prev = hole_index;
475 prev->next = hole_index;
479 sb->head = hole_index;
480 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
481 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
488 tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
490 sack_scoreboard_t *sb = &tc->sack_sb;
491 sack_block_t *blk, tmp;
492 sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole;
493 u32 blk_index = 0, old_sacked_bytes, hole_index;
496 sb->last_sacked_bytes = 0;
498 old_sacked_bytes = sb->sacked_bytes;
500 if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
503 /* Remove invalid blocks */
505 while (blk < vec_end (tc->opt.sacks))
507 if (seq_lt (blk->start, blk->end)
508 && seq_gt (blk->start, tc->snd_una)
509 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
514 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
517 /* Add block for cumulative ack */
518 if (seq_gt (ack, tc->snd_una))
520 tmp.start = tc->snd_una;
522 vec_add1 (tc->opt.sacks, tmp);
525 if (vec_len (tc->opt.sacks) == 0)
528 /* Make sure blocks are ordered */
529 for (i = 0; i < vec_len (tc->opt.sacks); i++)
530 for (j = i + 1; j < vec_len (tc->opt.sacks); j++)
531 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
533 tmp = tc->opt.sacks[i];
534 tc->opt.sacks[i] = tc->opt.sacks[j];
535 tc->opt.sacks[j] = tmp;
538 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
540 /* If no holes, insert the first that covers all outstanding bytes */
541 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
542 tc->snd_una, tc->snd_una_max);
543 sb->tail = scoreboard_hole_index (sb, last_hole);
547 /* If we have holes but snd_una_max is beyond the last hole, update
549 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
550 last_hole = scoreboard_last_hole (sb);
551 if (seq_gt (tc->snd_una_max, sb->max_byte_sacked)
552 && seq_gt (tc->snd_una_max, last_hole->end))
553 last_hole->end = tc->snd_una_max;
556 /* Walk the holes with the SACK blocks */
557 hole = pool_elt_at_index (sb->holes, sb->head);
558 while (hole && blk_index < vec_len (tc->opt.sacks))
560 blk = &tc->opt.sacks[blk_index];
562 if (seq_leq (blk->start, hole->start))
564 /* Block covers hole. Remove hole */
565 if (seq_geq (blk->end, hole->end))
567 next_hole = scoreboard_next_hole (sb, hole);
569 /* Byte accounting */
570 if (seq_leq (hole->end, ack))
572 /* Bytes lost because snd_wnd left edge advances */
573 if (next_hole && seq_leq (next_hole->start, ack))
574 sb->sacked_bytes -= next_hole->start - hole->end;
576 sb->sacked_bytes -= ack - hole->end;
580 sb->sacked_bytes += scoreboard_hole_bytes (hole);
583 /* snd_una needs to be advanced */
584 if (seq_geq (ack, hole->end))
586 if (next_hole && seq_lt (ack, next_hole->start))
587 sb->snd_una_adv = next_hole->start - ack;
589 sb->snd_una_adv = sb->max_byte_sacked - ack;
591 /* all these can be delivered */
592 sb->sacked_bytes -= sb->snd_una_adv;
595 /* About to remove last hole */
596 if (hole == last_hole)
598 sb->tail = hole->prev;
599 last_hole = scoreboard_last_hole (sb);
600 /* keep track of max byte sacked in case the last hole
602 if (seq_gt (hole->end, sb->max_byte_sacked))
603 sb->max_byte_sacked = hole->end;
605 scoreboard_remove_hole (sb, hole);
608 /* Partial 'head' overlap */
611 if (seq_gt (blk->end, hole->start))
613 sb->sacked_bytes += blk->end - hole->start;
614 hole->start = blk->end;
621 /* Hole must be split */
622 if (seq_lt (blk->end, hole->end))
624 sb->sacked_bytes += blk->end - blk->start;
625 hole_index = scoreboard_hole_index (sb, hole);
626 new_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
629 /* Pool might've moved */
630 hole = scoreboard_get_hole (sb, hole_index);
631 hole->end = blk->start;
633 /* New or split of tail */
634 if ((last_hole->end == new_hole->end)
635 || seq_lt (last_hole->end, new_hole->start))
637 last_hole = new_hole;
638 sb->tail = scoreboard_hole_index (sb, new_hole);
642 hole = scoreboard_next_hole (sb, hole);
646 sb->sacked_bytes += hole->end - blk->start;
647 hole->end = blk->start;
648 hole = scoreboard_next_hole (sb, hole);
653 sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv
659 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
660 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
662 tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
664 if (seq_lt (tc->snd_wl1, seq)
665 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
667 tc->snd_wnd = snd_wnd;
670 TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
672 /* Set probe timer if we just got 0 wnd */
673 if (tc->snd_wnd < tc->snd_mss
674 && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
675 tcp_persist_timer_set (tc);
677 tcp_persist_timer_reset (tc);
682 tcp_cc_congestion (tcp_connection_t * tc)
684 tc->snd_congestion = tc->snd_nxt;
685 tc->cc_algo->congestion (tc);
686 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
690 tcp_cc_recover (tcp_connection_t * tc)
692 /* TODO: check if time to recover was small. It might be that RTO popped
696 tc->cc_algo->recovered (tc);
700 tc->snd_nxt = tc->snd_una;
702 tc->cc_algo->rcv_ack (tc);
703 tc->tsecr_last_ack = tc->opt.tsecr;
705 tcp_cong_recovery_off (tc);
707 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
711 tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
715 if (tcp_in_cong_recovery (tc))
717 partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
720 /* Clear retransmitted bytes. */
725 TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
727 /* Clear retransmitted bytes. XXX should we clear all? */
729 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
731 /* In case snd_nxt is still in the past and output tries to
732 * shove some new bytes */
733 tc->snd_nxt = tc->snd_una_max;
735 /* XXX need proper RFC6675 support */
736 if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc))
738 tcp_fast_retransmit (tc);
742 /* Retransmit first unacked segment */
743 tcp_retransmit_first_unacked (tc);
749 tc->cc_algo->rcv_ack (tc);
750 tc->tsecr_last_ack = tc->opt.tsecr;
756 tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
758 // ASSERT (seq_geq(tc->snd_una, ack));
761 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
763 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
764 if (tc->opt.tsecr != tc->tsecr_last_ack)
770 tcp_fastrecovery_on (tc);
772 /* Handle congestion and dupack */
773 tcp_cc_congestion (tc);
774 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
776 tcp_fast_retransmit (tc);
778 /* Post retransmit update cwnd to ssthresh and account for the
779 * three segments that have left the network and should've been
780 * buffered at the receiver */
781 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
783 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
785 ASSERT (tcp_in_fastrecovery (tc));
787 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
792 tcp_cc_init (tcp_connection_t * tc)
794 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
795 tc->cc_algo->init (tc);
799 tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
800 tcp_header_t * th, u32 * next, u32 * error)
804 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
805 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
807 /* If we have outstanding data and this is within the window, accept it,
808 * probably retransmit has timed out. Otherwise ACK segment and then
810 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
812 tcp_make_ack (tc, b);
813 *next = tcp_next_output (tc->c_is_ip4);
814 *error = TCP_ERROR_ACK_INVALID;
815 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
816 vnet_buffer (b)->tcp.ack_number);
820 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
821 vnet_buffer (b)->tcp.ack_number);
823 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
824 *error = TCP_ERROR_ACK_FUTURE;
827 /* If old ACK, probably it's an old dupack */
828 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
830 *error = TCP_ERROR_ACK_OLD;
831 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
832 vnet_buffer (b)->tcp.ack_number);
833 if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
835 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
836 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
841 if (tcp_opts_sack_permitted (&tc->opt))
842 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
844 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
846 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
848 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
849 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
850 *error = TCP_ERROR_ACK_DUP;
858 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
859 tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
861 /* Dequeue ACKed data and update RTT */
862 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
863 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
864 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
866 /* If some of our sent bytes have been acked, update cc and retransmit
870 TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
872 /* Updates congestion control (slow start/congestion avoidance) */
873 tcp_cc_rcv_ack (tc, b);
875 /* If everything has been acked, stop retransmit timer
876 * otherwise update */
877 if (tc->snd_una == tc->snd_una_max)
878 tcp_retransmit_timer_reset (tc);
880 tcp_retransmit_timer_update (tc);
887 * Build SACK list as per RFC2018.
889 * Makes sure the first block contains the segment that generated the current
890 * ACK and the following ones are the ones most recently reported in SACK
893 * @param tc TCP connection for which the SACK list is updated
894 * @param start Start sequence number of the newest SACK block
895 * @param end End sequence of the newest SACK block
898 tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
900 sack_block_t *new_list = 0, *block = 0;
903 /* If the first segment is ooo add it to the list. Last write might've moved
904 * rcv_nxt over the first segment. */
905 if (seq_lt (tc->rcv_nxt, start))
907 vec_add2 (new_list, block, 1);
908 block->start = start;
912 /* Find the blocks still worth keeping. */
913 for (i = 0; i < vec_len (tc->snd_sacks); i++)
915 /* Discard if rcv_nxt advanced beyond current block */
916 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
919 /* Merge or drop if segment overlapped by the new segment */
920 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
921 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
923 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
924 new_list[0].start = tc->snd_sacks[i].start;
925 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
926 new_list[0].end = tc->snd_sacks[i].end;
930 /* Save to new SACK list if we have space. */
931 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
933 vec_add1 (new_list, tc->snd_sacks[i]);
937 clib_warning ("dropped sack blocks");
941 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
943 /* Replace old vector with new one */
944 vec_free (tc->snd_sacks);
945 tc->snd_sacks = new_list;
948 /** Enqueue data for delivery to application */
950 tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
955 /* Pure ACK. Update rcv_nxt and be done. */
956 if (PREDICT_FALSE (data_len == 0))
958 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
959 return TCP_ERROR_PURE_ACK;
962 written = stream_session_enqueue_data (&tc->connection,
963 vlib_buffer_get_current (b),
964 data_len, 1 /* queue event */ );
966 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
969 if (PREDICT_TRUE (written == data_len))
971 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
973 /* If more data written than expected, account for out-of-order bytes. */
974 else if (written > data_len)
976 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
978 /* Send ACK confirming the update */
979 tc->flags |= TCP_CONN_SNDACK;
981 else if (written > 0)
983 /* We've written something but FIFO is probably full now */
984 tc->rcv_nxt += written;
986 /* Depending on how fast the app is, all remaining buffers in burst will
987 * not be enqueued. Inform peer */
988 tc->flags |= TCP_CONN_SNDACK;
990 return TCP_ERROR_PARTIALLY_ENQUEUED;
994 tc->flags |= TCP_CONN_SNDACK;
995 return TCP_ERROR_FIFO_FULL;
998 /* Update SACK list if need be */
999 if (tcp_opts_sack_permitted (&tc->opt))
1001 /* Remove SACK blocks that have been delivered */
1002 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1005 return TCP_ERROR_ENQUEUED;
1008 /** Enqueue out-of-order data */
1010 tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
1013 stream_session_t *s0;
1017 /* Pure ACK. Do nothing */
1018 if (PREDICT_FALSE (data_len == 0))
1020 return TCP_ERROR_PURE_ACK;
1023 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
1024 offset = vnet_buffer (b)->tcp.seq_number - tc->irs;
1026 clib_warning ("ooo: offset %d len %d", offset, data_len);
1028 rv = svm_fifo_enqueue_with_offset (s0->server_rx_fifo, offset, data_len,
1029 vlib_buffer_get_current (b));
1031 /* Nothing written */
1034 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1035 return TCP_ERROR_FIFO_FULL;
1038 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
1040 /* Update SACK list if in use */
1041 if (tcp_opts_sack_permitted (&tc->opt))
1043 ooo_segment_t *newest;
1046 /* Get the newest segment from the fifo */
1047 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
1048 start = ooo_segment_offset (s0->server_rx_fifo, newest);
1049 end = ooo_segment_end_offset (s0->server_rx_fifo, newest);
1051 tcp_update_sack_list (tc, start, end);
1054 return TCP_ERROR_ENQUEUED;
1058 * Check if ACK could be delayed. If ack can be delayed, it should return
1059 * true for a full frame. If we're always acking return 0.
1062 tcp_can_delack (tcp_connection_t * tc)
1064 /* Send ack if ... */
1066 /* just sent a rcv wnd 0 */
1067 || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1068 /* constrained to send ack */
1069 || (tc->flags & TCP_CONN_SNDACK) != 0
1070 /* we're almost out of tx wnd */
1071 || tcp_available_snd_space (tc) < 2 * tc->snd_mss)
1078 tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
1079 u16 n_data_bytes, u32 * next0)
1083 /* Handle out-of-order data */
1084 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1086 /* Old sequence numbers allowed through because they overlapped
1089 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
1091 error = TCP_ERROR_SEGMENT_OLD;
1092 *next0 = TCP_NEXT_DROP;
1096 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1098 /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open
1099 * cwnd on remote peer when congested 2) acks leaving should have the
1100 * latest rcv_wnd since the burst may eaten up all of it, so only the
1101 * old ones could be filtered.
1104 /* RFC2581: Send DUPACK for fast retransmit */
1105 tcp_make_ack (tc, b);
1106 *next0 = tcp_next_output (tc->c_is_ip4);
1108 /* Mark as DUPACK. We may filter these in output if
1109 * the burst fills the holes. */
1111 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
1113 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
1117 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1118 * segments can be enqueued after fifo tail offset changes. */
1119 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1121 if (n_data_bytes == 0)
1123 *next0 = TCP_NEXT_DROP;
1127 /* Check if ACK can be delayed */
1128 if (tcp_can_delack (tc))
1130 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1131 tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
1135 *next0 = tcp_next_output (tc->c_is_ip4);
1136 tcp_make_ack (tc, b);
1144 tcp_header_t tcp_header;
1145 tcp_connection_t tcp_connection;
1149 format_tcp_rx_trace (u8 * s, va_list * args)
1151 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1152 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1153 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1154 uword indent = format_get_indent (s);
1156 s = format (s, "%U\n%U%U",
1157 format_tcp_header, &t->tcp_header, 128,
1158 format_white_space, indent,
1159 format_tcp_connection_verbose, &t->tcp_connection);
1165 format_tcp_rx_trace_short (u8 * s, va_list * args)
1167 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1168 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1169 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1171 s = format (s, "%d -> %d (%U)",
1172 clib_net_to_host_u16 (t->tcp_header.src_port),
1173 clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state,
1174 &t->tcp_connection.state);
1180 tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0,
1181 tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
1185 clib_memcpy (&t0->tcp_connection, tc0, sizeof (t0->tcp_connection));
1189 th0 = tcp_buffer_hdr (b0);
1191 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1195 tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
1197 if (PREDICT_TRUE (!val))
1201 vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val);
1203 vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val);
1207 tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1208 vlib_frame_t * from_frame, int is_ip4)
1210 u32 n_left_from, next_index, *from, *to_next;
1211 u32 my_thread_index = vm->thread_index, errors = 0;
1212 tcp_main_t *tm = vnet_get_tcp_main ();
1215 from = vlib_frame_vector_args (from_frame);
1216 n_left_from = from_frame->n_vectors;
1218 next_index = node->cached_next_index;
1220 while (n_left_from > 0)
1224 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1226 while (n_left_from > 0 && n_left_to_next > 0)
1230 tcp_header_t *th0 = 0;
1231 tcp_connection_t *tc0;
1232 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1239 n_left_to_next -= 1;
1241 b0 = vlib_get_buffer (vm, bi0);
1242 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1245 if (PREDICT_FALSE (tc0 == 0))
1247 error0 = TCP_ERROR_INVALID_CONNECTION;
1251 th0 = tcp_buffer_hdr (b0);
1253 is_fin = (th0->flags & TCP_FLAG_FIN) != 0;
1255 /* SYNs, FINs and data consume sequence numbers */
1256 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1257 + tcp_is_syn (th0) + is_fin + vnet_buffer (b0)->tcp.data_len;
1259 /* TODO header prediction fast path */
1261 /* 1-4: check SEQ, RST, SYN */
1262 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1264 error0 = TCP_ERROR_SEGMENT_INVALID;
1265 TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
1266 vnet_buffer (b0)->tcp.seq_number,
1267 vnet_buffer (b0)->tcp.seq_end);
1271 /* 5: check the ACK field */
1272 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1277 /* 6: check the URG bit TODO */
1279 /* 7: process the segment text */
1281 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1282 error0 = tcp_segment_rcv (tm, tc0, b0,
1283 vnet_buffer (b0)->tcp.data_len, &next0);
1285 /* N.B. buffer is rewritten if segment is ooo. Thus, th0 becomes a
1286 * dangling reference. */
1288 /* 8: check the FIN bit */
1291 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1292 * wait for session to call close. To avoid lingering
1293 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
1294 tc0->state = TCP_STATE_CLOSE_WAIT;
1295 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
1296 stream_session_disconnect_notify (&tc0->connection);
1297 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
1301 b0->error = node->errors[error0];
1302 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1304 tcp_rx_trace_t *t0 =
1305 vlib_add_trace (vm, node, b0, sizeof (*t0));
1306 tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
1309 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1310 n_left_to_next, bi0, next0);
1313 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1316 errors = session_manager_flush_enqueue_events (my_thread_index);
1317 tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors);
1319 return from_frame->n_vectors;
1323 tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1324 vlib_frame_t * from_frame)
1326 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1330 tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1331 vlib_frame_t * from_frame)
1333 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1337 VLIB_REGISTER_NODE (tcp4_established_node) =
1339 .function = tcp4_established,
1340 .name = "tcp4-established",
1341 /* Takes a vector of packets. */
1342 .vector_size = sizeof (u32),
1343 .n_errors = TCP_N_ERROR,
1344 .error_strings = tcp_error_strings,
1345 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1348 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1349 foreach_tcp_state_next
1352 .format_trace = format_tcp_rx_trace_short,
1356 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1359 VLIB_REGISTER_NODE (tcp6_established_node) =
1361 .function = tcp6_established,
1362 .name = "tcp6-established",
1363 /* Takes a vector of packets. */
1364 .vector_size = sizeof (u32),
1365 .n_errors = TCP_N_ERROR,
1366 .error_strings = tcp_error_strings,
1367 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1370 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1371 foreach_tcp_state_next
1374 .format_trace = format_tcp_rx_trace_short,
1379 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1381 vlib_node_registration_t tcp4_syn_sent_node;
1382 vlib_node_registration_t tcp6_syn_sent_node;
1385 tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1386 vlib_frame_t * from_frame, int is_ip4)
1388 tcp_main_t *tm = vnet_get_tcp_main ();
1389 u32 n_left_from, next_index, *from, *to_next;
1390 u32 my_thread_index = vm->thread_index, errors = 0;
1391 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1393 from = vlib_frame_vector_args (from_frame);
1394 n_left_from = from_frame->n_vectors;
1396 next_index = node->cached_next_index;
1398 while (n_left_from > 0)
1402 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1404 while (n_left_from > 0 && n_left_to_next > 0)
1406 u32 bi0, ack0, seq0;
1409 tcp_header_t *tcp0 = 0;
1410 tcp_connection_t *tc0;
1411 tcp_connection_t *new_tc0;
1412 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1419 n_left_to_next -= 1;
1421 b0 = vlib_get_buffer (vm, bi0);
1423 tcp_half_open_connection_get (vnet_buffer (b0)->
1424 tcp.connection_index);
1426 ack0 = vnet_buffer (b0)->tcp.ack_number;
1427 seq0 = vnet_buffer (b0)->tcp.seq_number;
1428 tcp0 = tcp_buffer_hdr (b0);
1431 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1434 /* SYNs, FINs and data consume sequence numbers */
1435 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
1436 + tcp_is_fin (tcp0) + vnet_buffer (b0)->tcp.data_len;
1439 * 1. check the ACK bit
1443 * If the ACK bit is set
1444 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1445 * the RST bit is set, if so drop the segment and return)
1446 * <SEQ=SEG.ACK><CTL=RST>
1447 * and discard the segment. Return.
1448 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1452 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1454 if (!tcp_rst (tcp0))
1455 tcp_send_reset (b0, is_ip4);
1460 /* Make sure ACK is valid */
1461 if (tc0->snd_una > ack0)
1466 * 2. check the RST bit
1471 /* If ACK is acceptable, signal client that peer is not
1472 * willing to accept connection and drop connection*/
1475 stream_session_connect_notify (&tc0->connection, sst,
1477 tcp_connection_cleanup (tc0);
1483 * 3. check the security and precedence (skipped)
1487 * 4. check the SYN bit
1490 /* No SYN flag. Drop. */
1491 if (!tcp_syn (tcp0))
1494 /* Stop connection establishment and retransmit timers */
1495 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1496 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1498 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1499 * current thread pool. */
1500 pool_get (tm->connections[my_thread_index], new_tc0);
1501 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1503 new_tc0->c_thread_index = my_thread_index;
1505 /* Cleanup half-open connection XXX lock */
1506 pool_put (tm->half_open_connections, tc0);
1508 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1509 new_tc0->irs = seq0;
1512 tcp_options_parse (tcp0, &new_tc0->opt);
1514 if (tcp_opts_tstamp (&new_tc0->opt))
1516 new_tc0->tsval_recent = new_tc0->opt.tsval;
1517 new_tc0->tsval_recent_age = tcp_time_now ();
1520 if (tcp_opts_wscale (&new_tc0->opt))
1521 new_tc0->snd_wscale = new_tc0->opt.wscale;
1524 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
1525 new_tc0->snd_wl1 = seq0;
1526 new_tc0->snd_wl2 = ack0;
1528 tcp_connection_init_vars (new_tc0);
1530 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1533 /* Our SYN is ACKed: we have iss < ack = snd_una */
1535 /* TODO Dequeue acknowledged segments if we support Fast Open */
1536 new_tc0->snd_una = ack0;
1537 new_tc0->state = TCP_STATE_ESTABLISHED;
1539 /* Make sure las is initialized for the wnd computation */
1540 new_tc0->rcv_las = new_tc0->rcv_nxt;
1542 /* Notify app that we have connection */
1543 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1545 /* Make sure after data segment processing ACK is sent */
1546 new_tc0->flags |= TCP_CONN_SNDACK;
1548 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1551 new_tc0->state = TCP_STATE_SYN_RCVD;
1553 /* Notify app that we have connection */
1554 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1556 tcp_make_synack (new_tc0, b0);
1557 next0 = tcp_next_output (is_ip4);
1562 /* Read data, if any */
1563 if (vnet_buffer (b0)->tcp.data_len)
1565 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1566 error0 = tcp_segment_rcv (tm, new_tc0, b0,
1567 vnet_buffer (b0)->tcp.data_len,
1569 if (error0 == TCP_ERROR_PURE_ACK)
1570 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1574 tcp_make_ack (new_tc0, b0);
1575 next0 = tcp_next_output (new_tc0->c_is_ip4);
1580 b0->error = error0 ? node->errors[error0] : 0;
1581 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1583 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1584 clib_memcpy (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
1585 clib_memcpy (&t0->tcp_connection, tc0,
1586 sizeof (t0->tcp_connection));
1589 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1590 n_left_to_next, bi0, next0);
1593 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1596 errors = session_manager_flush_enqueue_events (my_thread_index);
1600 vlib_node_increment_counter (vm, tcp4_established_node.index,
1601 TCP_ERROR_EVENT_FIFO_FULL, errors);
1603 vlib_node_increment_counter (vm, tcp6_established_node.index,
1604 TCP_ERROR_EVENT_FIFO_FULL, errors);
1607 return from_frame->n_vectors;
1611 tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1612 vlib_frame_t * from_frame)
1614 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1618 tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1619 vlib_frame_t * from_frame)
1621 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1625 VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1627 .function = tcp4_syn_sent,
1628 .name = "tcp4-syn-sent",
1629 /* Takes a vector of packets. */
1630 .vector_size = sizeof (u32),
1631 .n_errors = TCP_N_ERROR,
1632 .error_strings = tcp_error_strings,
1633 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1636 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1637 foreach_tcp_state_next
1640 .format_trace = format_tcp_rx_trace_short,
1644 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1647 VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1649 .function = tcp6_syn_sent_rcv,
1650 .name = "tcp6-syn-sent",
1651 /* Takes a vector of packets. */
1652 .vector_size = sizeof (u32),
1653 .n_errors = TCP_N_ERROR,
1654 .error_strings = tcp_error_strings,
1655 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1658 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1659 foreach_tcp_state_next
1662 .format_trace = format_tcp_rx_trace_short,
1666 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1668 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
1669 * as per RFC793 p. 64
1672 tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1673 vlib_frame_t * from_frame, int is_ip4)
1675 tcp_main_t *tm = vnet_get_tcp_main ();
1676 u32 n_left_from, next_index, *from, *to_next;
1677 u32 my_thread_index = vm->thread_index, errors = 0;
1679 from = vlib_frame_vector_args (from_frame);
1680 n_left_from = from_frame->n_vectors;
1682 next_index = node->cached_next_index;
1684 while (n_left_from > 0)
1688 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1690 while (n_left_from > 0 && n_left_to_next > 0)
1694 tcp_header_t *tcp0 = 0;
1695 tcp_connection_t *tc0;
1696 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1703 n_left_to_next -= 1;
1705 b0 = vlib_get_buffer (vm, bi0);
1706 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1708 if (PREDICT_FALSE (tc0 == 0))
1710 error0 = TCP_ERROR_INVALID_CONNECTION;
1714 tcp0 = tcp_buffer_hdr (b0);
1716 /* SYNs, FINs and data consume sequence numbers */
1717 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1718 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0)
1719 + vnet_buffer (b0)->tcp.data_len;
1722 * Special treatment for CLOSED
1726 case TCP_STATE_CLOSED:
1732 * For all other states (except LISTEN)
1735 /* 1-4: check SEQ, RST, SYN */
1737 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1739 error0 = TCP_ERROR_SEGMENT_INVALID;
1743 /* 5: check the ACK field */
1746 case TCP_STATE_SYN_RCVD:
1748 * If the segment acknowledgment is not acceptable, form a
1750 * <SEQ=SEG.ACK><CTL=RST>
1753 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1755 tcp_send_reset (b0, is_ip4);
1758 /* Switch state to ESTABLISHED */
1759 tc0->state = TCP_STATE_ESTABLISHED;
1761 /* Initialize session variables */
1762 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
1763 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
1765 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1766 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1768 /* Shoulder tap the server */
1769 stream_session_accept_notify (&tc0->connection);
1771 /* Reset SYN-ACK retransmit timer */
1772 tcp_retransmit_timer_reset (tc0);
1774 case TCP_STATE_ESTABLISHED:
1775 /* We can get packets in established state here because they
1776 * were enqueued before state change */
1777 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1781 case TCP_STATE_FIN_WAIT_1:
1782 /* In addition to the processing for the ESTABLISHED state, if
1783 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1784 * continue processing in that state. */
1785 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1788 /* If FIN is ACKed */
1789 if (tc0->snd_una == tc0->snd_una_max)
1791 tc0->state = TCP_STATE_FIN_WAIT_2;
1792 /* Stop all timers, 2MSL will be set lower */
1793 tcp_connection_timers_reset (tc0);
1796 case TCP_STATE_FIN_WAIT_2:
1797 /* In addition to the processing for the ESTABLISHED state, if
1798 * the retransmission queue is empty, the user's CLOSE can be
1799 * acknowledged ("ok") but do not delete the TCB. */
1800 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1802 /* check if rtx queue is empty and ack CLOSE TODO */
1804 case TCP_STATE_CLOSE_WAIT:
1805 /* Do the same processing as for the ESTABLISHED state. */
1806 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1809 case TCP_STATE_CLOSING:
1810 /* In addition to the processing for the ESTABLISHED state, if
1811 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1812 * otherwise ignore the segment. */
1813 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1816 /* XXX test that send queue empty */
1817 tc0->state = TCP_STATE_TIME_WAIT;
1821 case TCP_STATE_LAST_ACK:
1822 /* The only thing that can arrive in this state is an
1823 * acknowledgment of our FIN. If our FIN is now acknowledged,
1824 * delete the TCB, enter the CLOSED state, and return. */
1826 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1829 tc0->state = TCP_STATE_CLOSED;
1831 /* Don't delete the connection/session yet. Instead, wait a
1832 * reasonable amount of time until the pipes are cleared. In
1833 * particular, this makes sure that we won't have dead sessions
1834 * when processing events on the tx path */
1835 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1837 /* Stop retransmit */
1838 tcp_retransmit_timer_reset (tc0);
1843 case TCP_STATE_TIME_WAIT:
1844 /* The only thing that can arrive in this state is a
1845 * retransmission of the remote FIN. Acknowledge it, and restart
1846 * the 2 MSL timeout. */
1855 /* 6: check the URG bit TODO */
1857 /* 7: process the segment text */
1860 case TCP_STATE_ESTABLISHED:
1861 case TCP_STATE_FIN_WAIT_1:
1862 case TCP_STATE_FIN_WAIT_2:
1863 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1864 error0 = tcp_segment_rcv (tm, tc0, b0,
1865 vnet_buffer (b0)->tcp.data_len,
1868 case TCP_STATE_CLOSE_WAIT:
1869 case TCP_STATE_CLOSING:
1870 case TCP_STATE_LAST_ACK:
1871 case TCP_STATE_TIME_WAIT:
1872 /* This should not occur, since a FIN has been received from the
1873 * remote side. Ignore the segment text. */
1877 /* 8: check the FIN bit */
1878 if (!tcp_fin (tcp0))
1883 case TCP_STATE_ESTABLISHED:
1884 case TCP_STATE_SYN_RCVD:
1885 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1886 tcp_connection_timers_reset (tc0);
1887 tcp_make_fin (tc0, b0);
1888 next0 = tcp_next_output (tc0->c_is_ip4);
1889 stream_session_disconnect_notify (&tc0->connection);
1890 tc0->state = TCP_STATE_CLOSE_WAIT;
1892 case TCP_STATE_CLOSE_WAIT:
1893 case TCP_STATE_CLOSING:
1894 case TCP_STATE_LAST_ACK:
1897 case TCP_STATE_FIN_WAIT_1:
1898 tc0->state = TCP_STATE_TIME_WAIT;
1899 tcp_connection_timers_reset (tc0);
1900 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1902 case TCP_STATE_FIN_WAIT_2:
1903 /* Got FIN, send ACK! */
1904 tc0->state = TCP_STATE_TIME_WAIT;
1905 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
1906 tcp_make_ack (tc0, b0);
1907 next0 = tcp_next_output (is_ip4);
1909 case TCP_STATE_TIME_WAIT:
1910 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1913 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1916 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
1919 b0->error = error0 ? node->errors[error0] : 0;
1921 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1923 tcp_rx_trace_t *t0 =
1924 vlib_add_trace (vm, node, b0, sizeof (*t0));
1925 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
1928 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1929 n_left_to_next, bi0, next0);
1932 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1935 errors = session_manager_flush_enqueue_events (my_thread_index);
1939 vlib_node_increment_counter (vm, tcp4_established_node.index,
1940 TCP_ERROR_EVENT_FIFO_FULL, errors);
1942 vlib_node_increment_counter (vm, tcp6_established_node.index,
1943 TCP_ERROR_EVENT_FIFO_FULL, errors);
1946 return from_frame->n_vectors;
1950 tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1951 vlib_frame_t * from_frame)
1953 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1957 tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1958 vlib_frame_t * from_frame)
1960 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1964 VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
1966 .function = tcp4_rcv_process,
1967 .name = "tcp4-rcv-process",
1968 /* Takes a vector of packets. */
1969 .vector_size = sizeof (u32),
1970 .n_errors = TCP_N_ERROR,
1971 .error_strings = tcp_error_strings,
1972 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1975 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1976 foreach_tcp_state_next
1979 .format_trace = format_tcp_rx_trace_short,
1983 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
1986 VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
1988 .function = tcp6_rcv_process,
1989 .name = "tcp6-rcv-process",
1990 /* Takes a vector of packets. */
1991 .vector_size = sizeof (u32),
1992 .n_errors = TCP_N_ERROR,
1993 .error_strings = tcp_error_strings,
1994 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1997 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1998 foreach_tcp_state_next
2001 .format_trace = format_tcp_rx_trace_short,
2005 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
2007 vlib_node_registration_t tcp4_listen_node;
2008 vlib_node_registration_t tcp6_listen_node;
2011 * LISTEN state processing as per RFC 793 p. 65
2014 tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2015 vlib_frame_t * from_frame, int is_ip4)
2017 u32 n_left_from, next_index, *from, *to_next;
2018 u32 my_thread_index = vm->thread_index;
2019 tcp_main_t *tm = vnet_get_tcp_main ();
2020 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
2022 from = vlib_frame_vector_args (from_frame);
2023 n_left_from = from_frame->n_vectors;
2025 next_index = node->cached_next_index;
2027 while (n_left_from > 0)
2031 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2033 while (n_left_from > 0 && n_left_to_next > 0)
2038 tcp_header_t *th0 = 0;
2039 tcp_connection_t *lc0;
2042 tcp_connection_t *child0;
2043 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
2050 n_left_to_next -= 1;
2052 b0 = vlib_get_buffer (vm, bi0);
2053 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
2057 ip40 = vlib_buffer_get_current (b0);
2058 th0 = ip4_next_header (ip40);
2062 ip60 = vlib_buffer_get_current (b0);
2063 th0 = ip6_next_header (ip60);
2066 /* Create child session. For syn-flood protection use filter */
2068 /* 1. first check for an RST */
2072 /* 2. second check for an ACK */
2075 tcp_send_reset (b0, is_ip4);
2079 /* 3. check for a SYN (did that already) */
2081 /* Create child session and send SYN-ACK */
2082 pool_get (tm->connections[my_thread_index], child0);
2083 memset (child0, 0, sizeof (*child0));
2085 child0->c_c_index = child0 - tm->connections[my_thread_index];
2086 child0->c_lcl_port = lc0->c_lcl_port;
2087 child0->c_rmt_port = th0->src_port;
2088 child0->c_is_ip4 = is_ip4;
2089 child0->c_thread_index = my_thread_index;
2093 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
2094 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
2098 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
2099 sizeof (ip6_address_t));
2100 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
2101 sizeof (ip6_address_t));
2104 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
2107 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
2111 tcp_options_parse (th0, &child0->opt);
2113 child0->irs = vnet_buffer (b0)->tcp.seq_number;
2114 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
2115 child0->rcv_las = child0->rcv_nxt;
2116 child0->state = TCP_STATE_SYN_RCVD;
2118 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
2119 * segments are used to initialize PAWS. */
2120 if (tcp_opts_tstamp (&child0->opt))
2122 child0->tsval_recent = child0->opt.tsval;
2123 child0->tsval_recent_age = tcp_time_now ();
2126 if (tcp_opts_wscale (&child0->opt))
2127 child0->snd_wscale = child0->opt.wscale;
2130 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
2131 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2132 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2134 tcp_connection_init_vars (child0);
2136 TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0);
2138 /* Reuse buffer to make syn-ack and send */
2139 tcp_make_synack (child0, b0);
2140 next0 = tcp_next_output (is_ip4);
2143 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2145 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2146 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2147 clib_memcpy (&t0->tcp_connection, lc0,
2148 sizeof (t0->tcp_connection));
2151 b0->error = node->errors[error0];
2153 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2154 n_left_to_next, bi0, next0);
2157 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2159 return from_frame->n_vectors;
2163 tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2164 vlib_frame_t * from_frame)
2166 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2170 tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2171 vlib_frame_t * from_frame)
2173 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2177 VLIB_REGISTER_NODE (tcp4_listen_node) =
2179 .function = tcp4_listen,
2180 .name = "tcp4-listen",
2181 /* Takes a vector of packets. */
2182 .vector_size = sizeof (u32),
2183 .n_errors = TCP_N_ERROR,
2184 .error_strings = tcp_error_strings,
2185 .n_next_nodes = TCP_LISTEN_N_NEXT,
2188 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2189 foreach_tcp_state_next
2192 .format_trace = format_tcp_rx_trace_short,
2196 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2199 VLIB_REGISTER_NODE (tcp6_listen_node) =
2201 .function = tcp6_listen,
2202 .name = "tcp6-listen",
2203 /* Takes a vector of packets. */
2204 .vector_size = sizeof (u32),
2205 .n_errors = TCP_N_ERROR,
2206 .error_strings = tcp_error_strings,
2207 .n_next_nodes = TCP_LISTEN_N_NEXT,
2210 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2211 foreach_tcp_state_next
2214 .format_trace = format_tcp_rx_trace_short,
2218 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2220 vlib_node_registration_t tcp4_input_node;
2221 vlib_node_registration_t tcp6_input_node;
2223 typedef enum _tcp_input_next
2225 TCP_INPUT_NEXT_DROP,
2226 TCP_INPUT_NEXT_LISTEN,
2227 TCP_INPUT_NEXT_RCV_PROCESS,
2228 TCP_INPUT_NEXT_SYN_SENT,
2229 TCP_INPUT_NEXT_ESTABLISHED,
2230 TCP_INPUT_NEXT_RESET,
2234 #define foreach_tcp4_input_next \
2235 _ (DROP, "error-drop") \
2236 _ (LISTEN, "tcp4-listen") \
2237 _ (RCV_PROCESS, "tcp4-rcv-process") \
2238 _ (SYN_SENT, "tcp4-syn-sent") \
2239 _ (ESTABLISHED, "tcp4-established") \
2240 _ (RESET, "tcp4-reset")
2242 #define foreach_tcp6_input_next \
2243 _ (DROP, "error-drop") \
2244 _ (LISTEN, "tcp6-listen") \
2245 _ (RCV_PROCESS, "tcp6-rcv-process") \
2246 _ (SYN_SENT, "tcp6-syn-sent") \
2247 _ (ESTABLISHED, "tcp6-established") \
2248 _ (RESET, "tcp6-reset")
2250 #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2253 tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2254 vlib_frame_t * from_frame, int is_ip4)
2256 u32 n_left_from, next_index, *from, *to_next;
2257 u32 my_thread_index = vm->thread_index;
2258 tcp_main_t *tm = vnet_get_tcp_main ();
2260 from = vlib_frame_vector_args (from_frame);
2261 n_left_from = from_frame->n_vectors;
2263 next_index = node->cached_next_index;
2265 while (n_left_from > 0)
2269 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2271 while (n_left_from > 0 && n_left_to_next > 0)
2273 int n_advance_bytes0, n_data_bytes0;
2276 tcp_header_t *tcp0 = 0;
2277 tcp_connection_t *tc0;
2280 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2288 n_left_to_next -= 1;
2290 b0 = vlib_get_buffer (vm, bi0);
2291 vnet_buffer (b0)->tcp.flags = 0;
2293 /* Checksum computed by ipx_local no need to compute again */
2297 ip40 = vlib_buffer_get_current (b0);
2298 tcp0 = ip4_next_header (ip40);
2299 n_advance_bytes0 = (ip4_header_bytes (ip40)
2300 + tcp_header_bytes (tcp0));
2301 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
2304 /* lookup session */
2306 (tcp_connection_t *)
2307 stream_session_lookup_transport4 (&ip40->dst_address,
2311 SESSION_TYPE_IP4_TCP,
2316 ip60 = vlib_buffer_get_current (b0);
2317 tcp0 = ip6_next_header (ip60);
2318 n_advance_bytes0 = tcp_header_bytes (tcp0);
2319 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
2321 n_advance_bytes0 += sizeof (ip60[0]);
2324 (tcp_connection_t *)
2325 stream_session_lookup_transport6 (&ip60->src_address,
2329 SESSION_TYPE_IP6_TCP,
2334 if (PREDICT_FALSE (n_advance_bytes0 < 0))
2336 error0 = TCP_ERROR_LENGTH;
2340 /* Session exists */
2341 if (PREDICT_TRUE (0 != tc0))
2343 /* Save connection index */
2344 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2345 vnet_buffer (b0)->tcp.seq_number =
2346 clib_net_to_host_u32 (tcp0->seq_number);
2347 vnet_buffer (b0)->tcp.ack_number =
2348 clib_net_to_host_u32 (tcp0->ack_number);
2350 vnet_buffer (b0)->tcp.hdr_offset = (u8 *) tcp0
2351 - (u8 *) vlib_buffer_get_current (b0);
2352 vnet_buffer (b0)->tcp.data_offset = n_advance_bytes0;
2353 vnet_buffer (b0)->tcp.data_len = n_data_bytes0;
2355 flags0 = tcp0->flags & filter_flags;
2356 next0 = tm->dispatch_table[tc0->state][flags0].next;
2357 error0 = tm->dispatch_table[tc0->state][flags0].error;
2359 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH))
2361 tcp_state_t state0 = tc0->state;
2362 /* Overload tcp flags to store state */
2363 vnet_buffer (b0)->tcp.flags = tc0->state;
2364 clib_warning ("disp error state %U flags %U",
2365 format_tcp_state, &state0,
2366 format_tcp_flags, (int) flags0);
2372 next0 = TCP_INPUT_NEXT_RESET;
2373 error0 = TCP_ERROR_NO_LISTENER;
2377 b0->error = error0 ? node->errors[error0] : 0;
2379 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2381 tcp_rx_trace_t *t0 =
2382 vlib_add_trace (vm, node, b0, sizeof (*t0));
2383 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
2386 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2387 n_left_to_next, bi0, next0);
2390 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2393 return from_frame->n_vectors;
2397 tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2398 vlib_frame_t * from_frame)
2400 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2404 tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2405 vlib_frame_t * from_frame)
2407 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2411 VLIB_REGISTER_NODE (tcp4_input_node) =
2413 .function = tcp4_input,
2414 .name = "tcp4-input",
2415 /* Takes a vector of packets. */
2416 .vector_size = sizeof (u32),
2417 .n_errors = TCP_N_ERROR,
2418 .error_strings = tcp_error_strings,
2419 .n_next_nodes = TCP_INPUT_N_NEXT,
2422 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2423 foreach_tcp4_input_next
2426 .format_buffer = format_tcp_header,
2427 .format_trace = format_tcp_rx_trace,
2431 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2434 VLIB_REGISTER_NODE (tcp6_input_node) =
2436 .function = tcp6_input,
2437 .name = "tcp6-input",
2438 /* Takes a vector of packets. */
2439 .vector_size = sizeof (u32),
2440 .n_errors = TCP_N_ERROR,
2441 .error_strings = tcp_error_strings,
2442 .n_next_nodes = TCP_INPUT_N_NEXT,
2445 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2446 foreach_tcp6_input_next
2449 .format_buffer = format_tcp_header,
2450 .format_trace = format_tcp_rx_trace,
2454 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
2457 tcp_dispatch_table_init (tcp_main_t * tm)
2460 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2461 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2463 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2464 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2467 #define _(t,f,n,e) \
2469 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2470 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2473 /* SYNs for new connections -> tcp-listen. */
2474 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
2475 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2476 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2477 /* SYN-ACK for a SYN */
2478 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2480 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2481 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2482 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2484 /* ACK for for established connection -> tcp-established. */
2485 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2486 /* FIN for for established connection -> tcp-established. */
2487 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2488 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2490 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2491 /* ACK or FIN-ACK to our FIN */
2492 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2493 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2495 /* FIN in reply to our FIN from the other side */
2496 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2497 /* FIN confirming that the peer (app) has closed */
2498 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2499 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2501 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2506 tcp_input_init (vlib_main_t * vm)
2508 clib_error_t *error = 0;
2509 tcp_main_t *tm = vnet_get_tcp_main ();
2511 if ((error = vlib_call_init_function (vm, tcp_init)))
2514 /* Initialize dispatch table. */
2515 tcp_dispatch_table_init (tm);
2520 VLIB_INIT_FUNCTION (tcp_input_init);
2523 * fd.io coding-style-patch-verification: ON
2526 * eval: (c-set-style "gnu")