FD.io VPP  v19.01.3-6-g70449b9b9
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  || seq_leq (vnet_buffer (b)->tcp.ack_number,
1532  tc->snd_congestion)))
1533  {
1534  tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
1535  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1536  tc->snd_una_max = tc->snd_nxt;
1537  goto process_ack;
1538  }
1539 
1540  /* If we have outstanding data and this is within the window, accept it,
1541  * probably retransmit has timed out. Otherwise ACK segment and then
1542  * drop it */
1543  if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
1544  {
1545  tcp_program_ack (wrk, tc);
1546  *error = TCP_ERROR_ACK_FUTURE;
1547  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
1548  vnet_buffer (b)->tcp.ack_number);
1549  return -1;
1550  }
1551 
1552  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
1553  vnet_buffer (b)->tcp.ack_number);
1554 
1555  tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
1556  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1557  tc->snd_una_max = tc->snd_nxt;
1558 
1559  goto process_ack;
1560  }
1561 
1562  /* If old ACK, probably it's an old dupack */
1563  if (PREDICT_FALSE (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una)))
1564  {
1565  *error = TCP_ERROR_ACK_OLD;
1566  TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
1567  vnet_buffer (b)->tcp.ack_number);
1568  if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
1569  tcp_cc_handle_event (tc, 1);
1570  /* Don't drop yet */
1571  return 0;
1572  }
1573 
1574  /*
1575  * Looks okay, process feedback
1576  */
1577 process_ack:
1578  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1579  tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
1580 
1581  prev_snd_wnd = tc->snd_wnd;
1582  prev_snd_una = tc->snd_una;
1583  tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
1584  vnet_buffer (b)->tcp.ack_number,
1585  clib_net_to_host_u16 (th->window) << tc->snd_wscale);
1586  tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
1587  tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
1588  tcp_validate_txf_size (tc, tc->bytes_acked);
1589 
1590  if (tc->bytes_acked)
1591  {
1592  tcp_program_dequeue (wrk, tc);
1593  tcp_update_rtt (tc, vnet_buffer (b)->tcp.ack_number);
1594  }
1595 
1596  TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
1597 
1598  /*
1599  * Check if we have congestion event
1600  */
1601 
1602  if (tcp_ack_is_cc_event (tc, b, prev_snd_wnd, prev_snd_una, &is_dack))
1603  {
1604  tcp_cc_handle_event (tc, is_dack);
1605  if (!tcp_in_cong_recovery (tc))
1606  {
1607  *error = TCP_ERROR_ACK_OK;
1608  return 0;
1609  }
1610  *error = TCP_ERROR_ACK_DUP;
1611  if (vnet_buffer (b)->tcp.data_len || tcp_is_fin (th))
1612  return 0;
1613  return -1;
1614  }
1615 
1616  /*
1617  * Update congestion control (slow start/congestion avoidance)
1618  */
1619  tcp_cc_update (tc, b);
1620  *error = TCP_ERROR_ACK_OK;
1621  return 0;
1622 }
1623 
1624 static void
1626 {
1627  if (!tcp_disconnect_pending (tc))
1628  {
1629  vec_add1 (wrk->pending_disconnects, tc->c_c_index);
1631  }
1632 }
1633 
1634 static void
1636 {
1637  u32 thread_index, *pending_disconnects;
1638  tcp_connection_t *tc;
1639  int i;
1640 
1641  if (!vec_len (wrk->pending_disconnects))
1642  return;
1643 
1644  thread_index = wrk->vm->thread_index;
1645  pending_disconnects = wrk->pending_disconnects;
1646  for (i = 0; i < vec_len (pending_disconnects); i++)
1647  {
1648  tc = tcp_connection_get (pending_disconnects[i], thread_index);
1650  session_transport_closing_notify (&tc->connection);
1651  }
1652  _vec_len (wrk->pending_disconnects) = 0;
1653 }
1654 
1655 static void
1657  u32 * error)
1658 {
1659  /* Account for the FIN and send ack */
1660  tc->rcv_nxt += 1;
1661  tcp_program_ack (wrk, tc);
1662  /* Enter CLOSE-WAIT and notify session. To avoid lingering
1663  * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
1664  tcp_connection_set_state (tc, TCP_STATE_CLOSE_WAIT);
1665  tcp_program_disconnect (wrk, tc);
1666  tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
1667  TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc);
1668  *error = TCP_ERROR_FIN_RCVD;
1669 }
1670 
1671 static u8
1673 {
1674  int i;
1675  for (i = 1; i < vec_len (sacks); i++)
1676  {
1677  if (sacks[i - 1].end == sacks[i].start)
1678  return 0;
1679  }
1680  return 1;
1681 }
1682 
1683 /**
1684  * Build SACK list as per RFC2018.
1685  *
1686  * Makes sure the first block contains the segment that generated the current
1687  * ACK and the following ones are the ones most recently reported in SACK
1688  * blocks.
1689  *
1690  * @param tc TCP connection for which the SACK list is updated
1691  * @param start Start sequence number of the newest SACK block
1692  * @param end End sequence of the newest SACK block
1693  */
1694 void
1696 {
1697  sack_block_t *new_list = 0, *block = 0;
1698  int i;
1699 
1700  /* If the first segment is ooo add it to the list. Last write might've moved
1701  * rcv_nxt over the first segment. */
1702  if (seq_lt (tc->rcv_nxt, start))
1703  {
1704  vec_add2 (new_list, block, 1);
1705  block->start = start;
1706  block->end = end;
1707  }
1708 
1709  /* Find the blocks still worth keeping. */
1710  for (i = 0; i < vec_len (tc->snd_sacks); i++)
1711  {
1712  /* Discard if rcv_nxt advanced beyond current block */
1713  if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
1714  continue;
1715 
1716  /* Merge or drop if segment overlapped by the new segment */
1717  if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
1718  && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
1719  {
1720  if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
1721  new_list[0].start = tc->snd_sacks[i].start;
1722  if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
1723  new_list[0].end = tc->snd_sacks[i].end;
1724  continue;
1725  }
1726 
1727  /* Save to new SACK list if we have space. */
1728  if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
1729  {
1730  vec_add1 (new_list, tc->snd_sacks[i]);
1731  }
1732  else
1733  {
1734  clib_warning ("sack discarded");
1735  }
1736  }
1737 
1738  ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
1739 
1740  /* Replace old vector with new one */
1741  vec_free (tc->snd_sacks);
1742  tc->snd_sacks = new_list;
1743 
1744  /* Segments should not 'touch' */
1745  ASSERT (tcp_sack_vector_is_sane (tc->snd_sacks));
1746 }
1747 
1748 u32
1750 {
1751  u32 bytes = 0, i;
1752  for (i = 0; i < vec_len (tc->snd_sacks); i++)
1753  bytes += tc->snd_sacks[i].end - tc->snd_sacks[i].start;
1754  return bytes;
1755 }
1756 
1757 /** Enqueue data for delivery to application */
1758 static int
1760  u16 data_len)
1761 {
1762  int written, error = TCP_ERROR_ENQUEUED;
1763 
1764  ASSERT (seq_geq (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
1765  ASSERT (data_len);
1766  written = session_enqueue_stream_connection (&tc->connection, b, 0,
1767  1 /* queue event */ , 1);
1768 
1769  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
1770 
1771  /* Update rcv_nxt */
1772  if (PREDICT_TRUE (written == data_len))
1773  {
1774  tc->rcv_nxt += written;
1775  }
1776  /* If more data written than expected, account for out-of-order bytes. */
1777  else if (written > data_len)
1778  {
1779  tc->rcv_nxt += written;
1780  TCP_EVT_DBG (TCP_EVT_CC_INPUT, tc, data_len, written);
1781  }
1782  else if (written > 0)
1783  {
1784  /* We've written something but FIFO is probably full now */
1785  tc->rcv_nxt += written;
1786  error = TCP_ERROR_PARTIALLY_ENQUEUED;
1787  }
1788  else
1789  {
1790  return TCP_ERROR_FIFO_FULL;
1791  }
1792 
1793  /* Update SACK list if need be */
1794  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1795  {
1796  /* Remove SACK blocks that have been delivered */
1797  tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1798  }
1799 
1800  return error;
1801 }
1802 
1803 /** Enqueue out-of-order data */
1804 static int
1806  u16 data_len)
1807 {
1808  stream_session_t *s0;
1809  int rv, offset;
1810 
1811  ASSERT (seq_gt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
1812  ASSERT (data_len);
1813 
1814  /* Enqueue out-of-order data with relative offset */
1815  rv = session_enqueue_stream_connection (&tc->connection, b,
1816  vnet_buffer (b)->tcp.seq_number -
1817  tc->rcv_nxt, 0 /* queue event */ ,
1818  0);
1819 
1820  /* Nothing written */
1821  if (rv)
1822  {
1823  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1824  return TCP_ERROR_FIFO_FULL;
1825  }
1826 
1827  TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
1828 
1829  /* Update SACK list if in use */
1830  if (tcp_opts_sack_permitted (&tc->rcv_opts))
1831  {
1832  ooo_segment_t *newest;
1833  u32 start, end;
1834 
1835  s0 = session_get (tc->c_s_index, tc->c_thread_index);
1836 
1837  /* Get the newest segment from the fifo */
1838  newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
1839  if (newest)
1840  {
1841  offset = ooo_segment_offset (s0->server_rx_fifo, newest);
1842  ASSERT (offset <= vnet_buffer (b)->tcp.seq_number - tc->rcv_nxt);
1843  start = tc->rcv_nxt + offset;
1844  end = start + ooo_segment_length (s0->server_rx_fifo, newest);
1845  tcp_update_sack_list (tc, start, end);
1846  svm_fifo_newest_ooo_segment_reset (s0->server_rx_fifo);
1847  TCP_EVT_DBG (TCP_EVT_CC_SACKS, tc);
1848  }
1849  }
1850 
1851  return TCP_ERROR_ENQUEUED_OOO;
1852 }
1853 
1854 /**
1855  * Check if ACK could be delayed. If ack can be delayed, it should return
1856  * true for a full frame. If we're always acking return 0.
1857  */
1858 always_inline int
1860 {
1861  /* Send ack if ... */
1862  if (TCP_ALWAYS_ACK
1863  /* just sent a rcv wnd 0 */
1864  || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1865  /* constrained to send ack */
1866  || (tc->flags & TCP_CONN_SNDACK) != 0
1867  /* we're almost out of tx wnd */
1868  || tcp_available_cc_snd_space (tc) < 4 * tc->snd_mss)
1869  return 0;
1870 
1871  return 1;
1872 }
1873 
1874 static int
1876 {
1877  u32 discard, first = b->current_length;
1879 
1880  /* Handle multi-buffer segments */
1881  if (n_bytes_to_drop > b->current_length)
1882  {
1883  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
1884  return -1;
1885  do
1886  {
1887  discard = clib_min (n_bytes_to_drop, b->current_length);
1888  vlib_buffer_advance (b, discard);
1889  b = vlib_get_buffer (vm, b->next_buffer);
1890  n_bytes_to_drop -= discard;
1891  }
1892  while (n_bytes_to_drop);
1893  if (n_bytes_to_drop > first)
1894  b->total_length_not_including_first_buffer -= n_bytes_to_drop - first;
1895  }
1896  else
1897  vlib_buffer_advance (b, n_bytes_to_drop);
1898  vnet_buffer (b)->tcp.data_len -= n_bytes_to_drop;
1899  return 0;
1900 }
1901 
1902 /**
1903  * Receive buffer for connection and handle acks
1904  *
1905  * It handles both in order or out-of-order data.
1906  */
1907 static int
1909  vlib_buffer_t * b)
1910 {
1911  u32 error, n_bytes_to_drop, n_data_bytes;
1912 
1913  vlib_buffer_advance (b, vnet_buffer (b)->tcp.data_offset);
1914  n_data_bytes = vnet_buffer (b)->tcp.data_len;
1915  ASSERT (n_data_bytes);
1916 
1917  /* Handle out-of-order data */
1918  if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1919  {
1920  /* Old sequence numbers allowed through because they overlapped
1921  * the rx window */
1922  if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
1923  {
1924  /* Completely in the past (possible retransmit). Ack
1925  * retransmissions since we may not have any data to send */
1926  if (seq_leq (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
1927  {
1928  tcp_program_ack (wrk, tc);
1929  error = TCP_ERROR_SEGMENT_OLD;
1930  goto done;
1931  }
1932 
1933  /* Chop off the bytes in the past and see if what is left
1934  * can be enqueued in order */
1935  n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
1936  n_data_bytes -= n_bytes_to_drop;
1937  vnet_buffer (b)->tcp.seq_number = tc->rcv_nxt;
1938  if (tcp_buffer_discard_bytes (b, n_bytes_to_drop))
1939  {
1940  error = TCP_ERROR_SEGMENT_OLD;
1941  goto done;
1942  }
1943  goto in_order;
1944  }
1945 
1946  /* RFC2581: Enqueue and send DUPACK for fast retransmit */
1947  error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1948  tcp_program_dupack (wrk, tc);
1949  TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc, vnet_buffer (b)->tcp);
1950  goto done;
1951  }
1952 
1953 in_order:
1954 
1955  /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1956  * segments can be enqueued after fifo tail offset changes. */
1957  error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1958  if (tcp_can_delack (tc))
1959  {
1960  if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1961  tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
1962  goto done;
1963  }
1964 
1965  tcp_program_ack (wrk, tc);
1966 
1967 done:
1968  return error;
1969 }
1970 
1971 typedef struct
1972 {
1975 } tcp_rx_trace_t;
1976 
1977 static u8 *
1978 format_tcp_rx_trace (u8 * s, va_list * args)
1979 {
1980  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1981  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1982  tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1983  u32 indent = format_get_indent (s);
1984 
1985  s = format (s, "%U\n%U%U",
1986  format_tcp_header, &t->tcp_header, 128,
1987  format_white_space, indent,
1989 
1990  return s;
1991 }
1992 
1993 static u8 *
1994 format_tcp_rx_trace_short (u8 * s, va_list * args)
1995 {
1996  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1997  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1998  tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1999 
2000  s = format (s, "%d -> %d (%U)",
2001  clib_net_to_host_u16 (t->tcp_header.dst_port),
2002  clib_net_to_host_u16 (t->tcp_header.src_port), format_tcp_state,
2003  t->tcp_connection.state);
2004 
2005  return s;
2006 }
2007 
2008 static void
2010  tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
2011 {
2012  if (tc0)
2013  {
2014  clib_memcpy_fast (&t0->tcp_connection, tc0,
2015  sizeof (t0->tcp_connection));
2016  }
2017  else
2018  {
2019  th0 = tcp_buffer_hdr (b0);
2020  }
2021  clib_memcpy_fast (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2022 }
2023 
2024 static void
2026  vlib_frame_t * frame, u8 is_ip4)
2027 {
2028  u32 *from, n_left;
2029 
2030  n_left = frame->n_vectors;
2031  from = vlib_frame_vector_args (frame);
2032 
2033  while (n_left >= 1)
2034  {
2035  tcp_connection_t *tc0;
2036  tcp_rx_trace_t *t0;
2037  tcp_header_t *th0;
2038  vlib_buffer_t *b0;
2039  u32 bi0;
2040 
2041  bi0 = from[0];
2042  b0 = vlib_get_buffer (vm, bi0);
2043 
2044  if (b0->flags & VLIB_BUFFER_IS_TRACED)
2045  {
2046  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2047  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2048  vm->thread_index);
2049  th0 = tcp_buffer_hdr (b0);
2050  tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
2051  }
2052 
2053  from += 1;
2054  n_left -= 1;
2055  }
2056 }
2057 
2058 always_inline void
2059 tcp_node_inc_counter_i (vlib_main_t * vm, u32 tcp4_node, u32 tcp6_node,
2060  u8 is_ip4, u32 evt, u32 val)
2061 {
2062  if (is_ip4)
2063  vlib_node_increment_counter (vm, tcp4_node, evt, val);
2064  else
2065  vlib_node_increment_counter (vm, tcp6_node, evt, val);
2066 }
2067 
2068 #define tcp_maybe_inc_counter(node_id, err, count) \
2069 { \
2070  if (next0 != tcp_next_drop (is_ip4)) \
2071  tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index, \
2072  tcp6_##node_id##_node.index, is_ip4, err, \
2073  1); \
2074 }
2075 #define tcp_inc_counter(node_id, err, count) \
2076  tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index, \
2077  tcp6_##node_id##_node.index, is_ip4, \
2078  err, count)
2079 #define tcp_maybe_inc_err_counter(cnts, err) \
2080 { \
2081  cnts[err] += (next0 != tcp_next_drop (is_ip4)); \
2082 }
2083 #define tcp_inc_err_counter(cnts, err, val) \
2084 { \
2085  cnts[err] += val; \
2086 }
2087 #define tcp_store_err_counters(node_id, cnts) \
2088 { \
2089  int i; \
2090  for (i = 0; i < TCP_N_ERROR; i++) \
2091  if (cnts[i]) \
2092  tcp_inc_counter(node_id, i, cnts[i]); \
2093 }
2094 
2095 
2098  vlib_frame_t * frame, int is_ip4)
2099 {
2100  u32 thread_index = vm->thread_index, errors = 0;
2101  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
2102  u32 n_left_from, *from, *first_buffer;
2103  u16 err_counters[TCP_N_ERROR] = { 0 };
2104 
2105  if (node->flags & VLIB_NODE_FLAG_TRACE)
2106  tcp_established_trace_frame (vm, node, frame, is_ip4);
2107 
2108  first_buffer = from = vlib_frame_vector_args (frame);
2109  n_left_from = frame->n_vectors;
2110 
2111  while (n_left_from > 0)
2112  {
2113  u32 bi0, error0 = TCP_ERROR_ACK_OK;
2114  vlib_buffer_t *b0;
2115  tcp_header_t *th0;
2116  tcp_connection_t *tc0;
2117 
2118  if (n_left_from > 1)
2119  {
2120  vlib_buffer_t *pb;
2121  pb = vlib_get_buffer (vm, from[1]);
2122  vlib_prefetch_buffer_header (pb, LOAD);
2123  CLIB_PREFETCH (pb->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2124  }
2125 
2126  bi0 = from[0];
2127  from += 1;
2128  n_left_from -= 1;
2129 
2130  b0 = vlib_get_buffer (vm, bi0);
2131  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2132  thread_index);
2133 
2134  if (PREDICT_FALSE (tc0 == 0))
2135  {
2136  error0 = TCP_ERROR_INVALID_CONNECTION;
2137  goto done;
2138  }
2139 
2140  th0 = tcp_buffer_hdr (b0);
2141 
2142  /* TODO header prediction fast path */
2143 
2144  /* 1-4: check SEQ, RST, SYN */
2145  if (PREDICT_FALSE (tcp_segment_validate (wrk, tc0, b0, th0, &error0)))
2146  {
2147  TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0, vnet_buffer (b0)->tcp);
2148  goto done;
2149  }
2150 
2151  /* 5: check the ACK field */
2152  if (PREDICT_FALSE (tcp_rcv_ack (wrk, tc0, b0, th0, &error0)))
2153  goto done;
2154 
2155  /* 6: check the URG bit TODO */
2156 
2157  /* 7: process the segment text */
2158  if (vnet_buffer (b0)->tcp.data_len)
2159  error0 = tcp_segment_rcv (wrk, tc0, b0);
2160 
2161  /* 8: check the FIN bit */
2162  if (PREDICT_FALSE (tcp_is_fin (th0)))
2163  tcp_rcv_fin (wrk, tc0, b0, &error0);
2164 
2165  done:
2166  tcp_inc_err_counter (err_counters, error0, 1);
2167  }
2168 
2170  thread_index);
2171  err_counters[TCP_ERROR_MSG_QUEUE_FULL] = errors;
2172  tcp_store_err_counters (established, err_counters);
2174  tcp_handle_disconnects (wrk);
2175  vlib_buffer_free (vm, first_buffer, frame->n_vectors);
2176 
2177  return frame->n_vectors;
2178 }
2179 
2180 static uword
2182  vlib_frame_t * from_frame)
2183 {
2184  return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2185 }
2186 
2187 static uword
2189  vlib_frame_t * from_frame)
2190 {
2191  return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2192 }
2193 
2194 /* *INDENT-OFF* */
2196 {
2197  .function = tcp4_established,
2198  .name = "tcp4-established",
2199  /* Takes a vector of packets. */
2200  .vector_size = sizeof (u32),
2201  .n_errors = TCP_N_ERROR,
2202  .error_strings = tcp_error_strings,
2203  .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
2204  .next_nodes =
2205  {
2206 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
2208 #undef _
2209  },
2210  .format_trace = format_tcp_rx_trace_short,
2211 };
2212 /* *INDENT-ON* */
2213 
2215 
2216 /* *INDENT-OFF* */
2218 {
2219  .function = tcp6_established,
2220  .name = "tcp6-established",
2221  /* Takes a vector of packets. */
2222  .vector_size = sizeof (u32),
2223  .n_errors = TCP_N_ERROR,
2224  .error_strings = tcp_error_strings,
2225  .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
2226  .next_nodes =
2227  {
2228 #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
2230 #undef _
2231  },
2232  .format_trace = format_tcp_rx_trace_short,
2233 };
2234 /* *INDENT-ON* */
2235 
2236 
2238 
2241 
2242 static u8
2244 {
2245  transport_connection_t *tmp = 0;
2246  u64 handle;
2247 
2248  if (!tc)
2249  return 1;
2250 
2251  /* Proxy case */
2252  if (tc->c_lcl_port == 0 && tc->state == TCP_STATE_LISTEN)
2253  return 1;
2254 
2255  u8 is_valid = (tc->c_lcl_port == hdr->dst_port
2256  && (tc->state == TCP_STATE_LISTEN
2257  || tc->c_rmt_port == hdr->src_port));
2258 
2259  if (!is_valid)
2260  {
2261  handle = session_lookup_half_open_handle (&tc->connection);
2262  tmp = session_lookup_half_open_connection (handle & 0xFFFFFFFF,
2263  tc->c_proto, tc->c_is_ip4);
2264 
2265  if (tmp)
2266  {
2267  if (tmp->lcl_port == hdr->dst_port
2268  && tmp->rmt_port == hdr->src_port)
2269  {
2270  TCP_DBG ("half-open is valid!");
2271  }
2272  }
2273  }
2274  return is_valid;
2275 }
2276 
2277 /**
2278  * Lookup transport connection
2279  */
2280 static tcp_connection_t *
2281 tcp_lookup_connection (u32 fib_index, vlib_buffer_t * b, u8 thread_index,
2282  u8 is_ip4)
2283 {
2284  tcp_header_t *tcp;
2285  transport_connection_t *tconn;
2286  tcp_connection_t *tc;
2287  u8 is_filtered = 0;
2288  if (is_ip4)
2289  {
2290  ip4_header_t *ip4;
2291  ip4 = vlib_buffer_get_current (b);
2292  tcp = ip4_next_header (ip4);
2293  tconn = session_lookup_connection_wt4 (fib_index,
2294  &ip4->dst_address,
2295  &ip4->src_address,
2296  tcp->dst_port,
2297  tcp->src_port,
2299  thread_index, &is_filtered);
2300  tc = tcp_get_connection_from_transport (tconn);
2301  ASSERT (tcp_lookup_is_valid (tc, tcp));
2302  }
2303  else
2304  {
2305  ip6_header_t *ip6;
2306  ip6 = vlib_buffer_get_current (b);
2307  tcp = ip6_next_header (ip6);
2308  tconn = session_lookup_connection_wt6 (fib_index,
2309  &ip6->dst_address,
2310  &ip6->src_address,
2311  tcp->dst_port,
2312  tcp->src_port,
2314  thread_index, &is_filtered);
2315  tc = tcp_get_connection_from_transport (tconn);
2316  ASSERT (tcp_lookup_is_valid (tc, tcp));
2317  }
2318  return tc;
2319 }
2320 
2323  vlib_frame_t * from_frame, int is_ip4)
2324 {
2325  tcp_main_t *tm = vnet_get_tcp_main ();
2326  u32 n_left_from, *from, *first_buffer, errors = 0;
2327  u32 my_thread_index = vm->thread_index;
2328  tcp_worker_ctx_t *wrk = tcp_get_worker (my_thread_index);
2329 
2330  from = first_buffer = vlib_frame_vector_args (from_frame);
2331  n_left_from = from_frame->n_vectors;
2332 
2333  while (n_left_from > 0)
2334  {
2335  u32 bi0, ack0, seq0, error0 = TCP_ERROR_NONE;
2336  tcp_connection_t *tc0, *new_tc0;
2337  tcp_header_t *tcp0 = 0;
2338  tcp_rx_trace_t *t0;
2339  vlib_buffer_t *b0;
2340 
2341  bi0 = from[0];
2342  from += 1;
2343  n_left_from -= 1;
2344 
2345  b0 = vlib_get_buffer (vm, bi0);
2346  tc0 =
2347  tcp_half_open_connection_get (vnet_buffer (b0)->tcp.connection_index);
2348  if (PREDICT_FALSE (tc0 == 0))
2349  {
2350  error0 = TCP_ERROR_INVALID_CONNECTION;
2351  goto drop;
2352  }
2353 
2354  /* Half-open completed recently but the connection was't removed
2355  * yet by the owning thread */
2356  if (PREDICT_FALSE (tc0->flags & TCP_CONN_HALF_OPEN_DONE))
2357  {
2358  /* Make sure the connection actually exists */
2359  ASSERT (tcp_lookup_connection (tc0->c_fib_index, b0,
2360  my_thread_index, is_ip4));
2361  goto drop;
2362  }
2363 
2364  ack0 = vnet_buffer (b0)->tcp.ack_number;
2365  seq0 = vnet_buffer (b0)->tcp.seq_number;
2366  tcp0 = tcp_buffer_hdr (b0);
2367 
2368  /* Crude check to see if the connection handle does not match
2369  * the packet. Probably connection just switched to established */
2370  if (PREDICT_FALSE (tcp0->dst_port != tc0->c_lcl_port
2371  || tcp0->src_port != tc0->c_rmt_port))
2372  {
2373  error0 = TCP_ERROR_INVALID_CONNECTION;
2374  goto drop;
2375  }
2376 
2377  if (PREDICT_FALSE (!tcp_ack (tcp0) && !tcp_rst (tcp0)
2378  && !tcp_syn (tcp0)))
2379  {
2380  error0 = TCP_ERROR_SEGMENT_INVALID;
2381  goto drop;
2382  }
2383 
2384  /* SYNs consume sequence numbers */
2385  vnet_buffer (b0)->tcp.seq_end += tcp_is_syn (tcp0);
2386 
2387  /*
2388  * 1. check the ACK bit
2389  */
2390 
2391  /*
2392  * If the ACK bit is set
2393  * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
2394  * the RST bit is set, if so drop the segment and return)
2395  * <SEQ=SEG.ACK><CTL=RST>
2396  * and discard the segment. Return.
2397  * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
2398  */
2399  if (tcp_ack (tcp0))
2400  {
2401  if (seq_leq (ack0, tc0->iss) || seq_gt (ack0, tc0->snd_nxt))
2402  {
2403  if (!tcp_rst (tcp0))
2404  tcp_send_reset_w_pkt (tc0, b0, my_thread_index, is_ip4);
2405  error0 = TCP_ERROR_RCV_WND;
2406  goto drop;
2407  }
2408 
2409  /* Make sure ACK is valid */
2410  if (seq_gt (tc0->snd_una, ack0))
2411  {
2412  error0 = TCP_ERROR_ACK_INVALID;
2413  goto drop;
2414  }
2415  }
2416 
2417  /*
2418  * 2. check the RST bit
2419  */
2420 
2421  if (tcp_rst (tcp0))
2422  {
2423  /* If ACK is acceptable, signal client that peer is not
2424  * willing to accept connection and drop connection*/
2425  if (tcp_ack (tcp0))
2426  tcp_connection_reset (tc0);
2427  error0 = TCP_ERROR_RST_RCVD;
2428  goto drop;
2429  }
2430 
2431  /*
2432  * 3. check the security and precedence (skipped)
2433  */
2434 
2435  /*
2436  * 4. check the SYN bit
2437  */
2438 
2439  /* No SYN flag. Drop. */
2440  if (!tcp_syn (tcp0))
2441  {
2442  clib_warning ("not synack");
2443  error0 = TCP_ERROR_SEGMENT_INVALID;
2444  goto drop;
2445  }
2446 
2447  /* Parse options */
2448  if (tcp_options_parse (tcp0, &tc0->rcv_opts, 1))
2449  {
2450  clib_warning ("options parse fail");
2451  error0 = TCP_ERROR_OPTIONS;
2452  goto drop;
2453  }
2454 
2455  /* Valid SYN or SYN-ACK. Move connection from half-open pool to
2456  * current thread pool. */
2457  pool_get (tm->connections[my_thread_index], new_tc0);
2458  clib_memcpy_fast (new_tc0, tc0, sizeof (*new_tc0));
2459  new_tc0->c_c_index = new_tc0 - tm->connections[my_thread_index];
2460  new_tc0->c_thread_index = my_thread_index;
2461  new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
2462  new_tc0->irs = seq0;
2463  new_tc0->timers[TCP_TIMER_ESTABLISH_AO] = TCP_TIMER_HANDLE_INVALID;
2464  new_tc0->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
2465  new_tc0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2466 
2467  /* If this is not the owning thread, wait for syn retransmit to
2468  * expire and cleanup then */
2470  tc0->flags |= TCP_CONN_HALF_OPEN_DONE;
2471 
2472  if (tcp_opts_tstamp (&new_tc0->rcv_opts))
2473  {
2474  new_tc0->tsval_recent = new_tc0->rcv_opts.tsval;
2475  new_tc0->tsval_recent_age = tcp_time_now ();
2476  }
2477 
2478  if (tcp_opts_wscale (&new_tc0->rcv_opts))
2479  new_tc0->snd_wscale = new_tc0->rcv_opts.wscale;
2480  else
2481  new_tc0->rcv_wscale = 0;
2482 
2483  new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
2484  << new_tc0->snd_wscale;
2485  new_tc0->snd_wl1 = seq0;
2486  new_tc0->snd_wl2 = ack0;
2487 
2488  tcp_connection_init_vars (new_tc0);
2489 
2490  /* SYN-ACK: See if we can switch to ESTABLISHED state */
2491  if (PREDICT_TRUE (tcp_ack (tcp0)))
2492  {
2493  /* Our SYN is ACKed: we have iss < ack = snd_una */
2494 
2495  /* TODO Dequeue acknowledged segments if we support Fast Open */
2496  new_tc0->snd_una = ack0;
2497  new_tc0->state = TCP_STATE_ESTABLISHED;
2498 
2499  /* Make sure las is initialized for the wnd computation */
2500  new_tc0->rcv_las = new_tc0->rcv_nxt;
2501 
2502  /* Notify app that we have connection. If session layer can't
2503  * allocate session send reset */
2504  if (session_stream_connect_notify (&new_tc0->connection, 0))
2505  {
2506  clib_warning ("connect notify fail");
2507  tcp_send_reset_w_pkt (new_tc0, b0, my_thread_index, is_ip4);
2508  tcp_connection_cleanup (new_tc0);
2509  goto drop;
2510  }
2511 
2512  new_tc0->tx_fifo_size =
2513  transport_tx_fifo_size (&new_tc0->connection);
2514  /* Update rtt with the syn-ack sample */
2515  tcp_estimate_initial_rtt (new_tc0);
2516  TCP_EVT_DBG (TCP_EVT_SYNACK_RCVD, new_tc0);
2517  error0 = TCP_ERROR_SYN_ACKS_RCVD;
2518  }
2519  /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
2520  else
2521  {
2522  new_tc0->state = TCP_STATE_SYN_RCVD;
2523 
2524  /* Notify app that we have connection */
2525  if (session_stream_connect_notify (&new_tc0->connection, 0))
2526  {
2527  tcp_connection_cleanup (new_tc0);
2528  tcp_send_reset_w_pkt (tc0, b0, my_thread_index, is_ip4);
2529  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc0);
2530  goto drop;
2531  }
2532 
2533  new_tc0->tx_fifo_size =
2534  transport_tx_fifo_size (&new_tc0->connection);
2535  new_tc0->rtt_ts = 0;
2536  tcp_init_snd_vars (new_tc0);
2537  tcp_send_synack (new_tc0);
2538  error0 = TCP_ERROR_SYNS_RCVD;
2539  goto drop;
2540  }
2541 
2542  /* Read data, if any */
2543  if (PREDICT_FALSE (vnet_buffer (b0)->tcp.data_len))
2544  {
2545  clib_warning ("rcvd data in syn-sent");
2546  error0 = tcp_segment_rcv (wrk, new_tc0, b0);
2547  if (error0 == TCP_ERROR_ACK_OK)
2548  error0 = TCP_ERROR_SYN_ACKS_RCVD;
2549  }
2550  else
2551  {
2552  tcp_program_ack (wrk, new_tc0);
2553  }
2554 
2555  drop:
2556 
2557  tcp_inc_counter (syn_sent, error0, 1);
2558  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED) && tcp0 != 0))
2559  {
2560  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2561  clib_memcpy_fast (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
2562  clib_memcpy_fast (&t0->tcp_connection, tc0,
2563  sizeof (t0->tcp_connection));
2564  }
2565  }
2566 
2568  my_thread_index);
2569  tcp_inc_counter (syn_sent, TCP_ERROR_MSG_QUEUE_FULL, errors);
2570  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
2571 
2572  return from_frame->n_vectors;
2573 }
2574 
2575 static uword
2577  vlib_frame_t * from_frame)
2578 {
2579  return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2580 }
2581 
2582 static uword
2584  vlib_frame_t * from_frame)
2585 {
2586  return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2587 }
2588 
2589 /* *INDENT-OFF* */
2591 {
2592  .function = tcp4_syn_sent,
2593  .name = "tcp4-syn-sent",
2594  /* Takes a vector of packets. */
2595  .vector_size = sizeof (u32),
2596  .n_errors = TCP_N_ERROR,
2597  .error_strings = tcp_error_strings,
2598  .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2599  .next_nodes =
2600  {
2601 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2603 #undef _
2604  },
2605  .format_trace = format_tcp_rx_trace_short,
2606 };
2607 /* *INDENT-ON* */
2608 
2610 
2611 /* *INDENT-OFF* */
2613 {
2614  .function = tcp6_syn_sent_rcv,
2615  .name = "tcp6-syn-sent",
2616  /* Takes a vector of packets. */
2617  .vector_size = sizeof (u32),
2618  .n_errors = TCP_N_ERROR,
2619  .error_strings = tcp_error_strings,
2620  .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2621  .next_nodes =
2622  {
2623 #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2625 #undef _
2626  },
2627  .format_trace = format_tcp_rx_trace_short,
2628 };
2629 /* *INDENT-ON* */
2630 
2632 
2635 
2636 /**
2637  * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
2638  * as per RFC793 p. 64
2639  */
2642  vlib_frame_t * from_frame, int is_ip4)
2643 {
2644  u32 thread_index = vm->thread_index, errors = 0, *first_buffer;
2645  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
2646  u32 n_left_from, *from, max_dequeue;
2647 
2648  from = first_buffer = vlib_frame_vector_args (from_frame);
2649  n_left_from = from_frame->n_vectors;
2650 
2651  while (n_left_from > 0)
2652  {
2653  u32 bi0, error0 = TCP_ERROR_NONE;
2654  tcp_header_t *tcp0 = 0;
2655  tcp_connection_t *tc0;
2656  vlib_buffer_t *b0;
2657  u8 is_fin0;
2658 
2659  bi0 = from[0];
2660  from += 1;
2661  n_left_from -= 1;
2662 
2663  b0 = vlib_get_buffer (vm, bi0);
2664  tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
2665  thread_index);
2666  if (PREDICT_FALSE (tc0 == 0))
2667  {
2668  error0 = TCP_ERROR_INVALID_CONNECTION;
2669  goto drop;
2670  }
2671 
2672  tcp0 = tcp_buffer_hdr (b0);
2673  is_fin0 = tcp_is_fin (tcp0);
2674 
2675  if (CLIB_DEBUG)
2676  {
2677  tcp_connection_t *tmp;
2678  tmp = tcp_lookup_connection (tc0->c_fib_index, b0, thread_index,
2679  is_ip4);
2680  if (tmp->state != tc0->state)
2681  {
2682  if (tc0->state != TCP_STATE_CLOSED)
2683  clib_warning ("state changed");
2684  goto drop;
2685  }
2686  }
2687 
2688  /*
2689  * Special treatment for CLOSED
2690  */
2691  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
2692  {
2693  error0 = TCP_ERROR_CONNECTION_CLOSED;
2694  goto drop;
2695  }
2696 
2697  /*
2698  * For all other states (except LISTEN)
2699  */
2700 
2701  /* 1-4: check SEQ, RST, SYN */
2702  if (PREDICT_FALSE (tcp_segment_validate (wrk, tc0, b0, tcp0, &error0)))
2703  goto drop;
2704 
2705  /* 5: check the ACK field */
2706  switch (tc0->state)
2707  {
2708  case TCP_STATE_SYN_RCVD:
2709  /*
2710  * If the segment acknowledgment is not acceptable, form a
2711  * reset segment,
2712  * <SEQ=SEG.ACK><CTL=RST>
2713  * and send it.
2714  */
2715  if (!tcp_rcv_ack_is_acceptable (tc0, b0))
2716  {
2717  tcp_connection_reset (tc0);
2718  error0 = TCP_ERROR_ACK_INVALID;
2719  goto drop;
2720  }
2721 
2722  /* Make sure the ack is exactly right */
2723  if (tc0->rcv_nxt != vnet_buffer (b0)->tcp.seq_number || is_fin0)
2724  {
2725  tcp_connection_reset (tc0);
2726  error0 = TCP_ERROR_SEGMENT_INVALID;
2727  goto drop;
2728  }
2729 
2730  /* Update rtt and rto */
2732 
2733  /* Switch state to ESTABLISHED */
2734  tc0->state = TCP_STATE_ESTABLISHED;
2735  TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc0);
2736 
2737  /* Initialize session variables */
2738  tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
2739  tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
2740  << tc0->rcv_opts.wscale;
2741  tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2742  tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2743 
2744  /* Reset SYN-ACK retransmit and SYN_RCV establish timers */
2746  tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
2747  if (stream_session_accept_notify (&tc0->connection))
2748  {
2749  error0 = TCP_ERROR_MSG_QUEUE_FULL;
2750  tcp_connection_reset (tc0);
2751  goto drop;
2752  }
2753  error0 = TCP_ERROR_ACK_OK;
2754  break;
2755  case TCP_STATE_ESTABLISHED:
2756  /* We can get packets in established state here because they
2757  * were enqueued before state change */
2758  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2759  goto drop;
2760 
2761  break;
2762  case TCP_STATE_FIN_WAIT_1:
2763  /* In addition to the processing for the ESTABLISHED state, if
2764  * our FIN is now acknowledged then enter FIN-WAIT-2 and
2765  * continue processing in that state. */
2766  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2767  goto drop;
2768 
2769  /* Still have to send the FIN */
2770  if (tc0->flags & TCP_CONN_FINPNDG)
2771  {
2772  /* TX fifo finally drained */
2773  max_dequeue = session_tx_fifo_max_dequeue (&tc0->connection);
2774  if (max_dequeue <= tc0->burst_acked)
2775  tcp_send_fin (tc0);
2776  }
2777  /* If FIN is ACKed */
2778  else if (tc0->snd_una == tc0->snd_una_max)
2779  {
2780  tcp_connection_set_state (tc0, TCP_STATE_FIN_WAIT_2);
2781 
2782  /* Stop all retransmit timers because we have nothing more
2783  * to send. Enable waitclose though because we're willing to
2784  * wait for peer's FIN but not indefinitely. */
2786  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2787 
2788  /* Don't try to deq the FIN acked */
2789  if (tc0->burst_acked > 1)
2790  stream_session_dequeue_drop (&tc0->connection,
2791  tc0->burst_acked - 1);
2792  tc0->burst_acked = 0;
2793  }
2794  break;
2795  case TCP_STATE_FIN_WAIT_2:
2796  /* In addition to the processing for the ESTABLISHED state, if
2797  * the retransmission queue is empty, the user's CLOSE can be
2798  * acknowledged ("ok") but do not delete the TCB. */
2799  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2800  goto drop;
2801  tc0->burst_acked = 0;
2802  break;
2803  case TCP_STATE_CLOSE_WAIT:
2804  /* Do the same processing as for the ESTABLISHED state. */
2805  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2806  goto drop;
2807 
2808  if (tc0->flags & TCP_CONN_FINPNDG)
2809  {
2810  /* TX fifo finally drained */
2811  if (!session_tx_fifo_max_dequeue (&tc0->connection))
2812  {
2813  tcp_send_fin (tc0);
2815  tcp_connection_set_state (tc0, TCP_STATE_LAST_ACK);
2816  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2817  }
2818  }
2819  break;
2820  case TCP_STATE_CLOSING:
2821  /* In addition to the processing for the ESTABLISHED state, if
2822  * the ACK acknowledges our FIN then enter the TIME-WAIT state,
2823  * otherwise ignore the segment. */
2824  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2825  goto drop;
2826 
2828  tcp_connection_set_state (tc0, TCP_STATE_TIME_WAIT);
2829  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2830  goto drop;
2831 
2832  break;
2833  case TCP_STATE_LAST_ACK:
2834  /* The only thing that [should] arrive in this state is an
2835  * acknowledgment of our FIN. If our FIN is now acknowledged,
2836  * delete the TCB, enter the CLOSED state, and return. */
2837 
2838  if (!tcp_rcv_ack_is_acceptable (tc0, b0))
2839  {
2840  error0 = TCP_ERROR_ACK_INVALID;
2841  goto drop;
2842  }
2843  error0 = TCP_ERROR_ACK_OK;
2844  tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
2845  /* Apparently our ACK for the peer's FIN was lost */
2846  if (is_fin0 && tc0->snd_una != tc0->snd_una_max)
2847  {
2848  tcp_send_fin (tc0);
2849  goto drop;
2850  }
2851 
2852  tcp_connection_set_state (tc0, TCP_STATE_CLOSED);
2853 
2854  /* Don't free the connection from the data path since
2855  * we can't ensure that we have no packets already enqueued
2856  * to output. Rely instead on the waitclose timer */
2858  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
2859 
2860  goto drop;
2861 
2862  break;
2863  case TCP_STATE_TIME_WAIT:
2864  /* The only thing that can arrive in this state is a
2865  * retransmission of the remote FIN. Acknowledge it, and restart
2866  * the 2 MSL timeout. */
2867 
2868  if (tcp_rcv_ack (wrk, tc0, b0, tcp0, &error0))
2869  goto drop;
2870 
2871  tcp_program_ack (wrk, tc0);
2872  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2873  goto drop;
2874 
2875  break;
2876  default:
2877  ASSERT (0);
2878  }
2879 
2880  /* 6: check the URG bit TODO */
2881 
2882  /* 7: process the segment text */
2883  switch (tc0->state)
2884  {
2885  case TCP_STATE_ESTABLISHED:
2886  case TCP_STATE_FIN_WAIT_1:
2887  case TCP_STATE_FIN_WAIT_2:
2888  if (vnet_buffer (b0)->tcp.data_len)
2889  error0 = tcp_segment_rcv (wrk, tc0, b0);
2890  break;
2891  case TCP_STATE_CLOSE_WAIT:
2892  case TCP_STATE_CLOSING:
2893  case TCP_STATE_LAST_ACK:
2894  case TCP_STATE_TIME_WAIT:
2895  /* This should not occur, since a FIN has been received from the
2896  * remote side. Ignore the segment text. */
2897  break;
2898  }
2899 
2900  /* 8: check the FIN bit */
2901  if (!is_fin0)
2902  goto drop;
2903 
2904  TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
2905 
2906  switch (tc0->state)
2907  {
2908  case TCP_STATE_ESTABLISHED:
2909  /* Account for the FIN and send ack */
2910  tc0->rcv_nxt += 1;
2911  tcp_program_ack (wrk, tc0);
2912  tcp_connection_set_state (tc0, TCP_STATE_CLOSE_WAIT);
2913  tcp_program_disconnect (wrk, tc0);
2914  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
2915  break;
2916  case TCP_STATE_SYN_RCVD:
2917  /* Send FIN-ACK, enter LAST-ACK and because the app was not
2918  * notified yet, set a cleanup timer instead of relying on
2919  * disconnect notify and the implicit close call. */
2921  tc0->rcv_nxt += 1;
2922  tcp_send_fin (tc0);
2923  tcp_connection_set_state (tc0, TCP_STATE_LAST_ACK);
2924  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2925  break;
2926  case TCP_STATE_CLOSE_WAIT:
2927  case TCP_STATE_CLOSING:
2928  case TCP_STATE_LAST_ACK:
2929  /* move along .. */
2930  break;
2931  case TCP_STATE_FIN_WAIT_1:
2932  tc0->rcv_nxt += 1;
2933  tcp_connection_set_state (tc0, TCP_STATE_CLOSING);
2934  tcp_program_ack (wrk, tc0);
2935  /* Wait for ACK but not forever */
2936  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
2937  break;
2938  case TCP_STATE_FIN_WAIT_2:
2939  /* Got FIN, send ACK! Be more aggressive with resource cleanup */
2940  tc0->rcv_nxt += 1;
2941  tcp_connection_set_state (tc0, TCP_STATE_TIME_WAIT);
2943  tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2944  tcp_program_ack (wrk, tc0);
2945  break;
2946  case TCP_STATE_TIME_WAIT:
2947  /* Remain in the TIME-WAIT state. Restart the time-wait
2948  * timeout.
2949  */
2950  tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_TIMEWAIT_TIME);
2951  break;
2952  }
2953  error0 = TCP_ERROR_FIN_RCVD;
2954 
2955  drop:
2956 
2957  tcp_inc_counter (rcv_process, error0, 1);
2958  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2959  {
2960  tcp_rx_trace_t *t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2961  tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
2962  }
2963  }
2964 
2966  thread_index);
2967  tcp_inc_counter (rcv_process, TCP_ERROR_MSG_QUEUE_FULL, errors);
2969  tcp_handle_disconnects (wrk);
2970  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
2971 
2972  return from_frame->n_vectors;
2973 }
2974 
2975 static uword
2977  vlib_frame_t * from_frame)
2978 {
2979  return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2980 }
2981 
2982 static uword
2984  vlib_frame_t * from_frame)
2985 {
2986  return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2987 }
2988 
2989 /* *INDENT-OFF* */
2991 {
2992  .function = tcp4_rcv_process,
2993  .name = "tcp4-rcv-process",
2994  /* Takes a vector of packets. */
2995  .vector_size = sizeof (u32),
2996  .n_errors = TCP_N_ERROR,
2997  .error_strings = tcp_error_strings,
2998  .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2999  .next_nodes =
3000  {
3001 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
3003 #undef _
3004  },
3005  .format_trace = format_tcp_rx_trace_short,
3006 };
3007 /* *INDENT-ON* */
3008 
3010 
3011 /* *INDENT-OFF* */
3013 {
3014  .function = tcp6_rcv_process,
3015  .name = "tcp6-rcv-process",
3016  /* Takes a vector of packets. */
3017  .vector_size = sizeof (u32),
3018  .n_errors = TCP_N_ERROR,
3019  .error_strings = tcp_error_strings,
3020  .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
3021  .next_nodes =
3022  {
3023 #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
3025 #undef _
3026  },
3027  .format_trace = format_tcp_rx_trace_short,
3028 };
3029 /* *INDENT-ON* */
3030 
3032 
3035 
3036 /**
3037  * LISTEN state processing as per RFC 793 p. 65
3038  */
3041  vlib_frame_t * from_frame, int is_ip4)
3042 {
3043  u32 n_left_from, *from, n_syns = 0, *first_buffer;
3044  u32 my_thread_index = vm->thread_index;
3045 
3046  from = first_buffer = vlib_frame_vector_args (from_frame);
3047  n_left_from = from_frame->n_vectors;
3048 
3049  while (n_left_from > 0)
3050  {
3051  u32 bi0;
3052  vlib_buffer_t *b0;
3053  tcp_rx_trace_t *t0;
3054  tcp_header_t *th0 = 0;
3055  tcp_connection_t *lc0;
3056  ip4_header_t *ip40;
3057  ip6_header_t *ip60;
3058  tcp_connection_t *child0;
3059  u32 error0 = TCP_ERROR_NONE;
3060 
3061  bi0 = from[0];
3062  from += 1;
3063  n_left_from -= 1;
3064 
3065  b0 = vlib_get_buffer (vm, bi0);
3066  lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
3067 
3068  if (is_ip4)
3069  {
3070  ip40 = vlib_buffer_get_current (b0);
3071  th0 = ip4_next_header (ip40);
3072  }
3073  else
3074  {
3075  ip60 = vlib_buffer_get_current (b0);
3076  th0 = ip6_next_header (ip60);
3077  }
3078 
3079  /* Create child session. For syn-flood protection use filter */
3080 
3081  /* 1. first check for an RST: handled in dispatch */
3082  /* if (tcp_rst (th0))
3083  goto drop;
3084  */
3085 
3086  /* 2. second check for an ACK: handled in dispatch */
3087  /* if (tcp_ack (th0))
3088  {
3089  tcp_send_reset (b0, is_ip4);
3090  goto drop;
3091  }
3092  */
3093 
3094  /* 3. check for a SYN (did that already) */
3095 
3096  /* Make sure connection wasn't just created */
3097  child0 = tcp_lookup_connection (lc0->c_fib_index, b0, my_thread_index,
3098  is_ip4);
3099  if (PREDICT_FALSE (child0->state != TCP_STATE_LISTEN))
3100  {
3101  error0 = TCP_ERROR_CREATE_EXISTS;
3102  goto drop;
3103  }
3104 
3105  /* Create child session and send SYN-ACK */
3106  child0 = tcp_connection_alloc (my_thread_index);
3107  child0->c_lcl_port = th0->dst_port;
3108  child0->c_rmt_port = th0->src_port;
3109  child0->c_is_ip4 = is_ip4;
3110  child0->state = TCP_STATE_SYN_RCVD;
3111  child0->c_fib_index = lc0->c_fib_index;
3112 
3113  if (is_ip4)
3114  {
3115  child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
3116  child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
3117  }
3118  else
3119  {
3120  clib_memcpy_fast (&child0->c_lcl_ip6, &ip60->dst_address,
3121  sizeof (ip6_address_t));
3122  clib_memcpy_fast (&child0->c_rmt_ip6, &ip60->src_address,
3123  sizeof (ip6_address_t));
3124  }
3125 
3126  if (tcp_options_parse (th0, &child0->rcv_opts, 1))
3127  {
3128  error0 = TCP_ERROR_OPTIONS;
3129  tcp_connection_free (child0);
3130  goto drop;
3131  }
3132 
3133  child0->irs = vnet_buffer (b0)->tcp.seq_number;
3134  child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
3135  child0->rcv_las = child0->rcv_nxt;
3136  child0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3137 
3138  /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
3139  * segments are used to initialize PAWS. */
3140  if (tcp_opts_tstamp (&child0->rcv_opts))
3141  {
3142  child0->tsval_recent = child0->rcv_opts.tsval;
3143  child0->tsval_recent_age = tcp_time_now ();
3144  }
3145 
3146  if (tcp_opts_wscale (&child0->rcv_opts))
3147  child0->snd_wscale = child0->rcv_opts.wscale;
3148 
3149  child0->snd_wnd = clib_net_to_host_u16 (th0->window)
3150  << child0->snd_wscale;
3151  child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
3152  child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
3153 
3154  tcp_connection_init_vars (child0);
3155  child0->rto = TCP_RTO_MIN;
3156  TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0, 1);
3157 
3158  if (stream_session_accept (&child0->connection, lc0->c_s_index,
3159  0 /* notify */ ))
3160  {
3161  tcp_connection_cleanup (child0);
3162  error0 = TCP_ERROR_CREATE_SESSION_FAIL;
3163  goto drop;
3164  }
3165 
3166  child0->tx_fifo_size = transport_tx_fifo_size (&child0->connection);
3167  tcp_send_synack (child0);
3168  tcp_timer_set (child0, TCP_TIMER_ESTABLISH, TCP_SYN_RCVD_TIME);
3169 
3170  drop:
3171 
3172  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3173  {
3174  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
3175  clib_memcpy_fast (&t0->tcp_header, th0, sizeof (t0->tcp_header));
3176  clib_memcpy_fast (&t0->tcp_connection, lc0,
3177  sizeof (t0->tcp_connection));
3178  }
3179 
3180  n_syns += (error0 == TCP_ERROR_NONE);
3181  }
3182 
3183  tcp_inc_counter (listen, TCP_ERROR_SYNS_RCVD, n_syns);
3184  vlib_buffer_free (vm, first_buffer, from_frame->n_vectors);
3185 
3186  return from_frame->n_vectors;
3187 }
3188 
3189 static uword
3191  vlib_frame_t * from_frame)
3192 {
3193  return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
3194 }
3195 
3196 static uword
3198  vlib_frame_t * from_frame)
3199 {
3200  return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
3201 }
3202 
3203 /* *INDENT-OFF* */
3205 {
3206  .function = tcp4_listen,
3207  .name = "tcp4-listen",
3208  /* Takes a vector of packets. */
3209  .vector_size = sizeof (u32),
3210  .n_errors = TCP_N_ERROR,
3211  .error_strings = tcp_error_strings,
3212  .n_next_nodes = TCP_LISTEN_N_NEXT,
3213  .next_nodes =
3214  {
3215 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
3217 #undef _
3218  },
3219  .format_trace = format_tcp_rx_trace_short,
3220 };
3221 /* *INDENT-ON* */
3222 
3224 
3225 /* *INDENT-OFF* */
3227 {
3228  .function = tcp6_listen,
3229  .name = "tcp6-listen",
3230  /* Takes a vector of packets. */
3231  .vector_size = sizeof (u32),
3232  .n_errors = TCP_N_ERROR,
3233  .error_strings = tcp_error_strings,
3234  .n_next_nodes = TCP_LISTEN_N_NEXT,
3235  .next_nodes =
3236  {
3237 #define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
3239 #undef _
3240  },
3241  .format_trace = format_tcp_rx_trace_short,
3242 };
3243 /* *INDENT-ON* */
3244 
3246 
3249 
3250 typedef enum _tcp_input_next
3251 {
3261 
3262 #define foreach_tcp4_input_next \
3263  _ (DROP, "ip4-drop") \
3264  _ (LISTEN, "tcp4-listen") \
3265  _ (RCV_PROCESS, "tcp4-rcv-process") \
3266  _ (SYN_SENT, "tcp4-syn-sent") \
3267  _ (ESTABLISHED, "tcp4-established") \
3268  _ (RESET, "tcp4-reset") \
3269  _ (PUNT, "ip4-punt")
3270 
3271 #define foreach_tcp6_input_next \
3272  _ (DROP, "ip6-drop") \
3273  _ (LISTEN, "tcp6-listen") \
3274  _ (RCV_PROCESS, "tcp6-rcv-process") \
3275  _ (SYN_SENT, "tcp6-syn-sent") \
3276  _ (ESTABLISHED, "tcp6-established") \
3277  _ (RESET, "tcp6-reset") \
3278  _ (PUNT, "ip6-punt")
3279 
3280 #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
3281 
3282 static void
3284  vlib_buffer_t ** bs, u32 n_bufs, u8 is_ip4)
3285 {
3286  tcp_connection_t *tc;
3287  tcp_header_t *tcp;
3288  tcp_rx_trace_t *t;
3289  int i;
3290 
3291  for (i = 0; i < n_bufs; i++)
3292  {
3293  if (bs[i]->flags & VLIB_BUFFER_IS_TRACED)
3294  {
3295  t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
3296  tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
3297  vm->thread_index);
3298  tcp = vlib_buffer_get_current (bs[i]);
3299  tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
3300  }
3301  }
3302 }
3303 
3304 static void
3305 tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
3306 {
3307  if (*error == TCP_ERROR_FILTERED || *error == TCP_ERROR_WRONG_THREAD)
3308  {
3309  *next = TCP_INPUT_NEXT_DROP;
3310  }
3311  else if ((is_ip4 && tm->punt_unknown4) || (!is_ip4 && tm->punt_unknown6))
3312  {
3313  *next = TCP_INPUT_NEXT_PUNT;
3314  *error = TCP_ERROR_PUNT;
3315  }
3316  else
3317  {
3318  *next = TCP_INPUT_NEXT_RESET;
3319  *error = TCP_ERROR_NO_LISTENER;
3320  }
3321 }
3322 
3323 static inline tcp_connection_t *
3324 tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
3325  u8 is_ip4)
3326 {
3327  u32 fib_index = vnet_buffer (b)->ip.fib_index;
3328  int n_advance_bytes, n_data_bytes;
3330  tcp_header_t *tcp;
3331  u8 result = 0;
3332 
3333  if (is_ip4)
3334  {
3336  int ip_hdr_bytes = ip4_header_bytes (ip4);
3337  if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
3338  {
3339  *error = TCP_ERROR_LENGTH;
3340  return 0;
3341  }
3342  tcp = ip4_next_header (ip4);
3343  vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
3344  n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
3345  n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
3346 
3347  /* Length check. Checksum computed by ipx_local no need to compute again */
3348  if (PREDICT_FALSE (n_data_bytes < 0))
3349  {
3350  *error = TCP_ERROR_LENGTH;
3351  return 0;
3352  }
3353 
3354  tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
3355  &ip4->src_address, tcp->dst_port,
3356  tcp->src_port, TRANSPORT_PROTO_TCP,
3357  thread_index, &result);
3358  }
3359  else
3360  {
3362  if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
3363  {
3364  *error = TCP_ERROR_LENGTH;
3365  return 0;
3366  }
3367  tcp = ip6_next_header (ip6);
3368  vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
3369  n_advance_bytes = tcp_header_bytes (tcp);
3370  n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
3371  - n_advance_bytes;
3372  n_advance_bytes += sizeof (ip6[0]);
3373 
3374  if (PREDICT_FALSE (n_data_bytes < 0))
3375  {
3376  *error = TCP_ERROR_LENGTH;
3377  return 0;
3378  }
3379 
3380  tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
3381  &ip6->src_address, tcp->dst_port,
3382  tcp->src_port, TRANSPORT_PROTO_TCP,
3383  thread_index, &result);
3384  }
3385 
3386  vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
3387  vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
3388  vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
3389  vnet_buffer (b)->tcp.data_len = n_data_bytes;
3390  vnet_buffer (b)->tcp.seq_end = vnet_buffer (b)->tcp.seq_number
3391  + n_data_bytes;
3392  vnet_buffer (b)->tcp.flags = 0;
3393 
3394  *error = result ? TCP_ERROR_NONE + result : *error;
3395 
3397 }
3398 
3399 static inline void
3401  vlib_buffer_t * b, u16 * next, u32 * error)
3402 {
3403  tcp_header_t *tcp;
3404  u8 flags;
3405 
3406  tcp = tcp_buffer_hdr (b);
3407  flags = tcp->flags & filter_flags;
3408  *next = tm->dispatch_table[tc->state][flags].next;
3409  *error = tm->dispatch_table[tc->state][flags].error;
3410 
3411  if (PREDICT_FALSE (*error == TCP_ERROR_DISPATCH
3412  || *next == TCP_INPUT_NEXT_RESET))
3413  {
3414  /* Overload tcp flags to store state */
3415  tcp_state_t state = tc->state;
3416  vnet_buffer (b)->tcp.flags = tc->state;
3417 
3418  if (*error == TCP_ERROR_DISPATCH)
3419  clib_warning ("tcp conn %u disp error state %U flags %U",
3420  tc->c_c_index, format_tcp_state, state,
3421  format_tcp_flags, (int) flags);
3422  }
3423 }
3424 
3427  vlib_frame_t * frame, int is_ip4)
3428 {
3429  u32 n_left_from, *from, thread_index = vm->thread_index;
3430  tcp_main_t *tm = vnet_get_tcp_main ();
3431  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
3432  u16 nexts[VLIB_FRAME_SIZE], *next;
3433 
3434  tcp_set_time_now (tcp_get_worker (thread_index));
3435 
3436  from = vlib_frame_vector_args (frame);
3437  n_left_from = frame->n_vectors;
3438  vlib_get_buffers (vm, from, bufs, n_left_from);
3439 
3440  b = bufs;
3441  next = nexts;
3442 
3443  while (n_left_from >= 4)
3444  {
3445  u32 error0 = TCP_ERROR_NO_LISTENER, error1 = TCP_ERROR_NO_LISTENER;
3446  tcp_connection_t *tc0, *tc1;
3447 
3448  {
3449  vlib_prefetch_buffer_header (b[2], STORE);
3450  CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3451 
3452  vlib_prefetch_buffer_header (b[3], STORE);
3453  CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3454  }
3455 
3456  next[0] = next[1] = TCP_INPUT_NEXT_DROP;
3457 
3458  tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4);
3459  tc1 = tcp_input_lookup_buffer (b[1], thread_index, &error1, is_ip4);
3460 
3461  if (PREDICT_TRUE (!tc0 + !tc1 == 0))
3462  {
3463  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3464  ASSERT (tcp_lookup_is_valid (tc1, tcp_buffer_hdr (b[1])));
3465 
3466  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3467  vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
3468 
3469  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3470  tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], &error1);
3471  }
3472  else
3473  {
3474  if (PREDICT_TRUE (tc0 != 0))
3475  {
3476  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3477  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3478  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3479  }
3480  else
3481  tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
3482 
3483  if (PREDICT_TRUE (tc1 != 0))
3484  {
3485  ASSERT (tcp_lookup_is_valid (tc1, tcp_buffer_hdr (b[1])));
3486  vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
3487  tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], &error1);
3488  }
3489  else
3490  tcp_input_set_error_next (tm, &next[1], &error1, is_ip4);
3491  }
3492 
3493  b += 2;
3494  next += 2;
3495  n_left_from -= 2;
3496  }
3497  while (n_left_from > 0)
3498  {
3499  tcp_connection_t *tc0;
3500  u32 error0 = TCP_ERROR_NO_LISTENER;
3501 
3502  if (n_left_from > 1)
3503  {
3504  vlib_prefetch_buffer_header (b[1], STORE);
3505  CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3506  }
3507 
3508  next[0] = TCP_INPUT_NEXT_DROP;
3509  tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4);
3510  if (PREDICT_TRUE (tc0 != 0))
3511  {
3512  ASSERT (tcp_lookup_is_valid (tc0, tcp_buffer_hdr (b[0])));
3513  vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
3514  tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], &error0);
3515  }
3516  else
3517  tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
3518 
3519  b += 1;
3520  next += 1;
3521  n_left_from -= 1;
3522  }
3523 
3525  tcp_input_trace_frame (vm, node, bufs, frame->n_vectors, is_ip4);
3526 
3527  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
3528  return frame->n_vectors;
3529 }
3530 
3531 static uword
3533  vlib_frame_t * from_frame)
3534 {
3535  return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
3536 }
3537 
3538 static uword
3540  vlib_frame_t * from_frame)
3541 {
3542  return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
3543 }
3544 
3545 /* *INDENT-OFF* */
3547 {
3548  .function = tcp4_input,
3549  .name = "tcp4-input",
3550  /* Takes a vector of packets. */
3551  .vector_size = sizeof (u32),
3552  .n_errors = TCP_N_ERROR,
3553  .error_strings = tcp_error_strings,
3554  .n_next_nodes = TCP_INPUT_N_NEXT,
3555  .next_nodes =
3556  {
3557 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3559 #undef _
3560  },
3561  .format_buffer = format_tcp_header,
3562  .format_trace = format_tcp_rx_trace,
3563 };
3564 /* *INDENT-ON* */
3565 
3567 
3568 /* *INDENT-OFF* */
3570 {
3571  .function = tcp6_input,
3572  .name = "tcp6-input",
3573  /* Takes a vector of packets. */
3574  .vector_size = sizeof (u32),
3575  .n_errors = TCP_N_ERROR,
3576  .error_strings = tcp_error_strings,
3577  .n_next_nodes = TCP_INPUT_N_NEXT,
3578  .next_nodes =
3579  {
3580 #define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3582 #undef _
3583  },
3584  .format_buffer = format_tcp_header,
3585  .format_trace = format_tcp_rx_trace,
3586 };
3587 /* *INDENT-ON* */
3588 
3590 
3591 static void
3593 {
3594  int i, j;
3595  for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
3596  for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
3597  {
3598  tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
3599  tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
3600  }
3601 
3602 #define _(t,f,n,e) \
3603 do { \
3604  tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
3605  tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
3606 } while (0)
3607 
3608  /* RFC 793: In LISTEN if RST drop and if ACK return RST */
3609  _(LISTEN, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3610  _(LISTEN, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_ACK_INVALID);
3611  _(LISTEN, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_INVALID_CONNECTION);
3612  _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
3614  TCP_ERROR_ACK_INVALID);
3616  TCP_ERROR_SEGMENT_INVALID);
3618  TCP_ERROR_SEGMENT_INVALID);
3620  TCP_ERROR_INVALID_CONNECTION);
3621  _(LISTEN, TCP_FLAG_FIN, TCP_INPUT_NEXT_RESET, TCP_ERROR_SEGMENT_INVALID);
3623  TCP_ERROR_SEGMENT_INVALID);
3625  TCP_ERROR_SEGMENT_INVALID);
3627  TCP_ERROR_NONE);
3629  TCP_ERROR_SEGMENT_INVALID);
3631  TCP_ERROR_SEGMENT_INVALID);
3633  TCP_ERROR_SEGMENT_INVALID);
3635  TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3636  /* ACK for for a SYN-ACK -> tcp-rcv-process. */
3637  _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3638  _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3640  TCP_ERROR_NONE);
3641  _(SYN_RCVD, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3643  TCP_ERROR_NONE);
3645  TCP_ERROR_NONE);
3646  _(SYN_RCVD, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3647  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3648  _(SYN_RCVD, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3650  TCP_ERROR_NONE);
3652  TCP_ERROR_NONE);
3653  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3654  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3656  TCP_ERROR_NONE);
3657  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3658  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3659  _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3660  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3662  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3663  _(SYN_RCVD, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3664  /* SYN-ACK for a SYN */
3666  TCP_ERROR_NONE);
3667  _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3668  _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3670  TCP_ERROR_NONE);
3671  /* ACK for for established connection -> tcp-established. */
3672  _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3673  /* FIN for for established connection -> tcp-established. */
3674  _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3676  TCP_ERROR_NONE);
3678  TCP_ERROR_NONE);
3679  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3680  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3682  TCP_ERROR_NONE);
3683  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3684  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3685  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3686  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3687  _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3688  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3689  _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3691  TCP_ERROR_NONE);
3692  _(ESTABLISHED, TCP_FLAG_SYN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3694  TCP_ERROR_NONE);
3696  TCP_ERROR_NONE);
3697  _(ESTABLISHED, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3698  TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3699  _(ESTABLISHED, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3700  /* ACK or FIN-ACK to our FIN */
3701  _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3703  TCP_ERROR_NONE);
3704  /* FIN in reply to our FIN from the other side */
3705  _(FIN_WAIT_1, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3706  _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3708  TCP_ERROR_NONE);
3709  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3710  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3711  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3712  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3713  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3714  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3716  TCP_ERROR_NONE);
3717  _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3718  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3719  _(FIN_WAIT_1, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3721  TCP_ERROR_NONE);
3723  TCP_ERROR_NONE);
3724  _(FIN_WAIT_1, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3725  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3726  _(FIN_WAIT_1, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3728  TCP_ERROR_NONE);
3729  _(CLOSING, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3730  _(CLOSING, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3731  _(CLOSING, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3733  TCP_ERROR_NONE);
3735  TCP_ERROR_NONE);
3736  _(CLOSING, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3737  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3738  _(CLOSING, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3740  TCP_ERROR_NONE);
3741  _(CLOSING, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3743  TCP_ERROR_NONE);
3745  TCP_ERROR_NONE);
3746  _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3747  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3749  TCP_ERROR_NONE);
3750  _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3751  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3753  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3754  /* FIN confirming that the peer (app) has closed */
3755  _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3756  _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3758  TCP_ERROR_NONE);
3759  _(FIN_WAIT_2, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3761  TCP_ERROR_NONE);
3762  _(CLOSE_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3764  TCP_ERROR_NONE);
3765  _(CLOSE_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3767  TCP_ERROR_NONE);
3768  _(LAST_ACK, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
3769  _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3770  _(LAST_ACK, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3772  TCP_ERROR_NONE);
3774  TCP_ERROR_NONE);
3775  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3776  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3778  TCP_ERROR_NONE);
3779  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3780  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3781  _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3782  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3784  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3785  _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3787  TCP_ERROR_NONE);
3788  _(LAST_ACK, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3790  TCP_ERROR_NONE);
3792  TCP_ERROR_NONE);
3793  _(LAST_ACK, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3794  TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3795  _(TIME_WAIT, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3796  _(TIME_WAIT, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3798  TCP_ERROR_NONE);
3799  _(TIME_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3801  TCP_ERROR_NONE);
3802  _(TIME_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3803  /* RFC793 CLOSED: An incoming segment containing a RST is discarded. An
3804  * incoming segment not containing a RST causes a RST to be sent in
3805  * response.*/
3806  _(CLOSED, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
3808  TCP_ERROR_CONNECTION_CLOSED);
3809  _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
3810  _(CLOSED, TCP_FLAG_SYN, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
3812  TCP_ERROR_NONE);
3813 #undef _
3814 }
3815 
3816 static clib_error_t *
3818 {
3819  clib_error_t *error = 0;
3820  tcp_main_t *tm = vnet_get_tcp_main ();
3821 
3822  if ((error = vlib_call_init_function (vm, tcp_init)))
3823  return error;
3824 
3825  /* Initialize dispatch table. */
3827 
3828  return error;
3829 }
3830 
3832 
3833 /*
3834  * fd.io coding-style-patch-verification: ON
3835  *
3836  * Local Variables:
3837  * eval: (c-set-style "gnu")
3838  * End:
3839  */
static void tcp_program_disconnect(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_input.c:1625
#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:1805
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:2634
#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:1656
#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:3539
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:2083
unsigned long u64
Definition: types.h:89
#define tcp_store_err_counters(node_id, cnts)
Definition: tcp_input.c:2087
static void tcp_dispatch_table_init(tcp_main_t *tm)
Definition: tcp_input.c:3592
#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:1994
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:1908
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
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:2097
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:2633
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:1974
static u8 tcp_sack_vector_is_sane(sack_block_t *sacks)
Definition: tcp_input.c:1672
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:3197
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:2075
vlib_node_registration_t tcp6_syn_sent_node
(constructor) VLIB_REGISTER_NODE (tcp6_syn_sent_node)
Definition: tcp_input.c:2240
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:2281
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:3190
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:2983
static uword tcp4_syn_sent(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_input.c:2576
#define seq_gt(_s1, _s2)
Definition: tcp.h:647
static u8 * format_tcp_rx_trace(u8 *s, va_list *args)
Definition: tcp_input.c:1978
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:3248
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:2059
#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:1635
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:3040
#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:3305
unsigned short u16
Definition: types.h:57
#define foreach_tcp4_input_next
Definition: tcp_input.c:3262
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:3280
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:1875
#define foreach_tcp6_input_next
Definition: tcp_input.c:3271
#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:3283
#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:923
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:3033
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:2243
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:1749
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:1859
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
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:2583
#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:2239
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:2009
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:1973
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:3817
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:2181
#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:2025
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:1695
#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:2188
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:3247
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:2976
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:1759
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:244
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:3532
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:3324
u16 flags
Copy of main node flags.
Definition: node.h:531
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:3034
#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:3426
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:2322
#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:2641
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:3400
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