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