Add fixed-size, preallocated pool support
[vpp.git] / src / vnet / tcp / tcp_output.c
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
20 vlib_node_registration_t tcp4_output_node;
21 vlib_node_registration_t tcp6_output_node;
22
23 typedef enum _tcp_output_next
24 {
25   TCP_OUTPUT_NEXT_DROP,
26   TCP_OUTPUT_NEXT_IP_LOOKUP,
27   TCP_OUTPUT_N_NEXT
28 } tcp_output_next_t;
29
30 #define foreach_tcp4_output_next                \
31   _ (DROP, "error-drop")                        \
32   _ (IP_LOOKUP, "ip4-lookup")
33
34 #define foreach_tcp6_output_next                \
35   _ (DROP, "error-drop")                        \
36   _ (IP_LOOKUP, "ip6-lookup")
37
38 static char *tcp_error_strings[] = {
39 #define tcp_error(n,s) s,
40 #include <vnet/tcp/tcp_error.def>
41 #undef tcp_error
42 };
43
44 typedef struct
45 {
46   tcp_header_t tcp_header;
47   tcp_connection_t tcp_connection;
48 } tcp_tx_trace_t;
49
50 u16 dummy_mtu = 1460;
51
52 u8 *
53 format_tcp_tx_trace (u8 * s, va_list * args)
54 {
55   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57   tcp_tx_trace_t *t = va_arg (*args, tcp_tx_trace_t *);
58   uword indent = format_get_indent (s);
59
60   s = format (s, "%U\n%U%U",
61               format_tcp_header, &t->tcp_header, 128,
62               format_white_space, indent,
63               format_tcp_connection, &t->tcp_connection, 1);
64
65   return s;
66 }
67
68 static u8
69 tcp_window_compute_scale (u32 available_space)
70 {
71   u8 wnd_scale = 0;
72   while (wnd_scale < TCP_MAX_WND_SCALE
73          && (available_space >> wnd_scale) > TCP_WND_MAX)
74     wnd_scale++;
75   return wnd_scale;
76 }
77
78 /**
79  * Update max segment size we're able to process.
80  *
81  * The value is constrained by our interface's MTU and IP options. It is
82  * also what we advertise to our peer.
83  */
84 void
85 tcp_update_rcv_mss (tcp_connection_t * tc)
86 {
87   /* TODO find our iface MTU */
88   tc->mss = dummy_mtu - sizeof (tcp_header_t);
89 }
90
91 /**
92  * TCP's initial window
93  */
94 always_inline u32
95 tcp_initial_wnd_unscaled (tcp_connection_t * tc)
96 {
97   /* RFC 6928 recommends the value lower. However at the time our connections
98    * are initialized, fifos may not be allocated. Therefore, advertise the
99    * smallest possible unscaled window size and update once fifos are
100    * assigned to the session.
101    */
102   /*
103      tcp_update_rcv_mss (tc);
104      TCP_IW_N_SEGMENTS * tc->mss;
105    */
106   return TCP_MIN_RX_FIFO_SIZE;
107 }
108
109 /**
110  * Compute initial window and scale factor. As per RFC1323, window field in
111  * SYN and SYN-ACK segments is never scaled.
112  */
113 u32
114 tcp_initial_window_to_advertise (tcp_connection_t * tc)
115 {
116   u32 max_fifo;
117
118   /* Initial wnd for SYN. Fifos are not allocated yet.
119    * Use some predefined value. For SYN-ACK we still want the
120    * scale to be computed in the same way */
121   max_fifo = TCP_MAX_RX_FIFO_SIZE;
122
123   tc->rcv_wscale = tcp_window_compute_scale (max_fifo);
124   tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
125
126   return clib_min (tc->rcv_wnd, TCP_WND_MAX);
127 }
128
129 /**
130  * Compute and return window to advertise, scaled as per RFC1323
131  */
132 u32
133 tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state)
134 {
135   if (state < TCP_STATE_ESTABLISHED)
136     return tcp_initial_window_to_advertise (tc);
137
138   tcp_update_rcv_wnd (tc);
139
140   if (tc->rcv_wnd == 0)
141     {
142       tc->flags |= TCP_CONN_SENT_RCV_WND0;
143     }
144   else
145     {
146       tc->flags &= ~TCP_CONN_SENT_RCV_WND0;
147     }
148
149   return tc->rcv_wnd >> tc->rcv_wscale;
150 }
151
152 void
153 tcp_update_rcv_wnd (tcp_connection_t * tc)
154 {
155   i32 observed_wnd;
156   u32 available_space, max_fifo, wnd;
157
158   /*
159    * Figure out how much space we have available
160    */
161   available_space = stream_session_max_rx_enqueue (&tc->connection);
162   max_fifo = stream_session_rx_fifo_size (&tc->connection);
163
164   ASSERT (tc->rcv_opts.mss < max_fifo);
165   if (available_space < tc->rcv_opts.mss && available_space < max_fifo >> 3)
166     available_space = 0;
167
168   /*
169    * Use the above and what we know about what we've previously advertised
170    * to compute the new window
171    */
172   observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
173   if (observed_wnd < 0)
174     observed_wnd = 0;
175
176   /* Bad. Thou shalt not shrink */
177   if (available_space < observed_wnd)
178     {
179       wnd = observed_wnd;
180       TCP_EVT_DBG (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
181     }
182   else
183     {
184       wnd = available_space;
185     }
186
187   /* Make sure we have a multiple of rcv_wscale */
188   if (wnd && tc->rcv_wscale)
189     {
190       wnd &= ~(1 << tc->rcv_wscale);
191       if (wnd == 0)
192         wnd = 1 << tc->rcv_wscale;
193     }
194
195   tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale);
196 }
197
198 /**
199  * Write TCP options to segment.
200  */
201 u32
202 tcp_options_write (u8 * data, tcp_options_t * opts)
203 {
204   u32 opts_len = 0;
205   u32 buf, seq_len = 4;
206
207   if (tcp_opts_mss (opts))
208     {
209       *data++ = TCP_OPTION_MSS;
210       *data++ = TCP_OPTION_LEN_MSS;
211       buf = clib_host_to_net_u16 (opts->mss);
212       clib_memcpy (data, &buf, sizeof (opts->mss));
213       data += sizeof (opts->mss);
214       opts_len += TCP_OPTION_LEN_MSS;
215     }
216
217   if (tcp_opts_wscale (opts))
218     {
219       *data++ = TCP_OPTION_WINDOW_SCALE;
220       *data++ = TCP_OPTION_LEN_WINDOW_SCALE;
221       *data++ = opts->wscale;
222       opts_len += TCP_OPTION_LEN_WINDOW_SCALE;
223     }
224
225   if (tcp_opts_sack_permitted (opts))
226     {
227       *data++ = TCP_OPTION_SACK_PERMITTED;
228       *data++ = TCP_OPTION_LEN_SACK_PERMITTED;
229       opts_len += TCP_OPTION_LEN_SACK_PERMITTED;
230     }
231
232   if (tcp_opts_tstamp (opts))
233     {
234       *data++ = TCP_OPTION_TIMESTAMP;
235       *data++ = TCP_OPTION_LEN_TIMESTAMP;
236       buf = clib_host_to_net_u32 (opts->tsval);
237       clib_memcpy (data, &buf, sizeof (opts->tsval));
238       data += sizeof (opts->tsval);
239       buf = clib_host_to_net_u32 (opts->tsecr);
240       clib_memcpy (data, &buf, sizeof (opts->tsecr));
241       data += sizeof (opts->tsecr);
242       opts_len += TCP_OPTION_LEN_TIMESTAMP;
243     }
244
245   if (tcp_opts_sack (opts))
246     {
247       int i;
248       u32 n_sack_blocks = clib_min (vec_len (opts->sacks),
249                                     TCP_OPTS_MAX_SACK_BLOCKS);
250
251       if (n_sack_blocks != 0)
252         {
253           *data++ = TCP_OPTION_SACK_BLOCK;
254           *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
255           for (i = 0; i < n_sack_blocks; i++)
256             {
257               buf = clib_host_to_net_u32 (opts->sacks[i].start);
258               clib_memcpy (data, &buf, seq_len);
259               data += seq_len;
260               buf = clib_host_to_net_u32 (opts->sacks[i].end);
261               clib_memcpy (data, &buf, seq_len);
262               data += seq_len;
263             }
264           opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK;
265         }
266     }
267
268   /* Terminate TCP options */
269   if (opts_len % 4)
270     {
271       *data++ = TCP_OPTION_EOL;
272       opts_len += TCP_OPTION_LEN_EOL;
273     }
274
275   /* Pad with zeroes to a u32 boundary */
276   while (opts_len % 4)
277     {
278       *data++ = TCP_OPTION_NOOP;
279       opts_len += TCP_OPTION_LEN_NOOP;
280     }
281   return opts_len;
282 }
283
284 always_inline int
285 tcp_make_syn_options (tcp_options_t * opts, u8 wnd_scale)
286 {
287   u8 len = 0;
288
289   opts->flags |= TCP_OPTS_FLAG_MSS;
290   opts->mss = dummy_mtu;        /*XXX discover that */
291   len += TCP_OPTION_LEN_MSS;
292
293   opts->flags |= TCP_OPTS_FLAG_WSCALE;
294   opts->wscale = wnd_scale;
295   len += TCP_OPTION_LEN_WINDOW_SCALE;
296
297   opts->flags |= TCP_OPTS_FLAG_TSTAMP;
298   opts->tsval = tcp_time_now ();
299   opts->tsecr = 0;
300   len += TCP_OPTION_LEN_TIMESTAMP;
301
302   if (TCP_USE_SACKS)
303     {
304       opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
305       len += TCP_OPTION_LEN_SACK_PERMITTED;
306     }
307
308   /* Align to needed boundary */
309   len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
310   return len;
311 }
312
313 always_inline int
314 tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts)
315 {
316   u8 len = 0;
317
318   opts->flags |= TCP_OPTS_FLAG_MSS;
319   opts->mss = tc->mss;
320   len += TCP_OPTION_LEN_MSS;
321
322   if (tcp_opts_wscale (&tc->rcv_opts))
323     {
324       opts->flags |= TCP_OPTS_FLAG_WSCALE;
325       opts->wscale = tc->rcv_wscale;
326       len += TCP_OPTION_LEN_WINDOW_SCALE;
327     }
328
329   if (tcp_opts_tstamp (&tc->rcv_opts))
330     {
331       opts->flags |= TCP_OPTS_FLAG_TSTAMP;
332       opts->tsval = tcp_time_now ();
333       opts->tsecr = tc->tsval_recent;
334       len += TCP_OPTION_LEN_TIMESTAMP;
335     }
336
337   if (tcp_opts_sack_permitted (&tc->rcv_opts))
338     {
339       opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
340       len += TCP_OPTION_LEN_SACK_PERMITTED;
341     }
342
343   /* Align to needed boundary */
344   len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
345   return len;
346 }
347
348 always_inline int
349 tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts)
350 {
351   u8 len = 0;
352
353   opts->flags = 0;
354
355   if (tcp_opts_tstamp (&tc->rcv_opts))
356     {
357       opts->flags |= TCP_OPTS_FLAG_TSTAMP;
358       opts->tsval = tcp_time_now ();
359       opts->tsecr = tc->tsval_recent;
360       len += TCP_OPTION_LEN_TIMESTAMP;
361     }
362   if (tcp_opts_sack_permitted (&tc->rcv_opts))
363     {
364       if (vec_len (tc->snd_sacks))
365         {
366           opts->flags |= TCP_OPTS_FLAG_SACK;
367           opts->sacks = tc->snd_sacks;
368           opts->n_sack_blocks = clib_min (vec_len (tc->snd_sacks),
369                                           TCP_OPTS_MAX_SACK_BLOCKS);
370           len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks;
371         }
372     }
373
374   /* Align to needed boundary */
375   len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN;
376   return len;
377 }
378
379 always_inline int
380 tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
381                   tcp_state_t state)
382 {
383   switch (state)
384     {
385     case TCP_STATE_ESTABLISHED:
386     case TCP_STATE_FIN_WAIT_1:
387       return tcp_make_established_options (tc, opts);
388     case TCP_STATE_SYN_RCVD:
389       return tcp_make_synack_options (tc, opts);
390     case TCP_STATE_SYN_SENT:
391       return tcp_make_syn_options (opts, tc->rcv_wscale);
392     default:
393       clib_warning ("Not handled!");
394       return 0;
395     }
396 }
397
398 /**
399  * Update snd_mss to reflect the effective segment size that we can send
400  * by taking into account all TCP options, including SACKs
401  */
402 void
403 tcp_update_snd_mss (tcp_connection_t * tc)
404 {
405   /* Compute options to be used for connection. These may be reused when
406    * sending data or to compute the effective mss (snd_mss) */
407   tc->snd_opts_len =
408     tcp_make_options (tc, &tc->snd_opts, TCP_STATE_ESTABLISHED);
409
410   /* XXX check if MTU has been updated */
411   tc->snd_mss = clib_min (tc->mss, tc->rcv_opts.mss) - tc->snd_opts_len;
412   ASSERT (tc->snd_mss > 0);
413 }
414
415 void
416 tcp_init_mss (tcp_connection_t * tc)
417 {
418   u16 default_min_mss = 536;
419   tcp_update_rcv_mss (tc);
420
421   /* TODO cache mss and consider PMTU discovery */
422   tc->snd_mss = clib_min (tc->rcv_opts.mss, tc->mss);
423
424   if (tc->snd_mss < 45)
425     {
426       clib_warning ("snd mss is 0");
427       /* Assume that at least the min default mss works */
428       tc->snd_mss = default_min_mss;
429       tc->rcv_opts.mss = default_min_mss;
430     }
431
432   /* We should have enough space for 40 bytes of options */
433   ASSERT (tc->snd_mss > 45);
434
435   /* If we use timestamp option, account for it */
436   if (tcp_opts_tstamp (&tc->rcv_opts))
437     tc->snd_mss -= TCP_OPTION_LEN_TIMESTAMP;
438 }
439
440 always_inline int
441 tcp_alloc_tx_buffers (tcp_main_t * tm, u8 thread_index, u32 n_free_buffers)
442 {
443   u32 current_length = vec_len (tm->tx_buffers[thread_index]);
444
445   vec_validate (tm->tx_buffers[thread_index],
446                 current_length + n_free_buffers - 1);
447   _vec_len (tm->tx_buffers[thread_index]) =
448     current_length + vlib_buffer_alloc_from_free_list (vlib_get_main (),
449                                                        tm->tx_buffers
450                                                        [thread_index],
451                                                        n_free_buffers,
452                                                        VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
453   /* buffer shortage, report failure */
454   if (vec_len (tm->tx_buffers[thread_index]) == 0)
455     {
456       clib_warning ("out of buffers");
457       return -1;
458     }
459   return 0;
460 }
461
462 always_inline int
463 tcp_get_free_buffer_index (tcp_main_t * tm, u32 * bidx)
464 {
465   u32 *my_tx_buffers;
466   u32 thread_index = vlib_get_thread_index ();
467   if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
468     {
469       if (tcp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
470         return -1;
471     }
472   my_tx_buffers = tm->tx_buffers[thread_index];
473   *bidx = my_tx_buffers[_vec_len (my_tx_buffers) - 1];
474   _vec_len (my_tx_buffers) -= 1;
475   return 0;
476 }
477
478 always_inline void
479 tcp_return_buffer (tcp_main_t * tm)
480 {
481   u32 *my_tx_buffers;
482   u32 thread_index = vlib_get_thread_index ();
483   my_tx_buffers = tm->tx_buffers[thread_index];
484   _vec_len (my_tx_buffers) += 1;
485 }
486
487 always_inline void *
488 tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
489 {
490   if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
491     vlib_buffer_free_one (vm, b->next_buffer);
492   b->flags = 0;
493   b->current_data = 0;
494   b->current_length = 0;
495   b->total_length_not_including_first_buffer = 0;
496   vnet_buffer (b)->tcp.flags = 0;
497
498   /* Leave enough space for headers */
499   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
500 }
501
502 always_inline void *
503 tcp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
504 {
505   ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
506   b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
507   b->total_length_not_including_first_buffer = 0;
508   vnet_buffer (b)->tcp.flags = 0;
509
510   /* Leave enough space for headers */
511   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
512 }
513
514 /**
515  * Prepare ACK
516  */
517 void
518 tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state,
519                 u8 flags)
520 {
521   tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
522   u8 tcp_opts_len, tcp_hdr_opts_len;
523   tcp_header_t *th;
524   u16 wnd;
525
526   wnd = tcp_window_to_advertise (tc, state);
527
528   /* Make and write options */
529   tcp_opts_len = tcp_make_established_options (tc, snd_opts);
530   tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
531
532   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
533                              tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd);
534
535   tcp_options_write ((u8 *) (th + 1), snd_opts);
536   vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
537 }
538
539 /**
540  * Convert buffer to ACK
541  */
542 void
543 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
544 {
545   vlib_main_t *vm = vlib_get_main ();
546
547   tcp_reuse_buffer (vm, b);
548   tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
549   TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc);
550   vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
551   tc->rcv_las = tc->rcv_nxt;
552 }
553
554 /**
555  * Convert buffer to FIN-ACK
556  */
557 void
558 tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b)
559 {
560   vlib_main_t *vm = vlib_get_main ();
561   u8 flags = 0;
562
563   tcp_reuse_buffer (vm, b);
564
565   flags = TCP_FLAG_FIN | TCP_FLAG_ACK;
566   tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, flags);
567
568   /* Reset flags, make sure ack is sent */
569   vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
570
571   tc->snd_nxt += 1;
572 }
573
574 /**
575  * Convert buffer to SYN-ACK
576  */
577 void
578 tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b)
579 {
580   vlib_main_t *vm = vlib_get_main ();
581   tcp_options_t _snd_opts, *snd_opts = &_snd_opts;
582   u8 tcp_opts_len, tcp_hdr_opts_len;
583   tcp_header_t *th;
584   u16 initial_wnd;
585   u32 time_now;
586
587   memset (snd_opts, 0, sizeof (*snd_opts));
588
589   tcp_reuse_buffer (vm, b);
590
591   /* Set random initial sequence */
592   time_now = tcp_time_now ();
593
594   tc->iss = random_u32 (&time_now);
595   tc->snd_una = tc->iss;
596   tc->snd_nxt = tc->iss + 1;
597   tc->snd_una_max = tc->snd_nxt;
598
599   initial_wnd = tcp_initial_window_to_advertise (tc);
600
601   /* Make and write options */
602   tcp_opts_len = tcp_make_synack_options (tc, snd_opts);
603   tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
604
605   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
606                              tc->rcv_nxt, tcp_hdr_opts_len,
607                              TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd);
608
609   tcp_options_write ((u8 *) (th + 1), snd_opts);
610
611   vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
612   vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK;
613
614   /* Init retransmit timer */
615   tcp_retransmit_timer_set (tc);
616   TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc);
617 }
618
619 always_inline void
620 tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
621                           u8 is_ip4)
622 {
623   u32 *to_next, next_index;
624   vlib_frame_t *f;
625
626   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
627   b->error = 0;
628
629   /* Default FIB for now */
630   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
631
632   /* Send to IP lookup */
633   next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
634   f = vlib_get_frame_to_node (vm, next_index);
635
636   /* Enqueue the packet */
637   to_next = vlib_frame_vector_args (f);
638   to_next[0] = bi;
639   f->n_vectors = 1;
640   vlib_put_frame_to_node (vm, next_index, f);
641 }
642
643 always_inline void
644 tcp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
645                          u8 is_ip4, u8 flush)
646 {
647   tcp_main_t *tm = vnet_get_tcp_main ();
648   u32 thread_index = vlib_get_thread_index ();
649   u32 *to_next, next_index;
650   vlib_frame_t *f;
651
652   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
653   b->error = 0;
654
655   /* Decide where to send the packet */
656   next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
657
658   /* Initialize the trajectory trace, if configured */
659   if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
660     {
661       b->pre_data[0] = 1;
662       b->pre_data[1] = next_index;
663     }
664
665   /* Get frame to v4/6 output node */
666   f = tm->tx_frames[!is_ip4][thread_index];
667   if (!f)
668     {
669       f = vlib_get_frame_to_node (vm, next_index);
670       ASSERT (f);
671       tm->tx_frames[!is_ip4][thread_index] = f;
672     }
673   to_next = vlib_frame_vector_args (f);
674   to_next[f->n_vectors] = bi;
675   f->n_vectors += 1;
676   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
677     {
678       vlib_put_frame_to_node (vm, next_index, f);
679       tm->tx_frames[!is_ip4][thread_index] = 0;
680     }
681 }
682
683 always_inline void
684 tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4)
685 {
686   tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 0);
687 }
688
689 always_inline void
690 tcp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
691                            u8 is_ip4)
692 {
693   tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
694 }
695
696 int
697 tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0,
698                          tcp_state_t state, u8 thread_index, u8 is_ip4)
699 {
700   ip4_header_t *ih4;
701   ip6_header_t *ih6;
702   tcp_header_t *th0;
703   ip4_address_t src_ip40, dst_ip40;
704   ip6_address_t src_ip60, dst_ip60;
705   u16 src_port, dst_port;
706   u32 tmp;
707   u32 seq, ack;
708   u8 flags;
709
710   /* Find IP and TCP headers */
711   th0 = tcp_buffer_hdr (b0);
712
713   /* Save src and dst ip */
714   if (is_ip4)
715     {
716       ih4 = vlib_buffer_get_current (b0);
717       ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40);
718       src_ip40.as_u32 = ih4->src_address.as_u32;
719       dst_ip40.as_u32 = ih4->dst_address.as_u32;
720     }
721   else
722     {
723       ih6 = vlib_buffer_get_current (b0);
724       ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60);
725       clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t));
726       clib_memcpy (&dst_ip60, &ih6->dst_address, sizeof (ip6_address_t));
727     }
728
729   src_port = th0->src_port;
730   dst_port = th0->dst_port;
731
732   /* Try to determine what/why we're actually resetting */
733   if (state == TCP_STATE_CLOSED)
734     {
735       if (!tcp_syn (th0))
736         return -1;
737
738       tmp = clib_net_to_host_u32 (th0->seq_number);
739
740       /* Got a SYN for no listener. */
741       flags = TCP_FLAG_RST | TCP_FLAG_ACK;
742       ack = clib_host_to_net_u32 (tmp + 1);
743       seq = 0;
744     }
745   else
746     {
747       flags = TCP_FLAG_RST;
748       seq = th0->ack_number;
749       ack = 0;
750     }
751
752   tcp_reuse_buffer (vm, b0);
753   th0 = vlib_buffer_push_tcp_net_order (b0, dst_port, src_port, seq, ack,
754                                         sizeof (tcp_header_t), flags, 0);
755
756   if (is_ip4)
757     {
758       ih4 = vlib_buffer_push_ip4 (vm, b0, &dst_ip40, &src_ip40,
759                                   IP_PROTOCOL_TCP, 1);
760       th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4);
761     }
762   else
763     {
764       int bogus = ~0;
765       ih6 = vlib_buffer_push_ip6 (vm, b0, &dst_ip60, &src_ip60,
766                                   IP_PROTOCOL_TCP);
767       th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus);
768       ASSERT (!bogus);
769     }
770
771   return 0;
772 }
773
774 /**
775  *  Send reset without reusing existing buffer
776  *
777  *  It extracts connection info out of original packet
778  */
779 void
780 tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt, u8 is_ip4)
781 {
782   vlib_buffer_t *b;
783   u32 bi;
784   tcp_main_t *tm = vnet_get_tcp_main ();
785   vlib_main_t *vm = vlib_get_main ();
786   u8 tcp_hdr_len, flags = 0;
787   tcp_header_t *th, *pkt_th;
788   u32 seq, ack;
789   ip4_header_t *ih4, *pkt_ih4;
790   ip6_header_t *ih6, *pkt_ih6;
791
792   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
793     return;
794
795   b = vlib_get_buffer (vm, bi);
796   tcp_init_buffer (vm, b);
797
798   /* Make and write options */
799   tcp_hdr_len = sizeof (tcp_header_t);
800
801   if (is_ip4)
802     {
803       pkt_ih4 = vlib_buffer_get_current (pkt);
804       pkt_th = ip4_next_header (pkt_ih4);
805     }
806   else
807     {
808       pkt_ih6 = vlib_buffer_get_current (pkt);
809       pkt_th = ip6_next_header (pkt_ih6);
810     }
811
812   if (tcp_ack (pkt_th))
813     {
814       flags = TCP_FLAG_RST;
815       seq = pkt_th->ack_number;
816       ack = (tc && tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0;
817     }
818   else
819     {
820       flags = TCP_FLAG_RST | TCP_FLAG_ACK;
821       seq = 0;
822       ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end);
823     }
824
825   th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port,
826                                        seq, ack, tcp_hdr_len, flags, 0);
827
828   /* Swap src and dst ip */
829   if (is_ip4)
830     {
831       ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40);
832       ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address,
833                                   &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1);
834       th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
835     }
836   else
837     {
838       int bogus = ~0;
839       ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
840               0x60);
841       ih6 = vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
842                                   &pkt_ih6->src_address, IP_PROTOCOL_TCP);
843       th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
844       ASSERT (!bogus);
845     }
846
847   tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4);
848   TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
849 }
850
851 /**
852  * Build and set reset packet for connection
853  */
854 void
855 tcp_send_reset (tcp_connection_t * tc)
856 {
857   vlib_main_t *vm = vlib_get_main ();
858   tcp_main_t *tm = vnet_get_tcp_main ();
859   vlib_buffer_t *b;
860   u32 bi;
861   tcp_header_t *th;
862   u16 tcp_hdr_opts_len, advertise_wnd, opts_write_len;
863   u8 flags;
864
865   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
866     return;
867   b = vlib_get_buffer (vm, bi);
868   tcp_init_buffer (vm, b);
869
870   tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
871   tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
872   advertise_wnd = tcp_window_to_advertise (tc, TCP_STATE_ESTABLISHED);
873   flags = TCP_FLAG_RST;
874   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
875                              tc->rcv_nxt, tcp_hdr_opts_len, flags,
876                              advertise_wnd);
877   opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
878   ASSERT (opts_write_len == tc->snd_opts_len);
879   vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
880   tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4);
881 }
882
883 void
884 tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b)
885 {
886   tcp_header_t *th = vlib_buffer_get_current (b);
887   vlib_main_t *vm = vlib_get_main ();
888   if (tc->c_is_ip4)
889     {
890       ip4_header_t *ih;
891       ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4,
892                                  &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1);
893       th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih);
894     }
895   else
896     {
897       ip6_header_t *ih;
898       int bogus = ~0;
899
900       ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
901                                  &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
902       th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus);
903       ASSERT (!bogus);
904     }
905 }
906
907 /**
908  *  Send SYN
909  *
910  *  Builds a SYN packet for a half-open connection and sends it to ipx_lookup.
911  *  The packet is not forwarded through tcpx_output to avoid doing lookups
912  *  in the half_open pool.
913  */
914 void
915 tcp_send_syn (tcp_connection_t * tc)
916 {
917   vlib_buffer_t *b;
918   u32 bi;
919   tcp_main_t *tm = vnet_get_tcp_main ();
920   vlib_main_t *vm = vlib_get_main ();
921   u8 tcp_hdr_opts_len, tcp_opts_len;
922   tcp_header_t *th;
923   u32 time_now;
924   u16 initial_wnd;
925   tcp_options_t snd_opts;
926
927   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
928     return;
929
930   b = vlib_get_buffer (vm, bi);
931   tcp_init_buffer (vm, b);
932
933   /* Set random initial sequence */
934   time_now = tcp_time_now ();
935
936   tc->iss = random_u32 (&time_now);
937   tc->snd_una = tc->iss;
938   tc->snd_una_max = tc->snd_nxt = tc->iss + 1;
939
940   initial_wnd = tcp_initial_window_to_advertise (tc);
941
942   /* Make and write options */
943   memset (&snd_opts, 0, sizeof (snd_opts));
944   tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
945   tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
946
947   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
948                              tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN,
949                              initial_wnd);
950
951   tcp_options_write ((u8 *) (th + 1), &snd_opts);
952
953   /* Measure RTT with this */
954   tc->rtt_ts = tcp_time_now ();
955   tc->rtt_seq = tc->snd_nxt;
956
957   /* Start retransmit trimer  */
958   tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK);
959   tc->rto_boff = 0;
960
961   /* Set the connection establishment timer */
962   tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME);
963
964   tcp_push_ip_hdr (tm, tc, b);
965   tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
966   TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc);
967 }
968
969 /**
970  * Flush tx frame populated by retransmits and timer pops
971  */
972 void
973 tcp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
974 {
975   if (tcp_main.tx_frames[!is_ip4][thread_index])
976     {
977       u32 next_index;
978       next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
979       vlib_put_frame_to_node (vm, next_index,
980                               tcp_main.tx_frames[!is_ip4][thread_index]);
981       tcp_main.tx_frames[!is_ip4][thread_index] = 0;
982     }
983 }
984
985 /**
986  * Flush both v4 and v6 tx frames for thread index
987  */
988 void
989 tcp_flush_frames_to_output (u8 thread_index)
990 {
991   vlib_main_t *vm = vlib_get_main ();
992   tcp_flush_frame_to_output (vm, thread_index, 1);
993   tcp_flush_frame_to_output (vm, thread_index, 0);
994 }
995
996 /**
997  *  Send FIN
998  */
999 void
1000 tcp_send_fin (tcp_connection_t * tc)
1001 {
1002   vlib_buffer_t *b;
1003   u32 bi;
1004   tcp_main_t *tm = vnet_get_tcp_main ();
1005   vlib_main_t *vm = vlib_get_main ();
1006
1007   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1008     return;
1009   b = vlib_get_buffer (vm, bi);
1010   /* buffer will be initialized by in tcp_make_fin */
1011   tcp_make_fin (tc, b);
1012   tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4);
1013   tc->flags |= TCP_CONN_FINSNT;
1014   tc->flags &= ~TCP_CONN_FINPNDG;
1015   tcp_retransmit_timer_force_update (tc);
1016   TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc);
1017 }
1018
1019 always_inline u8
1020 tcp_make_state_flags (tcp_connection_t * tc, tcp_state_t next_state)
1021 {
1022   switch (next_state)
1023     {
1024     case TCP_STATE_ESTABLISHED:
1025       return TCP_FLAG_ACK;
1026     case TCP_STATE_SYN_RCVD:
1027       return TCP_FLAG_SYN | TCP_FLAG_ACK;
1028     case TCP_STATE_SYN_SENT:
1029       return TCP_FLAG_SYN;
1030     case TCP_STATE_LAST_ACK:
1031     case TCP_STATE_FIN_WAIT_1:
1032       if (tc->snd_nxt + 1 < tc->snd_una_max)
1033         return TCP_FLAG_ACK;
1034       else
1035         return TCP_FLAG_FIN;
1036     default:
1037       clib_warning ("Shouldn't be here!");
1038     }
1039   return 0;
1040 }
1041
1042 /**
1043  * Push TCP header and update connection variables
1044  */
1045 static void
1046 tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b,
1047                 tcp_state_t next_state, u8 compute_opts)
1048 {
1049   u32 advertise_wnd, data_len;
1050   u8 tcp_hdr_opts_len, opts_write_len, flags;
1051   tcp_header_t *th;
1052
1053   data_len = b->current_length + b->total_length_not_including_first_buffer;
1054   ASSERT (!b->total_length_not_including_first_buffer
1055           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1056   vnet_buffer (b)->tcp.flags = 0;
1057
1058   if (compute_opts)
1059     tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1060
1061   tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t);
1062   advertise_wnd = tcp_window_to_advertise (tc, next_state);
1063   flags = tcp_make_state_flags (tc, next_state);
1064
1065   /* Push header and options */
1066   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt,
1067                              tc->rcv_nxt, tcp_hdr_opts_len, flags,
1068                              advertise_wnd);
1069   opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
1070
1071   ASSERT (opts_write_len == tc->snd_opts_len);
1072   vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
1073
1074   /*
1075    * Update connection variables
1076    */
1077
1078   tc->snd_nxt += data_len;
1079   tc->rcv_las = tc->rcv_nxt;
1080
1081   /* TODO this is updated in output as well ... */
1082   if (seq_gt (tc->snd_nxt, tc->snd_una_max))
1083     {
1084       tc->snd_una_max = tc->snd_nxt;
1085       tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una);
1086     }
1087
1088   TCP_EVT_DBG (TCP_EVT_PKTIZE, tc);
1089 }
1090
1091 void
1092 tcp_send_ack (tcp_connection_t * tc)
1093 {
1094   tcp_main_t *tm = vnet_get_tcp_main ();
1095   vlib_main_t *vm = vlib_get_main ();
1096
1097   vlib_buffer_t *b;
1098   u32 bi;
1099
1100   /* Get buffer */
1101   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1102     return;
1103   b = vlib_get_buffer (vm, bi);
1104
1105   /* Fill in the ACK */
1106   tcp_make_ack (tc, b);
1107   tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1108 }
1109
1110 /**
1111  * Delayed ack timer handler
1112  *
1113  * Sends delayed ACK when timer expires
1114  */
1115 void
1116 tcp_timer_delack_handler (u32 index)
1117 {
1118   u32 thread_index = vlib_get_thread_index ();
1119   tcp_connection_t *tc;
1120
1121   tc = tcp_connection_get (index, thread_index);
1122   tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID;
1123   tcp_send_ack (tc);
1124 }
1125
1126 /**
1127  * Build a retransmit segment
1128  *
1129  * @return the number of bytes in the segment or 0 if there's nothing to
1130  *         retransmit
1131  */
1132 u32
1133 tcp_prepare_retransmit_segment (tcp_connection_t * tc, u32 offset,
1134                                 u32 max_deq_bytes, vlib_buffer_t ** b)
1135 {
1136   tcp_main_t *tm = vnet_get_tcp_main ();
1137   vlib_main_t *vm = vlib_get_main ();
1138   int n_bytes = 0;
1139   u32 start, bi, available_bytes, seg_size;
1140   u8 *data;
1141
1142   ASSERT (tc->state >= TCP_STATE_ESTABLISHED);
1143   ASSERT (max_deq_bytes != 0);
1144
1145   /*
1146    * Make sure we can retransmit something
1147    */
1148   available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1149   available_bytes -= offset;
1150   if (!available_bytes)
1151     return 0;
1152   max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes);
1153   max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1154
1155   /* Start is beyond snd_congestion */
1156   start = tc->snd_una + offset;
1157   if (seq_geq (start, tc->snd_congestion))
1158     goto done;
1159
1160   /* Don't overshoot snd_congestion */
1161   if (seq_gt (start + max_deq_bytes, tc->snd_congestion))
1162     {
1163       max_deq_bytes = tc->snd_congestion - start;
1164       if (max_deq_bytes == 0)
1165         goto done;
1166     }
1167
1168   seg_size = max_deq_bytes + MAX_HDRS_LEN;
1169
1170   /*
1171    * Prepare options
1172    */
1173   tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1174
1175   /*
1176    * Allocate and fill in buffer(s)
1177    */
1178
1179   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1180     return 0;
1181   *b = vlib_get_buffer (vm, bi);
1182   data = tcp_init_buffer (vm, *b);
1183
1184   /* Easy case, buffer size greater than mss */
1185   if (PREDICT_TRUE (seg_size <= tm->bytes_per_buffer))
1186     {
1187       n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1188                                            max_deq_bytes);
1189       ASSERT (n_bytes == max_deq_bytes);
1190       b[0]->current_length = n_bytes;
1191       tcp_push_hdr_i (tc, *b, tc->state, 0);
1192     }
1193   /* Split mss into multiple buffers */
1194   else
1195     {
1196       u32 chain_bi = ~0, n_bufs_per_seg;
1197       u32 thread_index = vlib_get_thread_index ();
1198       u16 n_peeked, len_to_deq, available_bufs;
1199       vlib_buffer_t *chain_b, *prev_b;
1200       int i;
1201
1202       n_bufs_per_seg = ceil ((double) seg_size / tm->bytes_per_buffer);
1203
1204       /* Make sure we have enough buffers */
1205       available_bufs = vec_len (tm->tx_buffers[thread_index]);
1206       if (n_bufs_per_seg > available_bufs)
1207         {
1208           if (tcp_alloc_tx_buffers (tm, thread_index,
1209                                     VLIB_FRAME_SIZE - available_bufs))
1210             {
1211               tcp_return_buffer (tm);
1212               return 0;
1213             }
1214         }
1215
1216       n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1217                                            tm->bytes_per_buffer -
1218                                            MAX_HDRS_LEN);
1219       b[0]->current_length = n_bytes;
1220       b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1221       b[0]->total_length_not_including_first_buffer = 0;
1222       max_deq_bytes -= n_bytes;
1223
1224       chain_b = *b;
1225       for (i = 1; i < n_bufs_per_seg; i++)
1226         {
1227           prev_b = chain_b;
1228           len_to_deq = clib_min (max_deq_bytes, tm->bytes_per_buffer);
1229           tcp_get_free_buffer_index (tm, &chain_bi);
1230           ASSERT (chain_bi != (u32) ~ 0);
1231           chain_b = vlib_get_buffer (vm, chain_bi);
1232           chain_b->current_data = 0;
1233           data = vlib_buffer_get_current (chain_b);
1234           n_peeked = stream_session_peek_bytes (&tc->connection, data,
1235                                                 offset + n_bytes, len_to_deq);
1236           ASSERT (n_peeked == len_to_deq);
1237           n_bytes += n_peeked;
1238           chain_b->current_length = n_peeked;
1239           chain_b->flags = 0;
1240           chain_b->next_buffer = 0;
1241
1242           /* update previous buffer */
1243           prev_b->next_buffer = chain_bi;
1244           prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
1245
1246           max_deq_bytes -= n_peeked;
1247           b[0]->total_length_not_including_first_buffer += n_peeked;
1248         }
1249
1250       tcp_push_hdr_i (tc, *b, tc->state, 0);
1251     }
1252
1253   ASSERT (n_bytes > 0);
1254   ASSERT (((*b)->current_data + (*b)->current_length) <=
1255           tm->bytes_per_buffer);
1256
1257   if (tcp_in_fastrecovery (tc))
1258     tc->snd_rxt_bytes += n_bytes;
1259
1260 done:
1261   TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes);
1262   return n_bytes;
1263 }
1264
1265 /**
1266  * Reset congestion control, switch cwnd to loss window and try again.
1267  */
1268 static void
1269 tcp_rtx_timeout_cc (tcp_connection_t * tc)
1270 {
1271   tc->prev_ssthresh = tc->ssthresh;
1272   tc->prev_cwnd = tc->cwnd;
1273
1274   /* Cleanly recover cc (also clears up fast retransmit) */
1275   if (tcp_in_fastrecovery (tc))
1276     tcp_cc_fastrecovery_exit (tc);
1277
1278   /* Start again from the beginning */
1279   tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
1280   tc->cwnd = tcp_loss_wnd (tc);
1281   tc->snd_congestion = tc->snd_una_max;
1282
1283   tcp_recovery_on (tc);
1284 }
1285
1286 static void
1287 tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
1288 {
1289   tcp_main_t *tm = vnet_get_tcp_main ();
1290   vlib_main_t *vm = vlib_get_main ();
1291   u32 thread_index = vlib_get_thread_index ();
1292   tcp_connection_t *tc;
1293   vlib_buffer_t *b = 0;
1294   u32 bi, n_bytes;
1295
1296   if (is_syn)
1297     {
1298       tc = tcp_half_open_connection_get (index);
1299       /* Note: the connection may have transitioned to ESTABLISHED... */
1300       if (PREDICT_FALSE (tc == 0))
1301         return;
1302       tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
1303     }
1304   else
1305     {
1306       tc = tcp_connection_get (index, thread_index);
1307       /* Note: the connection may have been closed and pool_put */
1308       if (PREDICT_FALSE (tc == 0))
1309         return;
1310       tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
1311     }
1312
1313   if (!tcp_in_recovery (tc) && tc->rto_boff > 0
1314       && tc->state >= TCP_STATE_ESTABLISHED)
1315     {
1316       tc->rto_boff = 0;
1317       tcp_update_rto (tc);
1318     }
1319
1320   /* Increment RTO backoff (also equal to number of retries) */
1321   tc->rto_boff += 1;
1322
1323   /* Go back to first un-acked byte */
1324   tc->snd_nxt = tc->snd_una;
1325
1326   if (tc->state >= TCP_STATE_ESTABLISHED)
1327     {
1328       /* Lost FIN, retransmit and return */
1329       if (tcp_is_lost_fin (tc))
1330         {
1331           tcp_send_fin (tc);
1332           return;
1333         }
1334
1335       /* First retransmit timeout */
1336       if (tc->rto_boff == 1)
1337         tcp_rtx_timeout_cc (tc);
1338
1339       /* Exponential backoff */
1340       tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1341
1342       TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
1343
1344       /* Send one segment. Note that n_bytes may be zero due to buffer shortfall  */
1345       n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1346
1347       /* TODO be less aggressive about this */
1348       scoreboard_clear (&tc->sack_sb);
1349
1350       if (n_bytes == 0)
1351         {
1352           if (b)
1353             {
1354               clib_warning ("retransmit fail: %U", format_tcp_connection, tc,
1355                             2);
1356               ASSERT (tc->rto_boff > 1 && tc->snd_una == tc->snd_congestion);
1357             }
1358           /* Try again eventually */
1359           tcp_retransmit_timer_set (tc);
1360           return;
1361         }
1362
1363       bi = vlib_get_buffer_index (vm, b);
1364
1365       /* For first retransmit, record timestamp (Eifel detection RFC3522) */
1366       if (tc->rto_boff == 1)
1367         tc->snd_rxt_ts = tcp_time_now ();
1368     }
1369   /* Retransmit for SYN/SYNACK */
1370   else if (tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_SYN_SENT)
1371     {
1372       /* Half-open connection actually moved to established but we were
1373        * waiting for syn retransmit to pop to call cleanup from the right
1374        * thread. */
1375       if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
1376         {
1377           ASSERT (tc->state == TCP_STATE_SYN_SENT);
1378           if (tcp_half_open_connection_cleanup (tc))
1379             {
1380               clib_warning ("could not remove half-open connection");
1381               ASSERT (0);
1382             }
1383           return;
1384         }
1385
1386       /* Try without increasing RTO a number of times. If this fails,
1387        * start growing RTO exponentially */
1388       if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
1389         tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1390
1391       if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1392         {
1393           clib_warning ("tcp_get_free_buffer_index FAIL");
1394           return;
1395         }
1396       b = vlib_get_buffer (vm, bi);
1397       tcp_init_buffer (vm, b);
1398       tcp_push_hdr_i (tc, b, tc->state, 1);
1399
1400       /* Account for the SYN */
1401       tc->snd_nxt += 1;
1402       tc->rtt_ts = 0;
1403       TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc,
1404                    (tc->state == TCP_STATE_SYN_SENT ? 0 : 1));
1405     }
1406   else
1407     {
1408       ASSERT (tc->state == TCP_STATE_CLOSED);
1409       clib_warning ("connection closed ...");
1410       return;
1411     }
1412
1413   if (!is_syn)
1414     {
1415       tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1416
1417       /* Re-enable retransmit timer */
1418       tcp_retransmit_timer_set (tc);
1419     }
1420   else
1421     {
1422       ASSERT (tc->state == TCP_STATE_SYN_SENT);
1423
1424       /* This goes straight to ipx_lookup */
1425       tcp_push_ip_hdr (tm, tc, b);
1426       tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4);
1427
1428       /* Re-enable retransmit timer */
1429       tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN,
1430                      tc->rto * TCP_TO_TIMER_TICK);
1431     }
1432 }
1433
1434 void
1435 tcp_timer_retransmit_handler (u32 index)
1436 {
1437   tcp_timer_retransmit_handler_i (index, 0);
1438 }
1439
1440 void
1441 tcp_timer_retransmit_syn_handler (u32 index)
1442 {
1443   tcp_timer_retransmit_handler_i (index, 1);
1444 }
1445
1446 /**
1447  * Got 0 snd_wnd from peer, try to do something about it.
1448  *
1449  */
1450 void
1451 tcp_timer_persist_handler (u32 index)
1452 {
1453   tcp_main_t *tm = vnet_get_tcp_main ();
1454   vlib_main_t *vm = vlib_get_main ();
1455   u32 thread_index = vlib_get_thread_index ();
1456   tcp_connection_t *tc;
1457   vlib_buffer_t *b;
1458   u32 bi, old_snd_nxt, max_snd_bytes, available_bytes, offset;
1459   int n_bytes = 0;
1460   u8 *data;
1461
1462   tc = tcp_connection_get_if_valid (index, thread_index);
1463
1464   if (!tc)
1465     return;
1466
1467   /* Make sure timer handle is set to invalid */
1468   tc->timers[TCP_TIMER_PERSIST] = TCP_TIMER_HANDLE_INVALID;
1469
1470   /* Problem already solved or worse */
1471   if (tc->state == TCP_STATE_CLOSED || tc->state > TCP_STATE_ESTABLISHED
1472       || tc->snd_wnd > tc->snd_mss || tcp_in_recovery (tc))
1473     return;
1474
1475   available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection);
1476   offset = tc->snd_una_max - tc->snd_una;
1477
1478   /* Reprogram persist if no new bytes available to send. We may have data
1479    * next time */
1480   if (!available_bytes)
1481     {
1482       tcp_persist_timer_set (tc);
1483       return;
1484     }
1485
1486   if (available_bytes <= offset)
1487     {
1488       ASSERT (tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT));
1489       return;
1490     }
1491
1492   /* Increment RTO backoff */
1493   tc->rto_boff += 1;
1494   tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
1495
1496   /*
1497    * Try to force the first unsent segment (or buffer)
1498    */
1499   if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
1500     return;
1501   b = vlib_get_buffer (vm, bi);
1502   data = tcp_init_buffer (vm, b);
1503
1504   tcp_validate_txf_size (tc, offset);
1505   tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state);
1506   max_snd_bytes = clib_min (tc->snd_mss, tm->bytes_per_buffer - MAX_HDRS_LEN);
1507   n_bytes = stream_session_peek_bytes (&tc->connection, data, offset,
1508                                        max_snd_bytes);
1509   b->current_length = n_bytes;
1510   ASSERT (n_bytes != 0 && (tc->snd_nxt == tc->snd_una_max || tc->rto_boff > 1
1511                            || tcp_timer_is_active (tc,
1512                                                    TCP_TIMER_RETRANSMIT)));
1513
1514   /* Allow updating of snd_una_max but don't update snd_nxt */
1515   old_snd_nxt = tc->snd_nxt;
1516   tcp_push_hdr_i (tc, b, tc->state, 0);
1517   tc->snd_nxt = old_snd_nxt;
1518   tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1519
1520   /* Just sent new data, enable retransmit */
1521   tcp_retransmit_timer_update (tc);
1522 }
1523
1524 /**
1525  * Retransmit first unacked segment
1526  */
1527 void
1528 tcp_retransmit_first_unacked (tcp_connection_t * tc)
1529 {
1530   vlib_main_t *vm = vlib_get_main ();
1531   vlib_buffer_t *b;
1532   u32 bi, old_snd_nxt, n_bytes;
1533
1534   old_snd_nxt = tc->snd_nxt;
1535   tc->snd_nxt = tc->snd_una;
1536
1537   TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
1538   n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
1539   if (!n_bytes)
1540     return;
1541   bi = vlib_get_buffer_index (vm, b);
1542   tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1543
1544   tc->snd_nxt = old_snd_nxt;
1545 }
1546
1547 /**
1548  * Do fast retransmit with SACKs
1549  */
1550 void
1551 tcp_fast_retransmit_sack (tcp_connection_t * tc)
1552 {
1553   vlib_main_t *vm = vlib_get_main ();
1554   u32 n_written = 0, offset, max_bytes;
1555   vlib_buffer_t *b = 0;
1556   sack_scoreboard_hole_t *hole;
1557   sack_scoreboard_t *sb;
1558   u32 bi, old_snd_nxt;
1559   int snd_space;
1560   u8 snd_limited = 0, can_rescue = 0;
1561
1562   ASSERT (tcp_in_fastrecovery (tc));
1563   TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1564
1565   old_snd_nxt = tc->snd_nxt;
1566   sb = &tc->sack_sb;
1567   snd_space = tcp_available_snd_space (tc);
1568
1569   hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
1570   while (hole && snd_space > 0)
1571     {
1572       hole = scoreboard_next_rxt_hole (sb, hole,
1573                                        tcp_fastrecovery_sent_1_smss (tc),
1574                                        &can_rescue, &snd_limited);
1575       if (!hole)
1576         {
1577           if (!can_rescue || !(seq_lt (sb->rescue_rxt, tc->snd_una)
1578                                || seq_gt (sb->rescue_rxt,
1579                                           tc->snd_congestion)))
1580             break;
1581
1582           /* If rescue rxt undefined or less than snd_una then one segment of
1583            * up to SMSS octets that MUST include the highest outstanding
1584            * unSACKed sequence number SHOULD be returned, and RescueRxt set to
1585            * RecoveryPoint. HighRxt MUST NOT be updated.
1586            */
1587           max_bytes = clib_min (tc->snd_mss,
1588                                 tc->snd_congestion - tc->snd_una);
1589           max_bytes = clib_min (max_bytes, snd_space);
1590           offset = tc->snd_congestion - tc->snd_una - max_bytes;
1591           sb->rescue_rxt = tc->snd_congestion;
1592           tc->snd_nxt = tc->snd_una + offset;
1593           n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes,
1594                                                       &b);
1595           ASSERT (n_written);
1596           bi = vlib_get_buffer_index (vm, b);
1597           tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1598           break;
1599         }
1600
1601       max_bytes = clib_min (hole->end - sb->high_rxt, snd_space);
1602       max_bytes = snd_limited ? clib_min (max_bytes, tc->snd_mss) : max_bytes;
1603       if (max_bytes == 0)
1604         break;
1605       offset = sb->high_rxt - tc->snd_una;
1606       tc->snd_nxt = sb->high_rxt;
1607       n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes, &b);
1608
1609       /* Nothing left to retransmit */
1610       if (n_written == 0)
1611         break;
1612
1613       bi = vlib_get_buffer_index (vm, b);
1614       sb->high_rxt += n_written;
1615       tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1616       ASSERT (n_written <= snd_space);
1617       snd_space -= n_written;
1618     }
1619
1620   /* If window allows, send 1 SMSS of new data */
1621   tc->snd_nxt = old_snd_nxt;
1622 }
1623
1624 /**
1625  * Fast retransmit without SACK info
1626  */
1627 void
1628 tcp_fast_retransmit_no_sack (tcp_connection_t * tc)
1629 {
1630   vlib_main_t *vm = vlib_get_main ();
1631   u32 n_written = 0, offset = 0, bi, old_snd_nxt;
1632   int snd_space;
1633   vlib_buffer_t *b;
1634
1635   ASSERT (tcp_in_fastrecovery (tc));
1636   TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
1637
1638   /* Start resending from first un-acked segment */
1639   old_snd_nxt = tc->snd_nxt;
1640   tc->snd_nxt = tc->snd_una;
1641   snd_space = tcp_available_snd_space (tc);
1642
1643   while (snd_space > 0)
1644     {
1645       offset += n_written;
1646       n_written = tcp_prepare_retransmit_segment (tc, offset, snd_space, &b);
1647
1648       /* Nothing left to retransmit */
1649       if (n_written == 0)
1650         break;
1651
1652       bi = vlib_get_buffer_index (vm, b);
1653       tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4);
1654       snd_space -= n_written;
1655     }
1656
1657   /* Restore snd_nxt. If window allows, send 1 SMSS of new data */
1658   tc->snd_nxt = old_snd_nxt;
1659 }
1660
1661 /**
1662  * Do fast retransmit
1663  */
1664 void
1665 tcp_fast_retransmit (tcp_connection_t * tc)
1666 {
1667   if (tcp_opts_sack_permitted (&tc->rcv_opts)
1668       && scoreboard_first_hole (&tc->sack_sb))
1669     tcp_fast_retransmit_sack (tc);
1670   else
1671     tcp_fast_retransmit_no_sack (tc);
1672 }
1673
1674 always_inline u32
1675 tcp_session_has_ooo_data (tcp_connection_t * tc)
1676 {
1677   stream_session_t *s =
1678     stream_session_get (tc->c_s_index, tc->c_thread_index);
1679   return svm_fifo_has_ooo_data (s->server_rx_fifo);
1680 }
1681
1682 always_inline uword
1683 tcp46_output_inline (vlib_main_t * vm,
1684                      vlib_node_runtime_t * node,
1685                      vlib_frame_t * from_frame, int is_ip4)
1686 {
1687   u32 n_left_from, next_index, *from, *to_next;
1688   u32 my_thread_index = vm->thread_index;
1689
1690   from = vlib_frame_vector_args (from_frame);
1691   n_left_from = from_frame->n_vectors;
1692   next_index = node->cached_next_index;
1693   tcp_set_time_now (my_thread_index);
1694
1695   while (n_left_from > 0)
1696     {
1697       u32 n_left_to_next;
1698
1699       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1700
1701       while (n_left_from > 0 && n_left_to_next > 0)
1702         {
1703           u32 bi0;
1704           vlib_buffer_t *b0;
1705           tcp_connection_t *tc0;
1706           tcp_tx_trace_t *t0;
1707           tcp_header_t *th0 = 0;
1708           u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
1709
1710           bi0 = from[0];
1711           to_next[0] = bi0;
1712           from += 1;
1713           to_next += 1;
1714           n_left_from -= 1;
1715           n_left_to_next -= 1;
1716
1717           b0 = vlib_get_buffer (vm, bi0);
1718           tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1719                                     my_thread_index);
1720           if (PREDICT_FALSE (tc0 == 0 || tc0->state == TCP_STATE_CLOSED))
1721             {
1722               error0 = TCP_ERROR_INVALID_CONNECTION;
1723               next0 = TCP_OUTPUT_NEXT_DROP;
1724               goto done;
1725             }
1726
1727           th0 = vlib_buffer_get_current (b0);
1728           TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
1729
1730           if (is_ip4)
1731             {
1732               vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
1733                                     IP_PROTOCOL_TCP, 1);
1734               b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1735               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1736               th0->checksum = 0;
1737             }
1738           else
1739             {
1740               ip6_header_t *ih0;
1741               ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6,
1742                                           &tc0->c_rmt_ip6, IP_PROTOCOL_TCP);
1743               b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
1744               vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data;
1745               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1746               th0->checksum = 0;
1747             }
1748
1749           /* Filter out DUPACKs if there are no OOO segments left */
1750           if (PREDICT_FALSE
1751               (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK))
1752             {
1753               if (!tcp_session_has_ooo_data (tc0))
1754                 {
1755                   error0 = TCP_ERROR_FILTERED_DUPACKS;
1756                   next0 = TCP_OUTPUT_NEXT_DROP;
1757                   goto done;
1758                 }
1759             }
1760
1761           /* Stop DELACK timer and fix flags */
1762           tc0->flags &= ~(TCP_CONN_SNDACK);
1763           tcp_timer_reset (tc0, TCP_TIMER_DELACK);
1764
1765           /* If not retransmitting
1766            * 1) update snd_una_max (SYN, SYNACK, FIN)
1767            * 2) If we're not tracking an ACK, start tracking */
1768           if (seq_lt (tc0->snd_una_max, tc0->snd_nxt))
1769             {
1770               tc0->snd_una_max = tc0->snd_nxt;
1771               if (tc0->rtt_ts == 0)
1772                 {
1773                   tc0->rtt_ts = tcp_time_now ();
1774                   tc0->rtt_seq = tc0->snd_nxt;
1775                 }
1776             }
1777
1778           /* Set the retransmit timer if not set already and not
1779            * doing a pure ACK */
1780           if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT)
1781               && tc0->snd_nxt != tc0->snd_una)
1782             {
1783               tcp_retransmit_timer_set (tc0);
1784               tc0->rto_boff = 0;
1785             }
1786
1787 #if 0
1788           /* Make sure we haven't lost route to our peer */
1789           if (PREDICT_FALSE (tc0->last_fib_check
1790                              < tc0->snd_opts.tsval + TCP_FIB_RECHECK_PERIOD))
1791             {
1792               if (PREDICT_TRUE
1793                   (tc0->c_rmt_fei == tcp_lookup_rmt_in_fib (tc0)))
1794                 {
1795                   tc0->last_fib_check = tc0->snd_opts.tsval;
1796                 }
1797               else
1798                 {
1799                   clib_warning ("lost connection to peer");
1800                   tcp_connection_reset (tc0);
1801                   goto done;
1802                 }
1803             }
1804
1805           /* Use pre-computed dpo to set next node */
1806           next0 = tc0->c_rmt_dpo.dpoi_next_node;
1807           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = tc0->c_rmt_dpo.dpoi_index;
1808 #endif
1809
1810           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1811           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1812
1813           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1814         done:
1815           b0->error = node->errors[error0];
1816           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1817             {
1818               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1819               if (th0)
1820                 {
1821                   clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1822                 }
1823               else
1824                 {
1825                   memset (&t0->tcp_header, 0, sizeof (t0->tcp_header));
1826                 }
1827               clib_memcpy (&t0->tcp_connection, tc0,
1828                            sizeof (t0->tcp_connection));
1829             }
1830
1831           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1832                                            n_left_to_next, bi0, next0);
1833         }
1834
1835       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1836     }
1837
1838   return from_frame->n_vectors;
1839 }
1840
1841 static uword
1842 tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1843              vlib_frame_t * from_frame)
1844 {
1845   return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1846 }
1847
1848 static uword
1849 tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1850              vlib_frame_t * from_frame)
1851 {
1852   return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1853 }
1854
1855 /* *INDENT-OFF* */
1856 VLIB_REGISTER_NODE (tcp4_output_node) =
1857 {
1858   .function = tcp4_output,.name = "tcp4-output",
1859     /* Takes a vector of packets. */
1860     .vector_size = sizeof (u32),
1861     .n_errors = TCP_N_ERROR,
1862     .error_strings = tcp_error_strings,
1863     .n_next_nodes = TCP_OUTPUT_N_NEXT,
1864     .next_nodes = {
1865 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1866     foreach_tcp4_output_next
1867 #undef _
1868     },
1869     .format_buffer = format_tcp_header,
1870     .format_trace = format_tcp_tx_trace,
1871 };
1872 /* *INDENT-ON* */
1873
1874 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output);
1875
1876 /* *INDENT-OFF* */
1877 VLIB_REGISTER_NODE (tcp6_output_node) =
1878 {
1879   .function = tcp6_output,
1880   .name = "tcp6-output",
1881     /* Takes a vector of packets. */
1882   .vector_size = sizeof (u32),
1883   .n_errors = TCP_N_ERROR,
1884   .error_strings = tcp_error_strings,
1885   .n_next_nodes = TCP_OUTPUT_N_NEXT,
1886   .next_nodes = {
1887 #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n,
1888     foreach_tcp6_output_next
1889 #undef _
1890   },
1891   .format_buffer = format_tcp_header,
1892   .format_trace = format_tcp_tx_trace,
1893 };
1894 /* *INDENT-ON* */
1895
1896 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output);
1897
1898 u32
1899 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
1900 {
1901   tcp_connection_t *tc;
1902
1903   tc = (tcp_connection_t *) tconn;
1904   tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED, 0);
1905   ASSERT (seq_leq (tc->snd_una_max, tc->snd_una + tc->snd_wnd));
1906
1907   if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc))
1908     {
1909       tc->rtt_ts = tcp_time_now ();
1910       tc->rtt_seq = tc->snd_nxt;
1911     }
1912   return 0;
1913 }
1914
1915 typedef enum _tcp_reset_next
1916 {
1917   TCP_RESET_NEXT_DROP,
1918   TCP_RESET_NEXT_IP_LOOKUP,
1919   TCP_RESET_N_NEXT
1920 } tcp_reset_next_t;
1921
1922 #define foreach_tcp4_reset_next         \
1923   _(DROP, "error-drop")                 \
1924   _(IP_LOOKUP, "ip4-lookup")
1925
1926 #define foreach_tcp6_reset_next         \
1927   _(DROP, "error-drop")                 \
1928   _(IP_LOOKUP, "ip6-lookup")
1929
1930 static uword
1931 tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1932                          vlib_frame_t * from_frame, u8 is_ip4)
1933 {
1934   u32 n_left_from, next_index, *from, *to_next;
1935   u32 my_thread_index = vm->thread_index;
1936
1937   from = vlib_frame_vector_args (from_frame);
1938   n_left_from = from_frame->n_vectors;
1939
1940   next_index = node->cached_next_index;
1941
1942   while (n_left_from > 0)
1943     {
1944       u32 n_left_to_next;
1945
1946       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1947
1948       while (n_left_from > 0 && n_left_to_next > 0)
1949         {
1950           u32 bi0;
1951           vlib_buffer_t *b0;
1952           tcp_tx_trace_t *t0;
1953           tcp_header_t *th0;
1954           u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP;
1955
1956           bi0 = from[0];
1957           to_next[0] = bi0;
1958           from += 1;
1959           to_next += 1;
1960           n_left_from -= 1;
1961           n_left_to_next -= 1;
1962
1963           b0 = vlib_get_buffer (vm, bi0);
1964
1965           if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags,
1966                                        my_thread_index, is_ip4))
1967             {
1968               error0 = TCP_ERROR_LOOKUP_DROPS;
1969               next0 = TCP_RESET_NEXT_DROP;
1970               goto done;
1971             }
1972
1973           /* Prepare to send to IP lookup */
1974           vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
1975           next0 = TCP_RESET_NEXT_IP_LOOKUP;
1976
1977         done:
1978           b0->error = node->errors[error0];
1979           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1980           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1981             {
1982               th0 = vlib_buffer_get_current (b0);
1983               if (is_ip4)
1984                 th0 = ip4_next_header ((ip4_header_t *) th0);
1985               else
1986                 th0 = ip6_next_header ((ip6_header_t *) th0);
1987               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1988               clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1989             }
1990
1991           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1992                                            n_left_to_next, bi0, next0);
1993         }
1994       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1995     }
1996   return from_frame->n_vectors;
1997 }
1998
1999 static uword
2000 tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
2001                  vlib_frame_t * from_frame)
2002 {
2003   return tcp46_send_reset_inline (vm, node, from_frame, 1);
2004 }
2005
2006 static uword
2007 tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node,
2008                  vlib_frame_t * from_frame)
2009 {
2010   return tcp46_send_reset_inline (vm, node, from_frame, 0);
2011 }
2012
2013 /* *INDENT-OFF* */
2014 VLIB_REGISTER_NODE (tcp4_reset_node) = {
2015   .function = tcp4_send_reset,
2016   .name = "tcp4-reset",
2017   .vector_size = sizeof (u32),
2018   .n_errors = TCP_N_ERROR,
2019   .error_strings = tcp_error_strings,
2020   .n_next_nodes = TCP_RESET_N_NEXT,
2021   .next_nodes = {
2022 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2023     foreach_tcp4_reset_next
2024 #undef _
2025   },
2026   .format_trace = format_tcp_tx_trace,
2027 };
2028 /* *INDENT-ON* */
2029
2030 VLIB_NODE_FUNCTION_MULTIARCH (tcp4_reset_node, tcp4_send_reset);
2031
2032 /* *INDENT-OFF* */
2033 VLIB_REGISTER_NODE (tcp6_reset_node) = {
2034   .function = tcp6_send_reset,
2035   .name = "tcp6-reset",
2036   .vector_size = sizeof (u32),
2037   .n_errors = TCP_N_ERROR,
2038   .error_strings = tcp_error_strings,
2039   .n_next_nodes = TCP_RESET_N_NEXT,
2040   .next_nodes = {
2041 #define _(s,n) [TCP_RESET_NEXT_##s] = n,
2042     foreach_tcp6_reset_next
2043 #undef _
2044   },
2045   .format_trace = format_tcp_tx_trace,
2046 };
2047 /* *INDENT-ON* */
2048
2049 VLIB_NODE_FUNCTION_MULTIARCH (tcp6_reset_node, tcp6_send_reset);
2050
2051 /*
2052  * fd.io coding-style-patch-verification: ON
2053  *
2054  * Local Variables:
2055  * eval: (c-set-style "gnu")
2056  * End:
2057  */