FD.io VPP  v19.01.1-17-ge106252
Vector Packet Processing
tcp_input.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
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>
20 #include <math.h>
21 
22 static char *tcp_error_strings[] = {
23 #define tcp_error(n,s) s,
24 #include <vnet/tcp/tcp_error.def>
25 #undef tcp_error
26 };
27 
28 /* All TCP nodes have the same outgoing arcs */
29 #define foreach_tcp_state_next \
30  _ (DROP4, "ip4-drop") \
31  _ (DROP6, "ip6-drop") \
32  _ (TCP4_OUTPUT, "tcp4-output") \
33  _ (TCP6_OUTPUT, "tcp6-output")
34 
35 typedef enum _tcp_established_next
36 {
37 #define _(s,n) TCP_ESTABLISHED_NEXT_##s,
39 #undef _
42 
43 typedef enum _tcp_rcv_process_next
44 {
45 #define _(s,n) TCP_RCV_PROCESS_NEXT_##s,
47 #undef _
50 
51 typedef enum _tcp_syn_sent_next
52 {
53 #define _(s,n) TCP_SYN_SENT_NEXT_##s,
55 #undef _
58 
59 typedef enum _tcp_listen_next
60 {
61 #define _(s,n) TCP_LISTEN_NEXT_##s,
63 #undef _
66 
67 /* Generic, state independent indices */
68 typedef enum _tcp_state_next
69 {
70 #define _(s,n) TCP_NEXT_##s,
72 #undef _
75 
76 #define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \
77  : TCP_NEXT_TCP6_OUTPUT)
78 
79 #define tcp_next_drop(is_ip4) (is_ip4 ? TCP_NEXT_DROP4 \
80  : TCP_NEXT_DROP6)
81 
84 
85 /**
86  * Validate segment sequence number. As per RFC793:
87  *
88  * Segment Receive Test
89  * Length Window
90  * ------- ------- -------------------------------------------
91  * 0 0 SEG.SEQ = RCV.NXT
92  * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
93  * >0 0 not acceptable
94  * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
95  * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
96  *
97  * This ultimately consists in checking if segment falls within the window.
98  * The one important difference compared to RFC793 is that we use rcv_las,
99  * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the
100  * peer's reference when computing our receive window.
101  *
102  * This:
103  * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las)
104  * however, is too strict when we have retransmits. Instead we just check that
105  * the seq is not beyond the right edge and that the end of the segment is not
106  * less than the left edge.
107  *
108  * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so
109  * use rcv_nxt in the right edge window test instead of rcv_las.
110  *
111  */
114 {
115  return (seq_geq (end_seq, tc->rcv_las)
116  && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd));
117 }
118 
119 /**
120  * Parse TCP header options.
121  *
122  * @param th TCP header
123  * @param to TCP options data structure to be populated
124  * @param is_syn set if packet is syn
125  * @return -1 if parsing failed
126  */
127 static inline int
129 {
130  const u8 *data;
131  u8 opt_len, opts_len, kind;
132  int j;
133  sack_block_t b;
134 
135  opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
136  data = (const u8 *) (th + 1);
137 
138  /* Zero out all flags but those set in SYN */
139  to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE
140  | TCP_OPTS_FLAG_TSTAMP | TCP_OPTION_MSS);
141 
142  for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
143  {
144  kind = data[0];
145 
146  /* Get options length */
147  if (kind == TCP_OPTION_EOL)
148  break;
149  else if (kind == TCP_OPTION_NOOP)
150  {
151  opt_len = 1;
152  continue;
153  }
154  else
155  {
156  /* broken options */
157  if (opts_len < 2)
158  return -1;
159  opt_len = data[1];
160 
161  /* weird option length */
162  if (opt_len < 2 || opt_len > opts_len)
163  return -1;
164  }
165 
166  /* Parse options */
167  switch (kind)
168  {
169  case TCP_OPTION_MSS:
170  if (!is_syn)
171  break;
172  if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
173  {
174  to->flags |= TCP_OPTS_FLAG_MSS;
175  to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
176  }
177  break;
179  if (!is_syn)
180  break;
181  if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
182  {
183  to->flags |= TCP_OPTS_FLAG_WSCALE;
184  to->wscale = data[2];
185  if (to->wscale > TCP_MAX_WND_SCALE)
187  }
188  break;
190  if (is_syn)
191  to->flags |= TCP_OPTS_FLAG_TSTAMP;
192  if ((to->flags & TCP_OPTS_FLAG_TSTAMP)
193  && opt_len == TCP_OPTION_LEN_TIMESTAMP)
194  {
195  to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
196  to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
197  }
198  break;
200  if (!is_syn)
201  break;
202  if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
203  to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
204  break;
206  /* If SACK permitted was not advertised or a SYN, break */
207  if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
208  break;
209 
210  /* If too short or not correctly formatted, break */
211  if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
212  break;
213 
214  to->flags |= TCP_OPTS_FLAG_SACK;
215  to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
216  vec_reset_length (to->sacks);
217  for (j = 0; j < to->n_sack_blocks; j++)
218  {
219  b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 8 * j));
220  b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 8 * j));
221  vec_add1 (to->sacks, b);
222  }
223  break;
224  default:
225  /* Nothing to see here */
226  continue;
227  }
228  }
229  return 0;
230 }
231 
232 /**
233  * RFC1323: Check against wrapped sequence numbers (PAWS). If we have
234  * timestamp to echo and it's less than tsval_recent, drop segment
235  * but still send an ACK in order to retain TCP's mechanism for detecting
236  * and recovering from half-open connections
237  *
238  * Or at least that's what the theory says. It seems that this might not work
239  * very well with packet reordering and fast retransmit. XXX
240  */
241 always_inline int
243 {
244  return tcp_opts_tstamp (&tc->rcv_opts)
245  && timestamp_lt (tc->rcv_opts.tsval, tc->tsval_recent);
246 }
247 
248 /**
249  * Update tsval recent
250  */
251 always_inline void
253 {
254  /*
255  * RFC1323: If Last.ACK.sent falls within the range of sequence numbers
256  * of an incoming segment:
257  * SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN
258  * then the TSval from the segment is copied to TS.Recent;
259  * otherwise, the TSval is ignored.
260  */
261  if (tcp_opts_tstamp (&tc->rcv_opts) && seq_leq (seq, tc->rcv_las)
262  && seq_leq (tc->rcv_las, seq_end))
263  {
264  ASSERT (timestamp_leq (tc->tsval_recent, tc->rcv_opts.tsval));
265  tc->tsval_recent = tc->rcv_opts.tsval;
266  tc->tsval_recent_age = tcp_time_now_w_thread (tc->c_thread_index);
267  }
268 }
269 
270 /**
271  * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
272  *
273  * It first verifies if segment has a wrapped sequence number (PAWS) and then
274  * does the processing associated to the first four steps (ignoring security
275  * and precedence): sequence number, rst bit and syn bit checks.
276  *
277  * @return 0 if segments passes validation.
278  */
279 static int
281  vlib_buffer_t * b0, tcp_header_t * th0, u32 * error0)
282 {
283  /* We could get a burst of RSTs interleaved with acks */
284  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
285  {
286  tcp_send_reset (tc0);
287  *error0 = TCP_ERROR_CONNECTION_CLOSED;
288  goto error;
289  }
290 
291  if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
292  {
293  *error0 = TCP_ERROR_SEGMENT_INVALID;
294  goto error;
295  }
296 
297  if (PREDICT_FALSE (tcp_options_parse (th0, &tc0->rcv_opts, 0)))
298  {
299  *error0 = TCP_ERROR_OPTIONS;
300  goto error;
301  }
302 
304  {
305  *error0 = TCP_ERROR_PAWS;
306  TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
307  vnet_buffer (b0)->tcp.seq_end);
308 
309  /* If it just so happens that a segment updates tsval_recent for a
310  * segment over 24 days old, invalidate tsval_recent. */
311  if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
312  tcp_time_now_w_thread (tc0->c_thread_index)))
313  {
314  tc0->tsval_recent = tc0->rcv_opts.tsval;
315  clib_warning ("paws failed: 24-day old segment");
316  }
317  /* Drop after ack if not rst. Resets can fail paws check as per
318  * RFC 7323 sec. 5.2: When an <RST> segment is received, it MUST NOT
319  * be subjected to the PAWS check by verifying an acceptable value in
320  * SEG.TSval */
321  else if (!tcp_rst (th0))
322  {
323  tcp_program_ack (wrk, tc0);
324  TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
325  goto error;
326  }
327  }
328 
329  /* 1st: check sequence number */
330  if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
331  vnet_buffer (b0)->tcp.seq_end))
332  {
333  *error0 = TCP_ERROR_RCV_WND;
334  /* If our window is 0 and the packet is in sequence, let it pass
335  * through for ack processing. It should be dropped later. */
336  if (!(tc0->rcv_wnd == 0
337  && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number))
338  {
339  /* If not RST, send dup ack */
340  if (!tcp_rst (th0))
341  {
342  tcp_program_dupack (wrk, tc0);
343  TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
344  }
345  goto error;
346  }
347  }
348 
349  /* 2nd: check the RST bit */
350  if (PREDICT_FALSE (tcp_rst (th0)))
351  {
352  tcp_connection_reset (tc0);
353  *error0 = TCP_ERROR_RST_RCVD;
354  goto error;
355  }
356 
357  /* 3rd: check security and precedence (skip) */
358 
359  /* 4th: check the SYN bit */
360  if (PREDICT_FALSE (tcp_syn (th0)))
361  {
362  *error0 = tcp_ack (th0) ? TCP_ERROR_SYN_ACKS_RCVD : TCP_ERROR_SYNS_RCVD;
363  /* TODO implement RFC 5961 */
364  if (tc0->state == TCP_STATE_SYN_RCVD)
365  {
366  tcp_options_parse (th0, &tc0->rcv_opts, 1);
367  tcp_send_synack (tc0);
368  TCP_EVT_DBG (TCP_EVT_SYN_RCVD, tc0, 0);
369  }
370  else
371  {
372  tcp_program_ack (wrk, tc0);
373  TCP_EVT_DBG (TCP_EVT_SYNACK_RCVD, tc0);
374  }
375  goto error;
376  }
377 
378  /* If segment in window, save timestamp */
379  tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number,
380  vnet_buffer (b0)->tcp.seq_end);
381  return 0;
382 
383 error:
384  return -1;
385 }
386 
387 always_inline int
389 {
390  /* SND.UNA =< SEG.ACK =< SND.NXT */
391  return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
392  && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
393 }
394 
395 /**
396  * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
397  *
398  * Note that although the original article, srtt and rttvar are scaled
399  * to minimize round-off errors, here we don't. Instead, we rely on
400  * better precision time measurements.
401  *
402  * TODO support us rtt resolution
403  */
404 static void
406 {
407  int err, diff;
408 
409  if (tc->srtt != 0)
410  {
411  err = mrtt - tc->srtt;
412 
413  /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
414  * The increase should be bound */
415  tc->srtt = clib_max ((int) tc->srtt + (err >> 3), 1);
416  diff = (clib_abs (err) - (int) tc->rttvar) >> 2;
417  tc->rttvar = clib_max ((int) tc->rttvar + diff, 1);
418  }
419  else
420  {
421  /* First measurement. */
422  tc->srtt = mrtt;
423  tc->rttvar = mrtt >> 1;
424  }
425 }
426 
427 void
429 {
430  tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
431  tc->rto = clib_max (tc->rto, TCP_RTO_MIN);
432 }
433 
434 /**
435  * Update RTT estimate and RTO timer
436  *
437  * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
438  * timing. Middle boxes are known to fiddle with TCP options so we
439  * should give higher priority to ACK timing.
440  *
441  * This should be called only if previously sent bytes have been acked.
442  *
443  * return 1 if valid rtt 0 otherwise
444  */
445 static int
447 {
448  u32 mrtt = 0;
449 
450  /* Karn's rule, part 1. Don't use retransmitted segments to estimate
451  * RTT because they're ambiguous. */
452  if (tcp_in_cong_recovery (tc) || tc->sack_sb.sacked_bytes)
453  {
454  if (tcp_in_recovery (tc))
455  return 0;
456  goto done;
457  }
458 
459  if (tc->rtt_ts && seq_geq (ack, tc->rtt_seq))
460  {
461  f64 sample = tcp_time_now_us (tc->c_thread_index) - tc->rtt_ts;
462  tc->mrtt_us = tc->mrtt_us + (sample - tc->mrtt_us) * 0.125;
463  mrtt = clib_max ((u32) (sample * THZ), 1);
464  /* Allow measuring of a new RTT */
465  tc->rtt_ts = 0;
466  }
467  /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
468  * snd_una, i.e., the left side of the send window:
469  * seq_lt (tc->snd_una, ack). This is a condition for calling update_rtt */
470  else if (tcp_opts_tstamp (&tc->rcv_opts) && tc->rcv_opts.tsecr)
471  {
472  u32 now = tcp_time_now_w_thread (tc->c_thread_index);
473  mrtt = clib_max (now - tc->rcv_opts.tsecr, 1);
474  }
475 
476  /* Ignore dubious measurements */
477  if (mrtt == 0 || mrtt > TCP_RTT_MAX)
478  goto done;
479 
480  tcp_estimate_rtt (tc, mrtt);
481 
482 done:
483 
484  /* If we got here something must've been ACKed so make sure boff is 0,
485  * even if mrtt is not valid since we update the rto lower */
486  tc->rto_boff = 0;
487  tcp_update_rto (tc);
488 
489  return 0;
490 }
491 
492 static void
494 {
495  u8 thread_index = vlib_num_workers ()? 1 : 0;
496  int mrtt;
497 
498  if (tc->rtt_ts)
499  {
500  tc->mrtt_us = tcp_time_now_us (thread_index) - tc->rtt_ts;
501  mrtt = clib_max ((u32) (tc->mrtt_us * THZ), 1);
502  tc->rtt_ts = 0;
503  }
504  else
505  {
506  mrtt = tcp_time_now_w_thread (thread_index) - tc->rcv_opts.tsecr;
507  mrtt = clib_max (mrtt, 1);
508  tc->mrtt_us = (f64) mrtt *TCP_TICK;
509  }
510 
511  if (mrtt > 0 && mrtt < TCP_RTT_MAX)
512  tcp_estimate_rtt (tc, mrtt);
513  tcp_update_rto (tc);
514 }
515 
516 /**
517  * Dequeue bytes for connections that have received acks in last burst
518  */
519 static void
521 {
522  u32 thread_index = wrk->vm->thread_index;
523  u32 *pending_deq_acked;
524  tcp_connection_t *tc;
525  int i;
526 
527  if (!vec_len (wrk->pending_deq_acked))
528  return;
529 
530  pending_deq_acked = wrk->pending_deq_acked;
531  for (i = 0; i < vec_len (pending_deq_acked); i++)
532  {
533  tc = tcp_connection_get (pending_deq_acked[i], thread_index);
534  tc->flags &= ~TCP_CONN_DEQ_PENDING;
535 
536  if (PREDICT_FALSE (!tc->burst_acked))
537  continue;
538 
539  /* Dequeue the newly ACKed bytes */
540  stream_session_dequeue_drop (&tc->connection, tc->burst_acked);
541  tc->burst_acked = 0;
542  tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
543 
544  if (PREDICT_FALSE (tc->flags & TCP_CONN_PSH_PENDING))
545  {
546  if (seq_leq (tc->psh_seq, tc->snd_una))
547  tc->flags &= ~TCP_CONN_PSH_PENDING;
548  }
549 
550  /* If everything has been acked, stop retransmit timer
551  * otherwise update. */
553 
554  /* If not congested, update pacer based on our new
555  * cwnd estimate */
556  if (!tcp_in_fastrecovery (tc))
558  }
559  _vec_len (wrk->pending_deq_acked) = 0;
560 }
561 
562 static void
564 {
565  if (!(tc->flags & TCP_CONN_DEQ_PENDING))
566  {
567  vec_add1 (wrk->pending_deq_acked, tc->c_c_index);
568  tc->flags |= TCP_CONN_DEQ_PENDING;
569  }
570  tc->burst_acked += tc->bytes_acked + tc->sack_sb.snd_una_adv;
571 }
572 
573 /**
574  * Check if duplicate ack as per RFC5681 Sec. 2
575  */
576 static u8
578  u32 prev_snd_una)
579 {
580  return ((vnet_buffer (b)->tcp.ack_number == prev_snd_una)
581  && seq_gt (tc->snd_una_max, tc->snd_una)
582  && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
583  && (prev_snd_wnd == tc->snd_wnd));
584 }
585 
586 /**
587  * Checks if ack is a congestion control event.
588  */
589 static u8
591  u32 prev_snd_wnd, u32 prev_snd_una, u8 * is_dack)
592 {
593  /* Check if ack is duplicate. Per RFC 6675, ACKs that SACK new data are
594  * defined to be 'duplicate' */
595  *is_dack = tc->sack_sb.last_sacked_bytes
596  || tcp_ack_is_dupack (tc, b, prev_snd_wnd, prev_snd_una);
597 
598  return ((*is_dack || tcp_in_cong_recovery (tc)) && !tcp_is_lost_fin (tc));
599 }
600 
601 static u32
603 {
604  ASSERT (!pool_is_free_index (sb->holes, hole - sb->holes));
605  return hole - sb->holes;
606 }
607 
608 static u32
610 {
611  return hole->end - hole->start;
612 }
613 
616 {
617  if (index != TCP_INVALID_SACK_HOLE_INDEX)
618  return pool_elt_at_index (sb->holes, index);
619  return 0;
620 }
621 
624 {
625  if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
626  return pool_elt_at_index (sb->holes, hole->next);
627  return 0;
628 }
629 
632 {
633  if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
634  return pool_elt_at_index (sb->holes, hole->prev);
635  return 0;
636 }
637 
640 {
641  if (sb->head != TCP_INVALID_SACK_HOLE_INDEX)
642  return pool_elt_at_index (sb->holes, sb->head);
643  return 0;
644 }
645 
648 {
649  if (sb->tail != TCP_INVALID_SACK_HOLE_INDEX)
650  return pool_elt_at_index (sb->holes, sb->tail);
651  return 0;
652 }
653 
654 static void
656 {
657  sack_scoreboard_hole_t *next, *prev;
658 
659  if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
660  {
661  next = pool_elt_at_index (sb->holes, hole->next);
662  next->prev = hole->prev;
663  }
664  else
665  {
666  sb->tail = hole->prev;
667  }
668 
669  if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
670  {
671  prev = pool_elt_at_index (sb->holes, hole->prev);
672  prev->next = hole->next;
673  }
674  else
675  {
676  sb->head = hole->next;
677  }
678 
679  if (scoreboard_hole_index (sb, hole) == sb->cur_rxt_hole)
680  sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
681 
682  /* Poison the entry */
683  if (CLIB_DEBUG > 0)
684  clib_memset (hole, 0xfe, sizeof (*hole));
685 
686  pool_put (sb->holes, hole);
687 }
688 
689 static sack_scoreboard_hole_t *
691  u32 start, u32 end)
692 {
693  sack_scoreboard_hole_t *hole, *next, *prev;
694  u32 hole_index;
695 
696  pool_get (sb->holes, hole);
697  clib_memset (hole, 0, sizeof (*hole));
698 
699  hole->start = start;
700  hole->end = end;
701  hole_index = scoreboard_hole_index (sb, hole);
702 
703  prev = scoreboard_get_hole (sb, prev_index);
704  if (prev)
705  {
706  hole->prev = prev_index;
707  hole->next = prev->next;
708 
709  if ((next = scoreboard_next_hole (sb, hole)))
710  next->prev = hole_index;
711  else
712  sb->tail = hole_index;
713 
714  prev->next = hole_index;
715  }
716  else
717  {
718  sb->head = hole_index;
719  hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
720  hole->next = TCP_INVALID_SACK_HOLE_INDEX;
721  }
722 
723  return hole;
724 }
725 
726 static void
728 {
730  u32 bytes = 0, blks = 0;
731 
732  sb->lost_bytes = 0;
733  sb->sacked_bytes = 0;
734  left = scoreboard_last_hole (sb);
735  if (!left)
736  return;
737 
738  if (seq_gt (sb->high_sacked, left->end))
739  {
740  bytes = sb->high_sacked - left->end;
741  blks = 1;
742  }
743 
744  while ((right = left)
745  && bytes < (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
746  && blks < TCP_DUPACK_THRESHOLD
747  /* left not updated if above conditions fail */
748  && (left = scoreboard_prev_hole (sb, right)))
749  {
750  bytes += right->start - left->end;
751  blks++;
752  }
753 
754  /* left is first lost */
755  if (left)
756  {
757  do
758  {
759  sb->lost_bytes += scoreboard_hole_bytes (right);
760  left->is_lost = 1;
761  left = scoreboard_prev_hole (sb, right);
762  if (left)
763  bytes += right->start - left->end;
764  }
765  while ((right = left));
766  }
767 
768  sb->sacked_bytes = bytes;
769 }
770 
771 /**
772  * Figure out the next hole to retransmit
773  *
774  * Follows logic proposed in RFC6675 Sec. 4, NextSeg()
775  */
778  sack_scoreboard_hole_t * start,
779  u8 have_unsent, u8 * can_rescue, u8 * snd_limited)
780 {
781  sack_scoreboard_hole_t *hole = 0;
782 
783  hole = start ? start : scoreboard_first_hole (sb);
784  while (hole && seq_leq (hole->end, sb->high_rxt) && hole->is_lost)
785  hole = scoreboard_next_hole (sb, hole);
786 
787  /* Nothing, return */
788  if (!hole)
789  {
790  sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
791  return 0;
792  }
793 
794  /* Rule (1): if higher than rxt, less than high_sacked and lost */
795  if (hole->is_lost && seq_lt (hole->start, sb->high_sacked))
796  {
797  sb->cur_rxt_hole = scoreboard_hole_index (sb, hole);
798  }
799  else
800  {
801  /* Rule (2): available unsent data */
802  if (have_unsent)
803  {
804  sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
805  return 0;
806  }
807  /* Rule (3): if hole not lost */
808  else if (seq_lt (hole->start, sb->high_sacked))
809  {
810  *snd_limited = 0;
811  sb->cur_rxt_hole = scoreboard_hole_index (sb, hole);
812  }
813  /* Rule (4): if hole beyond high_sacked */
814  else
815  {
816  ASSERT (seq_geq (hole->start, sb->high_sacked));
817  *snd_limited = 1;
818  *can_rescue = 1;
819  /* HighRxt MUST NOT be updated */
820  return 0;
821  }
822  }
823 
824  if (hole && seq_lt (sb->high_rxt, hole->start))
825  sb->high_rxt = hole->start;
826 
827  return hole;
828 }
829 
830 static void
832 {
834  hole = scoreboard_first_hole (sb);
835  if (hole)
836  {
837  snd_una = seq_gt (snd_una, hole->start) ? snd_una : hole->start;
838  sb->cur_rxt_hole = sb->head;
839  }
840  sb->high_rxt = snd_una;
841  sb->rescue_rxt = snd_una - 1;
842 }
843 
844 void
846 {
847  sb->head = TCP_INVALID_SACK_HOLE_INDEX;
848  sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
849  sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
850 }
851 
852 void
854 {
856  while ((hole = scoreboard_first_hole (sb)))
857  {
858  scoreboard_remove_hole (sb, hole);
859  }
860  ASSERT (sb->head == sb->tail && sb->head == TCP_INVALID_SACK_HOLE_INDEX);
861  ASSERT (pool_elts (sb->holes) == 0);
862  sb->sacked_bytes = 0;
863  sb->last_sacked_bytes = 0;
864  sb->last_bytes_delivered = 0;
865  sb->snd_una_adv = 0;
866  sb->high_sacked = 0;
867  sb->high_rxt = 0;
868  sb->lost_bytes = 0;
869  sb->cur_rxt_hole = TCP_INVALID_SACK_HOLE_INDEX;
870 }
871 
872 /**
873  * Test that scoreboard is sane after recovery
874  *
875  * Returns 1 if scoreboard is empty or if first hole beyond
876  * snd_una.
877  */
878 static u8
880 {
882  hole = scoreboard_first_hole (&tc->sack_sb);
883  return (!hole || (seq_geq (hole->start, tc->snd_una)
884  && seq_lt (hole->end, tc->snd_una_max)));
885 }
886 
887 void
889 {
890  sack_scoreboard_t *sb = &tc->sack_sb;
891  sack_block_t *blk, tmp;
892  sack_scoreboard_hole_t *hole, *next_hole, *last_hole;
893  u32 blk_index = 0, old_sacked_bytes, hole_index;
894  int i, j;
895 
896  sb->last_sacked_bytes = 0;
897  sb->last_bytes_delivered = 0;
898  sb->snd_una_adv = 0;
899 
900  if (!tcp_opts_sack (&tc->rcv_opts)
901  && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
902  return;
903 
904  old_sacked_bytes = sb->sacked_bytes;
905 
906  /* Remove invalid blocks */
907  blk = tc->rcv_opts.sacks;
908  while (blk < vec_end (tc->rcv_opts.sacks))
909  {
910  if (seq_lt (blk->start, blk->end)
911  && seq_gt (blk->start, tc->snd_una)
912  && seq_gt (blk->start, ack)
913  && seq_lt (blk->start, tc->snd_una_max)
914  && seq_leq (blk->end, tc->snd_una_max))
915  {
916  blk++;
917  continue;
918  }
919  vec_del1 (tc->rcv_opts.sacks, blk - tc->rcv_opts.sacks);
920  }
921 
922  /* Add block for cumulative ack */
923  if (seq_gt (ack, tc->snd_una))
924  {
925  tmp.start = tc->snd_una;
926  tmp.end = ack;
927  vec_add1 (tc->rcv_opts.sacks, tmp);
928  }
929 
930  if (vec_len (tc->rcv_opts.sacks) == 0)
931  return;
932 
933  tcp_scoreboard_trace_add (tc, ack);
934 
935  /* Make sure blocks are ordered */
936  for (i = 0; i < vec_len (tc->rcv_opts.sacks); i++)
937  for (j = i + 1; j < vec_len (tc->rcv_opts.sacks); j++)
938  if (seq_lt (tc->rcv_opts.sacks[j].start, tc->rcv_opts.sacks[i].start))
939  {
940  tmp = tc->rcv_opts.sacks[i];
941  tc->rcv_opts.sacks[i] = tc->rcv_opts.sacks[j];
942  tc->rcv_opts.sacks[j] = tmp;
943  }
944 
945  if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
946  {
947  /* If no holes, insert the first that covers all outstanding bytes */
949  tc->snd_una, tc->snd_una_max);
950  sb->tail = scoreboard_hole_index (sb, last_hole);
951  tmp = tc->rcv_opts.sacks[vec_len (tc->rcv_opts.sacks) - 1];
952  sb->high_sacked = tmp.end;
953  }
954  else
955  {
956  /* If we have holes but snd_una_max is beyond the last hole, update
957  * last hole end */
958  tmp = tc->rcv_opts.sacks[vec_len (tc->rcv_opts.sacks) - 1];
959  last_hole = scoreboard_last_hole (sb);
960  if (seq_gt (tc->snd_una_max, last_hole->end))
961  {
962  if (seq_geq (last_hole->start, sb->high_sacked))
963  {
964  last_hole->end = tc->snd_una_max;
965  }
966  /* New hole after high sacked block */
967  else if (seq_lt (sb->high_sacked, tc->snd_una_max))
968  {
969  scoreboard_insert_hole (sb, sb->tail, sb->high_sacked,
970  tc->snd_una_max);
971  }
972  }
973  /* Keep track of max byte sacked for when the last hole
974  * is acked */
975  if (seq_gt (tmp.end, sb->high_sacked))
976  sb->high_sacked = tmp.end;
977  }
978 
979  /* Walk the holes with the SACK blocks */
980  hole = pool_elt_at_index (sb->holes, sb->head);
981  while (hole && blk_index < vec_len (tc->rcv_opts.sacks))
982  {
983  blk = &tc->rcv_opts.sacks[blk_index];
984  if (seq_leq (blk->start, hole->start))
985  {
986  /* Block covers hole. Remove hole */
987  if (seq_geq (blk->end, hole->end))
988  {
989  next_hole = scoreboard_next_hole (sb, hole);
990 
991  /* Byte accounting: snd_una needs to be advanced */
992  if (blk->end == ack)
993  {
994  if (next_hole)
995  {
996  if (seq_lt (ack, next_hole->start))
997  sb->snd_una_adv = next_hole->start - ack;
998  sb->last_bytes_delivered +=
999  next_hole->start - hole->end;
1000  }
1001  else
1002  {
1003  ASSERT (seq_geq (sb->high_sacked, ack));
1004  sb->snd_una_adv = sb->high_sacked - ack;
1005  sb->last_bytes_delivered += sb->high_sacked - hole->end;
1006  }
1007  }
1008 
1009  scoreboard_remove_hole (sb, hole);
1010  hole = next_hole;
1011  }
1012  /* Partial 'head' overlap */
1013  else
1014  {
1015  if (seq_gt (blk->end, hole->start))
1016  {
1017  hole->start = blk->end;
1018  }
1019  blk_index++;
1020  }
1021  }
1022  else
1023  {
1024  /* Hole must be split */
1025  if (seq_lt (blk->end, hole->end))
1026  {
1027  hole_index = scoreboard_hole_index (sb, hole);
1028  next_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
1029  hole->end);
1030 
1031  /* Pool might've moved */
1032  hole = scoreboard_get_hole (sb, hole_index);
1033  hole->end = blk->start;
1034  blk_index++;
1035  ASSERT (hole->next == scoreboard_hole_index (sb, next_hole));
1036  }
1037  else if (seq_lt (blk->start, hole->end))
1038  {
1039  hole->end = blk->start;
1040  }
1041  hole = scoreboard_next_hole (sb, hole);
1042  }
1043  }
1044 
1045  if (pool_elts (sb->holes) == 1)
1046  {
1047  hole = scoreboard_first_hole (sb);
1048  if (hole->start == ack + sb->snd_una_adv
1049  && hole->end == tc->snd_una_max)
1050  scoreboard_remove_hole (sb, hole);
1051  }
1052 
1053  scoreboard_update_bytes (tc, sb);
1054  sb->last_sacked_bytes = sb->sacked_bytes
1055  - (old_sacked_bytes - sb->last_bytes_delivered);
1056  ASSERT (sb->last_sacked_bytes <= sb->sacked_bytes || tcp_in_recovery (tc));
1057  ASSERT (sb->sacked_bytes == 0 || tcp_in_recovery (tc)
1058  || sb->sacked_bytes < tc->snd_una_max - seq_max (tc->snd_una, ack));
1059  ASSERT (sb->last_sacked_bytes + sb->lost_bytes <= tc->snd_una_max
1060  - seq_max (tc->snd_una, ack) || tcp_in_recovery (tc));
1062  || sb->holes[sb->head].start == ack + sb->snd_una_adv);
1063  TCP_EVT_DBG (TCP_EVT_CC_SCOREBOARD, tc);
1064 }
1065 
1066 /**
1067  * Try to update snd_wnd based on feedback received from peer.
1068  *
1069  * If successful, and new window is 'effectively' 0, activate persist
1070  * timer.
1071  */
1072 static void
1073 tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
1074 {
1075  /* If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
1076  * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
1077  if (seq_lt (tc->snd_wl1, seq)
1078  || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
1079  {
1080  tc->snd_wnd = snd_wnd;
1081  tc->snd_wl1 = seq;
1082  tc->snd_wl2 = ack;
1083  TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
1084 
1085  if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
1086  {
1087  /* Set persist timer if not set and we just got 0 wnd */
1088  if (!tcp_timer_is_active (tc, TCP_TIMER_PERSIST)
1089  && !tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT))
1090  tcp_persist_timer_set (tc);
1091  }
1092  else
1093  {
1095  if (PREDICT_FALSE (!tcp_in_recovery (tc) && tc->rto_boff > 0))
1096  {
1097  tc->rto_boff = 0;
1098  tcp_update_rto (tc);
1099  }
1100  }
1101  }
1102 }
1103 
1104 /**
1105  * Init loss recovery/fast recovery.
1106  *
1107  * Triggered by dup acks as opposed to timer timeout. Note that cwnd is
1108  * updated in @ref tcp_cc_handle_event after fast retransmit
1109  */
1110 void
1112 {
1113  tcp_fastrecovery_on (tc);
1114  tc->snd_congestion = tc->snd_una_max;
1115  tc->cwnd_acc_bytes = 0;
1116  tc->snd_rxt_bytes = 0;
1117  tc->prev_ssthresh = tc->ssthresh;
1118  tc->prev_cwnd = tc->cwnd;
1119  tc->cc_algo->congestion (tc);
1120  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
1121 }
1122 
1123 static void
1125 {
1126  tc->rto_boff = 0;
1127  tcp_update_rto (tc);
1128  tc->snd_rxt_ts = 0;
1129  tc->snd_nxt = tc->snd_una_max;
1130  tc->rtt_ts = 0;
1131  tcp_recovery_off (tc);
1132  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
1133 }
1134 
1135 void
1137 {
1138  tc->cc_algo->recovered (tc);
1139  tc->snd_rxt_bytes = 0;
1140  tc->rcv_dupacks = 0;
1141  tc->snd_nxt = tc->snd_una_max;
1142  tc->snd_rxt_bytes = 0;
1143  tc->rtt_ts = 0;
1144 
1145  tcp_fastrecovery_off (tc);
1147 
1148  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
1149 }
1150 
1151 static void
1153 {
1154  tc->cwnd = tc->prev_cwnd;
1155  tc->ssthresh = tc->prev_ssthresh;
1156  tc->snd_nxt = tc->snd_una_max;
1157  tc->rcv_dupacks = 0;
1158  if (tcp_in_recovery (tc))
1159  tcp_cc_recovery_exit (tc);
1160  else if (tcp_in_fastrecovery (tc))
1162  ASSERT (tc->rto_boff == 0);
1163  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 5);
1164 }
1165 
1166 static inline u8
1168 {
1169  return (tcp_in_recovery (tc) && tc->rto_boff == 1
1170  && tc->snd_rxt_ts
1171  && tcp_opts_tstamp (&tc->rcv_opts)
1172  && timestamp_lt (tc->rcv_opts.tsecr, tc->snd_rxt_ts));
1173 }
1174 
1175 static inline u8
1177 {
1178  return (tcp_in_fastrecovery (tc)
1179  && tc->cwnd > tc->ssthresh + 3 * tc->snd_mss);
1180 }
1181 
1182 static u8
1184 {
1185  return (tcp_cc_is_spurious_timeout_rxt (tc)
1186  || tcp_cc_is_spurious_fast_rxt (tc));
1187 }
1188 
1189 static int
1191 {
1194  {
1196  return 1;
1197  }
1198 
1199  if (tcp_in_recovery (tc))
1200  tcp_cc_recovery_exit (tc);
1201  else if (tcp_in_fastrecovery (tc))
1203 
1204  ASSERT (tc->rto_boff == 0);
1205  ASSERT (!tcp_in_cong_recovery (tc));
1207  return 0;
1208 }
1209 
1210 static void
1212 {
1214 
1215  /* Congestion avoidance */
1216  tcp_cc_rcv_ack (tc);
1217 
1218  /* If a cumulative ack, make sure dupacks is 0 */
1219  tc->rcv_dupacks = 0;
1220 
1221  /* When dupacks hits the threshold we only enter fast retransmit if
1222  * cumulative ack covers more than snd_congestion. Should snd_una
1223  * wrap this test may fail under otherwise valid circumstances.
1224  * Therefore, proactively update snd_congestion when wrap detected. */
1225  if (PREDICT_FALSE
1226  (seq_leq (tc->snd_congestion, tc->snd_una - tc->bytes_acked)
1227  && seq_gt (tc->snd_congestion, tc->snd_una)))
1228  tc->snd_congestion = tc->snd_una - 1;
1229 }
1230 
1231 static u8
1233 {
1234  return (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss < tc->sack_sb.sacked_bytes;
1235 }
1236 
1237 static u8
1239 {
1240  return (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD
1241  || tcp_should_fastrecover_sack (tc));
1242 }
1243 
1244 void
1246 {
1247  if (!(tc->flags & TCP_CONN_FRXT_PENDING))
1248  {
1249  vec_add1 (wrk->pending_fast_rxt, tc->c_c_index);
1250  tc->flags |= TCP_CONN_FRXT_PENDING;
1251  }
1252 }
1253 
1254 void
1256 {
1257  u32 *ongoing_fast_rxt, burst_bytes, sent_bytes, thread_index;
1258  u32 max_burst_size, burst_size, n_segs = 0, n_segs_now;
1259  tcp_connection_t *tc;
1260  u64 last_cpu_time;
1261  int i;
1262 
1263  if (vec_len (wrk->pending_fast_rxt) == 0
1264  && vec_len (wrk->postponed_fast_rxt) == 0)
1265  return;
1266 
1267  thread_index = wrk->vm->thread_index;
1268  last_cpu_time = wrk->vm->clib_time.last_cpu_time;
1269  ongoing_fast_rxt = wrk->ongoing_fast_rxt;
1270  vec_append (ongoing_fast_rxt, wrk->postponed_fast_rxt);
1271  vec_append (ongoing_fast_rxt, wrk->pending_fast_rxt);
1272 
1273  _vec_len (wrk->postponed_fast_rxt) = 0;
1274  _vec_len (wrk->pending_fast_rxt) = 0;
1275 
1276  max_burst_size = VLIB_FRAME_SIZE / vec_len (ongoing_fast_rxt);
1277  max_burst_size = clib_max (max_burst_size, 1);
1278 
1279  for (i = 0; i < vec_len (ongoing_fast_rxt); i++)
1280  {
1281  if (n_segs >= VLIB_FRAME_SIZE)
1282  {
1283  vec_add1 (wrk->postponed_fast_rxt, ongoing_fast_rxt[i]);
1284  continue;
1285  }
1286 
1287  tc = tcp_connection_get (ongoing_fast_rxt[i], thread_index);
1288  tc->flags &= ~TCP_CONN_FRXT_PENDING;
1289 
1290  if (!tcp_in_fastrecovery (tc))
1291  continue;
1292 
1293  burst_size = clib_min (max_burst_size, VLIB_FRAME_SIZE - n_segs);
1294  burst_bytes = transport_connection_tx_pacer_burst (&tc->connection,
1295  last_cpu_time);
1296  burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
1297  if (!burst_size)
1298  {
1299  tcp_program_fastretransmit (wrk, tc);
1300  continue;
1301  }
1302 
1303  n_segs_now = tcp_fast_retransmit (wrk, tc, burst_size);
1304  sent_bytes = clib_min (n_segs_now * tc->snd_mss, burst_bytes);
1306  sent_bytes);
1307  n_segs += n_segs_now;
1308  }
1309  _vec_len (ongoing_fast_rxt) = 0;
1310  wrk->ongoing_fast_rxt = ongoing_fast_rxt;
1311 }
1312 
1313 /**
1314  * One function to rule them all ... and in the darkness bind them
1315  */
1316 static void
1318 {
1319  u32 rxt_delivered;
1320 
1321  if (tcp_in_fastrecovery (tc) && tcp_opts_sack_permitted (&tc->rcv_opts))
1322  {
1323  if (tc->bytes_acked)
1324  goto partial_ack;
1325  tcp_program_fastretransmit (tcp_get_worker (tc->c_thread_index), tc);
1326  return;
1327  }
1328  /*
1329  * Duplicate ACK. Check if we should enter fast recovery, or if already in
1330  * it account for the bytes that left the network.
1331  */
1332  else if (is_dack && !tcp_in_recovery (tc))
1333  {
1334  TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
1335  ASSERT (tc->snd_una != tc->snd_una_max
1336  || tc->sack_sb.last_sacked_bytes);
1337 
1338  tc->rcv_dupacks++;
1339 
1340  /* Pure duplicate ack. If some data got acked, it's handled lower */
1341  if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD && !tc->bytes_acked)
1342  {
1343  ASSERT (tcp_in_fastrecovery (tc));
1344  tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
1345  return;
1346  }
1347  else if (tcp_should_fastrecover (tc))
1348  {
1349  u32 pacer_wnd;
1350 
1351  ASSERT (!tcp_in_fastrecovery (tc));
1352 
1353  /* Heuristic to catch potential late dupacks
1354  * after fast retransmit exits */
1355  if (is_dack && tc->snd_una == tc->snd_congestion
1356  && timestamp_leq (tc->rcv_opts.tsecr, tc->tsecr_last_ack))
1357  {
1358  tc->rcv_dupacks = 0;
1359  return;
1360  }
1361 
1363  tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
1364 
1365  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1366  {
1367  tc->cwnd = tc->ssthresh;
1368  scoreboard_init_high_rxt (&tc->sack_sb, tc->snd_una);
1369  }
1370  else
1371  {
1372  /* Post retransmit update cwnd to ssthresh and account for the
1373  * three segments that have left the network and should've been
1374  * buffered at the receiver XXX */
1375  tc->cwnd = tc->ssthresh + 3 * tc->snd_mss;
1376  }
1377 
1378  /* Constrain rate until we get a partial ack */
1379  pacer_wnd = clib_max (0.1 * tc->cwnd, 2 * tc->snd_mss);
1380  tcp_connection_tx_pacer_reset (tc, pacer_wnd,
1381  0 /* start bucket */ );
1382  tcp_program_fastretransmit (tcp_get_worker (tc->c_thread_index),
1383  tc);
1384  return;
1385  }
1386  else if (!tc->bytes_acked
1387  || (tc->bytes_acked && !tcp_in_cong_recovery (tc)))
1388  {
1389  tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
1390  return;
1391  }
1392  else
1393  goto partial_ack;
1394  }
1395  /* Don't allow entry in fast recovery if still in recovery, for now */
1396  else if (0 && is_dack && tcp_in_recovery (tc))
1397  {
1398  /* If of of the two conditions lower hold, reset dupacks because
1399  * we're probably after timeout (RFC6582 heuristics).
1400  * If Cumulative ack does not cover more than congestion threshold,
1401  * and:
1402  * 1) The following doesn't hold: The congestion window is greater
1403  * than SMSS bytes and the difference between highest_ack
1404  * and prev_highest_ack is at most 4*SMSS bytes
1405  * 2) Echoed timestamp in the last non-dup ack does not equal the
1406  * stored timestamp
1407  */
1408  if (seq_leq (tc->snd_una, tc->snd_congestion)
1409  && ((!(tc->cwnd > tc->snd_mss
1410  && tc->bytes_acked <= 4 * tc->snd_mss))
1411  || (tc->rcv_opts.tsecr != tc->tsecr_last_ack)))
1412  {
1413  tc->rcv_dupacks = 0;
1414  return;
1415  }
1416  }
1417 
1418  if (!tc->bytes_acked)
1419  return;
1420 
1421 partial_ack:
1422  TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
1423 
1424  /*
1425  * Legitimate ACK. 1) See if we can exit recovery
1426  */
1427 
1428  /* Update the pacing rate. For the first partial ack we move from
1429  * the artificially constrained rate to the one after congestion */
1431 
1432  if (seq_geq (tc->snd_una, tc->snd_congestion))
1433  {
1435 
1436  /* If spurious return, we've already updated everything */
1437  if (tcp_cc_recover (tc))
1438  {
1439  tc->tsecr_last_ack = tc->rcv_opts.tsecr;
1440  return;
1441  }
1442 
1443  tc->snd_nxt = tc->snd_una_max;
1444 
1445  /* Treat as congestion avoidance ack */
1446  tcp_cc_rcv_ack (tc);
1447  return;
1448  }
1449 
1450  /*
1451  * Legitimate ACK. 2) If PARTIAL ACK try to retransmit
1452  */
1453 
1454  /* XXX limit this only to first partial ack? */
1456 
1457  /* RFC6675: If the incoming ACK is a cumulative acknowledgment,
1458  * reset dupacks to 0. Also needed if in congestion recovery */
1459  tc->rcv_dupacks = 0;
1460 
1461  /* Post RTO timeout don't try anything fancy */
1462  if (tcp_in_recovery (tc))
1463  {
1464  tcp_cc_rcv_ack (tc);
1465  transport_add_tx_event (&tc->connection);
1466  return;
1467  }
1468 
1469  /* Remove retransmitted bytes that have been delivered */
1470  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1471  {
1472  ASSERT (tc->bytes_acked + tc->sack_sb.snd_una_adv
1473  >= tc->sack_sb.last_bytes_delivered
1474  || (tc->flags & TCP_CONN_FINSNT));
1475 
1476  /* If we have sacks and we haven't gotten an ack beyond high_rxt,
1477  * remove sacked bytes delivered */
1478  if (seq_lt (tc->snd_una, tc->sack_sb.high_rxt))
1479  {
1480  rxt_delivered = tc->bytes_acked + tc->sack_sb.snd_una_adv
1481  - tc->sack_sb.last_bytes_delivered;
1482  ASSERT (tc->snd_rxt_bytes >= rxt_delivered);
1483  tc->snd_rxt_bytes -= rxt_delivered;
1484  }
1485  else
1486  {
1487  /* Apparently all retransmitted holes have been acked */
1488  tc->snd_rxt_bytes = 0;
1489  tc->sack_sb.high_rxt = tc->snd_una;
1490  }
1491  }
1492  else
1493  {
1495  /* Reuse last bytes delivered to track total bytes acked */
1496  tc->sack_sb.last_bytes_delivered += tc->bytes_acked;
1497  if (tc->snd_rxt_bytes > tc->bytes_acked)
1498  tc->snd_rxt_bytes -= tc->bytes_acked;
1499  else
1500  tc->snd_rxt_bytes = 0;
1501  }
1502 
1503  tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
1504 
1505  /*
1506  * Since this was a partial ack, try to retransmit some more data
1507  */
1508  tcp_program_fastretransmit (tcp_get_worker (tc->c_thread_index), tc);
1509 }
1510 
1511 /**
1512  * Process incoming ACK
1513  */
1514 static int
1516  tcp_header_t * th, u32 * error)
1517 {
1518  u32 prev_snd_wnd, prev_snd_una;
1519  u8 is_dack;
1520 
1521  TCP_EVT_DBG (TCP_EVT_CC_STAT, tc);
1522 
1523  /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
1524  if (PREDICT_FALSE (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)))
1525  {
1526  /* When we entered cong recovery, we reset snd_nxt to snd_una. Seems
1527  * peer still has the data so accept the ack */
1528  if (tcp_in_cong_recovery (tc)
1529  && seq_leq (vnet_buffer (b)->tcp.ack_number,
1530  tc->snd_una + tc->snd_wnd))
1531  {
1532  tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
1533  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1534  tc->snd_una_max = tc->snd_nxt;
1535  goto process_ack;
1536  }
1537 
1538  /* If we have outstanding data and this is within the window, accept it,
1539  * probably retransmit has timed out. Otherwise ACK segment and then
1540  * drop it */
1541  if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
1542  {
1543  tcp_program_ack (wrk, tc);
1544  *error = TCP_ERROR_ACK_FUTURE;
1545  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
1546  vnet_buffer (b)->tcp.ack_number);
1547  return -1;
1548  }
1549 
1550  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
1551  vnet_buffer (b)->tcp.ack_number);
1552 
1553  tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
1554  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1555  tc->snd_una_max = tc->snd_nxt;
1556 
1557  goto process_ack;
1558  }
1559 
1560  /* If old ACK, probably it's an old dupack */
1561  if (PREDICT_FALSE (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una)))
1562  {
1563  *error = TCP_ERROR_ACK_OLD;
1564  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
1565  vnet_buffer (b)->tcp.ack_number);
1566  if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
1567  tcp_cc_handle_event (tc, 1);
1568  /* Don't drop yet */
1569  return 0;
1570  }
1571 
1572  /*
1573  * Looks okay, process feedback
1574  */
1575 process_ack:
1576  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1577  tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
1578 
1579  prev_snd_wnd = tc->snd_wnd;
1580  prev_snd_una = tc->snd_una;
1581  tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
1582  vnet_buffer (b)->tcp.ack_number,
1583  clib_net_to_host_u16 (th->window) << tc->snd_wscale);
1584  tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
1585  tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
1586  tcp_validate_txf_size (tc, tc->bytes_acked);
1587 
1588  if (tc->bytes_acked)
1589  {
1590  tcp_program_dequeue (wrk, tc);
1591  tcp_update_rtt (tc, vnet_buffer (b)->tcp.ack_number);
1592  }
1593 
1594  TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
1595 
1596  /*
1597  * Check if we have congestion event
1598  */
1599 
1600  if (tcp_ack_is_cc_event (tc, b, prev_snd_wnd, prev_snd_una, &is_dack))
1601  {
1602  tcp_cc_handle_event (tc, is_dack);
1603  if (!tcp_in_cong_recovery (tc))
1604  {
1605  *error = TCP_ERROR_ACK_OK;
1606  return 0;
1607  }
1608  *error = TCP_ERROR_ACK_DUP;
1609  if (vnet_buffer (b)->tcp.data_len || tcp_is_fin (th))
1610  return 0;
1611  return -1;
1612  }
1613 
1614  /*
1615  * Update congestion control (slow start/congestion avoidance)
1616  */
1617  tcp_cc_update (tc, b);
1618  *error = TCP_ERROR_ACK_OK;
1619  return 0;
1620 }
1621 
1622 static void
1624 {
1625  if (!tcp_disconnect_pending (tc))
1626  {
1627  vec_add1 (wrk->pending_disconnects, tc->c_c_index);
1629  }
1630 }
1631 
1632 static void
1634 {
1635  u32 thread_index, *pending_disconnects;
1636  tcp_connection_t *tc;
1637  int i;
1638 
1639  if (!vec_len (wrk->pending_disconnects))
1640  return;
1641 
1642  thread_index = wrk->vm->thread_index;
1643  pending_disconnects = wrk->pending_disconnects;
1644  for (i = 0; i < vec_len (pending_disconnects); i++)
1645  {
1646  tc = tcp_connection_get (pending_disconnects[i], thread_index);
1648  session_transport_closing_notify (&tc->connection);
1649  }
1650  _vec_len (wrk->pending_disconnects) = 0;
1651 }
1652 
1653 static void
1655  u32 * error)
1656 {
1657  /* Account for the FIN and send ack */
1658  tc->rcv_nxt += 1;
1659  tcp_program_ack (wrk, tc);
1660  /* Enter CLOSE-WAIT and notify session. To avoid lingering
1661  * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
1662  tcp_connection_set_state (tc, TCP_STATE_CLOSE_WAIT);
1663  tcp_program_disconnect (wrk, tc);
1664  tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
1665  TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc);
1666  *error = TCP_ERROR_FIN_RCVD;
1667 }
1668 
1669 static u8
1671 {
1672  int i;
1673  for (i = 1; i < vec_len (sacks); i++)
1674  {
1675  if (sacks[i - 1].end == sacks[i].start)
1676  return 0;
1677  }
1678  return 1;
1679 }
1680 
1681 /**
1682  * Build SACK list as per RFC2018.
1683  *
1684  * Makes sure the first block contains the segment that generated the current
1685  * ACK and the following ones are the ones most recently reported in SACK
1686  * blocks.
1687  *
1688  * @param tc TCP connection for which the SACK list is updated
1689  * @param start Start sequence number of the newest SACK block
1690  * @param end End sequence of the newest SACK block
1691  */
1692 void
1694 {
1695  sack_block_t *new_list = 0, *block = 0;
1696  int i;
1697 
1698  /* If the first segment is ooo add it to the list. Last write might've moved
1699  * rcv_nxt over the first segment. */
1700  if (seq_lt (tc->rcv_nxt, start))
1701  {
1702  vec_add2 (new_list, block, 1);
1703  block->start = start;
1704  block->end = end;
1705  }
1706 
1707  /* Find the blocks still worth keeping. */
1708  for (i = 0; i < vec_len (tc->snd_sacks); i++)
1709  {
1710  /* Discard if rcv_nxt advanced beyond current block */
1711  if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
1712  continue;
1713 
1714  /* Merge or drop if segment overlapped by the new segment */
1715  if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
1716  && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
1717  {
1718  if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
1719  new_list[0].start = tc->snd_sacks[i].start;
1720  if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
1721  new_list[0].end = tc->snd_sacks[i].end;
1722  continue;
1723  }
1724 
1725  /* Save to new SACK list if we have space. */
1726  if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
1727  {
1728  vec_add1 (new_list, tc->snd_sacks[i]);
1729  }
1730  else
1731  {
1732  clib_warning ("sack discarded");
1733  }
1734  }
1735 
1736  ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
1737 
1738  /* Replace old vector with new one */
1739  vec_free (tc->snd_sacks);
1740  tc->snd_sacks = new_list;
1741 
1742  /* Segments should not 'touch' */
1743  ASSERT (tcp_sack_vector_is_sane (tc->snd_sacks));
1744 }
1745 
1746 u32
1748 {
1749  u32 bytes = 0, i;
1750  for (i = 0; i < vec_len (tc->snd_sacks); i++)
1751  bytes += tc->snd_sacks[i].end - tc->snd_sacks[i].start;
1752  return bytes;
1753 }
1754 
1755 /** Enqueue data for delivery to application */
1756 static int
1758  u16 data_len)
1759 {
1760  int written, error = TCP_ERROR_ENQUEUED;
1761 
1762  ASSERT (seq_geq (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
1763  ASSERT (data_len);
1764  written = session_enqueue_stream_connection (&tc->connection, b, 0,
1765  1 /* queue event */ , 1);
1766 
1767  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
1768 
1769  /* Update rcv_nxt */
1770  if (PREDICT_TRUE (written == data_len))
1771  {
1772  tc->rcv_nxt += written;
1773  }
1774  /* If more data written than expected, account for out-of-order bytes. */
1775  else if (written > data_len)
1776  {
1777  tc->rcv_nxt += written;
1778  TCP_EVT_DBG (TCP_EVT_CC_INPUT, tc, data_len, written);
1779  }
1780  else if (written > 0)
1781  {
1782  /* We've written something but FIFO is probably full now */
1783  tc->rcv_nxt += written;
1784  error = TCP_ERROR_PARTIALLY_ENQUEUED;
1785  }
1786  else
1787  {
1788  return TCP_ERROR_FIFO_FULL;
1789  }
1790 
1791  /* Update SACK list if need be */
1792  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1793  {
1794  /* Remove SACK blocks that have been delivered */
1795  tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1796  }
1797 
1798  return error;
1799 }
1800 
1801 /** Enqueue out-of-order data */
1802 static int
1804  u16 data_len)
1805 {
1806  stream_session_t *s0;
1807  int rv, offset;
1808 
1809  ASSERT (seq_gt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
1810  ASSERT (data_len);
1811 
1812  /* Enqueue out-of-order data with relative offset */
1813  rv = session_enqueue_stream_connection (&tc->connection, b,
1814  vnet_buffer (b)->tcp.seq_number -
1815  tc->rcv_nxt, 0 /* queue event */ ,
1816  0);
1817 
1818  /* Nothing written */
1819  if (rv)
1820  {
1821  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1822  return TCP_ERROR_FIFO_FULL;
1823  }
1824 
1825  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
1826 
1827  /* Update SACK list if in use */
1828  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1829  {
1830  ooo_segment_t *newest;
1831  u32 start, end;
1832 
1833  s0 = session_get (tc->c_s_index, tc->c_thread_index);
1834 
1835  /* Get the newest segment from the fifo */
1836  newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
1837  if (newest)
1838  {
1839  offset = ooo_segment_offset (s0->server_rx_fifo, newest);
1840  ASSERT (offset <= vnet_buffer (b)->tcp.seq_number - tc->rcv_nxt);
1841  start = tc->rcv_nxt + offset;
1842  end = start + ooo_segment_length (s0->server_rx_fifo, newest);
1843  tcp_update_sack_list (tc, start, end);
1844  svm_fifo_newest_ooo_segment_reset (s0->server_rx_fifo);
1845  TCP_EVT_DBG (TCP_EVT_CC_SACKS, tc);
1846  }
1847  }
1848 
1849  return TCP_ERROR_ENQUEUED_OOO;
1850 }
1851 
1852 /**
1853  * Check if ACK could be delayed. If ack can be delayed, it should return
1854  * true for a full frame. If we're always acking return 0.
1855  */
1856 always_inline int
1858 {
1859  /* Send ack if ... */
1860  if (TCP_ALWAYS_ACK
1861  /* just sent a rcv wnd 0 */
1862  || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1863  /* constrained to send ack */
1864  || (tc->flags & TCP_CONN_SNDACK) != 0
1865  /* we're almost out of tx wnd */
1866  || tcp_available_cc_snd_space (tc) < 4 * tc->snd_mss)
1867  return 0;
1868 
1869  return 1;
1870 }
1871 
1872 static int
1874 {
1875  u32 discard, first = b->current_length;
1877 
1878  /* Handle multi-buffer segments */
1879  if (n_bytes_to_drop > b->current_length)
1880  {
1881  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
1882  return -1;
1883  do
1884  {
1885  discard = clib_min (n_bytes_to_drop, b->current_length);
1886  vlib_buffer_advance (b, discard);
1887  b = vlib_get_buffer (vm, b->next_buffer);
1888  n_bytes_to_drop -= discard;
1889  }
1890  while (n_bytes_to_drop);
1891  if (n_bytes_to_drop > first)
1892  b->total_length_not_including_first_buffer -= n_bytes_to_drop - first;
1893  }
1894  else
1895  vlib_buffer_advance (b, n_bytes_to_drop);
1896  vnet_buffer (b)->tcp.data_len -= n_bytes_to_drop;
1897  return 0;
1898 }
1899 
1900 /**
1901  * Receive buffer for connection and handle acks
1902  *
1903  * It handles both in order or out-of-order data.
1904  */
1905 static int
1907  vlib_buffer_t * b)
1908 {
1909  u32 error, n_bytes_to_drop, n_data_bytes;
1910 
1911  vlib_buffer_advance (b, vnet_buffer (b)->tcp.data_offset);
1912  n_data_bytes = vnet_buffer (b)->tcp.data_len;
1913  ASSERT (n_data_bytes);
1914 
1915  /* Handle out-of-order data */
1916  if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1917  {
1918  /* Old sequence numbers allowed through because they overlapped
1919  * the rx window */
1920  if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
1921  {
1922  /* Completely in the past (possible retransmit). Ack
1923  * retransmissions since we may not have any data to send */
1924  if (seq_leq (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
1925  {
1926  tcp_program_ack (wrk, tc);
1927  error = TCP_ERROR_SEGMENT_OLD;
1928  goto done;
1929  }
1930 
1931  /* Chop off the bytes in the past and see if what is left
1932  * can be enqueued in order */
1933  n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
1934  n_data_bytes -= n_bytes_to_drop;
1935  vnet_buffer (b)->tcp.seq_number = tc->rcv_nxt;
1936  if (tcp_buffer_discard_bytes (b, n_bytes_to_drop))
1937  {
1938  error = TCP_ERROR_SEGMENT_OLD;
1939  goto done;
1940  }
1941  goto in_order;
1942  }
1943 
1944  /* RFC2581: Enqueue and send DUPACK for fast retransmit */
1945  error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1946  tcp_program_dupack (wrk, tc);
1947  TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc, vnet_buffer (b)->tcp);
1948  goto done;
1949  }
1950 
1951 in_order:
1952 
1953  /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1954  * segments can be enqueued after fifo tail offset changes. */
1955  error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1956  if (tcp_can_delack (tc))
1957  {
1958  if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1959  tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
1960  goto done;
1961  }
1962 
1963  tcp_program_ack (wrk, tc);
1964 
1965 done:
1966  return error;
1967 }
1968 
1969 typedef struct
1970 {
1973 } tcp_rx_trace_t;
1974 
1975 static u8 *
1976 format_tcp_rx_trace (u8 * s, va_list * args)
1977 {
1978  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1979  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1980  tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1981  u32 indent = format_get_indent (s);
1982 
1983  s = format (s, "%U\n%U%U",
1984  format_tcp_header, &t->tcp_header, 128,
1985  format_white_space, indent,
1987 
1988  return s;
1989 }
1990 
1991 static u8 *
1992 format_tcp_rx_trace_short (u8 * s, va_list * args)
1993 {
1994  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1995  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1996  tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1997 
1998  s = format (s, "%d -> %d (%U)",
1999  clib_net_to_host_u16 (t->tcp_header.dst_port),
2000  clib_net_to_host_u16 (t->tcp_header.src_port), format_tcp_state,
2001  t->tcp_connection.state);
2002 
2003  return s;
2004 }
2005 
2006 static void
2008  tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
2009 {
2010  if (tc0)
2011  {
2012  clib_memcpy_fast (&t0->tcp_connection, tc0,
2013  sizeof (t0->tcp_connection));
2014  }
2015  else
2016  {
2017  th0 = tcp_buffer_hdr (b0);
2018  }
2019  clib_memcpy_fast (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2020 }
2021 
2022 static void
2024  vlib_frame_t * frame, u8 is_ip4)
2025 {
2026  u32 *from, n_left;
2027 
2028  n_left = frame->n_vectors;
2029  from = vlib_frame_vector_args (frame);
2030 
2031  while (n_left >= 1)
2032  {
2033  tcp_connection_t *tc0;
2034  tcp_rx_trace_t *t0;
2035  tcp_header_t *th0;
2036  vlib_buffer_t *b0;
2037  u32 bi0;
2038 
2039  bi0 = from[0];
2040  b0 = vlib_get_buffer (vm, bi0);
2041 
2042  if (b0->flags & VLIB_BUFFER_IS_TRACED)
2043  {
2044  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2045  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2046  vm->thread_index);
2047  th0 = tcp_buffer_hdr (b0);
2048  tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
2049  }
2050 
2051  from += 1;
2052  n_left -= 1;
2053  }
2054 }
2055 
2056 always_inline void
2057 tcp_node_inc_counter_i (vlib_main_t * vm, u32 tcp4_node, u32 tcp6_node,
2058  u8 is_ip4, u32 evt, u32 val)
2059 {
2060  if (is_ip4)
2061  vlib_node_increment_counter (vm, tcp4_node, evt, val);
2062  else
2063  vlib_node_increment_counter (vm, tcp6_node, evt, val);
2064 }
2065 
2066 #define tcp_maybe_inc_counter(node_id, err, count) \
2067 { \
2068  if (next0 != tcp_next_drop (is_ip4)) \
2069  tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index, \
2070  tcp6_##node_id##_node.index, is_ip4, err, \
2071  1); \
2072 }
2073 #define tcp_inc_counter(node_id, err, count) \
2074  tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index, \
2075  tcp6_##node_id##_node.index, is_ip4, \
2076  err, count)
2077 #define tcp_maybe_inc_err_counter(cnts, err) \
2078 { \
2079  cnts[err] += (next0 != tcp_next_drop (is_ip4)); \
2080 }
2081 #define tcp_inc_err_counter(cnts, err, val) \
2082 { \
2083  cnts[err] += val; \
2084 }
2085 #define tcp_store_err_counters(node_id, cnts) \
2086 { \
2087  int i; \
2088  for (i = 0; i < TCP_N_ERROR; i++) \
2089  if (cnts[i]) \
2090  tcp_inc_counter(node_id, i, cnts[i]); \
2091 }
2092 
2093 
2096  vlib_frame_t * frame, int is_ip4)
2097 {
2098  u32 thread_index = vm->thread_index, errors = 0;
2099  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
2100  u32 n_left_from, *from, *first_buffer;
2101  u16 err_counters[TCP_N_ERROR] = { 0 };
2102 
2103  if (node->flags & VLIB_NODE_FLAG_TRACE)
2104  tcp_established_trace_frame (vm, node, frame, is_ip4);
2105 
2106  first_buffer = from = vlib_frame_vector_args (frame);
2107  n_left_from = frame->n_vectors;
2108 
2109  while (n_left_from > 0)
2110  {
2111  u32 bi0, error0 = TCP_ERROR_ACK_OK;
2112  vlib_buffer_t *b0;
2113  tcp_header_t *th0;
2114  tcp_connection_t *tc0;
2115 
2116  if (n_left_from > 1)
2117  {
2118  vlib_buffer_t *pb;
2119  pb = vlib_get_buffer (vm, from[1]);
2120  vlib_prefetch_buffer_header (pb, LOAD);
2121  CLIB_PREFETCH (pb->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2122  }
2123 
2124  bi0 = from[0];
2125  from += 1;
2126  n_left_from -= 1;
2127 
2128  b0 = vlib_get_buffer (vm, bi0);
2129  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2130  thread_index);
2131 
2132  if (PREDICT_FALSE (tc0 == 0))
2133  {
2134  error0 = TCP_ERROR_INVALID_CONNECTION;
2135  goto done;
2136  }
2137 
2138  th0 = tcp_buffer_hdr (b0);
2139 
2140  /* TODO header prediction fast path */
2141 
2142  /* 1-4: check SEQ, RST, SYN */
2143  if (PREDICT_FALSE (tcp_segment_validate (wrk, tc0, b0, th0, &error0)))
2144  {
2145  TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0, vnet_buffer (b0)->tcp);
2146  goto done;
2147  }
2148 
2149  /* 5: check the ACK field */
2150  if (PREDICT_FALSE (tcp_rcv_ack (wrk, tc0, b0, th0, &error0)))
2151  goto done;
2152 
2153  /* 6: check the URG bit TODO */
2154 
2155  /* 7: process the segment text */
2156  if (vnet_buffer (b0)->tcp.data_len)
2157  error0 = tcp_segment_rcv (wrk, tc0, b0);
2158 
2159  /* 8: check the FIN bit */
2160  if (PREDICT_FALSE (tcp_is_fin (th0)))
2161  tcp_rcv_fin (wrk, tc0, b0, &error0);
2162 
2163  done:
2164  tcp_inc_err_counter (err_counters, error0, 1);
2165  }
2166 
2168  thread_index);
2169  err_counters[TCP_ERROR_MSG_QUEUE_FULL] = errors;
2170  tcp_store_err_counters (established, err_counters);
2172  tcp_handle_disconnects (wrk);
2173  vlib_buffer_free (vm, first_buffer, frame->n_vectors);
2174 
2175  return frame->n_vectors;
2176 }
2177 
2178 static uword
2180  vlib_frame_t * from_frame)
2181 {
2182  return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2183 }
2184 
2185 static uword
2187  vlib_frame_t * from_frame)
2188 {
2189  return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2190 }
2191 
2192 /* *INDENT-OFF* */
2194 {
2195  .function = tcp4_established,
2196  .name = "tcp4-established",
2197  /* Takes a vector of packets. */
2198  .vector_size = sizeof (u32),
2199  .n_errors = TCP_N_ERROR,
2200  .error_strings = tcp_error_strings,
2201  .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
2202  .next_nodes =
2203  {
2204 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
2206 #undef _
2207  },
2208  .format_trace = format_tcp_rx_trace_short,
2209 };
2210 /* *INDENT-ON* */
2211 
2213 
2214 /* *INDENT-OFF* */
2216 {
2217  .function = tcp6_established,
2218  .name = "tcp6-established",
2219  /* Takes a vector of packets. */
2220  .vector_size = sizeof (u32),
2221  .n_errors = TCP_N_ERROR,
2222  .error_strings = tcp_error_strings,
2223  .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
2224  .next_nodes =
2225  {
2226 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
2228 #undef _
2229  },
2230  .format_trace = format_tcp_rx_trace_short,
2231 };
2232 /* *INDENT-ON* */
2233 
2234 
2236 
2239 
2240 static u8
2242 {
2243  transport_connection_t *tmp = 0;
2244  u64 handle;
2245 
2246  if (!tc)
2247  return 1;
2248 
2249  /* Proxy case */
2250  if (tc->c_lcl_port == 0 && tc->state == TCP_STATE_LISTEN)
2251  return 1;
2252 
2253  u8 is_valid = (tc->c_lcl_port == hdr->dst_port
2254  && (tc->state == TCP_STATE_LISTEN
2255  || tc->c_rmt_port == hdr->src_port));
2256 
2257  if (!is_valid)
2258  {
2259  handle = session_lookup_half_open_handle (&tc->connection);
2260  tmp = session_lookup_half_open_connection (handle & 0xFFFFFFFF,
2261  tc->c_proto, tc->c_is_ip4);
2262 
2263  if (tmp)
2264  {
2265  if (tmp->lcl_port == hdr->dst_port
2266  && tmp->rmt_port == hdr->src_port)
2267  {
2268  TCP_DBG ("half-open is valid!");
2269  }
2270  }
2271  }
2272  return is_valid;
2273 }
2274 
2275 /**
2276  * Lookup transport connection
2277  */
2278 static tcp_connection_t *
2279 tcp_lookup_connection (u32 fib_index, vlib_buffer_t * b, u8 thread_index,
2280  u8 is_ip4)
2281 {
2282  tcp_header_t *tcp;
2283  transport_connection_t *tconn;
2284  tcp_connection_t *tc;
2285  u8 is_filtered = 0;
2286  if (is_ip4)
2287  {
2288  ip4_header_t *ip4;
2289  ip4 = vlib_buffer_get_current (b);
2290  tcp = ip4_next_header (ip4);
2291  tconn = session_lookup_connection_wt4 (fib_index,
2292  &ip4->dst_address,
2293  &ip4->src_address,
2294  tcp->dst_port,
2295  tcp->src_port,
2297  thread_index, &is_filtered);
2298  tc = tcp_get_connection_from_transport (tconn);
2299  ASSERT (tcp_lookup_is_valid (tc, tcp));
2300  }
2301  else
2302  {
2303  ip6_header_t *ip6;
2304  ip6 = vlib_buffer_get_current (b);
2305  tcp = ip6_next_header (ip6);
2306  tconn = session_lookup_connection_wt6 (fib_index,
2307  &ip6->dst_address,
2308  &ip6->src_address,
2309  tcp->dst_port,
2310  tcp->src_port,
2312  thread_index, &is_filtered);
2313  tc = tcp_get_connection_from_transport (tconn);
2314  ASSERT (tcp_lookup_is_valid (tc, tcp));
2315  }
2316  return tc;
2317 }
2318 
2321  vlib_frame_t * from_frame, int is_ip4)
2322 {
2323  tcp_main_t *tm = vnet_get_tcp_main ();
2324  u32 n_left_from, *from, *first_buffer, errors = 0;
2325  u32 my_thread_index = vm->thread_index;
2326  tcp_worker_ctx_t *wrk = tcp_get_worker (my_thread_index);
2327 
2328  from = first_buffer = vlib_frame_vector_args (from_frame);
2329  n_left_from = from_frame->n_vectors;
2330 
2331  while (n_left_from > 0)
2332  {
2333  u32 bi0, ack0, seq0, error0 = TCP_ERROR_NONE;
2334  tcp_connection_t *tc0, *new_tc0;
2335  tcp_header_t *tcp0 = 0;
2336  tcp_rx_trace_t *t0;
2337  vlib_buffer_t *b0;
2338 
2339  bi0 = from[0];
2340  from += 1;
2341  n_left_from -= 1;
2342 
2343  b0 = vlib_get_buffer (vm, bi0);
2344  tc0 =
2345  tcp_half_open_connection_get (vnet_buffer (b0)->tcp.connection_index);
2346  if (PREDICT_FALSE (tc0 == 0))
2347  {
2348  error0 = TCP_ERROR_INVALID_CONNECTION;
2349  goto drop;
2350  }
2351 
2352  /* Half-open completed recently but the connection was't removed
2353  * yet by the owning thread */
2354  if (PREDICT_FALSE (tc0->flags & TCP_CONN_HALF_OPEN_DONE))
2355  {
2356  /* Make sure the connection actually exists */
2357  ASSERT (tcp_lookup_connection (tc0->c_fib_index, b0,
2358  my_thread_index, is_ip4));
2359  goto drop;
2360  }
2361 
2362  ack0 = vnet_buffer (b0)->tcp.ack_number;
2363  seq0 = vnet_buffer (b0)->tcp.seq_number;
2364  tcp0 = tcp_buffer_hdr (b0);
2365 
2366  /* Crude check to see if the connection handle does not match
2367  * the packet. Probably connection just switched to established */
2368  if (PREDICT_FALSE (tcp0->dst_port != tc0->c_lcl_port
2369  || tcp0->src_port != tc0->c_rmt_port))
2370  {
2371  error0 = TCP_ERROR_INVALID_CONNECTION;
2372  goto drop;
2373  }
2374 
2375  if (PREDICT_FALSE (!tcp_ack (tcp0) && !tcp_rst (tcp0)
2376  && !tcp_syn (tcp0)))
2377  {
2378  error0 = TCP_ERROR_SEGMENT_INVALID;
2379  goto drop;
2380  }
2381 
2382  /* SYNs consume sequence numbers */
2383  vnet_buffer (b0)->tcp.seq_end += tcp_is_syn (tcp0);
2384 
2385  /*
2386  * 1. check the ACK bit
2387  */
2388 
2389  /*
2390  * If the ACK bit is set
2391  * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
2392  * the RST bit is set, if so drop the segment and return)
2393  * <SEQ=SEG.ACK><CTL=RST>
2394  * and discard the segment. Return.
2395  * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
2396  */
2397  if (tcp_ack (tcp0))
2398  {
2399  if (seq_leq (ack0, tc0->iss) || seq_gt (ack0, tc0->snd_nxt))
2400  {
2401  if (!tcp_rst (tcp0))
2402  tcp_send_reset_w_pkt (tc0, b0, my_thread_index, is_ip4);
2403  error0 = TCP_ERROR_RCV_WND;
2404  goto drop;
2405  }
2406 
2407  /* Make sure ACK is valid */
2408  if (seq_gt (tc0->snd_una, ack0))
2409  {
2410  error0 = TCP_ERROR_ACK_INVALID;
2411  goto drop;
2412  }
2413  }
2414 
2415  /*
2416  * 2. check the RST bit
2417  */
2418 
2419  if (tcp_rst (tcp0))
2420  {
2421  /* If ACK is acceptable, signal client that peer is not
2422  * willing to accept connection and drop connection*/
2423  if (tcp_ack (tcp0))
2424  tcp_connection_reset (tc0);
2425  error0 = TCP_ERROR_RST_RCVD;
2426  goto drop;
2427  }
2428 
2429  /*
2430  * 3. check the security and precedence (skipped)
2431  */
2432 
2433  /*
2434  * 4. check the SYN bit
2435  */
2436 
2437  /* No SYN flag. Drop. */
2438  if (!tcp_syn (tcp0))
2439  {
2440  clib_warning ("not synack");
2441  error0 = TCP_ERROR_SEGMENT_INVALID;
2442  goto drop;
2443  }
2444 
2445  /* Parse options */
2446  if (tcp_options_parse (tcp0, &tc0->rcv_opts, 1))
2447  {
2448  clib_warning ("options parse fail");
2449  error0 = TCP_ERROR_OPTIONS;
2450  goto drop;
2451  }
2452 
2453  /* Valid SYN or SYN-ACK. Move connection from half-open pool to
2454  * current thread pool. */
2455  pool_get (tm->connections[my_thread_index], new_tc0);
2456  clib_memcpy_fast (new_tc0, tc0, sizeof (*new_tc0));
2457  new_tc0->c_c_index = new_tc0 - tm->connections[my_thread_index];
2458  new_tc0->c_thread_index = my_thread_index;
2459  new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
2460  new_tc0->irs = seq0;
2461  new_tc0->timers[TCP_TIMER_ESTABLISH_AO] = TCP_TIMER_HANDLE_INVALID;
2462  new_tc0->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
2463  new_tc0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2464 
2465  /* If this is not the owning thread, wait for syn retransmit to
2466  * expire and cleanup then */
2468  tc0->flags |= TCP_CONN_HALF_OPEN_DONE;
2469 
2470  if (tcp_opts_tstamp (&new_tc0->rcv_opts))
2471  {
2472  new_tc0->tsval_recent = new_tc0->rcv_opts.tsval;
2473  new_tc0->tsval_recent_age = tcp_time_now ();
2474  }
2475 
2476  if (tcp_opts_wscale (&new_tc0->rcv_opts))
2477  new_tc0->snd_wscale = new_tc0->rcv_opts.wscale;
2478  else
2479  new_tc0->rcv_wscale = 0;
2480 
2481  new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
2482  << new_tc0->snd_wscale;
2483  new_tc0->snd_wl1 = seq0;
2484  new_tc0->snd_wl2 = ack0;
2485 
2486  tcp_connection_init_vars (new_tc0);
2487 
2488  /* SYN-ACK: See if we can switch to ESTABLISHED state */
2489  if (PREDICT_TRUE (tcp_ack (tcp0)))
2490  {
2491  /* Our SYN is ACKed: we have iss < ack = snd_una */
2492 
2493  /* TODO Dequeue acknowledged segments if we support Fast Open */
2494  new_tc0->snd_una = ack0;
2495  new_tc0->state = TCP_STATE_ESTABLISHED;
2496 
2497  /* Make sure las is initialized for the wnd computation */
2498  new_tc0->rcv_las = new_tc0->rcv_nxt;
2499 
2500  /* Notify app that we have connection. If session layer can't
2501  * allocate session send reset */
2502  if (session_stream_connect_notify (&new_tc0->connection, 0))
2503  {
2504  clib_warning ("connect notify fail");
2505  tcp_send_reset_w_pkt (new_tc0, b0, my_thread_index, is_ip4);
2506  tcp_connection_cleanup (new_tc0);
2507  goto drop;
2508  }
2509 
2510  new_tc0->tx_fifo_size =
2511  transport_tx_fifo_size (&new_tc0->connection);
2512  /* Update rtt with the syn-ack sample */
2513  tcp_estimate_initial_rtt (new_tc0);
2514  TCP_EVT_DBG (TCP_EVT_SYNACK_RCVD, new_tc0);
2515  error0 = TCP_ERROR_SYN_ACKS_RCVD;
2516  }
2517  /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
2518  else
2519  {
2520  new_tc0->state = TCP_STATE_SYN_RCVD;
2521 
2522  /* Notify app that we have connection */
2523  if (session_stream_connect_notify (&new_tc0->connection, 0))
2524  {
2525  tcp_connection_cleanup (new_tc0);
2526  tcp_send_reset_w_pkt (tc0, b0, my_thread_index, is_ip4);
2527  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc0);
2528  goto drop;
2529  }
2530 
2531  new_tc0->tx_fifo_size =
2532  transport_tx_fifo_size (&new_tc0->connection);
2533  new_tc0->rtt_ts = 0;
2534  tcp_init_snd_vars (new_tc0);
2535  tcp_send_synack (new_tc0);
2536  error0 = TCP_ERROR_SYNS_RCVD;
2537  goto drop;
2538  }
2539 
2540  /* Read data, if any */
2541  if (PREDICT_FALSE (vnet_buffer (b0)->tcp.data_len))
2542  {
2543  clib_warning ("rcvd data in syn-sent");
2544  error0 = tcp_segment_rcv (wrk, new_tc0, b0);
2545  if (error0 == TCP_ERROR_ACK_OK)
2546  error0 = TCP_ERROR_SYN_ACKS_RCVD;
2547  }
2548  else
2549  {
2550  tcp_program_ack (wrk, new_tc0);
2551  }
2552 
2553  drop:
2554 
2555  tcp_inc_counter (syn_sent, error0, 1);
2556  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED) && tcp0 != 0))
2557  {
2558  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2559  clib_memcpy_fast (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
2560  clib_memcpy_fast (&t0->tcp_connection, tc0,
2561  sizeof (t0->tcp_connection));
2562  }
2563  }
2564 
2566  my_thread_index);
2567  tcp_inc_counter (syn_sent, TCP_ERROR_MSG_QUEUE_FULL, errors);
2568  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
2569 
2570  return from_frame->n_vectors;
2571 }
2572 
2573 static uword
2575  vlib_frame_t * from_frame)
2576 {
2577  return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2578 }
2579 
2580 static uword
2582  vlib_frame_t * from_frame)
2583 {
2584  return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2585 }
2586 
2587 /* *INDENT-OFF* */
2589 {
2590  .function = tcp4_syn_sent,
2591  .name = "tcp4-syn-sent",
2592  /* Takes a vector of packets. */
2593  .vector_size = sizeof (u32),
2594  .n_errors = TCP_N_ERROR,
2595  .error_strings = tcp_error_strings,
2596  .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2597  .next_nodes =
2598  {
2599 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2601 #undef _
2602  },
2603  .format_trace = format_tcp_rx_trace_short,
2604 };
2605 /* *INDENT-ON* */
2606 
2608 
2609 /* *INDENT-OFF* */
2611 {
2612  .function = tcp6_syn_sent_rcv,
2613  .name = "tcp6-syn-sent",
2614  /* Takes a vector of packets. */
2615  .vector_size = sizeof (u32),
2616  .n_errors = TCP_N_ERROR,
2617  .error_strings = tcp_error_strings,
2618  .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2619  .next_nodes =
2620  {
2621 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2623 #undef _
2624  },
2625  .format_trace = format_tcp_rx_trace_short,
2626 };
2627 /* *INDENT-ON* */
2628 
2630 
2633 
2634 /**
2635  * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
2636  * as per RFC793 p. 64
2637  */
2640  vlib_frame_t * from_frame, int is_ip4)
2641 {
2642  u32 thread_index = vm->thread_index, errors = 0, *first_buffer;
2643  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
2644  u32 n_left_from, *from, max_dequeue;
2645 
2646  from = first_buffer = vlib_frame_vector_args (from_frame);
2647  n_left_from = from_frame->n_vectors;
2648 
2649  while (n_left_from > 0)
2650  {
2651  u32 bi0, error0 = TCP_ERROR_NONE;
2652  tcp_header_t *tcp0 = 0;
2653  tcp_connection_t *tc0;
2654  vlib_buffer_t *b0;
2655  u8 is_fin0;
2656 
2657  bi0 = from[0];
2658  from += 1;
2659  n_left_from -= 1;
2660 
2661  b0 = vlib_get_buffer (vm, bi0);
2662  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2663  thread_index);
2664  if (PREDICT_FALSE (tc0 == 0))
2665  {
2666  error0 = TCP_ERROR_INVALID_CONNECTION;
2667  goto drop;
2668  }
2669 
2670  tcp0 = tcp_buffer_hdr (b0);
2671  is_fin0 = tcp_is_fin (tcp0);
2672 
2673  if (CLIB_DEBUG)
2674  {
2675  tcp_connection_t *tmp;
2676  tmp = tcp_lookup_connection (tc0->c_fib_index, b0, thread_index,
2677  is_ip4);
2678  if (tmp->state != tc0->state)
2679  {
2680  if (tc0->state != TCP_STATE_CLOSED)
2681  clib_warning ("state changed");
2682  goto drop;
2683  }
2684  }
2685 
2686  /*
2687  * Special treatment for CLOSED
2688  */
2689  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
2690  {
2691  error0 = TCP_ERROR_CONNECTION_CLOSED;
2692  goto drop;
2693  }
2694 
2695  /*
2696  * For all other states (except LISTEN)
2697  */
2698 
2699  /* 1-4: check SEQ, RST, SYN */
2700  if (PREDICT_FALSE (tcp_segment_validate (wrk, tc0, b0, tcp0, &error0)))
2701  goto drop;
2702 
2703  /* 5: check the ACK field */
2704  switch (tc0->state)
2705  {
2706  case TCP_STATE_SYN_RCVD:
2707  /*
2708  * If the segment acknowledgment is not acceptable, form a
2709  * reset segment,
2710  * <SEQ=SEG.ACK><CTL=RST>
2711  * and send it.
2712  */
2713  if (!tcp_rcv_ack_is_acceptable (tc0, b0))
2714  {
2715  tcp_connection_reset (tc0);
2716  error0 = TCP_ERROR_ACK_INVALID;
2717  goto drop;
2718  }
2719 
2720  /* Make sure the ack is exactly right */
2721  if (tc0->rcv_nxt != vnet_buffer (b0)->tcp.seq_number || is_fin0)
2722  {
2723  tcp_connection_reset (tc0);
2724  error0 = TCP_ERROR_SEGMENT_INVALID;
2725  goto drop;
2726  }
2727 
2728  /* Update rtt and rto */
2730 
2731  /* Switch state to ESTABLISHED */
2732  tc0->state = TCP_STATE_ESTABLISHED;
2733  TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc0);
2734 
2735  /* Initialize session variables */
2736  tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
2737  tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
2738  << tc0->rcv_opts.wscale;
2739  tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2740  tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2741 
2742  /* Reset SYN-ACK retransmit and SYN_RCV establish timers */
2744  tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
2745  if (stream_session_accept_notify (&tc0->connection))
2746  {
2747  error0 = TCP_ERROR_MSG_QUEUE_FULL;
2748  tcp_connection_reset (tc0);
2749  goto drop;
2750  }
2751  error0 = TCP_ERROR_ACK_OK;
2752  break;
2753  case TCP_STATE_ESTABLISHED:
2754  /* We can get packets in established state here because they
2755  * were enqueued before state change */
2756  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2757  goto drop;
2758 
2759  break;
2760  case TCP_STATE_FIN_WAIT_1:
2761  /* In addition to the processing for the ESTABLISHED state, if
2762  * our FIN is now acknowledged then enter FIN-WAIT-2 and
2763  * continue processing in that state. */
2764  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2765  goto drop;
2766 
2767  /* Still have to send the FIN */
2768  if (tc0->flags & TCP_CONN_FINPNDG)
2769  {
2770  /* TX fifo finally drained */
2771  max_dequeue = session_tx_fifo_max_dequeue (&tc0->connection);
2772  if (max_dequeue <= tc0->burst_acked)
2773  tcp_send_fin (tc0);
2774  }
2775  /* If FIN is ACKed */
2776  else if (tc0->snd_una == tc0->snd_una_max)
2777  {
2778  tcp_connection_set_state (tc0, TCP_STATE_FIN_WAIT_2);
2779 
2780  /* Stop all retransmit timers because we have nothing more
2781  * to send. Enable waitclose though because we're willing to
2782  * wait for peer's FIN but not indefinitely. */
2784  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2785 
2786  /* Don't try to deq the FIN acked */
2787  if (tc0->burst_acked > 1)
2788  stream_session_dequeue_drop (&tc0->connection,
2789  tc0->burst_acked - 1);
2790  tc0->burst_acked = 0;
2791  }
2792  break;
2793  case TCP_STATE_FIN_WAIT_2:
2794  /* In addition to the processing for the ESTABLISHED state, if
2795  * the retransmission queue is empty, the user's CLOSE can be
2796  * acknowledged ("ok") but do not delete the TCB. */
2797  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2798  goto drop;
2799  tc0->burst_acked = 0;
2800  break;
2801  case TCP_STATE_CLOSE_WAIT:
2802  /* Do the same processing as for the ESTABLISHED state. */
2803  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2804  goto drop;
2805 
2806  if (tc0->flags & TCP_CONN_FINPNDG)
2807  {
2808  /* TX fifo finally drained */
2809  if (!session_tx_fifo_max_dequeue (&tc0->connection))
2810  {
2811  tcp_send_fin (tc0);
2813  tcp_connection_set_state (tc0, TCP_STATE_LAST_ACK);
2814  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2815  }
2816  }
2817  break;
2818  case TCP_STATE_CLOSING:
2819  /* In addition to the processing for the ESTABLISHED state, if
2820  * the ACK acknowledges our FIN then enter the TIME-WAIT state,
2821  * otherwise ignore the segment. */
2822  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2823  goto drop;
2824 
2826  tcp_connection_set_state (tc0, TCP_STATE_TIME_WAIT);
2827  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2828  goto drop;
2829 
2830  break;
2831  case TCP_STATE_LAST_ACK:
2832  /* The only thing that [should] arrive in this state is an
2833  * acknowledgment of our FIN. If our FIN is now acknowledged,
2834  * delete the TCB, enter the CLOSED state, and return. */
2835 
2836  if (!tcp_rcv_ack_is_acceptable (tc0, b0))
2837  {
2838  error0 = TCP_ERROR_ACK_INVALID;
2839  goto drop;
2840  }
2841  error0 = TCP_ERROR_ACK_OK;
2842  tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
2843  /* Apparently our ACK for the peer's FIN was lost */
2844  if (is_fin0 && tc0->snd_una != tc0->snd_una_max)
2845  {
2846  tcp_send_fin (tc0);
2847  goto drop;
2848  }
2849 
2850  tcp_connection_set_state (tc0, TCP_STATE_CLOSED);
2851 
2852  /* Don't free the connection from the data path since
2853  * we can't ensure that we have no packets already enqueued
2854  * to output. Rely instead on the waitclose timer */
2856  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
2857 
2858  goto drop;
2859 
2860  break;
2861  case TCP_STATE_TIME_WAIT:
2862  /* The only thing that can arrive in this state is a
2863  * retransmission of the remote FIN. Acknowledge it, and restart
2864  * the 2 MSL timeout. */
2865 
2866  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2867  goto drop;
2868 
2869  tcp_program_ack (wrk, tc0);
2870  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2871  goto drop;
2872 
2873  break;
2874  default:
2875  ASSERT (0);
2876  }
2877 
2878  /* 6: check the URG bit TODO */
2879 
2880  /* 7: process the segment text */
2881  switch (tc0->state)
2882  {
2883  case TCP_STATE_ESTABLISHED:
2884  case TCP_STATE_FIN_WAIT_1:
2885  case TCP_STATE_FIN_WAIT_2:
2886  if (vnet_buffer (b0)->tcp.data_len)
2887  error0 = tcp_segment_rcv (wrk, tc0, b0);
2888  break;
2889  case TCP_STATE_CLOSE_WAIT:
2890  case TCP_STATE_CLOSING:
2891  case TCP_STATE_LAST_ACK:
2892  case TCP_STATE_TIME_WAIT:
2893  /* This should not occur, since a FIN has been received from the
2894  * remote side. Ignore the segment text. */
2895  break;
2896  }
2897 
2898  /* 8: check the FIN bit */
2899  if (!is_fin0)
2900  goto drop;
2901 
2902  TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
2903 
2904  switch (tc0->state)
2905  {
2906  case TCP_STATE_ESTABLISHED:
2907  /* Account for the FIN and send ack */
2908  tc0->rcv_nxt += 1;
2909  tcp_program_ack (wrk, tc0);
2910  tcp_connection_set_state (tc0, TCP_STATE_CLOSE_WAIT);
2911  tcp_program_disconnect (wrk, tc0);
2912  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
2913  break;
2914  case TCP_STATE_SYN_RCVD:
2915  /* Send FIN-ACK, enter LAST-ACK and because the app was not
2916  * notified yet, set a cleanup timer instead of relying on
2917  * disconnect notify and the implicit close call. */
2919  tc0->rcv_nxt += 1;
2920  tcp_send_fin (tc0);
2921  tcp_connection_set_state (tc0, TCP_STATE_LAST_ACK);
2922  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2923  break;
2924  case TCP_STATE_CLOSE_WAIT:
2925  case TCP_STATE_CLOSING:
2926  case TCP_STATE_LAST_ACK:
2927  /* move along .. */
2928  break;
2929  case TCP_STATE_FIN_WAIT_1:
2930  tc0->rcv_nxt += 1;
2931  tcp_connection_set_state (tc0, TCP_STATE_CLOSING);
2932  tcp_program_ack (wrk, tc0);
2933  /* Wait for ACK but not forever */
2934  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2935  break;
2936  case TCP_STATE_FIN_WAIT_2:
2937  /* Got FIN, send ACK! Be more aggressive with resource cleanup */
2938  tc0->rcv_nxt += 1;
2939  tcp_connection_set_state (tc0, TCP_STATE_TIME_WAIT);
2941  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2942  tcp_program_ack (wrk, tc0);
2943  break;
2944  case TCP_STATE_TIME_WAIT:
2945  /* Remain in the TIME-WAIT state. Restart the time-wait
2946  * timeout.
2947  */
2948  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2949  break;
2950  }
2951  error0 = TCP_ERROR_FIN_RCVD;
2952 
2953  drop:
2954 
2955  tcp_inc_counter (rcv_process, error0, 1);
2956  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2957  {
2958  tcp_rx_trace_t *t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2959  tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
2960  }
2961  }
2962 
2964  thread_index);
2965  tcp_inc_counter (rcv_process, TCP_ERROR_MSG_QUEUE_FULL, errors);
2967  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
2968 
2969  return from_frame->n_vectors;
2970 }
2971 
2972 static uword
2974  vlib_frame_t * from_frame)
2975 {
2976  return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2977 }
2978 
2979 static uword
2981  vlib_frame_t * from_frame)
2982 {
2983  return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2984 }
2985 
2986 /* *INDENT-OFF* */
2988 {
2989  .function = tcp4_rcv_process,
2990  .name = "tcp4-rcv-process",
2991  /* Takes a vector of packets. */
2992  .vector_size = sizeof (u32),
2993  .n_errors = TCP_N_ERROR,
2994  .error_strings = tcp_error_strings,
2995  .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2996  .next_nodes =
2997  {
2998 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
3000 #undef _
3001  },
3002  .format_trace = format_tcp_rx_trace_short,
3003 };
3004 /* *INDENT-ON* */
3005 
3007 
3008 /* *INDENT-OFF* */
3010 {
3011  .function = tcp6_rcv_process,
3012  .name = "tcp6-rcv-process",
3013  /* Takes a vector of packets. */
3014  .vector_size = sizeof (u32),
3015  .n_errors = TCP_N_ERROR,
3016  .error_strings = tcp_error_strings,
3017  .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
3018  .next_nodes =
3019  {
3020 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
3022 #undef _
3023  },
3024  .format_trace = format_tcp_rx_trace_short,
3025 };
3026 /* *INDENT-ON* */
3027 
3029 
3032 
3033 /**
3034  * LISTEN state processing as per RFC 793 p. 65
3035  */
3038  vlib_frame_t * from_frame, int is_ip4)
3039 {
3040  u32 n_left_from, *from, n_syns = 0, *first_buffer;
3041  u32 my_thread_index = vm->thread_index;
3042 
3043  from = first_buffer = vlib_frame_vector_args (from_frame);
3044  n_left_from = from_frame->n_vectors;
3045 
3046  while (n_left_from > 0)
3047  {
3048  u32 bi0;
3049  vlib_buffer_t *b0;
3050  tcp_rx_trace_t *t0;
3051  tcp_header_t *th0 = 0;
3052  tcp_connection_t *lc0;
3053  ip4_header_t *ip40;
3054  ip6_header_t *ip60;
3055  tcp_connection_t *child0;
3056  u32 error0 = TCP_ERROR_NONE;
3057 
3058  bi0 = from[0];
3059  from += 1;
3060  n_left_from -= 1;
3061 
3062  b0 = vlib_get_buffer (vm, bi0);
3063  lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
3064 
3065  if (is_ip4)
3066  {
3067  ip40 = vlib_buffer_get_current (b0);
3068  th0 = ip4_next_header (ip40);
3069  }
3070  else
3071  {
3072  ip60 = vlib_buffer_get_current (b0);
3073  th0 = ip6_next_header (ip60);
3074  }
3075 
3076  /* Create child session. For syn-flood protection use filter */
3077 
3078  /* 1. first check for an RST: handled in dispatch */
3079  /* if (tcp_rst (th0))
3080  goto drop;
3081  */
3082 
3083  /* 2. second check for an ACK: handled in dispatch */
3084  /* if (tcp_ack (th0))
3085  {
3086  tcp_send_reset (b0, is_ip4);
3087  goto drop;
3088  }
3089  */
3090 
3091  /* 3. check for a SYN (did that already) */
3092 
3093  /* Make sure connection wasn't just created */
3094  child0 = tcp_lookup_connection (lc0->c_fib_index, b0, my_thread_index,
3095  is_ip4);
3096  if (PREDICT_FALSE (child0->state != TCP_STATE_LISTEN))
3097  {
3098  error0 = TCP_ERROR_CREATE_EXISTS;
3099  goto drop;
3100  }
3101 
3102  /* Create child session and send SYN-ACK */
3103  child0 = tcp_connection_alloc (my_thread_index);
3104  child0->c_lcl_port = th0->dst_port;
3105  child0->c_rmt_port = th0->src_port;
3106  child0->c_is_ip4 = is_ip4;
3107  child0->state = TCP_STATE_SYN_RCVD;
3108  child0->c_fib_index = lc0->c_fib_index;
3109 
3110  if (is_ip4)
3111  {
3112  child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
3113  child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
3114  }
3115  else
3116  {
3117  clib_memcpy_fast (&child0->c_lcl_ip6, &ip60->dst_address,
3118  sizeof (ip6_address_t));
3119  clib_memcpy_fast (&child0->c_rmt_ip6, &ip60->src_address,
3120  sizeof (ip6_address_t));
3121  }
3122 
3123  if (tcp_options_parse (th0, &child0->rcv_opts, 1))
3124  {
3125  error0 = TCP_ERROR_OPTIONS;
3126  tcp_connection_free (child0);
3127  goto drop;
3128  }
3129 
3130  child0->irs = vnet_buffer (b0)->tcp.seq_number;
3131  child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
3132  child0->rcv_las = child0->rcv_nxt;
3133  child0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3134 
3135  /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
3136  * segments are used to initialize PAWS. */
3137  if (tcp_opts_tstamp (&child0->rcv_opts))
3138  {
3139  child0->tsval_recent = child0->rcv_opts.tsval;
3140  child0->tsval_recent_age = tcp_time_now ();
3141  }
3142 
3143  if (tcp_opts_wscale (&child0->rcv_opts))
3144  child0->snd_wscale = child0->rcv_opts.wscale;
3145 
3146  child0->snd_wnd = clib_net_to_host_u16 (th0->window)
3147  << child0->snd_wscale;
3148  child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
3149  child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
3150 
3151  tcp_connection_init_vars (child0);
3152  child0->rto = TCP_RTO_MIN;
3153  TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0, 1);
3154 
3155  if (stream_session_accept (&child0->connection, lc0->c_s_index,
3156  0 /* notify */ ))
3157  {
3158  tcp_connection_cleanup (child0);
3159  error0 = TCP_ERROR_CREATE_SESSION_FAIL;
3160  goto drop;
3161  }
3162 
3163  child0->tx_fifo_size = transport_tx_fifo_size (&child0->connection);
3164  tcp_send_synack (child0);
3165  tcp_timer_set (child0, TCP_TIMER_ESTABLISH, TCP_SYN_RCVD_TIME);
3166 
3167  drop:
3168 
3169  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3170  {
3171  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
3172  clib_memcpy_fast (&t0->tcp_header, th0, sizeof (t0->tcp_header));
3173  clib_memcpy_fast (&t0->tcp_connection, lc0,
3174  sizeof (t0->tcp_connection));
3175  }
3176 
3177  n_syns += (error0 == TCP_ERROR_NONE);
3178  }
3179 
3180  tcp_inc_counter (listen, TCP_ERROR_SYNS_RCVD, n_syns);
3181  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
3182 
3183  return from_frame->n_vectors;
3184 }
3185 
3186 static uword
3188  vlib_frame_t * from_frame)
3189 {
3190  return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
3191 }
3192 
3193 static uword
3195  vlib_frame_t * from_frame)
3196 {
3197  return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
3198 }
3199 
3200 /* *INDENT-OFF* */
3202 {
3203  .function = tcp4_listen,
3204  .name = "tcp4-listen",
3205  /* Takes a vector of packets. */
3206  .vector_size = sizeof (u32),
3207  .n_errors = TCP_N_ERROR,
3208  .error_strings = tcp_error_strings,
3209  .n_next_nodes = TCP_LISTEN_N_NEXT,
3210  .next_nodes =
3211  {
3212 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
3214 #undef _
3215  },
3216  .format_trace = format_tcp_rx_trace_short,
3217 };
3218 /* *INDENT-ON* */
3219 
3221 
3222 /* *INDENT-OFF* */
3224 {
3225  .function = tcp6_listen,
3226  .name = "tcp6-listen",
3227  /* Takes a vector of packets. */
3228  .vector_size = sizeof (u32),
3229  .n_errors = TCP_N_ERROR,
3230  .error_strings = tcp_error_strings,
3231  .n_next_nodes = TCP_LISTEN_N_NEXT,
3232  .next_nodes =
3233  {
3234 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
3236 #undef _
3237  },
3238  .format_trace = format_tcp_rx_trace_short,
3239 };
3240 /* *INDENT-ON* */
3241 
3243 
3246 
3247 typedef enum _tcp_input_next
3248 {
3258 
3259 #define foreach_tcp4_input_next \
3260  _ (DROP, "ip4-drop") \
3261  _ (LISTEN, "tcp4-listen") \
3262  _ (RCV_PROCESS, "tcp4-rcv-process") \
3263  _ (SYN_SENT, "tcp4-syn-sent") \
3264  _ (ESTABLISHED, "tcp4-established") \
3265  _ (RESET, "tcp4-reset") \
3266  _ (PUNT, "ip4-punt")
3267 
3268 #define foreach_tcp6_input_next \
3269  _ (DROP, "ip6-drop") \
3270  _ (LISTEN, "tcp6-listen") \
3271  _ (RCV_PROCESS, "tcp6-rcv-process") \
3272  _ (SYN_SENT, "tcp6-syn-sent") \
3273  _ (ESTABLISHED, "tcp6-established") \
3274  _ (RESET, "tcp6-reset") \
3275  _ (PUNT, "ip6-punt")
3276 
3277 #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
3278 
3279 static void
3281  vlib_buffer_t ** bs, u32 n_bufs, u8 is_ip4)
3282 {
3283  tcp_connection_t *tc;
3284  tcp_header_t *tcp;
3285  tcp_rx_trace_t *t;
3286  int i;
3287 
3288  for (i = 0; i < n_bufs; i++)
3289  {
3290  if (bs[i]->flags & VLIB_BUFFER_IS_TRACED)
3291  {
3292  t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
3293  tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
3294  vm->thread_index);
3295  tcp = vlib_buffer_get_current (bs[i]);
3296  tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
3297  }
3298  }
3299 }
3300 
3301 static void
3302 tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
3303 {
3304  if (*error == TCP_ERROR_FILTERED || *error == TCP_ERROR_WRONG_THREAD)
3305  {
3306  *next = TCP_INPUT_NEXT_DROP;
3307  }
3308  else if ((is_ip4 && tm->punt_unknown4) || (!is_ip4 && tm->punt_unknown6))
3309  {
3310  *next = TCP_INPUT_NEXT_PUNT;
3311  *error = TCP_ERROR_PUNT;
3312  }
3313  else
3314  {
3315  *next = TCP_INPUT_NEXT_RESET;
3316  *error = TCP_ERROR_NO_LISTENER;
3317  }
3318 }
3319 
3320 static inline tcp_connection_t *
3321 tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
3322  u8 is_ip4)
3323 {
3324  u32 fib_index = vnet_buffer (b)->ip.fib_index;
3325  int n_advance_bytes, n_data_bytes;
3327  tcp_header_t *tcp;
3328  u8 result = 0;
3329 
3330  if (is_ip4)
3331  {
3333  int ip_hdr_bytes = ip4_header_bytes (ip4);
3334  if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
3335  {
3336  *error = TCP_ERROR_LENGTH;
3337  return 0;
3338  }
3339  tcp = ip4_next_header (ip4);
3340  vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
3341  n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
3342  n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
3343 
3344  /* Length check. Checksum computed by ipx_local no need to compute again */
3345  if (PREDICT_FALSE (n_data_bytes < 0))
3346  {
3347  *error = TCP_ERROR_LENGTH;
3348  return 0;
3349  }
3350 
3351  tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
3352  &ip4->src_address, tcp->dst_port,
3353  tcp->src_port, TRANSPORT_PROTO_TCP,
3354  thread_index, &result);
3355  }
3356  else
3357  {
3359  if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
3360  {
3361  *error = TCP_ERROR_LENGTH;
3362  return 0;
3363  }
3364  tcp = ip6_next_header (ip6);
3365  vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
3366  n_advance_bytes = tcp_header_bytes (tcp);
3367  n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
3368  - n_advance_bytes;
3369  n_advance_bytes += sizeof (ip6[0]);
3370 
3371  if (PREDICT_FALSE (n_data_bytes < 0))
3372  {
3373  *error = TCP_ERROR_LENGTH;
3374  return 0;
3375  }
3376 
3377  tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
3378  &ip6->src_address, tcp->dst_port,
3379  tcp->src_port, TRANSPORT_PROTO_TCP,
3380  thread_index, &result);
3381  }
3382 
3383  vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
3384  vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
3385  vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
3386  vnet_buffer (b)->tcp.data_len = n_data_bytes;
3387  vnet_buffer (b)->tcp.seq_end = vnet_buffer (b)->tcp.seq_number
3388  + n_data_bytes;
3389  vnet_buffer (b)->tcp.flags = 0;
3390 
3391  *error = result ? TCP_ERROR_NONE + result : *error;
3392 
3394 }
3395 
3396 static inline void
3398  vlib_buffer_t * b, u16 * next, u32 * error)
3399 {
3400  tcp_header_t *tcp;
3401  u8 flags;
3402 
3403  tcp = tcp_buffer_hdr (b);
3404  flags = tcp->flags & filter_flags;
3405  *next = tm->dispatch_table[tc->state][flags].next;
3406  *error = tm->dispatch_table[tc->state][flags].error;
3407 
3408  if (PREDICT_FALSE (*error == TCP_ERROR_DISPATCH
3409  || *next == TCP_INPUT_NEXT_RESET))
3410  {
3411  /* Overload tcp flags to store state */
3412  tcp_state_t state = tc->state;
3413  vnet_buffer (b)->tcp.flags = tc->state;
3414 
3415  if (*error == TCP_ERROR_DISPATCH)
3416  clib_warning ("tcp conn %u disp error state %U flags %U",
3417  tc->c_c_index, format_tcp_state, state,
3418  format_tcp_flags, (int) flags);
3419  }
3420 }
3421 
3424  vlib_frame_t * frame, int is_ip4)
3425 {
3426  u32 n_left_from, *from, thread_index = vm->thread_index;
3427  tcp_main_t *tm = vnet_get_tcp_main ();
3428  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
3429  u16 nexts[VLIB_FRAME_SIZE], *next;
3430 
3431  tcp_set_time_now (tcp_get_worker (thread_index));
3432 
3433  from = vlib_frame_vector_args (frame);
3434  n_left_from = frame->n_vectors;
3435  vlib_get_buffers (vm, from, bufs, n_left_from);
3436 
3437  b = bufs;
3438  next = nexts;
3439 
3440  while (n_left_from >= 4)
3441  {
3442  u32 error0 = TCP_ERROR_NO_LISTENER, error1 = TCP_ERROR_NO_LISTENER;
3443  tcp_connection_t *tc0, *tc1;
3444 
3445  {
3446  vlib_prefetch_buffer_header (b[2], STORE);
3447  CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3448 
3449  vlib_prefetch_buffer_header (b[3], STORE);
3450  CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3451  }
3452 
3453  next[0] = next[1] = TCP_INPUT_NEXT_DROP;
3454 
3455  tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4);
3456  tc1 = tcp_input_lookup_buffer (b[1], thread_index, &error1, is_ip4);
3457 
3458  if (PREDICT_TRUE (!tc0 + !tc1 == 0))
3459  {
3460  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3461  ASSERT (tcp_lookup_is_valid (tc1, tcp_buffer_hdr (b[1])));
3462 
3463  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3464  vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
3465 
3466  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3467  tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], &error1);
3468  }
3469  else
3470  {
3471  if (PREDICT_TRUE (tc0 != 0))
3472  {
3473  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3474  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3475  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3476  }
3477  else
3478  tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
3479 
3480  if (PREDICT_TRUE (tc1 != 0))
3481  {
3482  ASSERT (tcp_lookup_is_valid (tc1, tcp_buffer_hdr (b[1])));
3483  vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
3484  tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], &error1);
3485  }
3486  else
3487  tcp_input_set_error_next (tm, &next[1], &error1, is_ip4);
3488  }
3489 
3490  b += 2;
3491  next += 2;
3492  n_left_from -= 2;
3493  }
3494  while (n_left_from > 0)
3495  {
3496  tcp_connection_t *tc0;
3497  u32 error0 = TCP_ERROR_NO_LISTENER;
3498 
3499  if (n_left_from > 1)
3500  {
3501  vlib_prefetch_buffer_header (b[1], STORE);
3502  CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3503  }
3504 
3505  next[0] = TCP_INPUT_NEXT_DROP;
3506  tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4);
3507  if (PREDICT_TRUE (tc0 != 0))
3508  {
3509  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3510  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3511  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3512  }
3513  else
3514  tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
3515 
3516  b += 1;
3517  next += 1;
3518  n_left_from -= 1;
3519  }
3520 
3522  tcp_input_trace_frame (vm, node, bufs, frame->n_vectors, is_ip4);
3523 
3524  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
3525  return frame->n_vectors;
3526 }
3527 
3528 static uword
3530  vlib_frame_t * from_frame)
3531 {
3532  return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
3533 }
3534 
3535 static uword
3537  vlib_frame_t * from_frame)
3538 {
3539  return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
3540 }
3541 
3542 /* *INDENT-OFF* */
3544 {
3545  .function = tcp4_input,
3546  .name = "tcp4-input",
3547  /* Takes a vector of packets. */
3548  .vector_size = sizeof (u32),
3549  .n_errors = TCP_N_ERROR,
3550  .error_strings = tcp_error_strings,
3551  .n_next_nodes = TCP_INPUT_N_NEXT,
3552  .next_nodes =
3553  {
3554 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3556 #undef _
3557  },
3558  .format_buffer = format_tcp_header,
3559  .format_trace = format_tcp_rx_trace,
3560 };
3561 /* *INDENT-ON* */
3562 
3564 
3565 /* *INDENT-OFF* */
3567 {
3568  .function = tcp6_input,
3569  .name = "tcp6-input",
3570  /* Takes a vector of packets. */
3571  .vector_size = sizeof (u32),
3572  .n_errors = TCP_N_ERROR,
3573  .error_strings = tcp_error_strings,
3574  .n_next_nodes = TCP_INPUT_N_NEXT,
3575  .next_nodes =
3576  {
3577 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3579 #undef _
3580  },
3581  .format_buffer = format_tcp_header,
3582  .format_trace = format_tcp_rx_trace,
3583 };
3584 /* *INDENT-ON* */
3585 
3587 
3588 static void
3590 {
3591  int i, j;
3592  for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
3593  for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
3594  {
3595  tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
3596  tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
3597  }
3598 
3599 #define _(t,f,n,e) \
3600 do { \
3601  tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
3602  tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
3603 } while (0)
3604 
3605  /* RFC 793: In LISTEN if RST drop and if ACK return RST */
3606  _(LISTEN, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3607  _(LISTEN, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_ACK_INVALID);
3608  _(LISTEN, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_INVALID_CONNECTION);
3609  _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
3611  TCP_ERROR_ACK_INVALID);
3613  TCP_ERROR_SEGMENT_INVALID);
3615  TCP_ERROR_SEGMENT_INVALID);
3617  TCP_ERROR_INVALID_CONNECTION);
3618  _(LISTEN, TCP_FLAG_FIN, TCP_INPUT_NEXT_RESET, TCP_ERROR_SEGMENT_INVALID);
3620  TCP_ERROR_SEGMENT_INVALID);
3622  TCP_ERROR_SEGMENT_INVALID);
3624  TCP_ERROR_NONE);
3626  TCP_ERROR_SEGMENT_INVALID);
3628  TCP_ERROR_SEGMENT_INVALID);
3630  TCP_ERROR_SEGMENT_INVALID);
3632  TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3633  /* ACK for for a SYN-ACK -> tcp-rcv-process. */
3634  _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3635  _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3637  TCP_ERROR_NONE);
3638  _(SYN_RCVD, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3640  TCP_ERROR_NONE);
3642  TCP_ERROR_NONE);
3643  _(SYN_RCVD, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3644  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3645  _(SYN_RCVD, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3647  TCP_ERROR_NONE);
3649  TCP_ERROR_NONE);
3650  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3651  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3653  TCP_ERROR_NONE);
3654  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3655  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3656  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3657  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3659  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3660  _(SYN_RCVD, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3661  /* SYN-ACK for a SYN */
3663  TCP_ERROR_NONE);
3664  _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3665  _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3667  TCP_ERROR_NONE);
3668  /* ACK for for established connection -> tcp-established. */
3669  _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3670  /* FIN for for established connection -> tcp-established. */
3671  _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3673  TCP_ERROR_NONE);
3675  TCP_ERROR_NONE);
3676  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3677  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3679  TCP_ERROR_NONE);
3680  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3681  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3682  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3683  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3684  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3685  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3686  _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3688  TCP_ERROR_NONE);
3689  _(ESTABLISHED, TCP_FLAG_SYN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3691  TCP_ERROR_NONE);
3693  TCP_ERROR_NONE);
3694  _(ESTABLISHED, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3695  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3696  _(ESTABLISHED, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3697  /* ACK or FIN-ACK to our FIN */
3698  _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3700  TCP_ERROR_NONE);
3701  /* FIN in reply to our FIN from the other side */
3702  _(FIN_WAIT_1, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3703  _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3705  TCP_ERROR_NONE);
3706  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3707  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3708  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3709  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3710  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3711  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3713  TCP_ERROR_NONE);
3714  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3715  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3716  _(FIN_WAIT_1, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3718  TCP_ERROR_NONE);
3720  TCP_ERROR_NONE);
3721  _(FIN_WAIT_1, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3722  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3723  _(FIN_WAIT_1, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3725  TCP_ERROR_NONE);
3726  _(CLOSING, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3727  _(CLOSING, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3728  _(CLOSING, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3730  TCP_ERROR_NONE);
3732  TCP_ERROR_NONE);
3733  _(CLOSING, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3734  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3735  _(CLOSING, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3737  TCP_ERROR_NONE);
3738  _(CLOSING, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3740  TCP_ERROR_NONE);
3742  TCP_ERROR_NONE);
3743  _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3744  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3746  TCP_ERROR_NONE);
3747  _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3748  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3750  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3751  /* FIN confirming that the peer (app) has closed */
3752  _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3753  _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3755  TCP_ERROR_NONE);
3756  _(FIN_WAIT_2, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3758  TCP_ERROR_NONE);
3759  _(CLOSE_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3761  TCP_ERROR_NONE);
3762  _(CLOSE_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3764  TCP_ERROR_NONE);
3765  _(LAST_ACK, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3766  _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3767  _(LAST_ACK, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3769  TCP_ERROR_NONE);
3771  TCP_ERROR_NONE);
3772  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3773  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3775  TCP_ERROR_NONE);
3776  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3777  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3778  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3779  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3781  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3782  _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3784  TCP_ERROR_NONE);
3785  _(LAST_ACK, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3787  TCP_ERROR_NONE);
3789  TCP_ERROR_NONE);
3790  _(LAST_ACK, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3791  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3792  _(TIME_WAIT, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3793  _(TIME_WAIT, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3795  TCP_ERROR_NONE);
3796  _(TIME_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3798  TCP_ERROR_NONE);
3799  _(TIME_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3800  /* RFC793 CLOSED: An incoming segment containing a RST is discarded. An
3801  * incoming segment not containing a RST causes a RST to be sent in
3802  * response.*/
3803  _(CLOSED, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
3805  TCP_ERROR_CONNECTION_CLOSED);
3806  _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
3807  _(CLOSED, TCP_FLAG_SYN, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
3809  TCP_ERROR_NONE);
3810 #undef _
3811 }
3812 
3813 static clib_error_t *
3815 {
3816  clib_error_t *error = 0;
3817  tcp_main_t *tm = vnet_get_tcp_main ();
3818 
3819  if ((error = vlib_call_init_function (vm, tcp_init)))
3820  return error;
3821 
3822  /* Initialize dispatch table. */
3824 
3825  return error;
3826 }
3827 
3829 
3830 /*
3831  * fd.io coding-style-patch-verification: ON
3832  *
3833  * Local Variables:
3834  * eval: (c-set-style "gnu")
3835  * End:
3836  */
static void tcp_program_disconnect(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_input.c:1623
#define tcp_in_cong_recovery(tc)
Definition: tcp.h:372
static int tcp_session_enqueue_ooo(tcp_connection_t *tc, vlib_buffer_t *b, u16 data_len)
Enqueue out-of-order data.
Definition: tcp_input.c:1803
static void tcp_update_timestamp(tcp_connection_t *tc, u32 seq, u32 seq_end)
Update tsval recent.
Definition: tcp_input.c:252
static sack_scoreboard_hole_t * scoreboard_insert_hole(sack_scoreboard_t *sb, u32 prev_index, u32 start, u32 end)
Definition: tcp_input.c:690
static u8 tcp_scoreboard_is_sane_post_recovery(tcp_connection_t *tc)
Test that scoreboard is sane after recovery.
Definition: tcp_input.c:879
void scoreboard_clear(sack_scoreboard_t *sb)
Definition: tcp_input.c:853
int stream_session_accept_notify(transport_connection_t *tc)
Definition: session.c:771
static u8 tcp_should_fastrecover(tcp_connection_t *tc)
Definition: tcp_input.c:1238
#define TCP_2MSL_TIME
Definition: tcp.h:103
End of options.
Definition: tcp_packet.h:104
u32 flags
Definition: vhost_user.h:115
#define clib_min(x, y)
Definition: clib.h:295
#define CLIB_UNUSED(x)
Definition: clib.h:82
static void tcp_cc_update(tcp_connection_t *tc, vlib_buffer_t *b)
Definition: tcp_input.c:1211
void tcp_program_fastretransmit(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_input.c:1245
u32 * pending_disconnects
vector of pending disconnect notifications
Definition: tcp.h:429
vlib_node_registration_t tcp6_rcv_process_node
(constructor) VLIB_REGISTER_NODE (tcp6_rcv_process_node)
Definition: tcp_input.c:2632
#define tcp_in_recovery(tc)
Definition: tcp.h:363
static f64 tcp_time_now_us(u32 thread_index)
Definition: tcp.h:803
static void tcp_rcv_fin(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, vlib_buffer_t *b, u32 *error)
Definition: tcp_input.c:1654
#define TCP_OPTION_LEN_SACK_PERMITTED
Definition: tcp_packet.h:167
static int tcp_rcv_ack_is_acceptable(tcp_connection_t *tc0, vlib_buffer_t *tb0)
Definition: tcp_input.c:388
#define seq_leq(_s1, _s2)
Definition: tcp.h:646
struct _sack_block sack_block_t
void tcp_rcv_sacks(tcp_connection_t *tc, u32 ack)
Definition: tcp_input.c:888
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:529
#define timestamp_leq(_t1, _t2)
Definition: tcp.h:653
ip4_address_t src_address
Definition: ip4_packet.h:170
static u8 tcp_cc_is_spurious_retransmit(tcp_connection_t *tc)
Definition: tcp_input.c:1183
transport_connection_t * session_lookup_connection_wt6(u32 fib_index, ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto, u32 thread_index, u8 *result)
Lookup connection with ip6 and transport layer information.
enum _tcp_state_next tcp_state_next_t
struct _transport_connection transport_connection_t
#define tcp_rst(_th)
Definition: tcp_packet.h:81
#define TCP_TIMEWAIT_TIME
Definition: tcp.h:105
static uword tcp6_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:3536
Selective Ack permitted.
Definition: tcp_packet.h:108
#define TCP_FLAG_SYN
Definition: fa_node.h:13
#define tcp_opts_tstamp(_to)
Definition: tcp_packet.h:157
#define PREDICT_TRUE(x)
Definition: clib.h:112
#define tcp_inc_err_counter(cnts, err, val)
Definition: tcp_input.c:2081
unsigned long u64
Definition: types.h:89
#define tcp_store_err_counters(node_id, cnts)
Definition: tcp_input.c:2085
static void tcp_dispatch_table_init(tcp_main_t *tm)
Definition: tcp_input.c:3589
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static u8 * format_tcp_rx_trace_short(u8 *s, va_list *args)
Definition: tcp_input.c:1992
static int tcp_segment_rcv(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, vlib_buffer_t *b)
Receive buffer for connection and handle acks.
Definition: tcp_input.c:1906
struct _sack_scoreboard sack_scoreboard_t
static uword tcp46_established_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4)
Definition: tcp_input.c:2095
static tcp_connection_t * tcp_half_open_connection_get(u32 conn_index)
Definition: tcp.h:606
void tcp_update_rto(tcp_connection_t *tc)
Definition: tcp_input.c:428
#define tcp_doff(_th)
Definition: tcp_packet.h:78
struct _tcp_main tcp_main_t
u32 thread_index
Definition: main.h:179
void tcp_connection_timers_reset(tcp_connection_t *tc)
Stop all connection timers.
Definition: tcp.c:426
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:525
#define tcp_recovery_off(tc)
Definition: tcp.h:361
#define clib_abs(x)
Definition: clib.h:302
static int tcp_update_rtt(tcp_connection_t *tc, u32 ack)
Update RTT estimate and RTO timer.
Definition: tcp_input.c:446
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:564
int i
#define THZ
TCP tick frequency.
Definition: tcp.h:28
static u32 format_get_indent(u8 *s)
Definition: format.h:72
vlib_node_registration_t tcp4_rcv_process_node
(constructor) VLIB_REGISTER_NODE (tcp4_rcv_process_node)
Definition: tcp_input.c:2631
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
struct _tcp_connection tcp_connection_t
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u32 tcp_available_cc_snd_space(const tcp_connection_t *tc)
Estimate of how many bytes we can still push into the network.
Definition: tcp.h:753
#define tcp_opts_sack(_to)
Definition: tcp_packet.h:159
clib_time_t clib_time
Definition: main.h:65
tcp_connection_t tcp_connection
Definition: tcp_input.c:1972
static u8 tcp_sack_vector_is_sane(sack_block_t *sacks)
Definition: tcp_input.c:1670
static tcp_connection_t * tcp_get_connection_from_transport(transport_connection_t *tconn)
Definition: tcp.h:571
static void tcp_cc_congestion_undo(tcp_connection_t *tc)
Definition: tcp_input.c:1152
#define tcp_disconnect_pending_on(tc)
Definition: tcp.h:366
int session_enqueue_stream_connection(transport_connection_t *tc, vlib_buffer_t *b, u32 offset, u8 queue_event, u8 is_in_order)
Definition: session.c:392
u64 session_lookup_half_open_handle(transport_connection_t *tc)
static void tcp_cc_rcv_ack(tcp_connection_t *tc)
Definition: tcp.h:826
No operation.
Definition: tcp_packet.h:105
format_function_t format_tcp_flags
Definition: tcp.h:65
u32 * ongoing_fast_rxt
vector of connections now doing fast rxt
Definition: tcp.h:417
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
u8 n_sack_blocks
Number of SACKs blocks.
Definition: tcp_packet.h:152
struct _tcp_header tcp_header_t
int tcp_half_open_connection_cleanup(tcp_connection_t *tc)
Try to cleanup half-open connection.
Definition: tcp.c:170
static uword tcp6_listen(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:3194
ip6_address_t src_address
Definition: ip6_packet.h:378
u32 * pending_deq_acked
vector of pending ack dequeues
Definition: tcp.h:423
unsigned char u8
Definition: types.h:56
void tcp_do_fastretransmits(tcp_worker_ctx_t *wrk)
Definition: tcp_input.c:1255
#define tcp_inc_counter(node_id, err, count)
Definition: tcp_input.c:2073
vlib_node_registration_t tcp6_syn_sent_node
(constructor) VLIB_REGISTER_NODE (tcp6_syn_sent_node)
Definition: tcp_input.c:2238
struct _sack_scoreboard_hole sack_scoreboard_hole_t
u8 wscale
Window scale advertised.
Definition: tcp_packet.h:148
u32 transport_connection_tx_pacer_burst(transport_connection_t *tc, u64 time_now)
Definition: transport.c:577
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static tcp_connection_t * tcp_lookup_connection(u32 fib_index, vlib_buffer_t *b, u8 thread_index, u8 is_ip4)
Lookup transport connection.
Definition: tcp_input.c:2279
double f64
Definition: types.h:142
#define tcp_fastrecovery_on(tc)
Definition: tcp.h:358
Limit MSS.
Definition: tcp_packet.h:106
static uword tcp4_listen(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:3187
void session_transport_closing_notify(transport_connection_t *tc)
Notification from transport that connection is being closed.
Definition: session.c:794
sack_scoreboard_hole_t * scoreboard_get_hole(sack_scoreboard_t *sb, u32 index)
Definition: tcp_input.c:615
#define TCP_TICK
TCP tick period (s)
Definition: tcp.h:27
#define tcp_is_fin(_th)
Definition: tcp_packet.h:90
static uword tcp6_rcv_process(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2980
static uword tcp4_syn_sent(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2574
#define seq_gt(_s1, _s2)
Definition: tcp.h:647
static u8 * format_tcp_rx_trace(u8 *s, va_list *args)
Definition: tcp_input.c:1976
static void tcp_connection_set_state(tcp_connection_t *tc, tcp_state_t state)
Definition: tcp.h:577
void tcp_init_snd_vars(tcp_connection_t *tc)
Initialize connection send variables.
Definition: tcp.c:581
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
vlib_node_registration_t tcp4_established_node
(constructor) VLIB_REGISTER_NODE (tcp4_established_node)
Definition: tcp_input.c:82
#define TCP_CLOSEWAIT_TIME
Definition: tcp.h:104
#define always_inline
Definition: clib.h:98
#define TCP_OPTION_LEN_SACK_BLOCK
Definition: tcp_packet.h:169
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define TCP_FLAG_ACK
Definition: fa_node.h:16
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
transport_connection_t * session_lookup_connection_wt4(u32 fib_index, ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto, u32 thread_index, u8 *result)
Lookup connection with ip4 and transport layer information.
#define TCP_DELACK_TIME
Definition: tcp.h:100
static tcp_header_t * tcp_buffer_hdr(vlib_buffer_t *b)
Definition: tcp.h:531
static void tcp_cc_recovery_exit(tcp_connection_t *tc)
Definition: tcp_input.c:1124
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:188
static int tcp_segment_validate(tcp_worker_ctx_t *wrk, tcp_connection_t *tc0, vlib_buffer_t *b0, tcp_header_t *th0, u32 *error0)
Validate incoming segment as per RFC793 p.
Definition: tcp_input.c:280
enum _tcp_state tcp_state_t
#define TCP_ALWAYS_ACK
On/off delayed acks.
Definition: tcp.h:39
vlib_node_registration_t tcp6_input_node
(constructor) VLIB_REGISTER_NODE (tcp6_input_node)
Definition: tcp_input.c:3245
static u8 tcp_ack_is_dupack(tcp_connection_t *tc, vlib_buffer_t *b, u32 prev_snd_wnd, u32 prev_snd_una)
Check if duplicate ack as per RFC5681 Sec.
Definition: tcp_input.c:577
vhost_vring_state_t state
Definition: vhost_user.h:120
#define TCP_RTO_MAX
Definition: tcp.h:110
static u32 ooo_segment_length(svm_fifo_t *f, ooo_segment_t *s)
Definition: svm_fifo.h:346
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
static u32 tcp_time_now(void)
Definition: tcp.h:791
sack_block_t * sacks
SACK blocks.
Definition: tcp_packet.h:151
unsigned int u32
Definition: types.h:88
#define vec_end(v)
End (last data address) of vector.
struct _stream_session_t stream_session_t
#define vlib_call_init_function(vm, x)
Definition: init.h:260
static void tcp_node_inc_counter_i(vlib_main_t *vm, u32 tcp4_node, u32 tcp6_node, u8 is_ip4, u32 evt, u32 val)
Definition: tcp_input.c:2057
#define TCP_MAX_SACK_BLOCKS
Max number of SACK blocks stored.
Definition: tcp.h:169
#define VLIB_FRAME_SIZE
Definition: node.h:401
#define tcp_validate_txf_size(_tc, _a)
Definition: tcp.h:935
#define TCP_EVT_DBG(_evt, _args...)
Definition: tcp_debug.h:238
static int tcp_options_parse(tcp_header_t *th, tcp_options_t *to, u8 is_syn)
Parse TCP header options.
Definition: tcp_input.c:128
#define timestamp_lt(_t1, _t2)
Definition: tcp.h:652
static void tcp_timer_set(tcp_connection_t *tc, u8 timer_id, u32 interval)
Definition: tcp.h:833
#define TCP_OPTION_LEN_WINDOW_SCALE
Definition: tcp_packet.h:166
static void svm_fifo_newest_ooo_segment_reset(svm_fifo_t *f)
Definition: svm_fifo.h:312
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
void scoreboard_init(sack_scoreboard_t *sb)
Definition: tcp_input.c:845
u32 stream_session_dequeue_drop(transport_connection_t *tc, u32 max_bytes)
Definition: session.c:515
vlib_main_t * vm
convenience pointer to this thread&#39;s vlib main
Definition: tcp.h:432
#define TCP_INVALID_SACK_HOLE_INDEX
Definition: tcp.h:170
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:511
u32 * postponed_fast_rxt
vector of connections that will do fast rxt
Definition: tcp.h:420
static void tcp_program_dequeue(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_input.c:563
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:114
static void tcp_handle_disconnects(tcp_worker_ctx_t *wrk)
Definition: tcp_input.c:1633
void tcp_cc_fastrecovery_exit(tcp_connection_t *tc)
Definition: tcp_input.c:1136
static uword tcp46_listen_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip4)
LISTEN state processing as per RFC 793 p.
Definition: tcp_input.c:3037
#define tcp_in_fastrecovery(tc)
Definition: tcp.h:362
void tcp_connection_tx_pacer_reset(tcp_connection_t *tc, u32 window, u32 start_bucket)
Definition: tcp.c:1213
static void tcp_input_set_error_next(tcp_main_t *tm, u16 *next, u32 *error, u8 is_ip4)
Definition: tcp_input.c:3302
unsigned short u16
Definition: types.h:57
#define foreach_tcp4_input_next
Definition: tcp_input.c:3259
tcp_connection_t * tcp_connection_alloc(u8 thread_index)
Definition: tcp.c:251
static u32 ooo_segment_offset(svm_fifo_t *f, ooo_segment_t *s)
Definition: svm_fifo.h:334
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
#define filter_flags
Definition: tcp_input.c:3277
void tcp_connection_tx_pacer_update(tcp_connection_t *tc)
Definition: tcp.c:1197
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
static int tcp_buffer_discard_bytes(vlib_buffer_t *b, u32 n_bytes_to_drop)
Definition: tcp_input.c:1873
#define foreach_tcp6_input_next
Definition: tcp_input.c:3268
#define TCP_TIMER_HANDLE_INVALID
Definition: tcp.h:95
static void tcp_input_trace_frame(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t **bs, u32 n_bufs, u8 is_ip4)
Definition: tcp_input.c:3280
#define TCP_CLEANUP_TIME
Definition: tcp.h:107
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:808
#define TCP_FLAG_FIN
Definition: fa_node.h:12
int stream_session_accept(transport_connection_t *tc, u32 listener_index, u8 notify)
Accept a stream session.
Definition: session.c:916
static u8 tcp_cc_is_spurious_fast_rxt(tcp_connection_t *tc)
Definition: tcp_input.c:1176
vlib_node_registration_t tcp4_listen_node
(constructor) VLIB_REGISTER_NODE (tcp4_listen_node)
Definition: tcp_input.c:3030
static void scoreboard_init_high_rxt(sack_scoreboard_t *sb, u32 snd_una)
Definition: tcp_input.c:831
#define TCP_OPTION_LEN_TIMESTAMP
Definition: tcp_packet.h:168
static u8 tcp_lookup_is_valid(tcp_connection_t *tc, tcp_header_t *hdr)
Definition: tcp_input.c:2241
static ooo_segment_t * svm_fifo_newest_ooo_segment(svm_fifo_t *f)
Definition: svm_fifo.h:304
u32 tcp_sack_list_bytes(tcp_connection_t *tc)
Definition: tcp_input.c:1747
Selective Ack block.
Definition: tcp_packet.h:109
vlib_node_registration_t tcp6_established_node
(constructor) VLIB_REGISTER_NODE (tcp6_established_node)
Definition: tcp_input.c:83
sack_scoreboard_hole_t * scoreboard_first_hole(sack_scoreboard_t *sb)
Definition: tcp_input.c:639
static int tcp_can_delack(tcp_connection_t *tc)
Check if ACK could be delayed.
Definition: tcp_input.c:1857
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
static int tcp_cc_recover(tcp_connection_t *tc)
Definition: tcp_input.c:1190
#define TCP_FLAG_RST
Definition: fa_node.h:14
static stream_session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:341
#define TCP_DBG(_fmt, _args...)
Definition: tcp_debug.h:89
static int tcp_rcv_ack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, vlib_buffer_t *b, tcp_header_t *th, u32 *error)
Process incoming ACK.
Definition: tcp_input.c:1515
#define TCP_MAX_WND_SCALE
Definition: tcp_packet.h:173
static void tcp_timer_reset(tcp_connection_t *tc, u8 timer_id)
Definition: tcp.h:844
void tcp_connection_free(tcp_connection_t *tc)
Definition: tcp.c:264
static uword tcp6_syn_sent_rcv(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2581
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
vlib_node_registration_t tcp4_syn_sent_node
(constructor) VLIB_REGISTER_NODE (tcp4_syn_sent_node)
Definition: tcp_input.c:2237
u16 n_vectors
Definition: node.h:420
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:301
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
static void tcp_set_rx_trace_data(tcp_rx_trace_t *t0, tcp_connection_t *tc0, tcp_header_t *th0, vlib_buffer_t *b0, u8 is_ip4)
Definition: tcp_input.c:2007
void tcp_send_reset(tcp_connection_t *tc)
Build and set reset packet for connection.
Definition: tcp_output.c:909
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define TCP_DUPACK_THRESHOLD
Definition: tcp.h:35
format_function_t format_tcp_state
Definition: tcp.h:64
void tcp_program_ack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_output.c:1258
#define clib_warning(format, args...)
Definition: error.h:59
tcp_header_t tcp_header
Definition: tcp_input.c:1971
format_function_t format_tcp_header
Definition: format.h:101
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
#define ARRAY_LEN(x)
Definition: clib.h:62
#define TCP_RTT_MAX
Definition: tcp.h:112
u16 mss
Option flags, see above.
Definition: tcp_packet.h:147
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:405
void tcp_send_synack(tcp_connection_t *tc)
Definition: tcp_output.c:1020
static void tcp_timer_update(tcp_connection_t *tc, u8 timer_id, u32 interval)
Definition: tcp.h:857
#define TCP_PAWS_IDLE
24 days
Definition: tcp.h:30
vslo right
#define ASSERT(truth)
u64 last_cpu_time
Definition: time.h:50
#define tcp_syn(_th)
Definition: tcp_packet.h:80
static clib_error_t * tcp_input_init(vlib_main_t *vm)
Definition: tcp_input.c:3814
u32 session_tx_fifo_max_dequeue(transport_connection_t *tc)
Definition: session.c:498
#define tcp_fastrecovery_first_on(tc)
Definition: tcp.h:369
static void tcp_estimate_rtt(tcp_connection_t *tc, u32 mrtt)
Compute smoothed RTT as per VJ&#39;s &#39;88 SIGCOMM and RFC6298.
Definition: tcp_input.c:405
enum _tcp_rcv_process_next tcp_rcv_process_next_t
static uword tcp4_established(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2179
#define seq_geq(_s1, _s2)
Definition: tcp.h:648
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:130
static void transport_add_tx_event(transport_connection_t *tc)
Definition: session.h:645
static void tcp_handle_postponed_dequeues(tcp_worker_ctx_t *wrk)
Dequeue bytes for connections that have received acks in last burst.
Definition: tcp_input.c:520
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:822
static void tcp_estimate_initial_rtt(tcp_connection_t *tc)
Definition: tcp_input.c:493
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:233
static int tcp_segment_check_paws(tcp_connection_t *tc)
RFC1323: Check against wrapped sequence numbers (PAWS).
Definition: tcp_input.c:242
static void tcp_cc_handle_event(tcp_connection_t *tc, u32 is_dack)
One function to rule them all ...
Definition: tcp_input.c:1317
static u8 tcp_cc_is_spurious_timeout_rxt(tcp_connection_t *tc)
Definition: tcp_input.c:1167
static void tcp_established_trace_frame(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 is_ip4)
Definition: tcp_input.c:2023
enum _tcp_input_next tcp_input_next_t
void tcp_update_sack_list(tcp_connection_t *tc, u32 start, u32 end)
Build SACK list as per RFC2018.
Definition: tcp_input.c:1693
#define tcp_fastrecovery_first_off(tc)
Definition: tcp.h:370
Out-of-order segment.
Definition: svm_fifo.h:27
static u8 tcp_segment_in_rcv_wnd(tcp_connection_t *tc, u32 seq, u32 end_seq)
Validate segment sequence number.
Definition: tcp_input.c:113
#define clib_max(x, y)
Definition: clib.h:288
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static u32 tcp_time_now_w_thread(u32 thread_index)
Definition: tcp.h:797
static clib_error_t * tcp_init(vlib_main_t *vm)
Definition: tcp.c:1511
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
VLIB_NODE_FUNCTION_MULTIARCH(tcp4_established_node, tcp4_established)
static uword tcp6_established(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2186
void transport_connection_tx_pacer_update_bytes(transport_connection_t *tc, u32 bytes)
Definition: transport.c:611
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:156
#define seq_lt(_s1, _s2)
Definition: tcp.h:645
struct _vlib_node_registration vlib_node_registration_t
#define tcp_is_syn(_th)
Definition: tcp_packet.h:89
#define tcp_opts_wscale(_to)
Definition: tcp_packet.h:158
enum _tcp_syn_sent_next tcp_syn_sent_next_t
void tcp_send_reset_w_pkt(tcp_connection_t *tc, vlib_buffer_t *pkt, u32 thread_index, u8 is_ip4)
Send reset without reusing existing buffer.
Definition: tcp_output.c:829
static void tcp_update_snd_wnd(tcp_connection_t *tc, u32 seq, u32 ack, u32 snd_wnd)
Try to update snd_wnd based on feedback received from peer.
Definition: tcp_input.c:1073
void tcp_connection_reset(tcp_connection_t *tc)
Notify session that connection has been reset.
Definition: tcp.c:277
u32 tsval
Timestamp value.
Definition: tcp_packet.h:149
enum _tcp_established_next tcp_established_next_t
u16 payload_length
Definition: ip6_packet.h:369
u32 tsecr
Echoed/reflected time stamp.
Definition: tcp_packet.h:150
vlib_node_registration_t tcp4_input_node
(constructor) VLIB_REGISTER_NODE (tcp4_input_node)
Definition: tcp_input.c:3244
void tcp_send_fin(tcp_connection_t *tc)
Send FIN.
Definition: tcp_output.c:1084
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
enum _tcp_listen_next tcp_listen_next_t
#define foreach_tcp_state_next
Definition: tcp_input.c:29
static u8 tcp_is_lost_fin(tcp_connection_t *tc)
Definition: tcp.h:765
static uword tcp4_rcv_process(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2973
static u32 scoreboard_hole_bytes(sack_scoreboard_hole_t *hole)
Definition: tcp_input.c:609
static tcp_worker_ctx_t * tcp_get_worker(u32 thread_index)
Definition: tcp.h:525
static void tcp_retransmit_timer_update(tcp_connection_t *tc)
Definition: tcp.h:916
static int tcp_session_enqueue_data(tcp_connection_t *tc, vlib_buffer_t *b, u16 data_len)
Enqueue data for delivery to application.
Definition: tcp_input.c:1757
static u8 tcp_should_fastrecover_sack(tcp_connection_t *tc)
Definition: tcp_input.c:1232
u64 uword
Definition: types.h:112
#define seq_max(_s1, _s2)
Definition: tcp.h:649
sack_scoreboard_hole_t * scoreboard_next_hole(sack_scoreboard_t *sb, sack_scoreboard_hole_t *hole)
Definition: tcp_input.c:623
sack_scoreboard_hole_t * scoreboard_prev_hole(sack_scoreboard_t *sb, sack_scoreboard_hole_t *hole)
Definition: tcp_input.c:631
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:274
void tcp_connection_init_vars(tcp_connection_t *tc)
Initialize tcp connection variables.
Definition: tcp.c:614
int tcp_fast_retransmit(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 burst_size)
Do fast retransmit.
Definition: tcp_output.c:2018
static void scoreboard_remove_hole(sack_scoreboard_t *sb, sack_scoreboard_hole_t *hole)
Definition: tcp_input.c:655
sack_scoreboard_hole_t * scoreboard_next_rxt_hole(sack_scoreboard_t *sb, sack_scoreboard_hole_t *start, u8 have_unsent, u8 *can_rescue, u8 *snd_limited)
Figure out the next hole to retransmit.
Definition: tcp_input.c:777
#define TCP_OPTION_LEN_MSS
Definition: tcp_packet.h:165
sack_scoreboard_hole_t * scoreboard_last_hole(sack_scoreboard_t *sb)
Definition: tcp_input.c:647
static void scoreboard_update_bytes(tcp_connection_t *tc, sack_scoreboard_t *sb)
Definition: tcp_input.c:727
#define tcp_disconnect_pending(tc)
Definition: tcp.h:365
left
#define TCP_RTO_MIN
Definition: tcp.h:111
struct clib_bihash_value offset
template key/value backing page structure
#define tcp_scoreboard_trace_add(_tc, _ack)
Definition: tcp.h:233
u8 * format_tcp_connection(u8 *s, va_list *args)
Definition: tcp.c:887
#define vnet_buffer(b)
Definition: buffer.h:368
int session_manager_flush_enqueue_events(u8 transport_proto, u32 thread_index)
Flushes queue of sessions that are to be notified of new data enqueued events.
Definition: session.c:576
static tcp_connection_t * tcp_connection_get(u32 conn_index, u32 thread_index)
Definition: tcp.h:552
static u32 scoreboard_hole_index(sack_scoreboard_t *sb, sack_scoreboard_hole_t *hole)
Definition: tcp_input.c:602
static int tcp_header_bytes(tcp_header_t *t)
Definition: tcp_packet.h:93
int session_stream_connect_notify(transport_connection_t *tc, u8 is_fail)
Definition: session.c:630
#define tcp_disconnect_pending_off(tc)
Definition: tcp.h:367
static u32 vlib_num_workers()
Definition: threads.h:366
void tcp_connection_cleanup(tcp_connection_t *tc)
Cleans up connection state.
Definition: tcp.c:199
u32 * pending_fast_rxt
vector of connections needing fast rxt
Definition: tcp.h:414
static uword tcp4_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:3529
u8 data[0]
Packet data.
Definition: buffer.h:176
static tcp_connection_t * tcp_input_lookup_buffer(vlib_buffer_t *b, u8 thread_index, u32 *error, u8 is_ip4)
Definition: tcp_input.c:3321
u16 flags
Copy of main node flags.
Definition: node.h:532
Window scale.
Definition: tcp_packet.h:107
vlib_node_registration_t tcp6_listen_node
(constructor) VLIB_REGISTER_NODE (tcp6_listen_node)
Definition: tcp_input.c:3031
#define tcp_opts_sack_permitted(_to)
Definition: tcp_packet.h:160
static int ip4_header_bytes(const ip4_header_t *i)
Definition: ip4_packet.h:235
Timestamps.
Definition: tcp_packet.h:110
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:145
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:326
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define TCP_SYN_RCVD_TIME
Definition: tcp.h:102
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:117
static uword tcp46_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4)
Definition: tcp_input.c:3423
static void tcp_persist_timer_set(tcp_connection_t *tc)
Definition: tcp.h:893
static tcp_main_t * vnet_get_tcp_main()
Definition: tcp.h:519
static uword tcp46_syn_sent_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip4)
Definition: tcp_input.c:2320
#define tcp_fastrecovery_off(tc)
Definition: tcp.h:359
static uword tcp46_rcv_process_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip4)
Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED as per RFC793 p...
Definition: tcp_input.c:2639
static void tcp_retransmit_timer_reset(tcp_connection_t *tc)
Definition: tcp.h:880
void tcp_program_dupack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_output.c:1268
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
static void tcp_input_dispatch_buffer(tcp_main_t *tm, tcp_connection_t *tc, vlib_buffer_t *b, u16 *next, u32 *error)
Definition: tcp_input.c:3397
static u32 tcp_set_time_now(tcp_worker_ctx_t *wrk)
Definition: tcp.h:809
#define tcp_ack(_th)
Definition: tcp_packet.h:83
static u32 transport_tx_fifo_size(transport_connection_t *tc)
Definition: session.h:537
static u8 tcp_timer_is_active(tcp_connection_t *tc, tcp_timers_e timer)
Definition: tcp.h:930
transport_connection_t * session_lookup_half_open_connection(u64 handle, u8 proto, u8 is_ip4)
Definition: defs.h:46
static tcp_connection_t * tcp_listener_get(u32 tli)
Definition: tcp.h:600
ip6_address_t dst_address
Definition: ip6_packet.h:378
static u8 tcp_ack_is_cc_event(tcp_connection_t *tc, vlib_buffer_t *b, u32 prev_snd_wnd, u32 prev_snd_una, u8 *is_dack)
Checks if ack is a congestion control event.
Definition: tcp_input.c:590
void tcp_cc_init_congestion(tcp_connection_t *tc)
Init loss recovery/fast recovery.
Definition: tcp_input.c:1111
static void tcp_persist_timer_reset(tcp_connection_t *tc)
Definition: tcp.h:910
static char * tcp_error_strings[]
Definition: tcp_input.c:22
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128