Fix TCP loss recovery, VPP-745
[vpp.git] / src / vnet / tcp / tcp.h
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 #ifndef _vnet_tcp_h_
17 #define _vnet_tcp_h_
18
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/tcp/tcp_packet.h>
22 #include <vnet/tcp/tcp_timer.h>
23 #include <vnet/session/transport.h>
24 #include <vnet/session/session.h>
25 #include <vnet/tcp/tcp_debug.h>
26
27 #define TCP_TICK 0.001                  /**< TCP tick period (s) */
28 #define THZ (u32) (1/TCP_TICK)          /**< TCP tick frequency */
29 #define TCP_TSTAMP_RESOLUTION TCP_TICK  /**< Time stamp resolution */
30 #define TCP_PAWS_IDLE 24 * 24 * 60 * 60 * THZ /**< 24 days */
31 #define TCP_MAX_OPTION_SPACE 40
32
33 #define TCP_DUPACK_THRESHOLD    3
34 #define TCP_MAX_RX_FIFO_SIZE    2 << 20
35 #define TCP_IW_N_SEGMENTS       10
36 #define TCP_ALWAYS_ACK          0       /**< If on, we always ack */
37
38 /** TCP FSM state definitions as per RFC793. */
39 #define foreach_tcp_fsm_state   \
40   _(CLOSED, "CLOSED")           \
41   _(LISTEN, "LISTEN")           \
42   _(SYN_SENT, "SYN_SENT")       \
43   _(SYN_RCVD, "SYN_RCVD")       \
44   _(ESTABLISHED, "ESTABLISHED") \
45   _(CLOSE_WAIT, "CLOSE_WAIT")   \
46   _(FIN_WAIT_1, "FIN_WAIT_1")   \
47   _(LAST_ACK, "LAST_ACK")       \
48   _(CLOSING, "CLOSING")         \
49   _(FIN_WAIT_2, "FIN_WAIT_2")   \
50   _(TIME_WAIT, "TIME_WAIT")
51
52 typedef enum _tcp_state
53 {
54 #define _(sym, str) TCP_STATE_##sym,
55   foreach_tcp_fsm_state
56 #undef _
57   TCP_N_STATES
58 } tcp_state_t;
59
60 format_function_t format_tcp_state;
61 format_function_t format_tcp_flags;
62 format_function_t format_tcp_sacks;
63
64 /** TCP timers */
65 #define foreach_tcp_timer               \
66   _(RETRANSMIT, "RETRANSMIT")           \
67   _(DELACK, "DELAYED ACK")              \
68   _(PERSIST, "PERSIST")                 \
69   _(KEEP, "KEEP")                       \
70   _(WAITCLOSE, "WAIT CLOSE")            \
71   _(RETRANSMIT_SYN, "RETRANSMIT SYN")   \
72   _(ESTABLISH, "ESTABLISH")
73
74 typedef enum _tcp_timers
75 {
76 #define _(sym, str) TCP_TIMER_##sym,
77   foreach_tcp_timer
78 #undef _
79   TCP_N_TIMERS
80 } tcp_timers_e;
81
82 typedef void (timer_expiration_handler) (u32 index);
83
84 extern timer_expiration_handler tcp_timer_delack_handler;
85 extern timer_expiration_handler tcp_timer_retransmit_handler;
86 extern timer_expiration_handler tcp_timer_persist_handler;
87 extern timer_expiration_handler tcp_timer_retransmit_syn_handler;
88
89 #define TCP_TIMER_HANDLE_INVALID ((u32) ~0)
90
91 /* Timer delays as multiples of 100ms */
92 #define TCP_TO_TIMER_TICK       TCP_TICK*10     /* Period for converting from TCP
93                                                  * ticks to timer units */
94 #define TCP_DELACK_TIME         1       /* 0.1s */
95 #define TCP_ESTABLISH_TIME      750     /* 75s */
96 #define TCP_2MSL_TIME           300     /* 30s */
97 #define TCP_CLOSEWAIT_TIME      1       /* 0.1s */
98 #define TCP_CLEANUP_TIME        5       /* 0.5s Time to wait before cleanup */
99 #define TCP_TIMER_PERSIST_MIN   2       /* 0.2s */
100
101 #define TCP_RTO_MAX 60 * THZ    /* Min max RTO (60s) as per RFC6298 */
102 #define TCP_RTT_MAX 30 * THZ    /* 30s (probably too much) */
103 #define TCP_RTO_SYN_RETRIES 3   /* SYN retries without doubling RTO */
104 #define TCP_RTO_INIT 1 * THZ    /* Initial retransmit timer */
105
106 /** TCP connection flags */
107 #define foreach_tcp_connection_flag             \
108   _(SNDACK, "Send ACK")                         \
109   _(FINSNT, "FIN sent")                         \
110   _(SENT_RCV_WND0, "Sent 0 receive window")     \
111   _(RECOVERY, "Recovery on")                    \
112   _(FAST_RECOVERY, "Fast Recovery on")          \
113   _(FR_1_SMSS, "Sent 1 SMSS")
114
115 typedef enum _tcp_connection_flag_bits
116 {
117 #define _(sym, str) TCP_CONN_##sym##_BIT,
118   foreach_tcp_connection_flag
119 #undef _
120   TCP_CONN_N_FLAG_BITS
121 } tcp_connection_flag_bits_e;
122
123 typedef enum _tcp_connection_flag
124 {
125 #define _(sym, str) TCP_CONN_##sym = 1 << TCP_CONN_##sym##_BIT,
126   foreach_tcp_connection_flag
127 #undef _
128   TCP_CONN_N_FLAGS
129 } tcp_connection_flags_e;
130
131 /** TCP buffer flags */
132 #define foreach_tcp_buf_flag                            \
133   _ (ACK)       /**< Sending ACK. */                    \
134   _ (DUPACK)    /**< Sending DUPACK. */                 \
135
136 enum
137 {
138 #define _(f) TCP_BUF_BIT_##f,
139   foreach_tcp_buf_flag
140 #undef _
141     TCP_N_BUF_BITS,
142 };
143
144 enum
145 {
146 #define _(f) TCP_BUF_FLAG_##f = 1 << TCP_BUF_BIT_##f,
147   foreach_tcp_buf_flag
148 #undef _
149 };
150
151 #define TCP_MAX_SACK_BLOCKS 5   /**< Max number of SACK blocks stored */
152 #define TCP_INVALID_SACK_HOLE_INDEX ((u32)~0)
153
154 typedef struct _sack_scoreboard_hole
155 {
156   u32 next;             /**< Index for next entry in linked list */
157   u32 prev;             /**< Index for previous entry in linked list */
158   u32 start;            /**< Start sequence number */
159   u32 end;              /**< End sequence number */
160 } sack_scoreboard_hole_t;
161
162 typedef struct _sack_scoreboard
163 {
164   sack_scoreboard_hole_t *holes;        /**< Pool of holes */
165   u32 head;                             /**< Index of first entry */
166   u32 tail;                             /**< Index of last entry */
167   u32 sacked_bytes;                     /**< Number of bytes sacked in sb */
168   u32 last_sacked_bytes;                /**< Number of bytes last sacked */
169   u32 snd_una_adv;                      /**< Bytes to add to snd_una */
170   u32 max_byte_sacked;                  /**< Highest byte acked */
171 } sack_scoreboard_t;
172
173 typedef enum _tcp_cc_algorithm_type
174 {
175   TCP_CC_NEWRENO,
176 } tcp_cc_algorithm_type_e;
177
178 typedef struct _tcp_cc_algorithm tcp_cc_algorithm_t;
179
180 typedef enum _tcp_cc_ack_t
181 {
182   TCP_CC_ACK,
183   TCP_CC_DUPACK,
184   TCP_CC_PARTIALACK
185 } tcp_cc_ack_t;
186
187 typedef struct _tcp_connection
188 {
189   transport_connection_t connection;  /**< Common transport data. First! */
190
191   u8 state;                     /**< TCP state as per tcp_state_t */
192   u16 flags;                    /**< Connection flags (see tcp_conn_flags_e) */
193   u32 timers[TCP_N_TIMERS];     /**< Timer handles into timer wheel */
194
195   /* TODO RFC4898 */
196
197   /** Send sequence variables RFC793 */
198   u32 snd_una;          /**< oldest unacknowledged sequence number */
199   u32 snd_una_max;      /**< newest unacknowledged sequence number + 1*/
200   u32 snd_wnd;          /**< send window */
201   u32 snd_wl1;          /**< seq number used for last snd.wnd update */
202   u32 snd_wl2;          /**< ack number used for last snd.wnd update */
203   u32 snd_nxt;          /**< next seq number to be sent */
204
205   /** Receive sequence variables RFC793 */
206   u32 rcv_nxt;          /**< next sequence number expected */
207   u32 rcv_wnd;          /**< receive window we expect */
208
209   u32 rcv_las;          /**< rcv_nxt at last ack sent/rcv_wnd update */
210   u32 iss;              /**< initial sent sequence */
211   u32 irs;              /**< initial remote sequence */
212
213   /* Options */
214   tcp_options_t opt;    /**< TCP connection options parsed */
215   u8 rcv_wscale;        /**< Window scale to advertise to peer */
216   u8 snd_wscale;        /**< Window scale to use when sending */
217   u32 tsval_recent;     /**< Last timestamp received */
218   u32 tsval_recent_age; /**< When last updated tstamp_recent*/
219
220   sack_block_t *snd_sacks;      /**< Vector of SACKs to send. XXX Fixed size? */
221   sack_scoreboard_t sack_sb;    /**< SACK "scoreboard" that tracks holes */
222
223   u16 rcv_dupacks;      /**< Number of DUPACKs received */
224   u8 snt_dupacks;       /**< Number of DUPACKs sent in a burst */
225
226   /* Congestion control */
227   u32 cwnd;             /**< Congestion window */
228   u32 ssthresh;         /**< Slow-start threshold */
229   u32 prev_ssthresh;    /**< ssthresh before congestion */
230   u32 bytes_acked;      /**< Bytes acknowledged by current segment */
231   u32 rtx_bytes;        /**< Retransmitted bytes */
232   u32 tsecr_last_ack;   /**< Timestamp echoed to us in last healthy ACK */
233   u32 snd_congestion;   /**< snd_una_max when congestion is detected */
234   tcp_cc_algorithm_t *cc_algo;  /**< Congestion control algorithm */
235
236   /* RTT and RTO */
237   u32 rto;              /**< Retransmission timeout */
238   u32 rto_boff;         /**< Index for RTO backoff */
239   u32 srtt;             /**< Smoothed RTT */
240   u32 rttvar;           /**< Smoothed mean RTT difference. Approximates variance */
241   u32 rtt_ts;           /**< Timestamp for tracked ACK */
242   u32 rtt_seq;          /**< Sequence number for tracked ACK */
243
244   u16 snd_mss;          /**< Send MSS */
245 } tcp_connection_t;
246
247 struct _tcp_cc_algorithm
248 {
249   void (*rcv_ack) (tcp_connection_t * tc);
250   void (*rcv_cong_ack) (tcp_connection_t * tc, tcp_cc_ack_t ack);
251   void (*congestion) (tcp_connection_t * tc);
252   void (*recovered) (tcp_connection_t * tc);
253   void (*init) (tcp_connection_t * tc);
254 };
255
256 #define tcp_fastrecovery_on(tc) (tc)->flags |= TCP_CONN_FAST_RECOVERY
257 #define tcp_fastrecovery_off(tc) (tc)->flags &= ~TCP_CONN_FAST_RECOVERY
258 #define tcp_recovery_on(tc) (tc)->flags |= TCP_CONN_RECOVERY
259 #define tcp_recovery_off(tc) (tc)->flags &= ~TCP_CONN_RECOVERY
260 #define tcp_in_fastrecovery(tc) ((tc)->flags & TCP_CONN_FAST_RECOVERY)
261 #define tcp_in_recovery(tc) ((tc)->flags & (TCP_CONN_RECOVERY))
262 #define tcp_in_slowstart(tc) (tc->cwnd < tc->ssthresh)
263 #define tcp_fastrecovery_sent_1_smss(tc) ((tc)->flags & TCP_CONN_FR_1_SMSS)
264 #define tcp_fastrecovery_1_smss_on(tc) ((tc)->flags |= TCP_CONN_FR_1_SMSS)
265 #define tcp_fastrecovery_1_smss_off(tc) ((tc)->flags &= ~TCP_CONN_FR_1_SMSS)
266
267 #define tcp_in_cong_recovery(tc) ((tc)->flags &                 \
268           (TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY))
269
270 always_inline void
271 tcp_cong_recovery_off (tcp_connection_t * tc)
272 {
273   tc->flags &= ~(TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY);
274   tcp_fastrecovery_1_smss_off (tc);
275 }
276
277 typedef enum
278 {
279   TCP_IP4,
280   TCP_IP6,
281   TCP_N_AF,
282 } tcp_af_t;
283
284 typedef enum _tcp_error
285 {
286 #define tcp_error(n,s) TCP_ERROR_##n,
287 #include <vnet/tcp/tcp_error.def>
288 #undef tcp_error
289   TCP_N_ERROR,
290 } tcp_error_t;
291
292 typedef struct _tcp_lookup_dispatch
293 {
294   u8 next, error;
295 } tcp_lookup_dispatch_t;
296
297 typedef struct _tcp_main
298 {
299   /* Per-worker thread tcp connection pools */
300   tcp_connection_t **connections;
301
302   /* Pool of listeners. */
303   tcp_connection_t *listener_pool;
304
305   /** Dispatch table by state and flags */
306   tcp_lookup_dispatch_t dispatch_table[TCP_N_STATES][64];
307
308   u8 log2_tstamp_clocks_per_tick;
309   f64 tstamp_ticks_per_clock;
310
311   /** per-worker tx buffer free lists */
312   u32 **tx_buffers;
313
314   /* Per worker-thread timer wheel for connections timers */
315   tw_timer_wheel_16t_2w_512sl_t *timer_wheels;
316
317 //  /* Convenience per worker-thread vector of connections to DELACK */
318 //  u32 **delack_connections;
319
320   /* Pool of half-open connections on which we've sent a SYN */
321   tcp_connection_t *half_open_connections;
322
323   /* Pool of local TCP endpoints */
324   transport_endpoint_t *local_endpoints;
325
326   /* Local endpoints lookup table */
327   transport_endpoint_table_t local_endpoints_table;
328
329   /* Congestion control algorithms registered */
330   tcp_cc_algorithm_t *cc_algos;
331
332   /* Flag that indicates if stack is on or off */
333   u8 is_enabled;
334
335   /* convenience */
336   vlib_main_t *vlib_main;
337   vnet_main_t *vnet_main;
338   ip4_main_t *ip4_main;
339   ip6_main_t *ip6_main;
340 } tcp_main_t;
341
342 extern tcp_main_t tcp_main;
343 extern vlib_node_registration_t tcp4_input_node;
344 extern vlib_node_registration_t tcp6_input_node;
345 extern vlib_node_registration_t tcp4_output_node;
346 extern vlib_node_registration_t tcp6_output_node;
347
348 always_inline tcp_main_t *
349 vnet_get_tcp_main ()
350 {
351   return &tcp_main;
352 }
353
354 always_inline tcp_header_t *
355 tcp_buffer_hdr (vlib_buffer_t * b)
356 {
357   ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE);
358   return (tcp_header_t *) (b->data + b->current_data
359                            + vnet_buffer (b)->tcp.hdr_offset);
360 }
361
362 clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en);
363
364 always_inline tcp_connection_t *
365 tcp_connection_get (u32 conn_index, u32 thread_index)
366 {
367   if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
368     return 0;
369   return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
370 }
371
372 always_inline tcp_connection_t *
373 tcp_connection_get_if_valid (u32 conn_index, u32 thread_index)
374 {
375   if (tcp_main.connections[thread_index] == 0)
376     return 0;
377   if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
378     return 0;
379   return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
380 }
381
382 void tcp_connection_close (tcp_connection_t * tc);
383 void tcp_connection_cleanup (tcp_connection_t * tc);
384 void tcp_connection_del (tcp_connection_t * tc);
385 void tcp_connection_reset (tcp_connection_t * tc);
386
387 u8 *format_tcp_connection (u8 * s, va_list * args);
388 u8 *format_tcp_connection_verbose (u8 * s, va_list * args);
389
390 always_inline tcp_connection_t *
391 tcp_listener_get (u32 tli)
392 {
393   return pool_elt_at_index (tcp_main.listener_pool, tli);
394 }
395
396 always_inline tcp_connection_t *
397 tcp_half_open_connection_get (u32 conn_index)
398 {
399   return pool_elt_at_index (tcp_main.half_open_connections, conn_index);
400 }
401
402 void tcp_make_ack (tcp_connection_t * ts, vlib_buffer_t * b);
403 void tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b);
404 void tcp_make_synack (tcp_connection_t * ts, vlib_buffer_t * b);
405 void tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4);
406 void tcp_send_syn (tcp_connection_t * tc);
407 void tcp_send_fin (tcp_connection_t * tc);
408 void tcp_set_snd_mss (tcp_connection_t * tc);
409
410 always_inline u32
411 tcp_end_seq (tcp_header_t * th, u32 len)
412 {
413   return th->seq_number + tcp_is_syn (th) + tcp_is_fin (th) + len;
414 }
415
416 /* Modulo arithmetic for TCP sequence numbers */
417 #define seq_lt(_s1, _s2) ((i32)((_s1)-(_s2)) < 0)
418 #define seq_leq(_s1, _s2) ((i32)((_s1)-(_s2)) <= 0)
419 #define seq_gt(_s1, _s2) ((i32)((_s1)-(_s2)) > 0)
420 #define seq_geq(_s1, _s2) ((i32)((_s1)-(_s2)) >= 0)
421
422 /* Modulo arithmetic for timestamps */
423 #define timestamp_lt(_t1, _t2) ((i32)((_t1)-(_t2)) < 0)
424 #define timestamp_leq(_t1, _t2) ((i32)((_t1)-(_t2)) <= 0)
425
426 always_inline u32
427 tcp_flight_size (const tcp_connection_t * tc)
428 {
429   int flight_size;
430
431   flight_size = (int) ((tc->snd_una_max - tc->snd_una) + tc->rtx_bytes)
432     - (tc->rcv_dupacks * tc->snd_mss) /* - tc->sack_sb.sacked_bytes */ ;
433
434   /* Happens if we don't clear sacked bytes */
435   if (flight_size < 0)
436     return 0;
437
438   return flight_size;
439 }
440
441 /**
442  * Initial cwnd as per RFC5681
443  */
444 always_inline u32
445 tcp_initial_cwnd (const tcp_connection_t * tc)
446 {
447   if (tc->snd_mss > 2190)
448     return 2 * tc->snd_mss;
449   else if (tc->snd_mss > 1095)
450     return 3 * tc->snd_mss;
451   else
452     return 4 * tc->snd_mss;
453 }
454
455 always_inline u32
456 tcp_loss_wnd (const tcp_connection_t * tc)
457 {
458   return tc->snd_mss;
459 }
460
461 always_inline u32
462 tcp_available_wnd (const tcp_connection_t * tc)
463 {
464   return clib_min (tc->cwnd, tc->snd_wnd);
465 }
466
467 always_inline u32
468 tcp_available_snd_space (const tcp_connection_t * tc)
469 {
470   u32 available_wnd = tcp_available_wnd (tc);
471   u32 flight_size = tcp_flight_size (tc);
472
473   if (available_wnd <= flight_size)
474     return 0;
475
476   return available_wnd - flight_size;
477 }
478
479 void tcp_update_rcv_wnd (tcp_connection_t * tc);
480
481 void tcp_retransmit_first_unacked (tcp_connection_t * tc);
482 void tcp_fast_retransmit (tcp_connection_t * tc);
483 void tcp_cc_congestion (tcp_connection_t * tc);
484 void tcp_cc_recover (tcp_connection_t * tc);
485
486 /* Made public for unit testing only */
487 void tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end);
488
489 always_inline u32
490 tcp_time_now (void)
491 {
492   return clib_cpu_time_now () * tcp_main.tstamp_ticks_per_clock;
493 }
494
495 always_inline void
496 tcp_update_time (f64 now, u32 thread_index)
497 {
498   tw_timer_expire_timers_16t_2w_512sl (&tcp_main.timer_wheels[thread_index],
499                                        now);
500 }
501
502 u32 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b);
503
504 u32
505 tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
506                                 u32 offset, u32 max_bytes);
507
508 void tcp_connection_timers_init (tcp_connection_t * tc);
509 void tcp_connection_timers_reset (tcp_connection_t * tc);
510 void tcp_connection_init_vars (tcp_connection_t * tc);
511
512 always_inline void
513 tcp_connection_force_ack (tcp_connection_t * tc, vlib_buffer_t * b)
514 {
515   /* Reset flags, make sure ack is sent */
516   tc->flags = TCP_CONN_SNDACK;
517   vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
518 }
519
520 always_inline void
521 tcp_timer_set (tcp_connection_t * tc, u8 timer_id, u32 interval)
522 {
523   tc->timers[timer_id]
524     = tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
525                                    tc->c_c_index, timer_id, interval);
526 }
527
528 always_inline void
529 tcp_timer_reset (tcp_connection_t * tc, u8 timer_id)
530 {
531   if (tc->timers[timer_id] == TCP_TIMER_HANDLE_INVALID)
532     return;
533
534   tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
535                               tc->timers[timer_id]);
536   tc->timers[timer_id] = TCP_TIMER_HANDLE_INVALID;
537 }
538
539 always_inline void
540 tcp_timer_update (tcp_connection_t * tc, u8 timer_id, u32 interval)
541 {
542   if (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID)
543     tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
544                                 tc->timers[timer_id]);
545   tc->timers[timer_id] =
546     tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
547                                  tc->c_c_index, timer_id, interval);
548 }
549
550 /* XXX Switch retransmit to faster TW */
551 always_inline void
552 tcp_retransmit_timer_set (tcp_connection_t * tc)
553 {
554   tcp_timer_set (tc, TCP_TIMER_RETRANSMIT,
555                  clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
556 }
557
558 always_inline void
559 tcp_retransmit_timer_update (tcp_connection_t * tc)
560 {
561   tcp_timer_update (tc, TCP_TIMER_RETRANSMIT,
562                     clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
563 }
564
565 always_inline void
566 tcp_retransmit_timer_reset (tcp_connection_t * tc)
567 {
568   tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
569 }
570
571 always_inline void
572 tcp_persist_timer_set (tcp_connection_t * tc)
573 {
574   /* Reuse RTO. It's backed off in handler */
575   tcp_timer_set (tc, TCP_TIMER_PERSIST,
576                  clib_max (tc->rto * TCP_TO_TIMER_TICK,
577                            TCP_TIMER_PERSIST_MIN));
578 }
579
580 always_inline void
581 tcp_persist_timer_update (tcp_connection_t * tc)
582 {
583   tcp_timer_update (tc, TCP_TIMER_PERSIST,
584                     clib_max (tc->rto * TCP_TO_TIMER_TICK,
585                               TCP_TIMER_PERSIST_MIN));
586 }
587
588 always_inline void
589 tcp_persist_timer_reset (tcp_connection_t * tc)
590 {
591   tcp_timer_reset (tc, TCP_TIMER_PERSIST);
592 }
593
594 always_inline u8
595 tcp_timer_is_active (tcp_connection_t * tc, tcp_timers_e timer)
596 {
597   return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID;
598 }
599
600 void
601 scoreboard_remove_hole (sack_scoreboard_t * sb,
602                         sack_scoreboard_hole_t * hole);
603
604 always_inline sack_scoreboard_hole_t *
605 scoreboard_get_hole (sack_scoreboard_t * sb, u32 index)
606 {
607   if (index != TCP_INVALID_SACK_HOLE_INDEX)
608     return pool_elt_at_index (sb->holes, index);
609   return 0;
610 }
611
612 always_inline sack_scoreboard_hole_t *
613 scoreboard_next_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
614 {
615   if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
616     return pool_elt_at_index (sb->holes, hole->next);
617   return 0;
618 }
619
620 always_inline sack_scoreboard_hole_t *
621 scoreboard_first_hole (sack_scoreboard_t * sb)
622 {
623   if (sb->head != TCP_INVALID_SACK_HOLE_INDEX)
624     return pool_elt_at_index (sb->holes, sb->head);
625   return 0;
626 }
627
628 always_inline sack_scoreboard_hole_t *
629 scoreboard_last_hole (sack_scoreboard_t * sb)
630 {
631   if (sb->tail != TCP_INVALID_SACK_HOLE_INDEX)
632     return pool_elt_at_index (sb->holes, sb->tail);
633   return 0;
634 }
635
636 always_inline void
637 scoreboard_clear (sack_scoreboard_t * sb)
638 {
639   sack_scoreboard_hole_t *hole = scoreboard_first_hole (sb);
640   while ((hole = scoreboard_first_hole (sb)))
641     {
642       scoreboard_remove_hole (sb, hole);
643     }
644   sb->sacked_bytes = 0;
645   sb->last_sacked_bytes = 0;
646   sb->snd_una_adv = 0;
647   sb->max_byte_sacked = 0;
648 }
649
650 always_inline u32
651 scoreboard_hole_bytes (sack_scoreboard_hole_t * hole)
652 {
653   return hole->end - hole->start;
654 }
655
656 always_inline u32
657 scoreboard_hole_index (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
658 {
659   return hole - sb->holes;
660 }
661
662 always_inline void
663 scoreboard_init (sack_scoreboard_t * sb)
664 {
665   sb->head = TCP_INVALID_SACK_HOLE_INDEX;
666   sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
667 }
668
669 void tcp_rcv_sacks (tcp_connection_t * tc, u32 ack);
670
671 always_inline void
672 tcp_cc_algo_register (tcp_cc_algorithm_type_e type,
673                       const tcp_cc_algorithm_t * vft)
674 {
675   tcp_main_t *tm = vnet_get_tcp_main ();
676   vec_validate (tm->cc_algos, type);
677
678   tm->cc_algos[type] = *vft;
679 }
680
681 always_inline tcp_cc_algorithm_t *
682 tcp_cc_algo_get (tcp_cc_algorithm_type_e type)
683 {
684   tcp_main_t *tm = vnet_get_tcp_main ();
685   return &tm->cc_algos[type];
686 }
687
688 void tcp_cc_init (tcp_connection_t * tc);
689
690 /**
691  * Push TCP header to buffer
692  *
693  * @param vm - vlib_main
694  * @param b - buffer to write the header to
695  * @param sp_net - source port net order
696  * @param dp_net - destination port net order
697  * @param seq - sequence number net order
698  * @param ack - ack number net order
699  * @param tcp_hdr_opts_len - header and options length in bytes
700  * @param flags - header flags
701  * @param wnd - window size
702  *
703  * @return - pointer to start of TCP header
704  */
705 always_inline void *
706 vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
707                                 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
708                                 u16 wnd)
709 {
710   tcp_header_t *th;
711
712   th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
713
714   th->src_port = sp;
715   th->dst_port = dp;
716   th->seq_number = seq;
717   th->ack_number = ack;
718   th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
719   th->flags = flags;
720   th->window = wnd;
721   th->checksum = 0;
722   th->urgent_pointer = 0;
723   return th;
724 }
725
726 /**
727  * Push TCP header to buffer
728  *
729  * @param b - buffer to write the header to
730  * @param sp_net - source port net order
731  * @param dp_net - destination port net order
732  * @param seq - sequence number host order
733  * @param ack - ack number host order
734  * @param tcp_hdr_opts_len - header and options length in bytes
735  * @param flags - header flags
736  * @param wnd - window size
737  *
738  * @return - pointer to start of TCP header
739  */
740 always_inline void *
741 vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
742                       u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
743 {
744   return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
745                                          clib_host_to_net_u32 (seq),
746                                          clib_host_to_net_u32 (ack),
747                                          tcp_hdr_opts_len, flags,
748                                          clib_host_to_net_u16 (wnd));
749 }
750
751 #endif /* _vnet_tcp_h_ */
752
753 /*
754  * fd.io coding-style-patch-verification: ON
755  *
756  * Local Variables:
757  * eval: (c-set-style "gnu")
758  * End:
759  */