Add sack tx unit test
[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 10e-3                  /**< TCP tick period (s) */
28 #define THZ 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 clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en);
355
356 always_inline tcp_connection_t *
357 tcp_connection_get (u32 conn_index, u32 thread_index)
358 {
359   if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
360     return 0;
361   return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
362 }
363
364 always_inline tcp_connection_t *
365 tcp_connection_get_if_valid (u32 conn_index, u32 thread_index)
366 {
367   if (tcp_main.connections[thread_index] == 0)
368     return 0;
369   if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
370     return 0;
371   return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
372 }
373
374 void tcp_connection_close (tcp_connection_t * tc);
375 void tcp_connection_cleanup (tcp_connection_t * tc);
376 void tcp_connection_del (tcp_connection_t * tc);
377 void tcp_connection_reset (tcp_connection_t * tc);
378
379 u8 *format_tcp_connection (u8 * s, va_list * args);
380 u8 *format_tcp_connection_verbose (u8 * s, va_list * args);
381
382 always_inline tcp_connection_t *
383 tcp_listener_get (u32 tli)
384 {
385   return pool_elt_at_index (tcp_main.listener_pool, tli);
386 }
387
388 always_inline tcp_connection_t *
389 tcp_half_open_connection_get (u32 conn_index)
390 {
391   return pool_elt_at_index (tcp_main.half_open_connections, conn_index);
392 }
393
394 void tcp_make_ack (tcp_connection_t * ts, vlib_buffer_t * b);
395 void tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b);
396 void tcp_make_synack (tcp_connection_t * ts, vlib_buffer_t * b);
397 void tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4);
398 void tcp_send_syn (tcp_connection_t * tc);
399 void tcp_send_fin (tcp_connection_t * tc);
400 void tcp_set_snd_mss (tcp_connection_t * tc);
401
402 always_inline u32
403 tcp_end_seq (tcp_header_t * th, u32 len)
404 {
405   return th->seq_number + tcp_is_syn (th) + tcp_is_fin (th) + len;
406 }
407
408 /* Modulo arithmetic for TCP sequence numbers */
409 #define seq_lt(_s1, _s2) ((i32)((_s1)-(_s2)) < 0)
410 #define seq_leq(_s1, _s2) ((i32)((_s1)-(_s2)) <= 0)
411 #define seq_gt(_s1, _s2) ((i32)((_s1)-(_s2)) > 0)
412 #define seq_geq(_s1, _s2) ((i32)((_s1)-(_s2)) >= 0)
413
414 /* Modulo arithmetic for timestamps */
415 #define timestamp_lt(_t1, _t2) ((i32)((_t1)-(_t2)) < 0)
416 #define timestamp_leq(_t1, _t2) ((i32)((_t1)-(_t2)) <= 0)
417
418 always_inline u32
419 tcp_flight_size (const tcp_connection_t * tc)
420 {
421   int flight_size;
422
423   flight_size = (int) ((tc->snd_una_max - tc->snd_una) + tc->rtx_bytes)
424     - (tc->rcv_dupacks * tc->snd_mss) /* - tc->sack_sb.sacked_bytes */ ;
425
426   /* Happens if we don't clear sacked bytes */
427   if (flight_size < 0)
428     return 0;
429
430   return flight_size;
431 }
432
433 /**
434  * Initial cwnd as per RFC5681
435  */
436 always_inline u32
437 tcp_initial_cwnd (const tcp_connection_t * tc)
438 {
439   if (tc->snd_mss > 2190)
440     return 2 * tc->snd_mss;
441   else if (tc->snd_mss > 1095)
442     return 3 * tc->snd_mss;
443   else
444     return 4 * tc->snd_mss;
445 }
446
447 always_inline u32
448 tcp_loss_wnd (const tcp_connection_t * tc)
449 {
450   return tc->snd_mss;
451 }
452
453 always_inline u32
454 tcp_available_wnd (const tcp_connection_t * tc)
455 {
456   return clib_min (tc->cwnd, tc->snd_wnd);
457 }
458
459 always_inline u32
460 tcp_available_snd_space (const tcp_connection_t * tc)
461 {
462   u32 available_wnd = tcp_available_wnd (tc);
463   u32 flight_size = tcp_flight_size (tc);
464
465   if (available_wnd <= flight_size)
466     return 0;
467
468   return available_wnd - flight_size;
469 }
470
471 void tcp_update_rcv_wnd (tcp_connection_t * tc);
472
473 void tcp_retransmit_first_unacked (tcp_connection_t * tc);
474 void tcp_fast_retransmit (tcp_connection_t * tc);
475 void tcp_cc_congestion (tcp_connection_t * tc);
476 void tcp_cc_recover (tcp_connection_t * tc);
477
478 /* Made public for unit testing only */
479 void tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end);
480
481 always_inline u32
482 tcp_time_now (void)
483 {
484   return clib_cpu_time_now () * tcp_main.tstamp_ticks_per_clock;
485 }
486
487 always_inline void
488 tcp_update_time (f64 now, u32 thread_index)
489 {
490   tw_timer_expire_timers_16t_2w_512sl (&tcp_main.timer_wheels[thread_index],
491                                        now);
492 }
493
494 u32 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b);
495
496 u32
497 tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
498                                 u32 offset, u32 max_bytes);
499
500 void tcp_connection_timers_init (tcp_connection_t * tc);
501 void tcp_connection_timers_reset (tcp_connection_t * tc);
502 void tcp_connection_init_vars (tcp_connection_t * tc);
503
504 always_inline void
505 tcp_connection_force_ack (tcp_connection_t * tc, vlib_buffer_t * b)
506 {
507   /* Reset flags, make sure ack is sent */
508   tc->flags = TCP_CONN_SNDACK;
509   vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
510 }
511
512 always_inline void
513 tcp_timer_set (tcp_connection_t * tc, u8 timer_id, u32 interval)
514 {
515   tc->timers[timer_id]
516     = tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
517                                    tc->c_c_index, timer_id, interval);
518 }
519
520 always_inline void
521 tcp_timer_reset (tcp_connection_t * tc, u8 timer_id)
522 {
523   if (tc->timers[timer_id] == TCP_TIMER_HANDLE_INVALID)
524     return;
525
526   tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
527                               tc->timers[timer_id]);
528   tc->timers[timer_id] = TCP_TIMER_HANDLE_INVALID;
529 }
530
531 always_inline void
532 tcp_timer_update (tcp_connection_t * tc, u8 timer_id, u32 interval)
533 {
534   if (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID)
535     tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
536                                 tc->timers[timer_id]);
537   tc->timers[timer_id] =
538     tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
539                                  tc->c_c_index, timer_id, interval);
540 }
541
542 /* XXX Switch retransmit to faster TW */
543 always_inline void
544 tcp_retransmit_timer_set (tcp_connection_t * tc)
545 {
546   tcp_timer_set (tc, TCP_TIMER_RETRANSMIT,
547                  clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
548 }
549
550 always_inline void
551 tcp_retransmit_timer_update (tcp_connection_t * tc)
552 {
553   tcp_timer_update (tc, TCP_TIMER_RETRANSMIT,
554                     clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
555 }
556
557 always_inline void
558 tcp_retransmit_timer_reset (tcp_connection_t * tc)
559 {
560   tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
561 }
562
563 always_inline void
564 tcp_persist_timer_set (tcp_connection_t * tc)
565 {
566   /* Reuse RTO. It's backed off in handler */
567   tcp_timer_set (tc, TCP_TIMER_PERSIST,
568                  clib_max (tc->rto * TCP_TO_TIMER_TICK,
569                            TCP_TIMER_PERSIST_MIN));
570 }
571
572 always_inline void
573 tcp_persist_timer_update (tcp_connection_t * tc)
574 {
575   tcp_timer_update (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_reset (tcp_connection_t * tc)
582 {
583   tcp_timer_reset (tc, TCP_TIMER_PERSIST);
584 }
585
586 always_inline u8
587 tcp_timer_is_active (tcp_connection_t * tc, tcp_timers_e timer)
588 {
589   return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID;
590 }
591
592 void
593 scoreboard_remove_hole (sack_scoreboard_t * sb,
594                         sack_scoreboard_hole_t * hole);
595
596 always_inline sack_scoreboard_hole_t *
597 scoreboard_get_hole (sack_scoreboard_t * sb, u32 index)
598 {
599   if (index != TCP_INVALID_SACK_HOLE_INDEX)
600     return pool_elt_at_index (sb->holes, index);
601   return 0;
602 }
603
604 always_inline sack_scoreboard_hole_t *
605 scoreboard_next_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
606 {
607   if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
608     return pool_elt_at_index (sb->holes, hole->next);
609   return 0;
610 }
611
612 always_inline sack_scoreboard_hole_t *
613 scoreboard_first_hole (sack_scoreboard_t * sb)
614 {
615   if (sb->head != TCP_INVALID_SACK_HOLE_INDEX)
616     return pool_elt_at_index (sb->holes, sb->head);
617   return 0;
618 }
619
620 always_inline sack_scoreboard_hole_t *
621 scoreboard_last_hole (sack_scoreboard_t * sb)
622 {
623   if (sb->tail != TCP_INVALID_SACK_HOLE_INDEX)
624     return pool_elt_at_index (sb->holes, sb->tail);
625   return 0;
626 }
627
628 always_inline void
629 scoreboard_clear (sack_scoreboard_t * sb)
630 {
631   sack_scoreboard_hole_t *hole = scoreboard_first_hole (sb);
632   while ((hole = scoreboard_first_hole (sb)))
633     {
634       scoreboard_remove_hole (sb, hole);
635     }
636   sb->sacked_bytes = 0;
637   sb->last_sacked_bytes = 0;
638   sb->snd_una_adv = 0;
639   sb->max_byte_sacked = 0;
640 }
641
642 always_inline u32
643 scoreboard_hole_bytes (sack_scoreboard_hole_t * hole)
644 {
645   return hole->end - hole->start;
646 }
647
648 always_inline u32
649 scoreboard_hole_index (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
650 {
651   return hole - sb->holes;
652 }
653
654 always_inline void
655 scoreboard_init (sack_scoreboard_t * sb)
656 {
657   sb->head = TCP_INVALID_SACK_HOLE_INDEX;
658   sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
659 }
660
661 void tcp_rcv_sacks (tcp_connection_t * tc, u32 ack);
662
663 always_inline void
664 tcp_cc_algo_register (tcp_cc_algorithm_type_e type,
665                       const tcp_cc_algorithm_t * vft)
666 {
667   tcp_main_t *tm = vnet_get_tcp_main ();
668   vec_validate (tm->cc_algos, type);
669
670   tm->cc_algos[type] = *vft;
671 }
672
673 always_inline tcp_cc_algorithm_t *
674 tcp_cc_algo_get (tcp_cc_algorithm_type_e type)
675 {
676   tcp_main_t *tm = vnet_get_tcp_main ();
677   return &tm->cc_algos[type];
678 }
679
680 void tcp_cc_init (tcp_connection_t * tc);
681
682 /**
683  * Push TCP header to buffer
684  *
685  * @param vm - vlib_main
686  * @param b - buffer to write the header to
687  * @param sp_net - source port net order
688  * @param dp_net - destination port net order
689  * @param seq - sequence number net order
690  * @param ack - ack number net order
691  * @param tcp_hdr_opts_len - header and options length in bytes
692  * @param flags - header flags
693  * @param wnd - window size
694  *
695  * @return - pointer to start of TCP header
696  */
697 always_inline void *
698 vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
699                                 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
700                                 u16 wnd)
701 {
702   tcp_header_t *th;
703
704   th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
705
706   th->src_port = sp;
707   th->dst_port = dp;
708   th->seq_number = seq;
709   th->ack_number = ack;
710   th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
711   th->flags = flags;
712   th->window = wnd;
713   th->checksum = 0;
714   th->urgent_pointer = 0;
715   return th;
716 }
717
718 /**
719  * Push TCP header to buffer
720  *
721  * @param b - buffer to write the header to
722  * @param sp_net - source port net order
723  * @param dp_net - destination port net order
724  * @param seq - sequence number host order
725  * @param ack - ack number host order
726  * @param tcp_hdr_opts_len - header and options length in bytes
727  * @param flags - header flags
728  * @param wnd - window size
729  *
730  * @return - pointer to start of TCP header
731  */
732 always_inline void *
733 vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
734                       u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
735 {
736   return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
737                                          clib_host_to_net_u32 (seq),
738                                          clib_host_to_net_u32 (ack),
739                                          tcp_hdr_opts_len, flags,
740                                          clib_host_to_net_u16 (wnd));
741 }
742
743 #endif /* _vnet_tcp_h_ */
744
745 /*
746  * fd.io coding-style-patch-verification: ON
747  *
748  * Local Variables:
749  * eval: (c-set-style "gnu")
750  * End:
751  */