blob: c3ebe22bde9b6680f17def385543f7976dc2e885 [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
Florin Coras3af90fc2017-05-03 21:09:42 -070027#define TCP_TICK 0.001 /**< TCP tick period (s) */
28#define THZ (u32) (1/TCP_TICK) /**< TCP tick frequency */
Dave Barach68b0fb02017-02-28 15:15:56 -050029#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 */
Florin Corasc8343412017-05-04 14:25:50 -0700214 tcp_options_t opt; /**< TCP connection options parsed */
215 tcp_options_t snd_opts; /**< Tx options for connection */
216 u8 snd_opts_len; /**< Tx options len */
Dave Barach68b0fb02017-02-28 15:15:56 -0500217 u8 rcv_wscale; /**< Window scale to advertise to peer */
218 u8 snd_wscale; /**< Window scale to use when sending */
219 u32 tsval_recent; /**< Last timestamp received */
220 u32 tsval_recent_age; /**< When last updated tstamp_recent*/
221
222 sack_block_t *snd_sacks; /**< Vector of SACKs to send. XXX Fixed size? */
223 sack_scoreboard_t sack_sb; /**< SACK "scoreboard" that tracks holes */
224
Florin Coras6792ec02017-03-13 03:49:51 -0700225 u16 rcv_dupacks; /**< Number of DUPACKs received */
Dave Barach68b0fb02017-02-28 15:15:56 -0500226 u8 snt_dupacks; /**< Number of DUPACKs sent in a burst */
227
228 /* Congestion control */
229 u32 cwnd; /**< Congestion window */
230 u32 ssthresh; /**< Slow-start threshold */
231 u32 prev_ssthresh; /**< ssthresh before congestion */
232 u32 bytes_acked; /**< Bytes acknowledged by current segment */
233 u32 rtx_bytes; /**< Retransmitted bytes */
Florin Corase69f4952017-03-07 10:06:24 -0800234 u32 tsecr_last_ack; /**< Timestamp echoed to us in last healthy ACK */
Florin Coras6792ec02017-03-13 03:49:51 -0700235 u32 snd_congestion; /**< snd_una_max when congestion is detected */
Dave Barach68b0fb02017-02-28 15:15:56 -0500236 tcp_cc_algorithm_t *cc_algo; /**< Congestion control algorithm */
237
238 /* RTT and RTO */
239 u32 rto; /**< Retransmission timeout */
240 u32 rto_boff; /**< Index for RTO backoff */
241 u32 srtt; /**< Smoothed RTT */
242 u32 rttvar; /**< Smoothed mean RTT difference. Approximates variance */
243 u32 rtt_ts; /**< Timestamp for tracked ACK */
244 u32 rtt_seq; /**< Sequence number for tracked ACK */
245
Florin Corasc8343412017-05-04 14:25:50 -0700246 u16 snd_mss; /**< Effective send max seg (data) size */
247 u16 mss; /**< Our max seg size that includes options */
Dave Barach68b0fb02017-02-28 15:15:56 -0500248} tcp_connection_t;
249
250struct _tcp_cc_algorithm
251{
252 void (*rcv_ack) (tcp_connection_t * tc);
253 void (*rcv_cong_ack) (tcp_connection_t * tc, tcp_cc_ack_t ack);
254 void (*congestion) (tcp_connection_t * tc);
255 void (*recovered) (tcp_connection_t * tc);
256 void (*init) (tcp_connection_t * tc);
257};
258
259#define tcp_fastrecovery_on(tc) (tc)->flags |= TCP_CONN_FAST_RECOVERY
260#define tcp_fastrecovery_off(tc) (tc)->flags &= ~TCP_CONN_FAST_RECOVERY
Florin Coras3e350af2017-03-30 02:54:28 -0700261#define tcp_recovery_on(tc) (tc)->flags |= TCP_CONN_RECOVERY
262#define tcp_recovery_off(tc) (tc)->flags &= ~TCP_CONN_RECOVERY
Dave Barach68b0fb02017-02-28 15:15:56 -0500263#define tcp_in_fastrecovery(tc) ((tc)->flags & TCP_CONN_FAST_RECOVERY)
Florin Coras3e350af2017-03-30 02:54:28 -0700264#define tcp_in_recovery(tc) ((tc)->flags & (TCP_CONN_RECOVERY))
Dave Barach68b0fb02017-02-28 15:15:56 -0500265#define tcp_in_slowstart(tc) (tc->cwnd < tc->ssthresh)
Florin Coras6792ec02017-03-13 03:49:51 -0700266#define tcp_fastrecovery_sent_1_smss(tc) ((tc)->flags & TCP_CONN_FR_1_SMSS)
267#define tcp_fastrecovery_1_smss_on(tc) ((tc)->flags |= TCP_CONN_FR_1_SMSS)
268#define tcp_fastrecovery_1_smss_off(tc) ((tc)->flags &= ~TCP_CONN_FR_1_SMSS)
Dave Barach68b0fb02017-02-28 15:15:56 -0500269
Florin Coras3e350af2017-03-30 02:54:28 -0700270#define tcp_in_cong_recovery(tc) ((tc)->flags & \
271 (TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY))
272
273always_inline void
274tcp_cong_recovery_off (tcp_connection_t * tc)
275{
276 tc->flags &= ~(TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY);
277 tcp_fastrecovery_1_smss_off (tc);
278}
279
Dave Barach68b0fb02017-02-28 15:15:56 -0500280typedef enum
281{
282 TCP_IP4,
283 TCP_IP6,
284 TCP_N_AF,
285} tcp_af_t;
286
287typedef enum _tcp_error
288{
289#define tcp_error(n,s) TCP_ERROR_##n,
290#include <vnet/tcp/tcp_error.def>
291#undef tcp_error
292 TCP_N_ERROR,
293} tcp_error_t;
294
295typedef struct _tcp_lookup_dispatch
296{
297 u8 next, error;
298} tcp_lookup_dispatch_t;
299
300typedef struct _tcp_main
301{
302 /* Per-worker thread tcp connection pools */
303 tcp_connection_t **connections;
304
305 /* Pool of listeners. */
306 tcp_connection_t *listener_pool;
307
308 /** Dispatch table by state and flags */
309 tcp_lookup_dispatch_t dispatch_table[TCP_N_STATES][64];
310
311 u8 log2_tstamp_clocks_per_tick;
312 f64 tstamp_ticks_per_clock;
313
314 /** per-worker tx buffer free lists */
315 u32 **tx_buffers;
316
317 /* Per worker-thread timer wheel for connections timers */
318 tw_timer_wheel_16t_2w_512sl_t *timer_wheels;
319
Florin Coras6792ec02017-03-13 03:49:51 -0700320// /* Convenience per worker-thread vector of connections to DELACK */
321// u32 **delack_connections;
Dave Barach68b0fb02017-02-28 15:15:56 -0500322
323 /* Pool of half-open connections on which we've sent a SYN */
324 tcp_connection_t *half_open_connections;
325
326 /* Pool of local TCP endpoints */
327 transport_endpoint_t *local_endpoints;
328
329 /* Local endpoints lookup table */
330 transport_endpoint_table_t local_endpoints_table;
331
332 /* Congestion control algorithms registered */
333 tcp_cc_algorithm_t *cc_algos;
334
Florin Corasa0b34a72017-03-07 01:20:52 -0800335 /* Flag that indicates if stack is on or off */
336 u8 is_enabled;
337
Dave Barach68b0fb02017-02-28 15:15:56 -0500338 /* convenience */
339 vlib_main_t *vlib_main;
340 vnet_main_t *vnet_main;
341 ip4_main_t *ip4_main;
342 ip6_main_t *ip6_main;
343} tcp_main_t;
344
345extern tcp_main_t tcp_main;
346extern vlib_node_registration_t tcp4_input_node;
347extern vlib_node_registration_t tcp6_input_node;
348extern vlib_node_registration_t tcp4_output_node;
349extern vlib_node_registration_t tcp6_output_node;
350
351always_inline tcp_main_t *
352vnet_get_tcp_main ()
353{
354 return &tcp_main;
355}
356
Florin Coras82b13a82017-04-25 11:58:06 -0700357always_inline tcp_header_t *
358tcp_buffer_hdr (vlib_buffer_t * b)
359{
360 ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE);
361 return (tcp_header_t *) (b->data + b->current_data
362 + vnet_buffer (b)->tcp.hdr_offset);
363}
364
Florin Corasa0b34a72017-03-07 01:20:52 -0800365clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en);
366
Dave Barach68b0fb02017-02-28 15:15:56 -0500367always_inline tcp_connection_t *
368tcp_connection_get (u32 conn_index, u32 thread_index)
369{
Florin Corasd79b41e2017-03-04 05:37:52 -0800370 if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
371 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
373}
374
375always_inline tcp_connection_t *
376tcp_connection_get_if_valid (u32 conn_index, u32 thread_index)
377{
378 if (tcp_main.connections[thread_index] == 0)
379 return 0;
380 if (pool_is_free_index (tcp_main.connections[thread_index], conn_index))
381 return 0;
382 return pool_elt_at_index (tcp_main.connections[thread_index], conn_index);
383}
384
385void tcp_connection_close (tcp_connection_t * tc);
386void tcp_connection_cleanup (tcp_connection_t * tc);
387void tcp_connection_del (tcp_connection_t * tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800388void tcp_connection_reset (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500389
Florin Corasbb292f42017-05-19 09:49:19 -0700390u8 *format_tcp_connection_id (u8 * s, va_list * args);
Florin Corase69f4952017-03-07 10:06:24 -0800391u8 *format_tcp_connection (u8 * s, va_list * args);
Florin Coras06d11012017-05-17 14:21:51 -0700392u8 *format_tcp_scoreboard (u8 * s, va_list * args);
Florin Corase69f4952017-03-07 10:06:24 -0800393
Dave Barach68b0fb02017-02-28 15:15:56 -0500394always_inline tcp_connection_t *
395tcp_listener_get (u32 tli)
396{
397 return pool_elt_at_index (tcp_main.listener_pool, tli);
398}
399
400always_inline tcp_connection_t *
401tcp_half_open_connection_get (u32 conn_index)
402{
403 return pool_elt_at_index (tcp_main.half_open_connections, conn_index);
404}
405
406void tcp_make_ack (tcp_connection_t * ts, vlib_buffer_t * b);
Florin Corasd79b41e2017-03-04 05:37:52 -0800407void tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b);
Dave Barach68b0fb02017-02-28 15:15:56 -0500408void tcp_make_synack (tcp_connection_t * ts, vlib_buffer_t * b);
409void tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4);
410void tcp_send_syn (tcp_connection_t * tc);
411void tcp_send_fin (tcp_connection_t * tc);
Florin Corasc8343412017-05-04 14:25:50 -0700412void tcp_init_mss (tcp_connection_t * tc);
413void tcp_update_snd_mss (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500414
415always_inline u32
416tcp_end_seq (tcp_header_t * th, u32 len)
417{
418 return th->seq_number + tcp_is_syn (th) + tcp_is_fin (th) + len;
419}
420
421/* Modulo arithmetic for TCP sequence numbers */
422#define seq_lt(_s1, _s2) ((i32)((_s1)-(_s2)) < 0)
423#define seq_leq(_s1, _s2) ((i32)((_s1)-(_s2)) <= 0)
424#define seq_gt(_s1, _s2) ((i32)((_s1)-(_s2)) > 0)
425#define seq_geq(_s1, _s2) ((i32)((_s1)-(_s2)) >= 0)
426
427/* Modulo arithmetic for timestamps */
428#define timestamp_lt(_t1, _t2) ((i32)((_t1)-(_t2)) < 0)
429#define timestamp_leq(_t1, _t2) ((i32)((_t1)-(_t2)) <= 0)
430
431always_inline u32
432tcp_flight_size (const tcp_connection_t * tc)
433{
Florin Coras6792ec02017-03-13 03:49:51 -0700434 int flight_size;
435
436 flight_size = (int) ((tc->snd_una_max - tc->snd_una) + tc->rtx_bytes)
437 - (tc->rcv_dupacks * tc->snd_mss) /* - tc->sack_sb.sacked_bytes */ ;
438
439 /* Happens if we don't clear sacked bytes */
440 if (flight_size < 0)
441 return 0;
442
443 return flight_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500444}
445
446/**
447 * Initial cwnd as per RFC5681
448 */
449always_inline u32
450tcp_initial_cwnd (const tcp_connection_t * tc)
451{
452 if (tc->snd_mss > 2190)
453 return 2 * tc->snd_mss;
454 else if (tc->snd_mss > 1095)
455 return 3 * tc->snd_mss;
456 else
457 return 4 * tc->snd_mss;
458}
459
460always_inline u32
461tcp_loss_wnd (const tcp_connection_t * tc)
462{
463 return tc->snd_mss;
464}
465
466always_inline u32
467tcp_available_wnd (const tcp_connection_t * tc)
468{
469 return clib_min (tc->cwnd, tc->snd_wnd);
470}
471
472always_inline u32
473tcp_available_snd_space (const tcp_connection_t * tc)
474{
475 u32 available_wnd = tcp_available_wnd (tc);
476 u32 flight_size = tcp_flight_size (tc);
477
478 if (available_wnd <= flight_size)
479 return 0;
480
481 return available_wnd - flight_size;
482}
483
Florin Corasbb292f42017-05-19 09:49:19 -0700484u32 tcp_rcv_wnd_available (tcp_connection_t * tc);
485u32 tcp_snd_space (tcp_connection_t * tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700486void tcp_update_rcv_wnd (tcp_connection_t * tc);
487
Dave Barach68b0fb02017-02-28 15:15:56 -0500488void tcp_retransmit_first_unacked (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500489void tcp_fast_retransmit (tcp_connection_t * tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700490void tcp_cc_congestion (tcp_connection_t * tc);
491void tcp_cc_recover (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500492
Florin Coras45d34962017-04-25 00:05:27 -0700493/* Made public for unit testing only */
494void tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end);
495
Dave Barach68b0fb02017-02-28 15:15:56 -0500496always_inline u32
497tcp_time_now (void)
498{
499 return clib_cpu_time_now () * tcp_main.tstamp_ticks_per_clock;
500}
501
Florin Coras6cf30ad2017-04-04 23:08:23 -0700502always_inline void
503tcp_update_time (f64 now, u32 thread_index)
504{
505 tw_timer_expire_timers_16t_2w_512sl (&tcp_main.timer_wheels[thread_index],
506 now);
507}
508
Dave Barach68b0fb02017-02-28 15:15:56 -0500509u32 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b);
510
511u32
512tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b,
Florin Coras6792ec02017-03-13 03:49:51 -0700513 u32 offset, u32 max_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500514
515void tcp_connection_timers_init (tcp_connection_t * tc);
516void tcp_connection_timers_reset (tcp_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500517void tcp_connection_init_vars (tcp_connection_t * tc);
518
519always_inline void
520tcp_connection_force_ack (tcp_connection_t * tc, vlib_buffer_t * b)
521{
522 /* Reset flags, make sure ack is sent */
523 tc->flags = TCP_CONN_SNDACK;
524 vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK;
525}
526
527always_inline void
528tcp_timer_set (tcp_connection_t * tc, u8 timer_id, u32 interval)
529{
530 tc->timers[timer_id]
531 = tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
532 tc->c_c_index, timer_id, interval);
533}
534
535always_inline void
Dave Barach68b0fb02017-02-28 15:15:56 -0500536tcp_timer_reset (tcp_connection_t * tc, u8 timer_id)
537{
538 if (tc->timers[timer_id] == TCP_TIMER_HANDLE_INVALID)
539 return;
540
541 tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
542 tc->timers[timer_id]);
543 tc->timers[timer_id] = TCP_TIMER_HANDLE_INVALID;
544}
545
546always_inline void
547tcp_timer_update (tcp_connection_t * tc, u8 timer_id, u32 interval)
548{
549 if (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID)
550 tw_timer_stop_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
551 tc->timers[timer_id]);
552 tc->timers[timer_id] =
553 tw_timer_start_16t_2w_512sl (&tcp_main.timer_wheels[tc->c_thread_index],
554 tc->c_c_index, timer_id, interval);
555}
556
Florin Coras6792ec02017-03-13 03:49:51 -0700557/* XXX Switch retransmit to faster TW */
558always_inline void
559tcp_retransmit_timer_set (tcp_connection_t * tc)
560{
561 tcp_timer_set (tc, TCP_TIMER_RETRANSMIT,
562 clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
563}
564
565always_inline void
566tcp_retransmit_timer_update (tcp_connection_t * tc)
567{
568 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT,
569 clib_max (tc->rto * TCP_TO_TIMER_TICK, 1));
570}
571
572always_inline void
573tcp_retransmit_timer_reset (tcp_connection_t * tc)
574{
575 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
576}
577
Florin Coras3e350af2017-03-30 02:54:28 -0700578always_inline void
579tcp_persist_timer_set (tcp_connection_t * tc)
580{
581 /* Reuse RTO. It's backed off in handler */
582 tcp_timer_set (tc, TCP_TIMER_PERSIST,
Florin Coras5921f982017-04-03 18:00:00 -0700583 clib_max (tc->rto * TCP_TO_TIMER_TICK,
584 TCP_TIMER_PERSIST_MIN));
Florin Coras3e350af2017-03-30 02:54:28 -0700585}
586
587always_inline void
588tcp_persist_timer_update (tcp_connection_t * tc)
589{
590 tcp_timer_update (tc, TCP_TIMER_PERSIST,
Florin Coras5921f982017-04-03 18:00:00 -0700591 clib_max (tc->rto * TCP_TO_TIMER_TICK,
592 TCP_TIMER_PERSIST_MIN));
Florin Coras3e350af2017-03-30 02:54:28 -0700593}
594
595always_inline void
596tcp_persist_timer_reset (tcp_connection_t * tc)
597{
598 tcp_timer_reset (tc, TCP_TIMER_PERSIST);
599}
600
Dave Barach68b0fb02017-02-28 15:15:56 -0500601always_inline u8
602tcp_timer_is_active (tcp_connection_t * tc, tcp_timers_e timer)
603{
604 return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID;
605}
606
607void
608scoreboard_remove_hole (sack_scoreboard_t * sb,
609 sack_scoreboard_hole_t * hole);
610
611always_inline sack_scoreboard_hole_t *
Florin Coras6792ec02017-03-13 03:49:51 -0700612scoreboard_get_hole (sack_scoreboard_t * sb, u32 index)
613{
614 if (index != TCP_INVALID_SACK_HOLE_INDEX)
615 return pool_elt_at_index (sb->holes, index);
616 return 0;
617}
618
619always_inline sack_scoreboard_hole_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500620scoreboard_next_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
621{
622 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
623 return pool_elt_at_index (sb->holes, hole->next);
624 return 0;
625}
626
627always_inline sack_scoreboard_hole_t *
628scoreboard_first_hole (sack_scoreboard_t * sb)
629{
630 if (sb->head != TCP_INVALID_SACK_HOLE_INDEX)
631 return pool_elt_at_index (sb->holes, sb->head);
632 return 0;
633}
634
Florin Coras6792ec02017-03-13 03:49:51 -0700635always_inline sack_scoreboard_hole_t *
636scoreboard_last_hole (sack_scoreboard_t * sb)
637{
638 if (sb->tail != TCP_INVALID_SACK_HOLE_INDEX)
639 return pool_elt_at_index (sb->holes, sb->tail);
640 return 0;
641}
642
Dave Barach68b0fb02017-02-28 15:15:56 -0500643always_inline void
644scoreboard_clear (sack_scoreboard_t * sb)
645{
646 sack_scoreboard_hole_t *hole = scoreboard_first_hole (sb);
647 while ((hole = scoreboard_first_hole (sb)))
648 {
649 scoreboard_remove_hole (sb, hole);
650 }
Florin Coras6792ec02017-03-13 03:49:51 -0700651 sb->sacked_bytes = 0;
652 sb->last_sacked_bytes = 0;
653 sb->snd_una_adv = 0;
654 sb->max_byte_sacked = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500655}
656
657always_inline u32
658scoreboard_hole_bytes (sack_scoreboard_hole_t * hole)
659{
660 return hole->end - hole->start;
661}
662
Florin Coras6792ec02017-03-13 03:49:51 -0700663always_inline u32
664scoreboard_hole_index (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
665{
666 return hole - sb->holes;
667}
668
669always_inline void
670scoreboard_init (sack_scoreboard_t * sb)
671{
672 sb->head = TCP_INVALID_SACK_HOLE_INDEX;
673 sb->tail = TCP_INVALID_SACK_HOLE_INDEX;
674}
675
676void tcp_rcv_sacks (tcp_connection_t * tc, u32 ack);
677
Dave Barach68b0fb02017-02-28 15:15:56 -0500678always_inline void
679tcp_cc_algo_register (tcp_cc_algorithm_type_e type,
680 const tcp_cc_algorithm_t * vft)
681{
682 tcp_main_t *tm = vnet_get_tcp_main ();
683 vec_validate (tm->cc_algos, type);
684
685 tm->cc_algos[type] = *vft;
686}
687
688always_inline tcp_cc_algorithm_t *
689tcp_cc_algo_get (tcp_cc_algorithm_type_e type)
690{
691 tcp_main_t *tm = vnet_get_tcp_main ();
692 return &tm->cc_algos[type];
693}
694
695void tcp_cc_init (tcp_connection_t * tc);
696
697/**
698 * Push TCP header to buffer
699 *
700 * @param vm - vlib_main
701 * @param b - buffer to write the header to
702 * @param sp_net - source port net order
703 * @param dp_net - destination port net order
704 * @param seq - sequence number net order
705 * @param ack - ack number net order
706 * @param tcp_hdr_opts_len - header and options length in bytes
707 * @param flags - header flags
708 * @param wnd - window size
709 *
710 * @return - pointer to start of TCP header
711 */
712always_inline void *
713vlib_buffer_push_tcp_net_order (vlib_buffer_t * b, u16 sp, u16 dp, u32 seq,
714 u32 ack, u8 tcp_hdr_opts_len, u8 flags,
715 u16 wnd)
716{
717 tcp_header_t *th;
718
719 th = vlib_buffer_push_uninit (b, tcp_hdr_opts_len);
720
721 th->src_port = sp;
722 th->dst_port = dp;
723 th->seq_number = seq;
724 th->ack_number = ack;
725 th->data_offset_and_reserved = (tcp_hdr_opts_len >> 2) << 4;
726 th->flags = flags;
727 th->window = wnd;
728 th->checksum = 0;
729 th->urgent_pointer = 0;
730 return th;
731}
732
733/**
734 * Push TCP header to buffer
735 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500736 * @param b - buffer to write the header to
737 * @param sp_net - source port net order
738 * @param dp_net - destination port net order
739 * @param seq - sequence number host order
740 * @param ack - ack number host order
741 * @param tcp_hdr_opts_len - header and options length in bytes
742 * @param flags - header flags
743 * @param wnd - window size
744 *
745 * @return - pointer to start of TCP header
746 */
747always_inline void *
748vlib_buffer_push_tcp (vlib_buffer_t * b, u16 sp_net, u16 dp_net, u32 seq,
749 u32 ack, u8 tcp_hdr_opts_len, u8 flags, u16 wnd)
750{
751 return vlib_buffer_push_tcp_net_order (b, sp_net, dp_net,
752 clib_host_to_net_u32 (seq),
753 clib_host_to_net_u32 (ack),
754 tcp_hdr_opts_len, flags,
755 clib_host_to_net_u16 (wnd));
756}
757
758#endif /* _vnet_tcp_h_ */
759
760/*
761 * fd.io coding-style-patch-verification: ON
762 *
763 * Local Variables:
764 * eval: (c-set-style "gnu")
765 * End:
766 */