FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
tcp_output.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vnet/tcp/tcp.h>
17 #include <vnet/lisp-cp/packets.h>
18 #include <math.h>
19 
22 
23 typedef enum _tcp_output_next
24 {
31 
32 #define foreach_tcp4_output_next \
33  _ (DROP, "error-drop") \
34  _ (IP_LOOKUP, "ip4-lookup") \
35  _ (IP_REWRITE, "ip4-rewrite") \
36  _ (IP_ARP, "ip4-arp")
37 
38 #define foreach_tcp6_output_next \
39  _ (DROP, "error-drop") \
40  _ (IP_LOOKUP, "ip6-lookup") \
41  _ (IP_REWRITE, "ip6-rewrite") \
42  _ (IP_ARP, "ip6-discover-neighbor")
43 
44 static char *tcp_error_strings[] = {
45 #define tcp_error(n,s) s,
46 #include <vnet/tcp/tcp_error.def>
47 #undef tcp_error
48 };
49 
50 typedef struct
51 {
55 
56 u16 dummy_mtu = 1460;
57 
58 u8 *
59 format_tcp_tx_trace (u8 * s, va_list * args)
60 {
61  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63  tcp_tx_trace_t *t = va_arg (*args, tcp_tx_trace_t *);
64  u32 indent = format_get_indent (s);
65 
66  s = format (s, "%U\n%U%U",
67  format_tcp_header, &t->tcp_header, 128,
68  format_white_space, indent,
70 
71  return s;
72 }
73 
74 static u8
76 {
77  u8 wnd_scale = 0;
78  while (wnd_scale < TCP_MAX_WND_SCALE && (window >> wnd_scale) > TCP_WND_MAX)
79  wnd_scale++;
80  return wnd_scale;
81 }
82 
83 /**
84  * Update max segment size we're able to process.
85  *
86  * The value is constrained by our interface's MTU and IP options. It is
87  * also what we advertise to our peer.
88  */
89 void
91 {
92  /* TODO find our iface MTU */
93  tc->mss = dummy_mtu - sizeof (tcp_header_t);
94 }
95 
96 /**
97  * TCP's initial window
98  */
101 {
102  /* RFC 6928 recommends the value lower. However at the time our connections
103  * are initialized, fifos may not be allocated. Therefore, advertise the
104  * smallest possible unscaled window size and update once fifos are
105  * assigned to the session.
106  */
107  /*
108  tcp_update_rcv_mss (tc);
109  TCP_IW_N_SEGMENTS * tc->mss;
110  */
111  return TCP_MIN_RX_FIFO_SIZE;
112 }
113 
114 /**
115  * Compute initial window and scale factor. As per RFC1323, window field in
116  * SYN and SYN-ACK segments is never scaled.
117  */
118 u32
120 {
121  tcp_main_t *tm = &tcp_main;
122  u32 max_fifo;
123 
124  /* Initial wnd for SYN. Fifos are not allocated yet.
125  * Use some predefined value. For SYN-ACK we still want the
126  * scale to be computed in the same way */
127  max_fifo = tm->max_rx_fifo ? tm->max_rx_fifo : TCP_MAX_RX_FIFO_SIZE;
128 
129  /* Compute rcv wscale only if peer advertised support for it */
130  if (tc->state != TCP_STATE_SYN_RCVD || tcp_opts_wscale (&tc->rcv_opts))
131  tc->rcv_wscale = tcp_window_compute_scale (max_fifo);
132 
133  tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
134 
135  return clib_min (tc->rcv_wnd, TCP_WND_MAX);
136 }
137 
138 static void
140 {
141  i32 observed_wnd;
142  u32 available_space, max_fifo, wnd;
143 
144  /*
145  * Figure out how much space we have available
146  */
147  available_space = transport_max_rx_enqueue (&tc->connection);
148  max_fifo = transport_rx_fifo_size (&tc->connection);
149 
150  ASSERT (tc->rcv_opts.mss < max_fifo);
151  if (available_space < tc->rcv_opts.mss && available_space < max_fifo >> 3)
152  available_space = 0;
153 
154  /*
155  * Use the above and what we know about what we've previously advertised
156  * to compute the new window
157  */
158  observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
159  if (observed_wnd < 0)
160  observed_wnd = 0;
161 
162  /* Bad. Thou shalt not shrink */
163  if (available_space < observed_wnd)
164  {
165  wnd = observed_wnd;
166  TCP_EVT_DBG (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
167  }
168  else
169  {
170  wnd = available_space;
171  }
172 
173  /* Make sure we have a multiple of rcv_wscale */
174  if (wnd && tc->rcv_wscale)
175  {
176  wnd &= ~(1 << tc->rcv_wscale);
177  if (wnd == 0)
178  wnd = 1 << tc->rcv_wscale;
179  }
180 
181  tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale);
182 }
183 
184 /**
185  * Compute and return window to advertise, scaled as per RFC1323
186  */
187 static u32
189 {
190  if (state < TCP_STATE_ESTABLISHED)
192 
193  tcp_update_rcv_wnd (tc);
194 
195  if (tc->rcv_wnd == 0)
196  {
197  tc->flags |= TCP_CONN_SENT_RCV_WND0;
198  }
199  else
200  {
201  tc->flags &= ~TCP_CONN_SENT_RCV_WND0;
202  }
203 
204  return tc->rcv_wnd >> tc->rcv_wscale;
205 }
206 
207 /**
208  * Write TCP options to segment.
209  */
210 static u32
212 {
213  u32 opts_len = 0;
214  u32 buf, seq_len = 4;
215 
216  if (tcp_opts_mss (opts))
217  {
218  *data++ = TCP_OPTION_MSS;
219  *data++ = TCP_OPTION_LEN_MSS;
220  buf = clib_host_to_net_u16 (opts->mss);
221  clib_memcpy_fast (data, &buf, sizeof (opts->mss));
222  data += sizeof (opts->mss);
223  opts_len += TCP_OPTION_LEN_MSS;
224  }
225 
226  if (tcp_opts_wscale (opts))
227  {
228  *data++ = TCP_OPTION_WINDOW_SCALE;
229  *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
230  *data++ = opts->wscale;
231  opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
232  }
233 
234  if (tcp_opts_sack_permitted (opts))
235  {
236  *data++ = TCP_OPTION_SACK_PERMITTED;
238  opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
239  }
240 
241  if (tcp_opts_tstamp (opts))
242  {
243  *data++ = TCP_OPTION_TIMESTAMP;
244  *data++ = TCP_OPTION_LEN_TIMESTAMP;
245  buf = clib_host_to_net_u32 (opts->tsval);
246  clib_memcpy_fast (data, &buf, sizeof (opts->tsval));
247  data += sizeof (opts->tsval);
248  buf = clib_host_to_net_u32 (opts->tsecr);
249  clib_memcpy_fast (data, &buf, sizeof (opts->tsecr));
250  data += sizeof (opts->tsecr);
251  opts_len += TCP_OPTION_LEN_TIMESTAMP;
252  }
253 
254  if (tcp_opts_sack (opts))
255  {
256  int i;
257  u32 n_sack_blocks = clib_min (vec_len (opts->sacks),
259 
260  if (n_sack_blocks != 0)
261  {
262  *data++ = TCP_OPTION_SACK_BLOCK;
263  *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
264  for (i = 0; i < n_sack_blocks; i++)
265  {
266  buf = clib_host_to_net_u32 (opts->sacks[i].start);
267  clib_memcpy_fast (data, &buf, seq_len);
268  data += seq_len;
269  buf = clib_host_to_net_u32 (opts->sacks[i].end);
270  clib_memcpy_fast (data, &buf, seq_len);
271  data += seq_len;
272  }
273  opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
274  }
275  }
276 
277  /* Terminate TCP options */
278  if (opts_len % 4)
279  {
280  *data++ = TCP_OPTION_EOL;
281  opts_len += TCP_OPTION_LEN_EOL;
282  }
283 
284  /* Pad with zeroes to a u32 boundary */
285  while (opts_len % 4)
286  {
287  *data++ = TCP_OPTION_NOOP;
288  opts_len += TCP_OPTION_LEN_NOOP;
289  }
290  return opts_len;
291 }
292 
293 static int
295 {
296  u8 len = 0;
297 
298  opts->flags |= TCP_OPTS_FLAG_MSS;
299  opts->mss = dummy_mtu; /*XXX discover that */
300  len += TCP_OPTION_LEN_MSS;
301 
302  opts->flags |= TCP_OPTS_FLAG_WSCALE;
303  opts->wscale = wnd_scale;
305 
306  opts->flags |= TCP_OPTS_FLAG_TSTAMP;
307  opts->tsval = tcp_time_now ();
308  opts->tsecr = 0;
310 
311  if (TCP_USE_SACKS)
312  {
313  opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
315  }
316 
317  /* Align to needed boundary */
318  len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
319  return len;
320 }
321 
322 static int
324 {
325  u8 len = 0;
326 
327  opts->flags |= TCP_OPTS_FLAG_MSS;
328  opts->mss = tc->mss;
329  len += TCP_OPTION_LEN_MSS;
330 
331  if (tcp_opts_wscale (&tc->rcv_opts))
332  {
333  opts->flags |= TCP_OPTS_FLAG_WSCALE;
334  opts->wscale = tc->rcv_wscale;
336  }
337 
338  if (tcp_opts_tstamp (&tc->rcv_opts))
339  {
340  opts->flags |= TCP_OPTS_FLAG_TSTAMP;
341  opts->tsval = tcp_time_now ();
342  opts->tsecr = tc->tsval_recent;
344  }
345 
346  if (tcp_opts_sack_permitted (&tc->rcv_opts))
347  {
348  opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
350  }
351 
352  /* Align to needed boundary */
353  len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
354  return len;
355 }
356 
357 static int
359 {
360  u8 len = 0;
361 
362  opts->flags = 0;
363 
364  if (tcp_opts_tstamp (&tc->rcv_opts))
365  {
366  opts->flags |= TCP_OPTS_FLAG_TSTAMP;
367  opts->tsval = tcp_time_now_w_thread (tc->c_thread_index);
368  opts->tsecr = tc->tsval_recent;
370  }
371  if (tcp_opts_sack_permitted (&tc->rcv_opts))
372  {
373  if (vec_len (tc->snd_sacks))
374  {
375  opts->flags |= TCP_OPTS_FLAG_SACK;
376  opts->sacks = tc->snd_sacks;
377  opts->n_sack_blocks = clib_min (vec_len (tc->snd_sacks),
379  len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
380  }
381  }
382 
383  /* Align to needed boundary */
384  len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
385  return len;
386 }
387 
388 always_inline int
391 {
392  switch (state)
393  {
394  case TCP_STATE_ESTABLISHED:
395  case TCP_STATE_CLOSE_WAIT:
396  case TCP_STATE_FIN_WAIT_1:
397  case TCP_STATE_LAST_ACK:
398  case TCP_STATE_CLOSING:
399  case TCP_STATE_FIN_WAIT_2:
400  case TCP_STATE_TIME_WAIT:
401  case TCP_STATE_CLOSED:
402  return tcp_make_established_options (tc, opts);
403  case TCP_STATE_SYN_RCVD:
404  return tcp_make_synack_options (tc, opts);
405  case TCP_STATE_SYN_SENT:
406  return tcp_make_syn_options (opts, tc->rcv_wscale);
407  default:
408  clib_warning ("State not handled! %d", state);
409  return 0;
410  }
411 }
412 
413 /**
414  * Update burst send vars
415  *
416  * - Updates snd_mss to reflect the effective segment size that we can send
417  * by taking into account all TCP options, including SACKs.
418  * - Cache 'on the wire' options for reuse
419  * - Updates receive window which can be reused for a burst.
420  *
421  * This should *only* be called when doing bursts
422  */
423 void
425 {
426  tcp_main_t *tm = &tcp_main;
427 
428  /* Compute options to be used for connection. These may be reused when
429  * sending data or to compute the effective mss (snd_mss) */
430  tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts,
431  TCP_STATE_ESTABLISHED);
432 
433  /* XXX check if MTU has been updated */
434  tc->snd_mss = clib_min (tc->mss, tc->rcv_opts.mss) - tc->snd_opts_len;
435  ASSERT (tc->snd_mss > 0);
436 
437  tcp_options_write (tm->wrk_ctx[tc->c_thread_index].cached_opts,
438  &tc->snd_opts);
439 
440  tcp_update_rcv_wnd (tc);
441 }
442 
443 void
445 {
446  u16 default_min_mss = 536;
447  tcp_update_rcv_mss (tc);
448 
449  /* TODO cache mss and consider PMTU discovery */
450  tc->snd_mss = clib_min (tc->rcv_opts.mss, tc->mss);
451 
452  if (tc->snd_mss < 45)
453  {
454  /* Assume that at least the min default mss works */
455  tc->snd_mss = default_min_mss;
456  tc->rcv_opts.mss = default_min_mss;
457  }
458 
459  /* We should have enough space for 40 bytes of options */
460  ASSERT (tc->snd_mss > 45);
461 
462  /* If we use timestamp option, account for it */
463  if (tcp_opts_tstamp (&tc->rcv_opts))
464  tc->snd_mss -= TCP_OPTION_LEN_TIMESTAMP;
465 }
466 
467 static int
468 tcp_alloc_tx_buffers (tcp_worker_ctx_t * wrk, u16 * n_bufs, u32 wanted)
469 {
471  u32 n_alloc;
472 
473  ASSERT (wanted > *n_bufs);
475  n_alloc = vlib_buffer_alloc (vm, &wrk->tx_buffers[*n_bufs],
476  wanted - *n_bufs);
477  *n_bufs += n_alloc;
478  _vec_len (wrk->tx_buffers) = *n_bufs;
479  return n_alloc;
480 }
481 
482 always_inline int
484 {
485  u16 n_bufs = vec_len (wrk->tx_buffers);
486 
488 
489  if (PREDICT_FALSE (!n_bufs))
490  {
491  if (!tcp_alloc_tx_buffers (wrk, &n_bufs, VLIB_FRAME_SIZE))
492  {
493  *bidx = ~0;
494  return -1;
495  }
496  }
497  *bidx = wrk->tx_buffers[--n_bufs];
498  _vec_len (wrk->tx_buffers) = n_bufs;
499  return 0;
500 }
501 
502 static void *
504 {
505  if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
507  /* Zero all flags but free list index and trace flag */
508  b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
509  b->current_data = 0;
510  b->current_length = 0;
512  vnet_buffer (b)->tcp.flags = 0;
513 
514  /* Leave enough space for headers */
516 }
517 
518 static void *
520 {
521  ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
522  b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
523  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
525  b->current_data = 0;
526  vnet_buffer (b)->tcp.flags = 0;
528  /* Leave enough space for headers */
530 }
531 
532 /**
533  * Prepare ACK
534  */
535 static void
537  u8 flags)
538 {
539  tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
540  u8 tcp_opts_len, tcp_hdr_opts_len;
541  tcp_header_t *th;
542  u16 wnd;
543 
544  wnd = tcp_window_to_advertise (tc, state);
545 
546  /* Make and write options */
547  tcp_opts_len = tcp_make_established_options (tc, snd_opts);
548  tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
549 
550  th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
551  tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
552 
553  tcp_options_write ((u8 *) (th + 1), snd_opts);
554  vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
555 }
556 
557 /**
558  * Convert buffer to ACK
559  */
560 void
562 {
564 
565  tcp_reuse_buffer (vm, b);
566  tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
567  TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc);
568  tc->rcv_las = tc->rcv_nxt;
569 }
570 
571 /**
572  * Convert buffer to FIN-ACK
573  */
574 void
576 {
578  u8 flags = 0;
579 
580  tcp_reuse_buffer (vm, b);
581 
582  flags = TCP_FLAG_FIN | TCP_FLAG_ACK;
583  tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, flags);
584 
585  /* Reset flags, make sure ack is sent */
586  vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
587 }
588 
589 /**
590  * Convert buffer to SYN
591  */
592 void
594 {
595  u8 tcp_hdr_opts_len, tcp_opts_len;
596  tcp_header_t *th;
597  u16 initial_wnd;
598  tcp_options_t snd_opts;
599 
600  initial_wnd = tcp_initial_window_to_advertise (tc);
601 
602  /* Make and write options */
603  clib_memset (&snd_opts, 0, sizeof (snd_opts));
604  tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
605  tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
606 
607  th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
608  tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
609  initial_wnd);
610  vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
611  tcp_options_write ((u8 *) (th + 1), &snd_opts);
612 }
613 
614 /**
615  * Convert buffer to SYN-ACK
616  */
617 void
619 {
621  tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
622  u8 tcp_opts_len, tcp_hdr_opts_len;
623  tcp_header_t *th;
624  u16 initial_wnd;
625 
626  clib_memset (snd_opts, 0, sizeof (*snd_opts));
627  tcp_reuse_buffer (vm, b);
628 
629  initial_wnd = tcp_initial_window_to_advertise (tc);
630  tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
631  tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
632 
633  th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
634  tc->rcv_nxt, tcp_hdr_opts_len,
635  TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
636  tcp_options_write ((u8 *) (th + 1), snd_opts);
637 
638  vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
639 
640  /* Init retransmit timer. Use update instead of set because of
641  * retransmissions */
643  TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc);
644 }
645 
646 always_inline void
648  u8 is_ip4, u32 fib_index, u8 flush)
649 {
650  vlib_main_t *vm = wrk->vm;
651  u32 *to_next, next_index;
652  vlib_frame_t *f;
653 
654  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
655  b->error = 0;
656 
657  vnet_buffer (b)->sw_if_index[VLIB_TX] = fib_index;
658  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
659 
660  /* Send to IP lookup */
661  next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
663 
664  f = wrk->ip_lookup_tx_frames[!is_ip4];
665  if (!f)
666  {
667  f = vlib_get_frame_to_node (vm, next_index);
668  ASSERT (f);
669  wrk->ip_lookup_tx_frames[!is_ip4] = f;
670  }
671 
672  to_next = vlib_frame_vector_args (f);
673  to_next[f->n_vectors] = bi;
674  f->n_vectors += 1;
675  if (flush || f->n_vectors == VLIB_FRAME_SIZE)
676  {
677  vlib_put_frame_to_node (vm, next_index, f);
678  wrk->ip_lookup_tx_frames[!is_ip4] = 0;
679  }
680 }
681 
682 static void
684  u32 bi, u8 is_ip4, u32 fib_index)
685 {
686  tcp_enqueue_to_ip_lookup_i (wrk, b, bi, is_ip4, fib_index, 1);
687 }
688 
689 static void
691  u8 is_ip4, u32 fib_index)
692 {
693  tcp_enqueue_to_ip_lookup_i (wrk, b, bi, is_ip4, fib_index, 0);
694  if (wrk->vm->thread_index == 0 && vlib_num_workers ())
696 }
697 
698 always_inline void
700  u8 is_ip4, u8 flush)
701 {
702  u32 *to_next, next_index;
703  vlib_frame_t *f;
704 
705  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
706  b->error = 0;
707 
708  /* Decide where to send the packet */
709  next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
711 
712  /* Get frame to v4/6 output node */
713  f = wrk->tx_frames[!is_ip4];
714  if (!f)
715  {
716  f = vlib_get_frame_to_node (wrk->vm, next_index);
717  ASSERT (f);
718  wrk->tx_frames[!is_ip4] = f;
719  }
720  to_next = vlib_frame_vector_args (f);
721  to_next[f->n_vectors] = bi;
722  f->n_vectors += 1;
723  if (flush || f->n_vectors == VLIB_FRAME_SIZE)
724  {
725  vlib_put_frame_to_node (wrk->vm, next_index, f);
726  wrk->tx_frames[!is_ip4] = 0;
727  }
728 }
729 
730 static void
732  u8 is_ip4)
733 {
734  tcp_enqueue_to_output_i (wrk, b, bi, is_ip4, 0);
735 }
736 
737 static void
739  u8 is_ip4)
740 {
741  tcp_enqueue_to_output_i (wrk, b, bi, is_ip4, 1);
742 }
743 
744 static int
746  tcp_state_t state, u8 thread_index, u8 is_ip4)
747 {
748  ip4_header_t *ih4;
749  ip6_header_t *ih6;
750  tcp_header_t *th0;
751  ip4_address_t src_ip40, dst_ip40;
752  ip6_address_t src_ip60, dst_ip60;
754  u32 tmp;
755  u32 seq, ack;
756  u8 flags;
757 
758  /* Find IP and TCP headers */
759  th0 = tcp_buffer_hdr (b0);
760 
761  /* Save src and dst ip */
762  if (is_ip4)
763  {
764  ih4 = vlib_buffer_get_current (b0);
765  ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
766  src_ip40.as_u32 = ih4->src_address.as_u32;
767  dst_ip40.as_u32 = ih4->dst_address.as_u32;
768  }
769  else
770  {
771  ih6 = vlib_buffer_get_current (b0);
772  ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
773  clib_memcpy_fast (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
774  clib_memcpy_fast (&dst_ip60, &ih6->dst_address, sizeof (ip6_address_t));
775  }
776 
777  src_port = th0->src_port;
778  dst_port = th0->dst_port;
779 
780  /* Try to determine what/why we're actually resetting */
781  if (state == TCP_STATE_CLOSED)
782  {
783  if (!tcp_syn (th0))
784  return -1;
785 
786  tmp = clib_net_to_host_u32 (th0->seq_number);
787 
788  /* Got a SYN for no listener. */
789  flags = TCP_FLAG_RST | TCP_FLAG_ACK;
790  ack = clib_host_to_net_u32 (tmp + 1);
791  seq = 0;
792  }
793  else
794  {
795  flags = TCP_FLAG_RST;
796  seq = th0->ack_number;
797  ack = 0;
798  }
799 
800  tcp_reuse_buffer (vm, b0);
801  tcp_trajectory_add_start (b0, 4);
802  th0 = vlib_buffer_push_tcp_net_order (b0, dst_port, src_port, seq, ack,
803  sizeof (tcp_header_t), flags, 0);
804 
805  if (is_ip4)
806  {
807  ih4 = vlib_buffer_push_ip4 (vm, b0, &dst_ip40, &src_ip40,
808  IP_PROTOCOL_TCP, 1);
809  th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
810  }
811  else
812  {
813  int bogus = ~0;
814  ih6 = vlib_buffer_push_ip6 (vm, b0, &dst_ip60, &src_ip60,
815  IP_PROTOCOL_TCP);
816  th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
817  ASSERT (!bogus);
818  }
819 
820  return 0;
821 }
822 
823 /**
824  * Send reset without reusing existing buffer
825  *
826  * It extracts connection info out of original packet
827  */
828 void
830  u32 thread_index, u8 is_ip4)
831 {
832  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
833  vlib_main_t *vm = wrk->vm;
834  vlib_buffer_t *b;
835  u32 bi, sw_if_index, fib_index;
836  u8 tcp_hdr_len, flags = 0;
837  tcp_header_t *th, *pkt_th;
838  u32 seq, ack;
839  ip4_header_t *ih4, *pkt_ih4;
840  ip6_header_t *ih6, *pkt_ih6;
841  fib_protocol_t fib_proto;
842 
843  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
844  return;
845 
846  b = vlib_get_buffer (vm, bi);
847  sw_if_index = vnet_buffer (pkt)->sw_if_index[VLIB_RX];
848  fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
849  fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
850  tcp_init_buffer (vm, b);
851 
852  /* Make and write options */
853  tcp_hdr_len = sizeof (tcp_header_t);
854 
855  if (is_ip4)
856  {
857  pkt_ih4 = vlib_buffer_get_current (pkt);
858  pkt_th = ip4_next_header (pkt_ih4);
859  }
860  else
861  {
862  pkt_ih6 = vlib_buffer_get_current (pkt);
863  pkt_th = ip6_next_header (pkt_ih6);
864  }
865 
866  if (tcp_ack (pkt_th))
867  {
868  flags = TCP_FLAG_RST;
869  seq = pkt_th->ack_number;
870  ack = (tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0;
871  }
872  else
873  {
874  flags = TCP_FLAG_RST | TCP_FLAG_ACK;
875  seq = 0;
876  ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
877  }
878 
879  th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
880  seq, ack, tcp_hdr_len, flags, 0);
881 
882  /* Swap src and dst ip */
883  if (is_ip4)
884  {
885  ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
886  ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
887  &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1);
888  th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
889  }
890  else
891  {
892  int bogus = ~0;
893  ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
894  0x60);
895  ih6 = vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
896  &pkt_ih6->src_address, IP_PROTOCOL_TCP);
897  th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
898  ASSERT (!bogus);
899  }
900 
901  tcp_enqueue_to_ip_lookup_now (wrk, b, bi, is_ip4, fib_index);
902  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
903 }
904 
905 /**
906  * Build and set reset packet for connection
907  */
908 void
910 {
911  tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
912  vlib_main_t *vm = wrk->vm;
913  vlib_buffer_t *b;
914  u32 bi;
915  tcp_header_t *th;
916  u16 tcp_hdr_opts_len, advertise_wnd, opts_write_len;
917  u8 flags;
918 
919  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
920  return;
921  b = vlib_get_buffer (vm, bi);
922  tcp_init_buffer (vm, b);
923 
924  tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
925  tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
926  advertise_wnd = tcp_window_to_advertise (tc, TCP_STATE_ESTABLISHED);
927  flags = TCP_FLAG_RST;
928  th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
929  tc->rcv_nxt, tcp_hdr_opts_len, flags,
930  advertise_wnd);
931  opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
932  ASSERT (opts_write_len == tc->snd_opts_len);
933  vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
934  if (tc->c_is_ip4)
935  {
936  ip4_header_t *ih4;
937  ih4 = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip.ip4,
938  &tc->c_rmt_ip.ip4, IP_PROTOCOL_TCP, 0);
939  th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
940  }
941  else
942  {
943  int bogus = ~0;
944  ip6_header_t *ih6;
945  ih6 = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip.ip6,
946  &tc->c_rmt_ip.ip6, IP_PROTOCOL_TCP);
947  th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
948  ASSERT (!bogus);
949  }
950  tcp_enqueue_to_ip_lookup_now (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
951  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
952 }
953 
954 static void
956  vlib_buffer_t * b)
957 {
959  vlib_main_t *vm = wrk->vm;
960  if (tc->c_is_ip4)
961  {
962  ip4_header_t *ih;
963  ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4,
964  &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1);
965  th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih);
966  }
967  else
968  {
969  ip6_header_t *ih;
970  int bogus = ~0;
971 
972  ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
973  &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
974  th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus);
975  ASSERT (!bogus);
976  }
977 }
978 
979 /**
980  * Send SYN
981  *
982  * Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
983  * The packet is not forwarded through tcpx_output to avoid doing lookups
984  * in the half_open pool.
985  */
986 void
988 {
989  tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
990  vlib_main_t *vm = wrk->vm;
991  vlib_buffer_t *b;
992  u32 bi;
993 
994  /*
995  * Setup retransmit and establish timers before requesting buffer
996  * such that we can return if we've ran out.
997  */
998  tcp_timer_set (tc, TCP_TIMER_ESTABLISH_AO, TCP_ESTABLISH_TIME);
999  tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
1000  tc->rto * TCP_TO_TIMER_TICK);
1001 
1002  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1003  return;
1004 
1005  b = vlib_get_buffer (vm, bi);
1006  tcp_init_buffer (vm, b);
1007  tcp_make_syn (tc, b);
1008 
1009  /* Measure RTT with this */
1010  tc->rtt_ts = tcp_time_now_us (vlib_num_workers ()? 1 : 0);
1011  tc->rtt_seq = tc->snd_nxt;
1012  tc->rto_boff = 0;
1013 
1014  tcp_push_ip_hdr (wrk, tc, b);
1015  tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
1016  TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc);
1017 }
1018 
1019 void
1021 {
1022  tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1023  vlib_main_t *vm = wrk->vm;
1024  vlib_buffer_t *b;
1025  u32 bi;
1026 
1027  /* Get buffer */
1028  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1029  return;
1030 
1031  tc->rtt_ts = tcp_time_now_us (tc->c_thread_index);
1032  b = vlib_get_buffer (vm, bi);
1033  tcp_make_synack (tc, b);
1034  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1035 }
1036 
1037 /**
1038  * Flush tx frame populated by retransmits and timer pops
1039  */
1040 void
1042 {
1043  if (wrk->tx_frames[!is_ip4])
1044  {
1045  u32 next_index;
1046  next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
1047  vlib_put_frame_to_node (wrk->vm, next_index, wrk->tx_frames[!is_ip4]);
1048  wrk->tx_frames[!is_ip4] = 0;
1049  }
1050 }
1051 
1052 /**
1053  * Flush ip lookup tx frames populated by timer pops
1054  */
1055 static void
1057 {
1058  if (wrk->ip_lookup_tx_frames[!is_ip4])
1059  {
1060  u32 next_index;
1061  next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
1062  vlib_put_frame_to_node (wrk->vm, next_index,
1063  wrk->ip_lookup_tx_frames[!is_ip4]);
1064  wrk->ip_lookup_tx_frames[!is_ip4] = 0;
1065  }
1066 }
1067 
1068 /**
1069  * Flush v4 and v6 tcp and ip-lookup tx frames for thread index
1070  */
1071 void
1073 {
1074  tcp_flush_frame_to_output (wrk, 1);
1075  tcp_flush_frame_to_output (wrk, 0);
1078 }
1079 
1080 /**
1081  * Send FIN
1082  */
1083 void
1085 {
1086  tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1087  vlib_main_t *vm = wrk->vm;
1088  vlib_buffer_t *b;
1089  u32 bi;
1090  u8 fin_snt = 0;
1091 
1092  fin_snt = tc->flags & TCP_CONN_FINSNT;
1093  if (fin_snt)
1094  tc->snd_nxt = tc->snd_una;
1095 
1096  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1097  {
1098  /* Out of buffers so program fin retransmit ASAP */
1099  tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
1100  if (fin_snt)
1101  tc->snd_nxt = tc->snd_una_max;
1102  return;
1103  }
1104 
1106  b = vlib_get_buffer (vm, bi);
1107  tcp_init_buffer (vm, b);
1108  tcp_make_fin (tc, b);
1109  tcp_enqueue_to_output_now (wrk, b, bi, tc->c_is_ip4);
1110  TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc);
1111 
1112  if (!fin_snt)
1113  {
1114  tc->flags |= TCP_CONN_FINSNT;
1115  tc->flags &= ~TCP_CONN_FINPNDG;
1116  /* Account for the FIN */
1117  tc->snd_una_max += 1;
1118  tc->snd_nxt = tc->snd_una_max;
1119  }
1120  else
1121  {
1122  tc->snd_nxt = tc->snd_una_max;
1123  }
1124 }
1125 
1128 {
1129  switch (next_state)
1130  {
1131  case TCP_STATE_ESTABLISHED:
1132  case TCP_STATE_CLOSE_WAIT:
1133  case TCP_STATE_TIME_WAIT:
1134  case TCP_STATE_FIN_WAIT_2:
1135  return TCP_FLAG_ACK;
1136  case TCP_STATE_SYN_RCVD:
1137  return TCP_FLAG_SYN | TCP_FLAG_ACK;
1138  case TCP_STATE_SYN_SENT:
1139  return TCP_FLAG_SYN;
1140  case TCP_STATE_LAST_ACK:
1141  case TCP_STATE_FIN_WAIT_1:
1142  case TCP_STATE_CLOSING:
1143  if (tc->snd_nxt + 1 < tc->snd_una_max)
1144  return TCP_FLAG_ACK;
1145  else
1146  return TCP_FLAG_FIN;
1147  default:
1148  clib_warning ("Shouldn't be here!");
1149  }
1150  return 0;
1151 }
1152 
1153 /**
1154  * Push TCP header and update connection variables
1155  */
1156 always_inline void
1158  tcp_state_t next_state, u8 compute_opts, u8 maybe_burst)
1159 {
1160  u32 advertise_wnd, data_len;
1161  u8 tcp_hdr_opts_len, flags;
1162  tcp_main_t *tm = &tcp_main;
1163  tcp_header_t *th;
1164 
1165  data_len = b->current_length;
1166  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
1168 
1169  vnet_buffer (b)->tcp.flags = 0;
1170  vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
1171 
1172  if (compute_opts)
1173  tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1174 
1175  tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
1176 
1177  if (maybe_burst)
1178  advertise_wnd = tc->rcv_wnd >> tc->rcv_wscale;
1179  else
1180  advertise_wnd = tcp_window_to_advertise (tc, next_state);
1181 
1182  flags = tcp_make_state_flags (tc, next_state);
1183  if (PREDICT_FALSE (tc->flags & TCP_CONN_PSH_PENDING))
1184  {
1185  if (seq_geq (tc->psh_seq, tc->snd_nxt)
1186  && seq_lt (tc->psh_seq, tc->snd_nxt + data_len))
1187  flags |= TCP_FLAG_PSH;
1188  }
1189  th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
1190  tc->rcv_nxt, tcp_hdr_opts_len, flags,
1191  advertise_wnd);
1192 
1193  if (maybe_burst)
1194  {
1195  clib_memcpy_fast ((u8 *) (th + 1),
1196  tm->wrk_ctx[tc->c_thread_index].cached_opts,
1197  tc->snd_opts_len);
1198  }
1199  else
1200  {
1201  u8 len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
1202  ASSERT (len == tc->snd_opts_len);
1203  }
1204 
1205  /*
1206  * Update connection variables
1207  */
1208 
1209  tc->snd_nxt += data_len;
1210  tc->rcv_las = tc->rcv_nxt;
1211 
1212  TCP_EVT_DBG (TCP_EVT_PKTIZE, tc);
1213 }
1214 
1215 u32
1217 {
1218  tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED, /* compute opts */ 0,
1219  /* burst */ 1);
1220  tc->snd_una_max = tc->snd_nxt;
1221  ASSERT (seq_leq (tc->snd_una_max, tc->snd_una + tc->snd_wnd));
1222  tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1223  /* If not tracking an ACK, start tracking */
1224  if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc))
1225  {
1226  tc->rtt_ts = tcp_time_now_us (tc->c_thread_index);
1227  tc->rtt_seq = tc->snd_nxt;
1228  }
1229  if (PREDICT_FALSE (!tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT)))
1230  {
1232  tc->rto_boff = 0;
1233  }
1234  tcp_trajectory_add_start (b, 3);
1235  return 0;
1236 }
1237 
1238 void
1240 {
1241  tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
1242  vlib_main_t *vm = wrk->vm;
1243  vlib_buffer_t *b;
1244  u32 bi;
1245 
1246  /* Get buffer */
1247  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1248  return;
1249  b = vlib_get_buffer (vm, bi);
1250  tcp_init_buffer (vm, b);
1251 
1252  /* Fill in the ACK */
1253  tcp_make_ack (tc, b);
1254  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1255 }
1256 
1257 void
1259 {
1260  if (!(tc->flags & TCP_CONN_SNDACK))
1261  {
1262  vec_add1 (wrk->pending_acks, tc->c_c_index);
1263  tc->flags |= TCP_CONN_SNDACK;
1264  }
1265 }
1266 
1267 void
1269 {
1270  if (!(tc->flags & TCP_CONN_SNDACK))
1271  {
1272  vec_add1 (wrk->pending_acks, tc->c_c_index);
1273  tc->flags |= TCP_CONN_SNDACK;
1274  }
1275  if (tc->pending_dupacks < 255)
1276  tc->pending_dupacks += 1;
1277 }
1278 
1279 void
1281 {
1282  u32 thread_index, *pending_acks;
1283  tcp_connection_t *tc;
1284  int i, j, n_acks;
1285 
1286  if (!vec_len (wrk->pending_acks))
1287  return;
1288 
1289  thread_index = wrk->vm->thread_index;
1290  pending_acks = wrk->pending_acks;
1291  for (i = 0; i < vec_len (pending_acks); i++)
1292  {
1293  tc = tcp_connection_get (pending_acks[i], thread_index);
1294  tc->flags &= ~TCP_CONN_SNDACK;
1295  n_acks = clib_max (1, tc->pending_dupacks);
1296  /* If we're supposed to send dupacks but have no ooo data
1297  * send only one ack */
1298  if (tc->pending_dupacks && !vec_len (tc->snd_sacks))
1299  n_acks = 1;
1300  for (j = 0; j < n_acks; j++)
1301  tcp_send_ack (tc);
1302  tc->pending_dupacks = 0;
1303  }
1304  _vec_len (wrk->pending_acks) = 0;
1305 }
1306 
1307 /**
1308  * Delayed ack timer handler
1309  *
1310  * Sends delayed ACK when timer expires
1311  */
1312 void
1314 {
1315  u32 thread_index = vlib_get_thread_index ();
1316  tcp_connection_t *tc;
1317 
1318  tc = tcp_connection_get (index, thread_index);
1319  tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
1320  tcp_send_ack (tc);
1321 }
1322 
1323 /**
1324  * Allocate a new buffer and build a new tcp segment
1325  *
1326  * @param wrk tcp worker
1327  * @param tc connection for which the segment will be allocated
1328  * @param offset offset of the first byte in the tx fifo
1329  * @param max_deq_byte segment size
1330  * @param[out] b pointer to buffer allocated
1331  *
1332  * @return the number of bytes in the segment or 0 if buffer cannot be
1333  * allocated or no data available
1334  */
1335 static int
1337  u32 offset, u32 max_deq_bytes, vlib_buffer_t ** b)
1338 {
1339  u32 bytes_per_buffer = vnet_get_tcp_main ()->bytes_per_buffer;
1340  u32 bi, seg_size;
1341  vlib_main_t *vm = wrk->vm;
1342  int n_bytes = 0;
1343  u8 *data;
1344 
1345  seg_size = max_deq_bytes + MAX_HDRS_LEN;
1346 
1347  /*
1348  * Prepare options
1349  */
1350  tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1351 
1352  /*
1353  * Allocate and fill in buffer(s)
1354  */
1355 
1356  /* Easy case, buffer size greater than mss */
1357  if (PREDICT_TRUE (seg_size <= bytes_per_buffer))
1358  {
1359  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1360  return 0;
1361  *b = vlib_get_buffer (vm, bi);
1362  data = tcp_init_buffer (vm, *b);
1363  n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1364  max_deq_bytes);
1365  ASSERT (n_bytes == max_deq_bytes);
1366  b[0]->current_length = n_bytes;
1367  tcp_push_hdr_i (tc, *b, tc->state, /* compute opts */ 0, /* burst */ 0);
1368  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1369  tc->snd_una_max = tc->snd_nxt;
1370  }
1371  /* Split mss into multiple buffers */
1372  else
1373  {
1374  u32 chain_bi = ~0, n_bufs_per_seg;
1375  u16 n_peeked, len_to_deq, available_bufs;
1376  vlib_buffer_t *chain_b, *prev_b;
1377  int i;
1378 
1379  /* Make sure we have enough buffers */
1380  n_bufs_per_seg = ceil ((double) seg_size / bytes_per_buffer);
1381  available_bufs = vec_len (wrk->tx_buffers);
1382  if (n_bufs_per_seg > available_bufs)
1383  {
1384  tcp_alloc_tx_buffers (wrk, &available_bufs, VLIB_FRAME_SIZE);
1385  if (n_bufs_per_seg > available_bufs)
1386  {
1387  *b = 0;
1388  return 0;
1389  }
1390  }
1391 
1392  (void) tcp_get_free_buffer_index (wrk, &bi);
1393  ASSERT (bi != (u32) ~ 0);
1394  *b = vlib_get_buffer (vm, bi);
1395  data = tcp_init_buffer (vm, *b);
1396  n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1397  bytes_per_buffer - MAX_HDRS_LEN);
1398  b[0]->current_length = n_bytes;
1399  b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1401  max_deq_bytes -= n_bytes;
1402 
1403  chain_b = *b;
1404  for (i = 1; i < n_bufs_per_seg; i++)
1405  {
1406  prev_b = chain_b;
1407  len_to_deq = clib_min (max_deq_bytes, bytes_per_buffer);
1408  tcp_get_free_buffer_index (wrk, &chain_bi);
1409  ASSERT (chain_bi != (u32) ~ 0);
1410  chain_b = vlib_get_buffer (vm, chain_bi);
1411  chain_b->current_data = 0;
1412  data = vlib_buffer_get_current (chain_b);
1413  n_peeked = stream_session_peek_bytes (&tc->connection, data,
1414  offset + n_bytes, len_to_deq);
1415  ASSERT (n_peeked == len_to_deq);
1416  n_bytes += n_peeked;
1417  chain_b->current_length = n_peeked;
1418  chain_b->next_buffer = 0;
1419 
1420  /* update previous buffer */
1421  prev_b->next_buffer = chain_bi;
1422  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
1423 
1424  max_deq_bytes -= n_peeked;
1425  b[0]->total_length_not_including_first_buffer += n_peeked;
1426  }
1427 
1428  tcp_push_hdr_i (tc, *b, tc->state, /* compute opts */ 0, /* burst */ 0);
1429  if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1430  tc->snd_una_max = tc->snd_nxt;
1431  }
1432 
1433  ASSERT (n_bytes > 0);
1434  ASSERT (((*b)->current_data + (*b)->current_length) <= bytes_per_buffer);
1435 
1436  return n_bytes;
1437 }
1438 
1439 /**
1440  * Build a retransmit segment
1441  *
1442  * @return the number of bytes in the segment or 0 if there's nothing to
1443  * retransmit
1444  */
1445 static u32
1447  tcp_connection_t * tc, u32 offset,
1448  u32 max_deq_bytes, vlib_buffer_t ** b)
1449 {
1450  u32 start, available_bytes;
1451  int n_bytes = 0;
1452 
1453  ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
1454  ASSERT (max_deq_bytes != 0);
1455 
1456  /*
1457  * Make sure we can retransmit something
1458  */
1459  available_bytes = session_tx_fifo_max_dequeue (&tc->connection);
1460  ASSERT (available_bytes >= offset);
1461  available_bytes -= offset;
1462  if (!available_bytes)
1463  return 0;
1464 
1465  max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes);
1466  max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1467 
1468  /* Start is beyond snd_congestion */
1469  start = tc->snd_una + offset;
1470  if (seq_geq (start, tc->snd_congestion))
1471  goto done;
1472 
1473  /* Don't overshoot snd_congestion */
1474  if (seq_gt (start + max_deq_bytes, tc->snd_congestion))
1475  {
1476  max_deq_bytes = tc->snd_congestion - start;
1477  if (max_deq_bytes == 0)
1478  goto done;
1479  }
1480 
1481  n_bytes = tcp_prepare_segment (wrk, tc, offset, max_deq_bytes, b);
1482  if (!n_bytes)
1483  return 0;
1484 
1485  if (tcp_in_fastrecovery (tc))
1486  tc->snd_rxt_bytes += n_bytes;
1487 
1488 done:
1489  TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes);
1490  return n_bytes;
1491 }
1492 
1493 /**
1494  * Reset congestion control, switch cwnd to loss window and try again.
1495  */
1496 static void
1498 {
1499  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 6);
1500  tc->prev_ssthresh = tc->ssthresh;
1501  tc->prev_cwnd = tc->cwnd;
1502 
1503  /* Cleanly recover cc (also clears up fast retransmit) */
1504  if (tcp_in_fastrecovery (tc))
1505  {
1506  /* TODO be less aggressive about this */
1507  scoreboard_clear (&tc->sack_sb);
1509  }
1510 
1511  /* Start again from the beginning */
1512  tc->cc_algo->congestion (tc);
1513  tc->cwnd = tcp_loss_wnd (tc);
1514  tc->snd_congestion = tc->snd_una_max;
1515  tc->rtt_ts = 0;
1516  tc->cwnd_acc_bytes = 0;
1517  tcp_connection_tx_pacer_reset (tc, tc->cwnd, 2 * tc->snd_mss);
1518  tcp_recovery_on (tc);
1519 }
1520 
1521 static inline void
1523 {
1524  u32 thread_index = vlib_get_thread_index ();
1525  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
1526  vlib_main_t *vm = wrk->vm;
1527  tcp_connection_t *tc;
1528  vlib_buffer_t *b = 0;
1529  u32 bi, n_bytes;
1530 
1531  if (is_syn)
1532  {
1533  tc = tcp_half_open_connection_get (index);
1534  /* Note: the connection may have transitioned to ESTABLISHED... */
1535  if (PREDICT_FALSE (tc == 0 || tc->state != TCP_STATE_SYN_SENT))
1536  return;
1537  tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
1538  }
1539  else
1540  {
1541  tc = tcp_connection_get (index, thread_index);
1542  /* Note: the connection may have been closed and pool_put */
1543  if (PREDICT_FALSE (tc == 0 || tc->state == TCP_STATE_SYN_SENT))
1544  return;
1545  tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
1546  /* Wait-close and retransmit could pop at the same time */
1547  if (tc->state == TCP_STATE_CLOSED)
1548  return;
1549  }
1550 
1551  if (tc->state >= TCP_STATE_ESTABLISHED)
1552  {
1553  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1554 
1555  /* Lost FIN, retransmit and return */
1556  if (tcp_is_lost_fin (tc))
1557  {
1558  tcp_send_fin (tc);
1559  tc->rto_boff += 1;
1560  tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1561  return;
1562  }
1563 
1564  /* Shouldn't be here. This condition is tricky because it has to take
1565  * into account boff > 0 due to persist timeout. */
1566  if ((tc->rto_boff == 0 && tc->snd_una == tc->snd_una_max)
1567  || (tc->rto_boff > 0 && seq_geq (tc->snd_una, tc->snd_congestion)
1568  && !tcp_flight_size (tc)))
1569  {
1570  ASSERT (!tcp_in_recovery (tc));
1571  tc->rto_boff = 0;
1572  return;
1573  }
1574 
1575  /* We're not in recovery so make sure rto_boff is 0. Can be non 0 due
1576  * to persist timer timeout */
1577  if (!tcp_in_recovery (tc) && tc->rto_boff > 0)
1578  {
1579  tc->rto_boff = 0;
1580  tcp_update_rto (tc);
1581  }
1582 
1583  /* Increment RTO backoff (also equal to number of retries) and go back
1584  * to first un-acked byte */
1585  tc->rto_boff += 1;
1586 
1587  /* First retransmit timeout */
1588  if (tc->rto_boff == 1)
1589  tcp_rxt_timeout_cc (tc);
1590  else
1591  scoreboard_clear (&tc->sack_sb);
1592 
1593  /* If we've sent beyond snd_congestion, update it */
1594  if (seq_gt (tc->snd_una_max, tc->snd_congestion))
1595  tc->snd_congestion = tc->snd_una_max;
1596 
1597  tc->snd_una_max = tc->snd_nxt = tc->snd_una;
1598  tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1599 
1600  /* Send one segment. Note that n_bytes may be zero due to buffer
1601  * shortfall */
1602  n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
1603  if (!n_bytes)
1604  {
1606  return;
1607  }
1608 
1609  bi = vlib_get_buffer_index (vm, b);
1610 
1611  /* For first retransmit, record timestamp (Eifel detection RFC3522) */
1612  if (tc->rto_boff == 1)
1613  tc->snd_rxt_ts = tcp_time_now_w_thread (tc->c_thread_index);
1614 
1615  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1617  }
1618  /* Retransmit for SYN */
1619  else if (tc->state == TCP_STATE_SYN_SENT)
1620  {
1621  /* Half-open connection actually moved to established but we were
1622  * waiting for syn retransmit to pop to call cleanup from the right
1623  * thread. */
1624  if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
1625  {
1627  TCP_DBG ("could not remove half-open connection");
1628  return;
1629  }
1630 
1631  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1632 
1633  /* Try without increasing RTO a number of times. If this fails,
1634  * start growing RTO exponentially */
1635  tc->rto_boff += 1;
1636  if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1637  tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1638 
1639  tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
1640  tc->rto * TCP_TO_TIMER_TICK);
1641 
1642  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1643  return;
1644 
1645  b = vlib_get_buffer (vm, bi);
1646  tcp_init_buffer (vm, b);
1647  tcp_make_syn (tc, b);
1648 
1649  tc->rtt_ts = 0;
1650  TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc, 0);
1651 
1652  /* This goes straight to ipx_lookup. Retransmit timer set already */
1653  tcp_push_ip_hdr (wrk, tc, b);
1654  tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
1655  }
1656  /* Retransmit SYN-ACK */
1657  else if (tc->state == TCP_STATE_SYN_RCVD)
1658  {
1659  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1660 
1661  tc->rto_boff += 1;
1662  if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1663  tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1664  tc->rtt_ts = 0;
1665 
1666  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1667  {
1669  return;
1670  }
1671 
1672  b = vlib_get_buffer (vm, bi);
1673  tcp_init_buffer (vm, b);
1674  tcp_make_synack (tc, b);
1675  TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc, 1);
1676 
1677  /* Retransmit timer already updated, just enqueue to output */
1678  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1679  }
1680  else
1681  {
1682  ASSERT (tc->state == TCP_STATE_CLOSED);
1683  return;
1684  }
1685 }
1686 
1687 void
1689 {
1690  tcp_timer_retransmit_handler_i (index, 0);
1691 }
1692 
1693 void
1695 {
1696  tcp_timer_retransmit_handler_i (index, 1);
1697 }
1698 
1699 /**
1700  * Got 0 snd_wnd from peer, try to do something about it.
1701  *
1702  */
1703 void
1705 {
1706  u32 thread_index = vlib_get_thread_index ();
1707  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
1708  u32 bi, max_snd_bytes, available_bytes, offset;
1709  tcp_main_t *tm = vnet_get_tcp_main ();
1710  vlib_main_t *vm = wrk->vm;
1711  tcp_connection_t *tc;
1712  vlib_buffer_t *b;
1713  int n_bytes = 0;
1714  u8 *data;
1715 
1716  tc = tcp_connection_get_if_valid (index, thread_index);
1717 
1718  if (!tc)
1719  return;
1720 
1721  /* Make sure timer handle is set to invalid */
1722  tc->timers[TCP_TIMER_PERSIST] = TCP_TIMER_HANDLE_INVALID;
1723 
1724  /* Problem already solved or worse */
1725  if (tc->state == TCP_STATE_CLOSED || tc->state > TCP_STATE_ESTABLISHED
1726  || tc->snd_wnd > tc->snd_mss)
1727  return;
1728 
1729  available_bytes = session_tx_fifo_max_dequeue (&tc->connection);
1730  offset = tc->snd_una_max - tc->snd_una;
1731 
1732  /* Reprogram persist if no new bytes available to send. We may have data
1733  * next time */
1734  if (!available_bytes)
1735  {
1736  tcp_persist_timer_set (tc);
1737  return;
1738  }
1739 
1740  if (available_bytes <= offset)
1741  {
1742  ASSERT (tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT));
1743  return;
1744  }
1745 
1746  /* Increment RTO backoff */
1747  tc->rto_boff += 1;
1748  tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1749 
1750  /*
1751  * Try to force the first unsent segment (or buffer)
1752  */
1753  if (PREDICT_FALSE (tcp_get_free_buffer_index (wrk, &bi)))
1754  {
1755  tcp_persist_timer_set (tc);
1756  return;
1757  }
1758  b = vlib_get_buffer (vm, bi);
1759  data = tcp_init_buffer (vm, b);
1760 
1761  tcp_validate_txf_size (tc, offset);
1762  tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1763  max_snd_bytes = clib_min (tc->snd_mss, tm->bytes_per_buffer - MAX_HDRS_LEN);
1764  n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1765  max_snd_bytes);
1766  b->current_length = n_bytes;
1767  ASSERT (n_bytes != 0 && (tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT)
1768  || tc->snd_nxt == tc->snd_una_max
1769  || tc->rto_boff > 1));
1770 
1771  tcp_push_hdr_i (tc, b, tc->state, /* compute opts */ 0, /* burst */ 0);
1772  tc->snd_una_max = tc->snd_nxt;
1773  tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1774  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1775 
1776  /* Just sent new data, enable retransmit */
1778 }
1779 
1780 /**
1781  * Retransmit first unacked segment
1782  */
1783 int
1785 {
1786  u32 bi, old_snd_nxt, n_bytes;
1787  vlib_main_t *vm = wrk->vm;
1788  vlib_buffer_t *b;
1789 
1790  old_snd_nxt = tc->snd_nxt;
1791  tc->snd_nxt = tc->snd_una;
1792 
1793  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
1794 
1795  n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
1796  if (!n_bytes)
1797  return -1;
1798 
1799  bi = vlib_get_buffer_index (vm, b);
1800  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1801  tc->snd_nxt = old_snd_nxt;
1802 
1803  return 0;
1804 }
1805 
1806 static int
1808  u32 burst_size)
1809 {
1810  u32 offset, n_segs = 0, n_written, bi;
1811  vlib_main_t *vm = wrk->vm;
1812  vlib_buffer_t *b = 0;
1813 
1814  tc->snd_nxt = tc->snd_una_max;
1815  offset = tc->snd_una_max - tc->snd_una;
1816  while (n_segs < burst_size)
1817  {
1818  n_written = tcp_prepare_segment (wrk, tc, offset, tc->snd_mss, &b);
1819  if (!n_written)
1820  goto done;
1821 
1822  bi = vlib_get_buffer_index (vm, b);
1823  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1824  offset += n_written;
1825  n_segs += 1;
1826  }
1827 
1828 done:
1829  return n_segs;
1830 }
1831 
1832 #define scoreboard_rescue_rxt_valid(_sb, _tc) \
1833  (seq_geq (_sb->rescue_rxt, _tc->snd_una) \
1834  && seq_leq (_sb->rescue_rxt, _tc->snd_congestion))
1835 
1836 /**
1837  * Do fast retransmit with SACKs
1838  */
1839 int
1841  u32 burst_size)
1842 {
1843  u32 n_written = 0, offset, max_bytes, n_segs = 0, n_segs_now;
1844  sack_scoreboard_hole_t *hole;
1845  vlib_main_t *vm = wrk->vm;
1846  vlib_buffer_t *b = 0;
1847  sack_scoreboard_t *sb;
1848  u32 bi, old_snd_nxt;
1849  int snd_space;
1850  u32 max_deq;
1851  u8 snd_limited = 0, can_rescue = 0;
1852 
1853  ASSERT (tcp_in_fastrecovery (tc));
1854 
1855  snd_space = tcp_available_cc_snd_space (tc);
1856  if (snd_space < tc->snd_mss)
1857  {
1858  tcp_program_fastretransmit (wrk, tc);
1859  return 0;
1860  }
1861 
1862  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1863  old_snd_nxt = tc->snd_nxt;
1864  sb = &tc->sack_sb;
1865  hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
1866 
1867  max_deq = session_tx_fifo_max_dequeue (&tc->connection);
1868  max_deq -= tc->snd_una_max - tc->snd_una;
1869 
1870  while (snd_space > 0 && n_segs < burst_size)
1871  {
1872  hole = scoreboard_next_rxt_hole (sb, hole, max_deq, &can_rescue,
1873  &snd_limited);
1874  if (!hole)
1875  {
1876  if (max_deq)
1877  {
1878  snd_space = clib_min (max_deq, snd_space);
1879  burst_size = clib_min (burst_size - n_segs,
1880  snd_space / tc->snd_mss);
1881  n_segs_now = tcp_fast_retransmit_unsent (wrk, tc, burst_size);
1882  if (max_deq > n_segs_now * tc->snd_mss)
1883  tcp_program_fastretransmit (wrk, tc);
1884  n_segs += n_segs_now;
1885  goto done;
1886  }
1887 
1888  if (!can_rescue || scoreboard_rescue_rxt_valid (sb, tc))
1889  break;
1890 
1891  /* If rescue rxt undefined or less than snd_una then one segment of
1892  * up to SMSS octets that MUST include the highest outstanding
1893  * unSACKed sequence number SHOULD be returned, and RescueRxt set to
1894  * RecoveryPoint. HighRxt MUST NOT be updated.
1895  */
1896  max_bytes = clib_min (tc->snd_mss,
1897  tc->snd_congestion - tc->snd_una);
1898  max_bytes = clib_min (max_bytes, snd_space);
1899  offset = tc->snd_congestion - tc->snd_una - max_bytes;
1900  sb->rescue_rxt = tc->snd_congestion;
1901  tc->snd_nxt = tc->snd_una + offset;
1902  n_written = tcp_prepare_retransmit_segment (wrk, tc, offset,
1903  max_bytes, &b);
1904  if (!n_written)
1905  goto done;
1906 
1907  bi = vlib_get_buffer_index (vm, b);
1908  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1909  n_segs += 1;
1910  break;
1911  }
1912 
1913  max_bytes = clib_min (hole->end - sb->high_rxt, snd_space);
1914  max_bytes = snd_limited ? clib_min (max_bytes, tc->snd_mss) : max_bytes;
1915  if (max_bytes == 0)
1916  break;
1917 
1918  offset = sb->high_rxt - tc->snd_una;
1919  tc->snd_nxt = sb->high_rxt;
1920  n_written = tcp_prepare_retransmit_segment (wrk, tc, offset, max_bytes,
1921  &b);
1922  ASSERT (n_written <= snd_space);
1923 
1924  /* Nothing left to retransmit */
1925  if (n_written == 0)
1926  break;
1927 
1928  bi = vlib_get_buffer_index (vm, b);
1929  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1930 
1931  sb->high_rxt += n_written;
1932  snd_space -= n_written;
1933  n_segs += 1;
1934  }
1935 
1936  if (hole)
1937  tcp_program_fastretransmit (wrk, tc);
1938 
1939 done:
1940  /* If window allows, send 1 SMSS of new data */
1941  tc->snd_nxt = old_snd_nxt;
1942  return n_segs;
1943 }
1944 
1945 /**
1946  * Fast retransmit without SACK info
1947  */
1948 int
1950  u32 burst_size)
1951 {
1952  u32 n_written = 0, offset = 0, bi, old_snd_nxt, max_deq, n_segs_now;
1953  vlib_main_t *vm = wrk->vm;
1954  int snd_space, n_segs = 0;
1955  vlib_buffer_t *b;
1956 
1957  ASSERT (tcp_in_fastrecovery (tc));
1958  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1959  old_snd_nxt = tc->snd_nxt;
1960 
1961  if (!tcp_fastrecovery_first (tc))
1962  goto send_unsent;
1963 
1964  /* RFC 6582: [If a partial ack], retransmit the first unacknowledged
1965  * segment. */
1966  snd_space = tc->sack_sb.last_bytes_delivered;
1967  tc->snd_nxt = tc->snd_una;
1968  while (snd_space > 0 && n_segs < burst_size)
1969  {
1970  n_written = tcp_prepare_retransmit_segment (wrk, tc, offset,
1971  tc->snd_mss, &b);
1972 
1973  /* Nothing left to retransmit */
1974  if (n_written == 0)
1975  break;
1976 
1977  bi = vlib_get_buffer_index (vm, b);
1978  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
1979  snd_space -= n_written;
1980  offset += n_written;
1981  n_segs += 1;
1982  }
1983 
1984  if (n_segs == burst_size)
1985  goto done;
1986 
1987 send_unsent:
1988 
1989  /* RFC 6582: Send a new segment if permitted by the new value of cwnd. */
1990  snd_space = tcp_available_cc_snd_space (tc);
1991  if (snd_space < tc->snd_mss || tc->snd_mss == 0)
1992  goto done;
1993 
1994  max_deq = session_tx_fifo_max_dequeue (&tc->connection);
1995  max_deq -= tc->snd_una_max - tc->snd_una;
1996  if (max_deq)
1997  {
1998  snd_space = clib_min (max_deq, snd_space);
1999  burst_size = clib_min (burst_size - n_segs, snd_space / tc->snd_mss);
2000  n_segs_now = tcp_fast_retransmit_unsent (wrk, tc, burst_size);
2001  if (max_deq > n_segs_now * tc->snd_mss)
2002  tcp_program_fastretransmit (wrk, tc);
2003  n_segs += n_segs_now;
2004  }
2005 
2006  /* Restore snd_nxt */
2007  tc->snd_nxt = old_snd_nxt;
2008 
2009 done:
2011  return n_segs;
2012 }
2013 
2014 /**
2015  * Do fast retransmit
2016  */
2017 int
2019  u32 burst_size)
2020 {
2021  if (tcp_opts_sack_permitted (&tc->rcv_opts))
2022  return tcp_fast_retransmit_sack (wrk, tc, burst_size);
2023  else
2024  return tcp_fast_retransmit_no_sack (wrk, tc, burst_size);
2025 }
2026 
2027 static void
2029  u16 * next0, u32 * error0)
2030 {
2031  ip_adjacency_t *adj;
2032  adj_index_t ai;
2033 
2034  /* Not thread safe but as long as the connection exists the adj should
2035  * not be removed */
2036  ai = adj_nbr_find (FIB_PROTOCOL_IP6, VNET_LINK_IP6, &tc0->c_rmt_ip,
2037  tc0->sw_if_index);
2038  if (ai == ADJ_INDEX_INVALID)
2039  {
2040  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
2041  *next0 = TCP_OUTPUT_NEXT_DROP;
2042  *error0 = TCP_ERROR_LINK_LOCAL_RW;
2043  return;
2044  }
2045 
2046  adj = adj_get (ai);
2048  *next0 = TCP_OUTPUT_NEXT_IP_REWRITE;
2049  else if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
2050  *next0 = TCP_OUTPUT_NEXT_IP_ARP;
2051  else
2052  {
2053  *next0 = TCP_OUTPUT_NEXT_DROP;
2054  *error0 = TCP_ERROR_LINK_LOCAL_RW;
2055  }
2056  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ai;
2057 }
2058 
2059 static void
2061  u32 * to_next, u32 n_bufs)
2062 {
2063  u32 n_trace = vlib_get_trace_count (vm, node);
2064  tcp_connection_t *tc;
2065  tcp_tx_trace_t *t;
2066  vlib_buffer_t *b;
2067  tcp_header_t *th;
2068  int i;
2069 
2070  for (i = 0; i < clib_min (n_trace, n_bufs); i++)
2071  {
2072  b = vlib_get_buffer (vm, to_next[i]);
2073  th = vlib_buffer_get_current (b);
2074  tc = tcp_connection_get (vnet_buffer (b)->tcp.connection_index,
2075  vm->thread_index);
2076  t = vlib_add_trace (vm, node, b, sizeof (*t));
2077  clib_memcpy_fast (&t->tcp_header, th, sizeof (t->tcp_header));
2078  clib_memcpy_fast (&t->tcp_connection, tc, sizeof (t->tcp_connection));
2079  }
2080 }
2081 
2082 always_inline void
2084  tcp_connection_t * tc0, u8 is_ip4)
2085 {
2086  tcp_header_t *th0 = 0;
2087 
2088  th0 = vlib_buffer_get_current (b0);
2089  TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
2090  if (is_ip4)
2091  {
2092  vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
2093  IP_PROTOCOL_TCP, 1);
2094  b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
2095  vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
2096  th0->checksum = 0;
2097  }
2098  else
2099  {
2100  ip6_header_t *ih0;
2101  ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
2102  &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
2103  b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
2104  vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data;
2105  vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
2106  th0->checksum = 0;
2107  }
2108 }
2109 
2110 always_inline void
2112  u32 * error0, u16 * next0, u8 is_ip4)
2113 {
2114 
2115  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
2116  {
2117  *error0 = TCP_ERROR_INVALID_CONNECTION;
2118  *next0 = TCP_OUTPUT_NEXT_DROP;
2119  return;
2120  }
2121 
2122  vnet_buffer (b0)->sw_if_index[VLIB_TX] = tc0->c_fib_index;
2123  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
2124 
2125  if (!is_ip4)
2126  {
2127  if (PREDICT_FALSE (ip6_address_is_link_local_unicast (&tc0->c_rmt_ip6)))
2128  tcp_output_handle_link_local (tc0, b0, next0, error0);
2129  }
2130 
2131  if (!TCP_ALWAYS_ACK)
2132  tcp_timer_reset (tc0, TCP_TIMER_DELACK);
2133 }
2134 
2137  vlib_frame_t * frame, int is_ip4)
2138 {
2139  u32 n_left_from, *from, thread_index = vm->thread_index;
2140  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
2141  u16 nexts[VLIB_FRAME_SIZE], *next;
2142 
2143  from = vlib_frame_vector_args (frame);
2144  n_left_from = frame->n_vectors;
2145  tcp_set_time_now (tcp_get_worker (thread_index));
2146 
2148  tcp46_output_trace_frame (vm, node, from, n_left_from);
2149 
2150  vlib_get_buffers (vm, from, bufs, n_left_from);
2151  b = bufs;
2152  next = nexts;
2153 
2154  while (n_left_from >= 4)
2155  {
2156  u32 error0 = TCP_ERROR_PKTS_SENT, error1 = TCP_ERROR_PKTS_SENT;
2157  tcp_connection_t *tc0, *tc1;
2158 
2159  {
2160  vlib_prefetch_buffer_header (b[2], STORE);
2161  CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2162 
2163  vlib_prefetch_buffer_header (b[3], STORE);
2164  CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2165  }
2166 
2167  next[0] = next[1] = TCP_OUTPUT_NEXT_IP_LOOKUP;
2168 
2169  tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
2170  thread_index);
2171  tc1 = tcp_connection_get (vnet_buffer (b[1])->tcp.connection_index,
2172  thread_index);
2173 
2174  tcp_output_push_ip (vm, b[0], tc0, is_ip4);
2175  tcp_output_push_ip (vm, b[1], tc1, is_ip4);
2176 
2177  tcp_output_handle_packet (tc0, b[0], &error0, &next[0], is_ip4);
2178  tcp_output_handle_packet (tc1, b[1], &error1, &next[1], is_ip4);
2179 
2180  b += 2;
2181  next += 2;
2182  n_left_from -= 2;
2183  }
2184  while (n_left_from > 0)
2185  {
2186  u32 error0 = TCP_ERROR_PKTS_SENT;
2187  tcp_connection_t *tc0;
2188 
2189  if (n_left_from > 1)
2190  {
2191  vlib_prefetch_buffer_header (b[1], STORE);
2192  CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
2193  }
2194 
2195  next[0] = TCP_OUTPUT_NEXT_IP_LOOKUP;
2196  tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
2197  thread_index);
2198 
2199  tcp_output_push_ip (vm, b[0], tc0, is_ip4);
2200  tcp_output_handle_packet (tc0, b[0], &error0, &next[0], is_ip4);
2201 
2202  b += 1;
2203  next += 1;
2204  n_left_from -= 1;
2205  }
2206 
2207  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
2208  return frame->n_vectors;
2209 }
2210 
2211 static uword
2213  vlib_frame_t * from_frame)
2214 {
2215  return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2216 }
2217 
2218 static uword
2220  vlib_frame_t * from_frame)
2221 {
2222  return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2223 }
2224 
2225 /* *INDENT-OFF* */
2227 {
2228  .function = tcp4_output,
2229  .name = "tcp4-output",
2230  /* Takes a vector of packets. */
2231  .vector_size = sizeof (u32),
2232  .n_errors = TCP_N_ERROR,
2233  .protocol_hint = VLIB_NODE_PROTO_HINT_TCP,
2234  .error_strings = tcp_error_strings,
2235  .n_next_nodes = TCP_OUTPUT_N_NEXT,
2236  .next_nodes = {
2237 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
2239 #undef _
2240  },
2241  .format_buffer = format_tcp_header,
2242  .format_trace = format_tcp_tx_trace,
2243 };
2244 /* *INDENT-ON* */
2245 
2247 
2248 /* *INDENT-OFF* */
2250 {
2251  .function = tcp6_output,
2252  .name = "tcp6-output",
2253  /* Takes a vector of packets. */
2254  .vector_size = sizeof (u32),
2255  .n_errors = TCP_N_ERROR,
2256  .protocol_hint = VLIB_NODE_PROTO_HINT_TCP,
2257  .error_strings = tcp_error_strings,
2258  .n_next_nodes = TCP_OUTPUT_N_NEXT,
2259  .next_nodes = {
2260 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
2262 #undef _
2263  },
2264  .format_buffer = format_tcp_header,
2265  .format_trace = format_tcp_tx_trace,
2266 };
2267 /* *INDENT-ON* */
2268 
2270 
2271 typedef enum _tcp_reset_next
2272 {
2277 
2278 #define foreach_tcp4_reset_next \
2279  _(DROP, "error-drop") \
2280  _(IP_LOOKUP, "ip4-lookup")
2281 
2282 #define foreach_tcp6_reset_next \
2283  _(DROP, "error-drop") \
2284  _(IP_LOOKUP, "ip6-lookup")
2285 
2286 static uword
2288  vlib_frame_t * from_frame, u8 is_ip4)
2289 {
2290  u32 n_left_from, next_index, *from, *to_next;
2291  u32 my_thread_index = vm->thread_index;
2292 
2293  from = vlib_frame_vector_args (from_frame);
2294  n_left_from = from_frame->n_vectors;
2295 
2296  next_index = node->cached_next_index;
2297 
2298  while (n_left_from > 0)
2299  {
2300  u32 n_left_to_next;
2301 
2302  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2303 
2304  while (n_left_from > 0 && n_left_to_next > 0)
2305  {
2306  u32 bi0;
2307  vlib_buffer_t *b0;
2308  tcp_tx_trace_t *t0;
2309  tcp_header_t *th0;
2310  u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
2311 
2312  bi0 = from[0];
2313  to_next[0] = bi0;
2314  from += 1;
2315  to_next += 1;
2316  n_left_from -= 1;
2317  n_left_to_next -= 1;
2318 
2319  b0 = vlib_get_buffer (vm, bi0);
2320 
2321  if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
2322  my_thread_index, is_ip4))
2323  {
2324  error0 = TCP_ERROR_LOOKUP_DROPS;
2325  next0 = TCP_RESET_NEXT_DROP;
2326  goto done;
2327  }
2328 
2329  /* Prepare to send to IP lookup */
2330  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
2331  next0 = TCP_RESET_NEXT_IP_LOOKUP;
2332 
2333  done:
2334  b0->error = node->errors[error0];
2335  b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2336  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2337  {
2338  th0 = vlib_buffer_get_current (b0);
2339  if (is_ip4)
2340  th0 = ip4_next_header ((ip4_header_t *) th0);
2341  else
2342  th0 = ip6_next_header ((ip6_header_t *) th0);
2343  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2344  clib_memcpy_fast (&t0->tcp_header, th0,
2345  sizeof (t0->tcp_header));
2346  }
2347 
2348  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2349  n_left_to_next, bi0, next0);
2350  }
2351  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2352  }
2353  return from_frame->n_vectors;
2354 }
2355 
2356 static uword
2358  vlib_frame_t * from_frame)
2359 {
2360  return tcp46_send_reset_inline (vm, node, from_frame, 1);
2361 }
2362 
2363 static uword
2365  vlib_frame_t * from_frame)
2366 {
2367  return tcp46_send_reset_inline (vm, node, from_frame, 0);
2368 }
2369 
2370 /* *INDENT-OFF* */
2372  .function = tcp4_send_reset,
2373  .name = "tcp4-reset",
2374  .vector_size = sizeof (u32),
2375  .n_errors = TCP_N_ERROR,
2376  .error_strings = tcp_error_strings,
2377  .n_next_nodes = TCP_RESET_N_NEXT,
2378  .next_nodes = {
2379 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2381 #undef _
2382  },
2383  .format_trace = format_tcp_tx_trace,
2384 };
2385 /* *INDENT-ON* */
2386 
2388 
2389 /* *INDENT-OFF* */
2391  .function = tcp6_send_reset,
2392  .name = "tcp6-reset",
2393  .vector_size = sizeof (u32),
2394  .n_errors = TCP_N_ERROR,
2395  .error_strings = tcp_error_strings,
2396  .n_next_nodes = TCP_RESET_N_NEXT,
2397  .next_nodes = {
2398 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2400 #undef _
2401  },
2402  .format_trace = format_tcp_tx_trace,
2403 };
2404 /* *INDENT-ON* */
2405 
2407 
2408 /*
2409  * fd.io coding-style-patch-verification: ON
2410  *
2411  * Local Variables:
2412  * eval: (c-set-style "gnu")
2413  * End:
2414  */
void tcp_make_fin(tcp_connection_t *tc, vlib_buffer_t *b)
Convert buffer to FIN-ACK.
Definition: tcp_output.c:575
#define tcp_in_cong_recovery(tc)
Definition: tcp.h:372
#define TCP_DBG_BUFFER_ALLOC_MAYBE_FAIL(thread_index)
Definition: tcp_debug.h:915
void session_flush_frames_main_thread(vlib_main_t *vm)
Definition: session.c:1398
End of options.
Definition: tcp_packet.h:104
static u32 tcp_options_write(u8 *data, tcp_options_t *opts)
Write TCP options to segment.
Definition: tcp_output.c:211
static void tcp_rxt_timeout_cc(tcp_connection_t *tc)
Reset congestion control, switch cwnd to loss window and try again.
Definition: tcp_output.c:1497
u32 flags
Definition: vhost_user.h:115
#define clib_min(x, y)
Definition: clib.h:295
#define TCP_OPTION_LEN_EOL
Definition: tcp_packet.h:163
#define CLIB_UNUSED(x)
Definition: clib.h:82
u32 * pending_acks
vector of pending acks
Definition: tcp.h:426
#define tcp_in_recovery(tc)
Definition: tcp.h:363
void scoreboard_clear(sack_scoreboard_t *sb)
Definition: tcp_input.c:853
static f64 tcp_time_now_us(u32 thread_index)
Definition: tcp.h:803
static void tcp_retransmit_timer_set(tcp_connection_t *tc)
Definition: tcp.h:872
static u32 transport_rx_fifo_size(transport_connection_t *tc)
Definition: session.h:530
#define TCP_OPTION_LEN_SACK_PERMITTED
Definition: tcp_packet.h:167
#define seq_leq(_s1, _s2)
Definition: tcp.h:646
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:156
void tcp_timer_retransmit_handler(u32 index)
Definition: tcp_output.c:1688
ip4_address_t src_address
Definition: ip4_packet.h:170
#define TCP_TO_TIMER_TICK
Definition: tcp.h:98
Selective Ack permitted.
Definition: tcp_packet.h:108
#define TCP_FLAG_SYN
Definition: fa_node.h:13
#define TCP_MIN_RX_FIFO_SIZE
Definition: tcp.h:37
#define tcp_opts_tstamp(_to)
Definition: tcp_packet.h:157
static uword tcp4_send_reset(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_output.c:2357
void tcp_make_synack(tcp_connection_t *tc, vlib_buffer_t *b)
Convert buffer to SYN-ACK.
Definition: tcp_output.c:618
#define PREDICT_TRUE(x)
Definition: clib.h:112
static void tcp_flush_frame_to_ip_lookup(tcp_worker_ctx_t *wrk, u8 is_ip4)
Flush ip lookup tx frames populated by timer pops.
Definition: tcp_output.c:1056
static tcp_connection_t * tcp_connection_get_if_valid(u32 conn_index, u32 thread_index)
Definition: tcp.h:561
static int tcp_make_syn_options(tcp_options_t *opts, u8 wnd_scale)
Definition: tcp_output.c:294
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
struct _sack_scoreboard sack_scoreboard_t
IP unicast adjacency.
Definition: adj.h:221
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
Definition: fib_table.c:956
static tcp_connection_t * tcp_half_open_connection_get(u32 conn_index)
Definition: tcp.h:606
void tcp_update_rcv_mss(tcp_connection_t *tc)
Update max segment size we&#39;re able to process.
Definition: tcp_output.c:90
vlib_frame_t * tx_frames[2]
tx frames for tcp 4/6 output nodes
Definition: tcp.h:408
void tcp_send_acks(tcp_worker_ctx_t *wrk)
Definition: tcp_output.c:1280
struct _tcp_main tcp_main_t
u32 thread_index
Definition: main.h:179
This packet is to be rewritten and forwarded to the next processing node.
Definition: adj.h:73
#define TCP_OPTS_ALIGN
Definition: tcp_packet.h:174
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:525
static u32 tcp_initial_wnd_unscaled(tcp_connection_t *tc)
TCP&#39;s initial window.
Definition: tcp_output.c:100
enum _tcp_output_next tcp_output_next_t
int i
static u32 format_get_indent(u8 *s)
Definition: format.h:72
struct _tcp_connection tcp_connection_t
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u32 tcp_available_cc_snd_space(const tcp_connection_t *tc)
Estimate of how many bytes we can still push into the network.
Definition: tcp.h:753
#define tcp_opts_sack(_to)
Definition: tcp_packet.h:159
static void tcp_push_ip_hdr(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, vlib_buffer_t *b)
Definition: tcp_output.c:955
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
static uword tcp46_send_reset_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, u8 is_ip4)
Definition: tcp_output.c:2287
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:493
No operation.
Definition: tcp_packet.h:105
int tcp_fast_retransmit_no_sack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 burst_size)
Fast retransmit without SACK info.
Definition: tcp_output.c:1949
u8 n_sack_blocks
Number of SACKs blocks.
Definition: tcp_packet.h:152
static void tcp_enqueue_to_output_i(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4, u8 flush)
Definition: tcp_output.c:699
struct _tcp_header tcp_header_t
int tcp_half_open_connection_cleanup(tcp_connection_t *tc)
Try to cleanup half-open connection.
Definition: tcp.c:170
#define scoreboard_rescue_rxt_valid(_sb, _tc)
Definition: tcp_output.c:1832
ip6_address_t src_address
Definition: ip6_packet.h:378
unsigned char u8
Definition: types.h:56
struct _sack_scoreboard_hole sack_scoreboard_hole_t
u8 wscale
Window scale advertised.
Definition: tcp_packet.h:148
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
#define TCP_OPTS_MAX_SACK_BLOCKS
Definition: tcp_packet.h:175
#define TCP_MAX_RX_FIFO_SIZE
Definition: tcp.h:36
u16 dummy_mtu
Definition: tcp_output.c:56
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:103
static void tcp_timer_retransmit_handler_i(u32 index, u8 is_syn)
Definition: tcp_output.c:1522
#define foreach_tcp4_reset_next
Definition: tcp_output.c:2278
static u32 tcp_prepare_retransmit_segment(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 offset, u32 max_deq_bytes, vlib_buffer_t **b)
Build a retransmit segment.
Definition: tcp_output.c:1446
u16 src_port
Definition: udp.api:41
Limit MSS.
Definition: tcp_packet.h:106
static uword tcp46_output_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4)
Definition: tcp_output.c:2136
static void * tcp_init_buffer(vlib_main_t *vm, vlib_buffer_t *b)
Definition: tcp_output.c:519
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:433
void tcp_make_syn(tcp_connection_t *tc, vlib_buffer_t *b)
Convert buffer to SYN.
Definition: tcp_output.c:593
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
static int tcp_prepare_segment(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 offset, u32 max_deq_bytes, vlib_buffer_t **b)
Allocate a new buffer and build a new tcp segment.
Definition: tcp_output.c:1336
#define seq_gt(_s1, _s2)
Definition: tcp.h:647
sack_scoreboard_hole_t * scoreboard_get_hole(sack_scoreboard_t *sb, u32 index)
Definition: tcp_input.c:615
u32 sw_if_index
Definition: vxlan_gbp.api:37
#define always_inline
Definition: clib.h:98
#define TCP_OPTION_LEN_SACK_BLOCK
Definition: tcp_packet.h:169
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define TCP_FLAG_ACK
Definition: fa_node.h:16
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
tcp_main_t tcp_main
Definition: tcp.c:29
static tcp_header_t * tcp_buffer_hdr(vlib_buffer_t *b)
Definition: tcp.h:531
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:188
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:181
u8 * format_tcp_tx_trace(u8 *s, va_list *args)
Definition: tcp_output.c:59
enum _tcp_state tcp_state_t
#define TCP_ALWAYS_ACK
On/off delayed acks.
Definition: tcp.h:39
#define TCP_RTO_MAX
Definition: tcp.h:110
vhost_vring_state_t state
Definition: vhost_user.h:120
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
static u32 tcp_time_now(void)
Definition: tcp.h:791
sack_block_t * sacks
SACK blocks.
Definition: tcp_packet.h:151
unsigned int u32
Definition: types.h:88
static void tcp46_output_trace_frame(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *to_next, u32 n_bufs)
Definition: tcp_output.c:2060
#define TCP_ESTABLISH_TIME
Definition: tcp.h:101
sack_scoreboard_hole_t * scoreboard_next_rxt_hole(sack_scoreboard_t *sb, sack_scoreboard_hole_t *start, u8 have_sent_1_smss, u8 *can_rescue, u8 *snd_limited)
Figure out the next hole to retransmit.
Definition: tcp_input.c:777
#define tcp_validate_txf_size(_tc, _a)
Definition: tcp.h:935
#define VLIB_FRAME_SIZE
Definition: node.h:401
static void tcp_enqueue_to_ip_lookup_now(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4, u32 fib_index)
Definition: tcp_output.c:683
#define TCP_EVT_DBG(_evt, _args...)
Definition: tcp_debug.h:238
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:158
static int tcp_alloc_tx_buffers(tcp_worker_ctx_t *wrk, u16 *n_bufs, u32 wanted)
Definition: tcp_output.c:468
static void tcp_timer_set(tcp_connection_t *tc, u8 timer_id, u32 interval)
Definition: tcp.h:833
#define TCP_OPTION_LEN_WINDOW_SCALE
Definition: tcp_packet.h:166
vlib_node_registration_t tcp6_reset_node
(constructor) VLIB_REGISTER_NODE (tcp6_reset_node)
Definition: tcp_output.c:2390
#define TCP_RTO_SYN_RETRIES
Definition: tcp.h:113
int tcp_fast_retransmit_sack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 burst_size)
Do fast retransmit with SACKs.
Definition: tcp_output.c:1840
#define tcp_trajectory_add_start(b, start)
Definition: tcp.h:544
vlib_main_t * vm
convenience pointer to this thread&#39;s vlib main
Definition: tcp.h:432
void tcp_send_reset(tcp_connection_t *tc)
Build and set reset packet for connection.
Definition: tcp_output.c:909
void tcp_send_synack(tcp_connection_t *tc)
Definition: tcp_output.c:1020
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
static int tcp_make_synack_options(tcp_connection_t *tc, tcp_options_t *opts)
Definition: tcp_output.c:323
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:114
static void * vlib_buffer_make_headroom(vlib_buffer_t *b, u8 size)
Make head room, typically for packet headers.
Definition: buffer.h:335
#define MAX_HDRS_LEN
Definition: session.h:31
#define tcp_in_fastrecovery(tc)
Definition: tcp.h:362
void tcp_connection_tx_pacer_reset(tcp_connection_t *tc, u32 window, u32 start_bucket)
Definition: tcp.c:1213
static void * vlib_buffer_push_tcp_net_order(vlib_buffer_t *b, u16 sp, u16 dp, u32 seq, u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
Push TCP header to buffer.
Definition: tcp.h:971
#define tcp_opts_mss(_to)
Definition: tcp_packet.h:156
unsigned short u16
Definition: types.h:57
void tcp_flush_frames_to_output(tcp_worker_ctx_t *wrk)
Flush v4 and v6 tcp and ip-lookup tx frames for thread index.
Definition: tcp_output.c:1072
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:190
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
u32 tcp_push_header(tcp_connection_t *tc, vlib_buffer_t *b)
Definition: tcp_output.c:1216
#define TCP_TIMER_HANDLE_INVALID
Definition: tcp.h:95
static void tcp_output_handle_link_local(tcp_connection_t *tc0, vlib_buffer_t *b0, u16 *next0, u32 *error0)
Definition: tcp_output.c:2028
#define foreach_tcp6_output_next
Definition: tcp_output.c:38
static u32 tcp_flight_size(const tcp_connection_t *tc)
Our estimate of the number of bytes in flight (pipe size)
Definition: tcp.h:671
#define PREDICT_FALSE(x)
Definition: clib.h:111
static int tcp_make_reset_in_place(vlib_main_t *vm, vlib_buffer_t *b0, tcp_state_t state, u8 thread_index, u8 is_ip4)
Definition: tcp_output.c:745
#define TCP_FLAG_FIN
Definition: fa_node.h:12
static u8 tcp_window_compute_scale(u32 window)
Definition: tcp_output.c:75
void tcp_timer_retransmit_syn_handler(u32 index)
Definition: tcp_output.c:1694
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
#define TCP_OPTION_LEN_TIMESTAMP
Definition: tcp_packet.h:168
#define foreach_tcp4_output_next
Definition: tcp_output.c:32
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:139
#define TCP_WND_MAX
Definition: tcp_packet.h:172
static void tcp_enqueue_to_ip_lookup(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4, u32 fib_index)
Definition: tcp_output.c:690
Selective Ack block.
Definition: tcp_packet.h:109
void tcp_make_ack(tcp_connection_t *tc, vlib_buffer_t *b)
Convert buffer to ACK.
Definition: tcp_output.c:561
#define TCP_FLAG_RST
Definition: fa_node.h:14
#define TCP_DBG(_fmt, _args...)
Definition: tcp_debug.h:89
u8 len
Definition: ip_types.api:49
#define TCP_MAX_WND_SCALE
Definition: tcp_packet.h:173
static void tcp_timer_reset(tcp_connection_t *tc, u8 timer_id)
Definition: tcp.h:844
This packet matches an "incomplete adjacency" and packets need to be passed to ARP to find rewrite st...
Definition: adj.h:63
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
static void * vlib_buffer_push_tcp(vlib_buffer_t *b, u16 sp_net, u16 dp_net, u32 seq, u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
Push TCP header to buffer.
Definition: tcp.h:1006
tcp_header_t tcp_header
Definition: tcp_output.c:52
u16 n_vectors
Definition: node.h:420
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:301
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
void tcp_send_reset_w_pkt(tcp_connection_t *tc, vlib_buffer_t *pkt, u32 thread_index, u8 is_ip4)
Send reset without reusing existing buffer.
Definition: tcp_output.c:829
#define clib_warning(format, args...)
Definition: error.h:59
static u8 tcp_make_state_flags(tcp_connection_t *tc, tcp_state_t next_state)
Definition: tcp_output.c:1127
format_function_t format_tcp_header
Definition: format.h:101
#define TCP_USE_SACKS
Disable only for testing.
Definition: tcp.h:40
#define tcp_recovery_on(tc)
Definition: tcp.h:360
static u32 tcp_window_to_advertise(tcp_connection_t *tc, tcp_state_t state)
Compute and return window to advertise, scaled as per RFC1323.
Definition: tcp_output.c:188
#define tcp_fastrecovery_first(tc)
Definition: tcp.h:368
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:452
u16 mss
Option flags, see above.
Definition: tcp_packet.h:147
static void tcp_output_handle_packet(tcp_connection_t *tc0, vlib_buffer_t *b0, u32 *error0, u16 *next0, u8 is_ip4)
Definition: tcp_output.c:2111
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:405
void tcp_program_fastretransmit(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_input.c:1245
static void tcp_timer_update(tcp_connection_t *tc, u8 timer_id, u32 interval)
Definition: tcp.h:857
u16 ip6_tcp_udp_icmp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip6_header_t *ip0, int *bogus_lengthp)
Definition: ip6_forward.c:944
signed int i32
Definition: types.h:77
vlib_node_registration_t ip6_lookup_node
(constructor) VLIB_REGISTER_NODE (ip6_lookup_node)
Definition: ip6_forward.c:546
static int tcp_make_established_options(tcp_connection_t *tc, tcp_options_t *opts)
Definition: tcp_output.c:358
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:537
#define ASSERT(truth)
static void tcp_output_push_ip(vlib_main_t *vm, vlib_buffer_t *b0, tcp_connection_t *tc0, u8 is_ip4)
Definition: tcp_output.c:2083
#define tcp_syn(_th)
Definition: tcp_packet.h:80
u32 session_tx_fifo_max_dequeue(transport_connection_t *tc)
Definition: session.c:498
static uword tcp6_output(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_output.c:2219
static int tcp_get_free_buffer_index(tcp_worker_ctx_t *wrk, u32 *bidx)
Definition: tcp_output.c:483
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: ip4_forward.c:1129
void tcp_update_burst_snd_vars(tcp_connection_t *tc)
Update burst send vars.
Definition: tcp_output.c:424
#define seq_geq(_s1, _s2)
Definition: tcp.h:648
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:130
void tcp_init_mss(tcp_connection_t *tc)
Definition: tcp_output.c:444
static uword tcp6_send_reset(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_output.c:2364
static uword ip6_address_is_link_local_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:321
#define tcp_fastrecovery_first_off(tc)
Definition: tcp.h:370
static void tcp_update_rcv_wnd(tcp_connection_t *tc)
Definition: tcp_output.c:139
void tcp_send_fin(tcp_connection_t *tc)
Send FIN.
Definition: tcp_output.c:1084
#define clib_max(x, y)
Definition: clib.h:288
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static u32 tcp_time_now_w_thread(u32 thread_index)
Definition: tcp.h:797
VLIB_NODE_FUNCTION_MULTIARCH(tcp4_output_node, tcp4_output)
void tcp_send_ack(tcp_connection_t *tc)
Definition: tcp_output.c:1239
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:156
#define seq_lt(_s1, _s2)
Definition: tcp.h:645
struct _vlib_node_registration vlib_node_registration_t
int tcp_retransmit_first_unacked(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Retransmit first unacked segment.
Definition: tcp_output.c:1784
template key/value backing page structure
Definition: bihash_doc.h:44
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:365
#define tcp_opts_wscale(_to)
Definition: tcp_packet.h:158
Definition: defs.h:47
u32 tsval
Timestamp value.
Definition: tcp_packet.h:149
u32 tsecr
Echoed/reflected time stamp.
Definition: tcp_packet.h:150
static void * vlib_buffer_push_ip6(vlib_main_t *vm, vlib_buffer_t *b, ip6_address_t *src, ip6_address_t *dst, int proto)
Push IPv6 header to buffer.
Definition: ip6.h:632
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
ip_lookup_next_t lookup_next_index
Next hop after ip4-lookup.
Definition: adj.h:236
static u8 tcp_is_lost_fin(tcp_connection_t *tc)
Definition: tcp.h:765
#define foreach_tcp6_reset_next
Definition: tcp_output.c:2282
static int tcp_fast_retransmit_unsent(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 burst_size)
Definition: tcp_output.c:1807
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:495
static tcp_worker_ctx_t * tcp_get_worker(u32 thread_index)
Definition: tcp.h:525
static void tcp_retransmit_timer_update(tcp_connection_t *tc)
Definition: tcp.h:916
u64 uword
Definition: types.h:112
static void tcp_enqueue_to_ip_lookup_i(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4, u32 fib_index, u8 flush)
Definition: tcp_output.c:647
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
static void tcp_make_ack_i(tcp_connection_t *tc, vlib_buffer_t *b, tcp_state_t state, u8 flags)
Prepare ACK.
Definition: tcp_output.c:536
void tcp_timer_delack_handler(u32 index)
Delayed ack timer handler.
Definition: tcp_output.c:1313
#define TCP_OPTION_LEN_MSS
Definition: tcp_packet.h:165
void tcp_flush_frame_to_output(tcp_worker_ctx_t *wrk, u8 is_ip4)
Flush tx frame populated by retransmits and timer pops.
Definition: tcp_output.c:1041
struct clib_bihash_value offset
template key/value backing page structure
static void tcp_retransmit_timer_force_update(tcp_connection_t *tc)
Definition: tcp.h:886
int tcp_fast_retransmit(tcp_worker_ctx_t *wrk, tcp_connection_t *tc, u32 burst_size)
Do fast retransmit.
Definition: tcp_output.c:2018
u8 * format_tcp_connection(u8 *s, va_list *args)
Definition: tcp.c:887
u32 tcp_initial_window_to_advertise(tcp_connection_t *tc)
Compute initial window and scale factor.
Definition: tcp_output.c:119
#define vnet_buffer(b)
Definition: buffer.h:368
static tcp_connection_t * tcp_connection_get(u32 conn_index, u32 thread_index)
Definition: tcp.h:552
void tcp_update_rto(tcp_connection_t *tc)
Definition: tcp_input.c:428
static u32 vlib_num_workers()
Definition: threads.h:366
u8 data[0]
Packet data.
Definition: buffer.h:176
#define TCP_OPTION_LEN_NOOP
Definition: tcp_packet.h:164
void tcp_send_syn(tcp_connection_t *tc)
Send SYN.
Definition: tcp_output.c:987
vlib_node_registration_t tcp6_output_node
(constructor) VLIB_REGISTER_NODE (tcp6_output_node)
Definition: tcp_output.c:21
u16 flags
Copy of main node flags.
Definition: node.h:531
Window scale.
Definition: tcp_packet.h:107
void tcp_program_ack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_output.c:1258
enum _tcp_reset_next tcp_reset_next_t
static u32 transport_max_rx_enqueue(transport_connection_t *tc)
Definition: session.h:516
#define tcp_opts_sack_permitted(_to)
Definition: tcp_packet.h:160
static void vlib_buffer_free_one(vlib_main_t *vm, u32 buffer_index)
Free one buffer Shorthand to free a single buffer chain.
Definition: buffer_funcs.h:570
tcp_connection_t tcp_connection
Definition: tcp_output.c:53
static u32 tcp_loss_wnd(const tcp_connection_t *tc)
Definition: tcp.h:726
u16 dst_port
Definition: udp.api:42
vlib_frame_t * ip_lookup_tx_frames[2]
tx frames for ip 4/6 lookup nodes
Definition: tcp.h:411
static void * tcp_reuse_buffer(vlib_main_t *vm, vlib_buffer_t *b)
Definition: tcp_output.c:503
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
Timestamps.
Definition: tcp_packet.h:110
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:145
static void tcp_enqueue_to_output_now(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4)
Definition: tcp_output.c:738
vlib_node_registration_t tcp4_reset_node
(constructor) VLIB_REGISTER_NODE (tcp4_reset_node)
Definition: tcp_output.c:2371
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:326
vlib_node_registration_t tcp4_output_node
(constructor) VLIB_REGISTER_NODE (tcp4_output_node)
Definition: tcp_output.c:20
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:117
static void tcp_enqueue_to_output(tcp_worker_ctx_t *wrk, vlib_buffer_t *b, u32 bi, u8 is_ip4)
Definition: tcp_output.c:731
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:485
static void tcp_persist_timer_set(tcp_connection_t *tc)
Definition: tcp.h:893
static tcp_main_t * vnet_get_tcp_main()
Definition: tcp.h:519
int stream_session_peek_bytes(transport_connection_t *tc, u8 *buffer, u32 offset, u32 max_bytes)
Definition: session.c:507
static char * tcp_error_strings[]
Definition: tcp_output.c:44
static uword tcp4_output(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: tcp_output.c:2212
static void * vlib_buffer_push_ip4(vlib_main_t *vm, vlib_buffer_t *b, ip4_address_t *src, ip4_address_t *dst, int proto, u8 csum_offload)
Push IPv4 header to buffer.
Definition: ip4.h:370
static void tcp_push_hdr_i(tcp_connection_t *tc, vlib_buffer_t *b, tcp_state_t next_state, u8 compute_opts, u8 maybe_burst)
Push TCP header and update connection variables.
Definition: tcp_output.c:1157
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
void tcp_cc_fastrecovery_exit(tcp_connection_t *tc)
Definition: tcp_input.c:1136
static u32 tcp_set_time_now(tcp_worker_ctx_t *wrk)
Definition: tcp.h:809
void tcp_timer_persist_handler(u32 index)
Got 0 snd_wnd from peer, try to do something about it.
Definition: tcp_output.c:1704
#define tcp_ack(_th)
Definition: tcp_packet.h:83
static u8 tcp_timer_is_active(tcp_connection_t *tc, tcp_timers_e timer)
Definition: tcp.h:930
Definition: defs.h:46
ip6_address_t dst_address
Definition: ip6_packet.h:378
u32 * tx_buffers
tx buffer free list
Definition: tcp.h:405
adj_index_t adj_nbr_find(fib_protocol_t nh_proto, vnet_link_t link_type, const ip46_address_t *nh_addr, u32 sw_if_index)
Lookup neighbor adjancency.
Definition: adj_nbr.c:99
static int tcp_make_options(tcp_connection_t *tc, tcp_options_t *opts, tcp_state_t state)
Definition: tcp_output.c:389
void tcp_program_dupack(tcp_worker_ctx_t *wrk, tcp_connection_t *tc)
Definition: tcp_output.c:1268