blob: f61a1b528822f3c25d81b4ec15a0ea307ab2955e [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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>
Florin Corase69f4952017-03-07 10:06:24 -080025#include <vnet/tcp/tcp_debug.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
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
Florin Coras6792ec02017-03-13 03:49:51 -070033#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 */
Dave Barach68b0fb02017-02-28 15:15:56 -050037
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
52typedef 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
60format_function_t format_tcp_state;
Dave Barach1f75cfd2017-04-14 16:46:44 -040061format_function_t format_tcp_flags;
Florin Coras45d34962017-04-25 00:05:27 -070062format_function_t format_tcp_sacks;
Dave Barach68b0fb02017-02-28 15:15:56 -050063
64/** TCP timers */
65#define foreach_tcp_timer \
66 _(RETRANSMIT, "RETRANSMIT") \
67 _(DELACK, "DELAYED ACK") \
68 _(PERSIST, "PERSIST") \
69 _(KEEP, "KEEP") \
Florin Corasd79b41e2017-03-04 05:37:52 -080070 _(WAITCLOSE, "WAIT CLOSE") \
71 _(RETRANSMIT_SYN, "RETRANSMIT SYN") \
Dave Barach68b0fb02017-02-28 15:15:56 -050072 _(ESTABLISH, "ESTABLISH")
73
74typedef 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
82typedef void (timer_expiration_handler) (u32 index);
83
84extern timer_expiration_handler tcp_timer_delack_handler;
85extern timer_expiration_handler tcp_timer_retransmit_handler;
Florin Coras3e350af2017-03-30 02:54:28 -070086extern timer_expiration_handler tcp_timer_persist_handler;
Dave Barach68b0fb02017-02-28 15:15:56 -050087extern 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 */
Florin Corasd79b41e2017-03-04 05:37:52 -080097#define TCP_CLOSEWAIT_TIME 1 /* 0.1s */
98#define TCP_CLEANUP_TIME 5 /* 0.5s Time to wait before cleanup */
Florin Coras5921f982017-04-03 18:00:00 -070099#define TCP_TIMER_PERSIST_MIN 2 /* 0.2s */
Dave Barach68b0fb02017-02-28 15:15:56 -0500100
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
Dave Barach68b0fb02017-02-28 15:15:56 -0500106/** TCP connection flags */
107#define foreach_tcp_connection_flag \
Dave Barach68b0fb02017-02-28 15:15:56 -0500108 _(SNDACK, "Send ACK") \
Florin Corasd79b41e2017-03-04 05:37:52 -0800109 _(FINSNT, "FIN sent") \
Dave Barach68b0fb02017-02-28 15:15:56 -0500110 _(SENT_RCV_WND0, "Sent 0 receive window") \
111 _(RECOVERY, "Recovery on") \
Florin Coras6792ec02017-03-13 03:49:51 -0700112 _(FAST_RECOVERY, "Fast Recovery on") \
113 _(FR_1_SMSS, "Sent 1 SMSS")
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
115typedef 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
123typedef 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
136enum
137{
138#define _(f) TCP_BUF_BIT_##f,
139 foreach_tcp_buf_flag
140#undef _
141 TCP_N_BUF_BITS,
142};
143
144enum
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
154typedef 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
162typedef struct _sack_scoreboard
163{
164 sack_scoreboard_hole_t *holes; /**< Pool of holes */
Florin Coras6792ec02017-03-13 03:49:51 -0700165 u32 head; /**< Index of first entry */
166 u32 tail; /**< Index of last entry */
Dave Barach68b0fb02017-02-28 15:15:56 -0500167 u32 sacked_bytes; /**< Number of bytes sacked in sb */
Florin Coras6792ec02017-03-13 03:49:51 -0700168 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 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500171} sack_scoreboard_t;
172
173typedef enum _tcp_cc_algorithm_type
174{
175 TCP_CC_NEWRENO,
176} tcp_cc_algorithm_type_e;
177
178typedef struct _tcp_cc_algorithm tcp_cc_algorithm_t;
179
180typedef enum _tcp_cc_ack_t
181{
182 TCP_CC_ACK,
183 TCP_CC_DUPACK,
184 TCP_CC_PARTIALACK
185} tcp_cc_ack_t;
186
187typedef 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
Florin Coras6792ec02017-03-13 03:49:51 -0700223 u16 rcv_dupacks; /**< Number of DUPACKs received */
Dave Barach68b0fb02017-02-28 15:15:56 -0500224 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 */
Florin Corase69f4952017-03-07 10:06:24 -0800232 u32 tsecr_last_ack; /**< Timestamp echoed to us in last healthy ACK */
Florin Coras6792ec02017-03-13 03:49:51 -0700233 u32 snd_congestion; /**< snd_una_max when congestion is detected */
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 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
247struct _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
Florin Coras3e350af2017-03-30 02:54:28 -0700258#define tcp_recovery_on(tc) (tc)->flags |= TCP_CONN_RECOVERY
259#define tcp_recovery_off(tc) (tc)->flags &= ~TCP_CONN_RECOVERY
Dave Barach68b0fb02017-02-28 15:15:56 -0500260#define tcp_in_fastrecovery(tc) ((tc)->flags & TCP_CONN_FAST_RECOVERY)
Florin Coras3e350af2017-03-30 02:54:28 -0700261#define tcp_in_recovery(tc) ((tc)->flags & (TCP_CONN_RECOVERY))
Dave Barach68b0fb02017-02-28 15:15:56 -0500262#define tcp_in_slowstart(tc) (tc->cwnd < tc->ssthresh)
Florin Coras6792ec02017-03-13 03:49:51 -0700263#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)
Dave Barach68b0fb02017-02-28 15:15:56 -0500266
Florin Coras3e350af2017-03-30 02:54:28 -0700267#define tcp_in_cong_recovery(tc) ((tc)->flags & \
268 (TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY))
269
270always_inline void
271tcp_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
Dave Barach68b0fb02017-02-28 15:15:56 -0500277typedef enum
278{
279 TCP_IP4,
280 TCP_IP6,
281 TCP_N_AF,
282} tcp_af_t;
283
284typedef 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
292typedef struct _tcp_lookup_dispatch
293{
294 u8 next, error;
295} tcp_lookup_dispatch_t;
296
297typedef 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
Florin Coras6792ec02017-03-13 03:49:51 -0700317// /* Convenience per worker-thread vector of connections to DELACK */
318// u32 **delack_connections;
Dave Barach68b0fb02017-02-28 15:15:56 -0500319
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
Florin Corasa0b34a72017-03-07 01:20:52 -0800332 /* Flag that indicates if stack is on or off */
333 u8 is_enabled;
334
Dave Barach68b0fb02017-02-28 15:15:56 -0500335 /* 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
342extern tcp_main_t tcp_main;
343extern vlib_node_registration_t tcp4_input_node;
344extern vlib_node_registration_t tcp6_input_node;
345extern vlib_node_registration_t tcp4_output_node;
346extern vlib_node_registration_t tcp6_output_node;
347
348always_inline tcp_main_t *
349vnet_get_tcp_main ()
350{
351 return &tcp_main;
352}
353
Florin Coras82b13a82017-04-25 11:58:06 -0700354always_inline tcp_header_t *
355tcp_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
Florin Corasa0b34a72017-03-07 01:20:52 -0800362clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en);
363
Dave Barach68b0fb02017-02-28 15:15:56 -0500364always_inline tcp_connection_t *
365tcp_connection_get (u32 conn_index, u32 thread_index)
366{
Florin Corasd79b41e2017-03-04 05:37:52 -0800367 if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
368 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500369 return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
370}
371
372always_inline tcp_connection_t *
373tcp_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
382void tcp_connection_close (tcp_connection_t * tc);
383void tcp_connection_cleanup (tcp_connection_t * tc);
384void tcp_connection_del (tcp_connection_t * tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800385void tcp_connection_reset (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386
Florin Corase69f4952017-03-07 10:06:24 -0800387u8 *format_tcp_connection (u8 * s, va_list * args);
388u8 *format_tcp_connection_verbose (u8 * s, va_list * args);
389
Dave Barach68b0fb02017-02-28 15:15:56 -0500390always_inline tcp_connection_t *
391tcp_listener_get (u32 tli)
392{
393 return pool_elt_at_index (tcp_main.listener_pool, tli);
394}
395
396always_inline tcp_connection_t *
397tcp_half_open_connection_get (u32 conn_index)
398{
399 return pool_elt_at_index (tcp_main.half_open_connections, conn_index);
400}
401
402void tcp_make_ack (tcp_connection_t * ts, vlib_buffer_t * b);
Florin Corasd79b41e2017-03-04 05:37:52 -0800403void tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b);
Dave Barach68b0fb02017-02-28 15:15:56 -0500404void tcp_make_synack (tcp_connection_t * ts, vlib_buffer_t * b);
405void tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4);
406void tcp_send_syn (tcp_connection_t * tc);
407void tcp_send_fin (tcp_connection_t * tc);
408void tcp_set_snd_mss (tcp_connection_t * tc);
409
410always_inline u32
411tcp_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
426always_inline u32
427tcp_flight_size (const tcp_connection_t * tc)
428{
Florin Coras6792ec02017-03-13 03:49:51 -0700429 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;
Dave Barach68b0fb02017-02-28 15:15:56 -0500439}
440
441/**
442 * Initial cwnd as per RFC5681
443 */
444always_inline u32
445tcp_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
455always_inline u32
456tcp_loss_wnd (const tcp_connection_t * tc)
457{
458 return tc->snd_mss;
459}
460
461always_inline u32
462tcp_available_wnd (const tcp_connection_t * tc)
463{
464 return clib_min (tc->cwnd, tc->snd_wnd);
465}
466
467always_inline u32
468tcp_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
Florin Coras6792ec02017-03-13 03:49:51 -0700479void tcp_update_rcv_wnd (tcp_connection_t * tc);
480
Dave Barach68b0fb02017-02-28 15:15:56 -0500481void tcp_retransmit_first_unacked (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500482void tcp_fast_retransmit (tcp_connection_t * tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700483void tcp_cc_congestion (tcp_connection_t * tc);
484void tcp_cc_recover (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500485
Florin Coras45d34962017-04-25 00:05:27 -0700486/* Made public for unit testing only */
487void tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end);
488
Dave Barach68b0fb02017-02-28 15:15:56 -0500489always_inline u32
490tcp_time_now (void)
491{
492 return clib_cpu_time_now () * tcp_main.tstamp_ticks_per_clock;
493}
494
Florin Coras6cf30ad2017-04-04 23:08:23 -0700495always_inline void
496tcp_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
Dave Barach68b0fb02017-02-28 15:15:56 -0500502u32 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b);
503
504u32
505tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
Florin Coras6792ec02017-03-13 03:49:51 -0700506 u32 offset, u32 max_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500507
508void tcp_connection_timers_init (tcp_connection_t * tc);
509void tcp_connection_timers_reset (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500510void tcp_connection_init_vars (tcp_connection_t * tc);
511
512always_inline void
513tcp_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
520always_inline void
521tcp_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
528always_inline void
Dave Barach68b0fb02017-02-28 15:15:56 -0500529tcp_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
539always_inline void
540tcp_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
Florin Coras6792ec02017-03-13 03:49:51 -0700550/* XXX Switch retransmit to faster TW */
551always_inline void
552tcp_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
558always_inline void
559tcp_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
565always_inline void
566tcp_retransmit_timer_reset (tcp_connection_t * tc)
567{
568 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
569}
570
Florin Coras3e350af2017-03-30 02:54:28 -0700571always_inline void
572tcp_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,
Florin Coras5921f982017-04-03 18:00:00 -0700576 clib_max (tc->rto * TCP_TO_TIMER_TICK,
577 TCP_TIMER_PERSIST_MIN));
Florin Coras3e350af2017-03-30 02:54:28 -0700578}
579
580always_inline void
581tcp_persist_timer_update (tcp_connection_t * tc)
582{
583 tcp_timer_update (tc, TCP_TIMER_PERSIST,
Florin Coras5921f982017-04-03 18:00:00 -0700584 clib_max (tc->rto * TCP_TO_TIMER_TICK,
585 TCP_TIMER_PERSIST_MIN));
Florin Coras3e350af2017-03-30 02:54:28 -0700586}
587
588always_inline void
589tcp_persist_timer_reset (tcp_connection_t * tc)
590{
591 tcp_timer_reset (tc, TCP_TIMER_PERSIST);
592}
593
Dave Barach68b0fb02017-02-28 15:15:56 -0500594always_inline u8
595tcp_timer_is_active (tcp_connection_t * tc, tcp_timers_e timer)
596{
597 return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID;
598}
599
600void
601scoreboard_remove_hole (sack_scoreboard_t * sb,
602 sack_scoreboard_hole_t * hole);
603
604always_inline sack_scoreboard_hole_t *
Florin Coras6792ec02017-03-13 03:49:51 -0700605scoreboard_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
612always_inline sack_scoreboard_hole_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500613scoreboard_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
620always_inline sack_scoreboard_hole_t *
621scoreboard_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
Florin Coras6792ec02017-03-13 03:49:51 -0700628always_inline sack_scoreboard_hole_t *
629scoreboard_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
Dave Barach68b0fb02017-02-28 15:15:56 -0500636always_inline void
637scoreboard_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 }
Florin Coras6792ec02017-03-13 03:49:51 -0700644 sb->sacked_bytes = 0;
645 sb->last_sacked_bytes = 0;
646 sb->snd_una_adv = 0;
647 sb->max_byte_sacked = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500648}
649
650always_inline u32
651scoreboard_hole_bytes (sack_scoreboard_hole_t * hole)
652{
653 return hole->end - hole->start;
654}
655
Florin Coras6792ec02017-03-13 03:49:51 -0700656always_inline u32
657scoreboard_hole_index (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
658{
659 return hole - sb->holes;
660}
661
662always_inline void
663scoreboard_init (sack_scoreboard_t * sb)
664{
665 sb->head = TCP_INVALID_SACK_HOLE_INDEX;
666 sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
667}
668
669void tcp_rcv_sacks (tcp_connection_t * tc, u32 ack);
670
Dave Barach68b0fb02017-02-28 15:15:56 -0500671always_inline void
672tcp_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
681always_inline tcp_cc_algorithm_t *
682tcp_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
688void 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 */
705always_inline void *
706vlib_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 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500729 * @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 */
740always_inline void *
741vlib_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 */