2 * Copyright (c) 2020 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef SRC_VNET_TCP_TCP_TYPES_H_
17 #define SRC_VNET_TCP_TCP_TYPES_H_
19 #include <vppinfra/clib.h>
20 #include <vppinfra/rbtree.h>
21 #include <vnet/tcp/tcp_packet.h>
22 #include <vnet/session/transport.h>
23 #include <vppinfra/tw_timer_16t_2w_512sl.h>
25 #define TCP_TICK 0.000001 /**< TCP tick period (s) */
26 #define THZ (u32) (1/TCP_TICK) /**< TCP tick frequency */
28 #define TCP_TSTP_TICK 0.001 /**< Timestamp tick (s) */
29 #define TCP_TSTP_HZ (u32) (1/TCP_TSTP_TICK) /**< Timestamp freq */
30 #define TCP_PAWS_IDLE (24 * 86400 * TCP_TSTP_HZ)/**< 24 days */
31 #define TCP_TSTP_TO_HZ (u32) (TCP_TSTP_TICK * THZ)
33 #define TCP_FIB_RECHECK_PERIOD 1 * THZ /**< Recheck every 1s */
34 #define TCP_MAX_OPTION_SPACE 40
35 #define TCP_CC_DATA_SZ 24
36 #define TCP_MAX_GSO_SZ 65536
37 #define TCP_RXT_MAX_BURST 10
39 #define TCP_DUPACK_THRESHOLD 3
40 #define TCP_IW_N_SEGMENTS 10
41 #define TCP_ALWAYS_ACK 1 /**< On/off delayed acks */
42 #define TCP_USE_SACKS 1 /**< Disable only for testing */
44 /** TCP FSM state definitions as per RFC793. */
45 #define foreach_tcp_fsm_state \
48 _(SYN_SENT, "SYN_SENT") \
49 _(SYN_RCVD, "SYN_RCVD") \
50 _(ESTABLISHED, "ESTABLISHED") \
51 _(CLOSE_WAIT, "CLOSE_WAIT") \
52 _(FIN_WAIT_1, "FIN_WAIT_1") \
53 _(LAST_ACK, "LAST_ACK") \
54 _(CLOSING, "CLOSING") \
55 _(FIN_WAIT_2, "FIN_WAIT_2") \
56 _(TIME_WAIT, "TIME_WAIT")
58 typedef enum _tcp_state
60 #define _(sym, str) TCP_STATE_##sym,
67 #define foreach_tcp_timer \
68 _(RETRANSMIT, "RETRANSMIT") \
69 _(PERSIST, "PERSIST") \
70 _(WAITCLOSE, "WAIT CLOSE") \
71 _(RETRANSMIT_SYN, "RETRANSMIT SYN") \
73 typedef enum _tcp_timers
75 #define _(sym, str) TCP_TIMER_##sym,
81 #define TCP_TIMER_HANDLE_INVALID ((u32) ~0)
83 #define TCP_TIMER_TICK 0.1 /**< Timer tick in seconds */
84 #define TCP_TO_TIMER_TICK TCP_TICK*10 /**< Factor for converting
85 ticks to timer ticks */
87 #define TCP_RTO_MAX 60 * THZ /* Min max RTO (60s) as per RFC6298 */
88 #define TCP_RTO_MIN 0.2 * THZ /* Min RTO (200ms) - lower than standard */
89 #define TCP_RTT_MAX 30 * THZ /* 30s (probably too much) */
90 #define TCP_RTO_SYN_RETRIES 3 /* SYN retries without doubling RTO */
91 #define TCP_RTO_INIT 1 * THZ /* Initial retransmit timer */
92 #define TCP_RTO_BOFF_MAX 8 /* Max number of retries before reset */
93 #define TCP_ESTABLISH_TIME (60 * THZ) /* Connection establish timeout */
95 /** Connection configuration flags */
96 #define foreach_tcp_cfg_flag \
97 _(RATE_SAMPLE, "Rate sampling") \
98 _(NO_CSUM_OFFLOAD, "No csum offload") \
99 _(NO_TSO, "TSO off") \
101 _(NO_ENDPOINT,"No endpoint") \
103 typedef enum tcp_cfg_flag_bits_
105 #define _(sym, str) TCP_CFG_F_##sym##_BIT,
109 } tcp_cfg_flag_bits_e;
111 typedef enum tcp_cfg_flag_
113 #define _(sym, str) TCP_CFG_F_##sym = 1 << TCP_CFG_F_##sym##_BIT,
119 /** TCP connection flags */
120 #define foreach_tcp_connection_flag \
121 _(SNDACK, "Send ACK") \
122 _(FINSNT, "FIN sent") \
123 _(RECOVERY, "Recovery") \
124 _(FAST_RECOVERY, "Fast Recovery") \
125 _(DCNT_PENDING, "Disconnect pending") \
126 _(HALF_OPEN_DONE, "Half-open completed") \
127 _(FINPNDG, "FIN pending") \
128 _(RXT_PENDING, "Retransmit pending") \
129 _(FRXT_FIRST, "Retransmit first") \
130 _(DEQ_PENDING, "Dequeue pending ") \
131 _(PSH_PENDING, "PSH pending") \
132 _(FINRCVD, "FIN received") \
133 _(ZERO_RWND_SENT, "Zero RWND sent") \
135 typedef enum tcp_connection_flag_bits_
137 #define _(sym, str) TCP_CONN_##sym##_BIT,
138 foreach_tcp_connection_flag
141 } tcp_connection_flag_bits_e;
143 typedef enum tcp_connection_flag_
145 #define _(sym, str) TCP_CONN_##sym = 1 << TCP_CONN_##sym##_BIT,
146 foreach_tcp_connection_flag
149 } tcp_connection_flags_e;
151 #define TCP_SCOREBOARD_TRACE (0)
152 #define TCP_MAX_SACK_BLOCKS 255 /**< Max number of SACK blocks stored */
153 #define TCP_INVALID_SACK_HOLE_INDEX ((u32)~0)
155 typedef struct _scoreboard_trace_elt
162 } scoreboard_trace_elt_t;
164 typedef struct _sack_scoreboard_hole
166 u32 next; /**< Index for next entry in linked list */
167 u32 prev; /**< Index for previous entry in linked list */
168 u32 start; /**< Start sequence number */
169 u32 end; /**< End sequence number */
170 u8 is_lost; /**< Mark hole as lost */
171 } sack_scoreboard_hole_t;
173 typedef struct _sack_scoreboard
175 sack_scoreboard_hole_t *holes; /**< Pool of holes */
176 u32 head; /**< Index of first entry */
177 u32 tail; /**< Index of last entry */
178 u32 sacked_bytes; /**< Number of bytes sacked in sb */
179 u32 last_sacked_bytes; /**< Number of bytes last sacked */
180 u32 last_bytes_delivered; /**< Sack bytes delivered to app */
181 u32 rxt_sacked; /**< Rxt bytes last delivered */
182 u32 high_sacked; /**< Highest byte sacked (fack) */
183 u32 high_rxt; /**< Highest retransmitted sequence */
184 u32 rescue_rxt; /**< Rescue sequence number */
185 u32 lost_bytes; /**< Bytes lost as per RFC6675 */
186 u32 last_lost_bytes; /**< Number of bytes last lost */
187 u32 cur_rxt_hole; /**< Retransmitting from this hole */
190 #if TCP_SCOREBOARD_TRACE
191 scoreboard_trace_elt_t *trace;
196 #define TCP_BTS_INVALID_INDEX ((u32)~0)
198 typedef enum tcp_bts_flags_
201 TCP_BTS_IS_APP_LIMITED = 1 << 1,
202 TCP_BTS_IS_SACKED = 1 << 2,
203 TCP_BTS_IS_RXT_LOST = 1 << 3,
204 } __clib_packed tcp_bts_flags_t;
206 typedef struct tcp_bt_sample_
208 u32 next; /**< Next sample index in list */
209 u32 prev; /**< Previous sample index in list */
210 u32 min_seq; /**< Min seq number in sample */
211 u32 max_seq; /**< Max seq number. Set for rxt samples */
212 u64 delivered; /**< Total delivered bytes for sample */
213 f64 delivered_time; /**< Delivered time when sample taken */
214 f64 tx_time; /**< Transmit time for the burst */
215 f64 first_tx_time; /**< Connection first tx time at tx */
216 u64 tx_in_flight; /**< In flight at tx time */
217 u64 tx_lost; /**< Lost at tx time */
218 tcp_bts_flags_t flags; /**< Sample flag */
221 typedef struct tcp_rate_sample_
223 u64 prior_delivered; /**< Delivered of sample used for rate, i.e.,
224 total bytes delivered at prior_time */
225 f64 prior_time; /**< Delivered time of sample used for rate */
226 f64 interval_time; /**< Time to ack the bytes delivered */
227 f64 rtt_time; /**< RTT for sample */
228 u64 tx_in_flight; /**< In flight at (re)transmit time */
229 u64 tx_lost; /**< Lost over interval */
230 u32 delivered; /**< Bytes delivered in interval_time */
231 u32 acked_and_sacked; /**< Bytes acked + sacked now */
232 u32 last_lost; /**< Bytes lost now */
233 u32 lost; /**< Number of bytes lost over interval */
234 tcp_bts_flags_t flags; /**< Rate sample flags from bt sample */
237 typedef struct tcp_byte_tracker_
239 tcp_bt_sample_t *samples; /**< Pool of samples */
240 rb_tree_t sample_lookup; /**< Rbtree for sample lookup by min_seq */
241 u32 head; /**< Head of samples linked list */
242 u32 tail; /**< Tail of samples linked list */
243 u32 last_ooo; /**< Cached last ooo sample */
244 } tcp_byte_tracker_t;
246 typedef enum _tcp_cc_algorithm_type
250 TCP_CC_LAST = TCP_CC_CUBIC
251 } tcp_cc_algorithm_type_e;
253 typedef struct _tcp_cc_algorithm tcp_cc_algorithm_t;
255 typedef enum _tcp_cc_ack_t
262 typedef enum tcp_cc_event_
268 * As per RFC4898 tcpEStatsStackSoftErrors
270 typedef struct tcp_errors_
272 u32 below_data_wnd; /**< All data in seg is below snd_una */
273 u32 above_data_wnd; /**< Some data in segment is above snd_wnd */
274 u32 below_ack_wnd; /**< Acks for data below snd_una */
275 u32 above_ack_wnd; /**< Acks for data not sent */
278 typedef struct _tcp_connection
280 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
281 transport_connection_t connection; /**< Common transport data. First! */
283 u8 state; /**< TCP state as per tcp_state_t */
284 u8 cfg_flags; /**< Connection configuration flags */
285 u16 flags; /**< Connection flags (see tcp_conn_flags_e) */
286 u32 timers[TCP_N_TIMERS]; /**< Timer handles into timer wheel */
287 u32 pending_timers; /**< Expired timers not yet handled */
289 u64 segs_in; /** RFC4022/4898 tcpHCInSegs/tcpEStatsPerfSegsIn */
290 u64 bytes_in; /** RFC4898 tcpEStatsPerfHCDataOctetsIn */
291 u64 segs_out; /** RFC4898 tcpEStatsPerfSegsOut */
292 u64 bytes_out; /** RFC4898 tcpEStatsPerfHCDataOctetsOut */
294 /** Send sequence variables RFC793 */
295 u32 snd_una; /**< oldest unacknowledged sequence number */
296 u32 snd_una_max; /**< newest unacknowledged sequence number + 1*/
297 u32 snd_wnd; /**< send window */
298 u32 snd_wl1; /**< seq number used for last snd.wnd update */
299 u32 snd_wl2; /**< ack number used for last snd.wnd update */
300 u32 snd_nxt; /**< next seq number to be sent */
301 u16 snd_mss; /**< Effective send max seg (data) size */
303 u64 data_segs_in; /** RFC4898 tcpEStatsPerfDataSegsIn */
304 u64 data_segs_out; /** RFC4898 tcpEStatsPerfDataSegsOut */
306 /** Receive sequence variables RFC793 */
307 u32 rcv_nxt; /**< next sequence number expected */
308 u32 rcv_wnd; /**< receive window we expect */
310 u32 rcv_las; /**< rcv_nxt at last ack sent/rcv_wnd update */
311 u32 iss; /**< initial sent sequence */
312 u32 irs; /**< initial remote sequence */
315 u8 snd_opts_len; /**< Tx options len */
316 u8 rcv_wscale; /**< Window scale to advertise to peer */
317 u8 snd_wscale; /**< Window scale to use when sending */
318 u32 tsval_recent; /**< Last timestamp received */
319 u32 tsval_recent_age; /**< When last updated tstamp_recent*/
320 tcp_options_t snd_opts; /**< Tx options for connection */
321 tcp_options_t rcv_opts; /**< Rx options for connection */
323 sack_block_t *snd_sacks; /**< Vector of SACKs to send. XXX Fixed size? */
324 u8 snd_sack_pos; /**< Position in vec of first block to send */
325 sack_block_t *snd_sacks_fl; /**< Vector for building new list */
326 sack_scoreboard_t sack_sb; /**< SACK "scoreboard" that tracks holes */
328 u16 rcv_dupacks; /**< Number of recent DUPACKs received */
329 u32 dupacks_in; /**< RFC4898 tcpEStatsStackDupAcksIn*/
330 u8 pending_dupacks; /**< Number of DUPACKs to be sent */
331 u32 dupacks_out; /**< RFC4898 tcpEStatsPathDupAcksOut */
333 /* Congestion control */
334 u32 cwnd; /**< Congestion window */
335 u32 cwnd_acc_bytes; /**< Bytes accumulated for cwnd increment */
336 u32 ssthresh; /**< Slow-start threshold */
337 u32 prev_ssthresh; /**< ssthresh before congestion */
338 u32 prev_cwnd; /**< ssthresh before congestion */
339 u32 bytes_acked; /**< Bytes acknowledged by current segment */
340 u32 burst_acked; /**< Bytes acknowledged in current burst */
341 u32 snd_rxt_bytes; /**< Retransmitted bytes during current cc event */
342 u32 snd_rxt_ts; /**< Timestamp when first packet is retransmitted */
343 u32 prr_delivered; /**< RFC6937 bytes delivered during current event */
344 u32 prr_start; /**< snd_una when prr starts */
345 u32 rxt_delivered; /**< Rxt bytes delivered during current cc event */
346 u32 rxt_head; /**< snd_una last time we re rxted the head */
347 u32 tsecr_last_ack; /**< Timestamp echoed to us in last healthy ACK */
348 u32 snd_congestion; /**< snd_una_max when congestion is detected */
349 u32 tx_fifo_size; /**< Tx fifo size. Used to constrain cwnd */
350 tcp_cc_algorithm_t *cc_algo; /**< Congestion control algorithm */
351 u8 cc_data[TCP_CC_DATA_SZ]; /**< Congestion control algo private data */
353 u32 fr_occurences; /**< fast-retransmit occurrences RFC4898
354 tcpEStatsStackFastRetran */
355 u32 tr_occurences; /**< timer-retransmit occurrences */
356 u64 bytes_retrans; /**< RFC4898 tcpEStatsPerfOctetsRetrans */
357 u64 segs_retrans; /**< RFC4898 tcpEStatsPerfSegsRetrans*/
360 u32 rto; /**< Retransmission timeout */
361 u32 rto_boff; /**< Index for RTO backoff */
362 u32 srtt; /**< Smoothed RTT measured in @ref TCP_TICK */
363 u32 rttvar; /**< Smoothed mean RTT difference. Approximates variance */
364 u32 rtt_seq; /**< Sequence number for tracked ACK */
365 f64 rtt_ts; /**< Timestamp for tracked ACK */
366 f64 mrtt_us; /**< High precision mrtt from tracked acks */
368 u32 psh_seq; /**< Add psh header for seg that includes this */
369 u32 next_node_index; /**< Can be used to control next node in output */
370 u32 next_node_opaque; /**< Opaque to pass to next node */
371 u32 limited_transmit; /**< snd_nxt when limited transmit starts */
372 u32 sw_if_index; /**< Interface for the connection */
374 /* Delivery rate estimation */
375 u64 delivered; /**< Total bytes delivered to peer */
376 u64 app_limited; /**< Delivered when app-limited detected */
377 f64 delivered_time; /**< Time last bytes were acked */
378 f64 first_tx_time; /**< Send time for recently delivered/sent */
379 u64 lost; /**< Total bytes lost */
380 tcp_byte_tracker_t *bt; /**< Tx byte tracker */
382 tcp_errors_t errors; /**< Soft connection errors */
384 f64 start_ts; /**< Timestamp when connection initialized */
385 u32 last_fib_check; /**< Last time we checked fib route for peer */
386 u16 mss; /**< Our max seg size that includes options */
387 u32 timestamp_delta; /**< Offset for timestamp */
388 u32 ipv6_flow_label; /**< flow label for ipv6 header */
390 #define rst_state snd_wl1
394 struct _tcp_cc_algorithm
397 uword (*unformat_cfg) (unformat_input_t * input);
398 void (*init) (tcp_connection_t * tc);
399 void (*cleanup) (tcp_connection_t * tc);
400 void (*rcv_ack) (tcp_connection_t * tc, tcp_rate_sample_t *rs);
401 void (*rcv_cong_ack) (tcp_connection_t * tc, tcp_cc_ack_t ack,
402 tcp_rate_sample_t *rs);
403 void (*congestion) (tcp_connection_t * tc);
404 void (*loss) (tcp_connection_t * tc);
405 void (*recovered) (tcp_connection_t * tc);
406 void (*undo_recovery) (tcp_connection_t * tc);
407 void (*event) (tcp_connection_t *tc, tcp_cc_event_t evt);
408 u64 (*get_pacing_rate) (tcp_connection_t *tc);
412 #define tcp_fastrecovery_on(tc) (tc)->flags |= TCP_CONN_FAST_RECOVERY
413 #define tcp_fastrecovery_off(tc) (tc)->flags &= ~TCP_CONN_FAST_RECOVERY
414 #define tcp_recovery_on(tc) (tc)->flags |= TCP_CONN_RECOVERY
415 #define tcp_recovery_off(tc) (tc)->flags &= ~TCP_CONN_RECOVERY
416 #define tcp_in_fastrecovery(tc) ((tc)->flags & TCP_CONN_FAST_RECOVERY)
417 #define tcp_in_recovery(tc) ((tc)->flags & (TCP_CONN_RECOVERY))
418 #define tcp_in_slowstart(tc) (tc->cwnd < tc->ssthresh)
419 #define tcp_disconnect_pending(tc) ((tc)->flags & TCP_CONN_DCNT_PENDING)
420 #define tcp_disconnect_pending_on(tc) ((tc)->flags |= TCP_CONN_DCNT_PENDING)
421 #define tcp_disconnect_pending_off(tc) ((tc)->flags &= ~TCP_CONN_DCNT_PENDING)
422 #define tcp_fastrecovery_first(tc) ((tc)->flags & TCP_CONN_FRXT_FIRST)
423 #define tcp_fastrecovery_first_on(tc) ((tc)->flags |= TCP_CONN_FRXT_FIRST)
424 #define tcp_fastrecovery_first_off(tc) ((tc)->flags &= ~TCP_CONN_FRXT_FIRST)
426 #define tcp_in_cong_recovery(tc) ((tc)->flags & \
427 (TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY))
430 tcp_cong_recovery_off (tcp_connection_t * tc)
432 tc->flags &= ~(TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY);
433 tcp_fastrecovery_first_off (tc);
436 #define tcp_csum_offload(tc) (!((tc)->cfg_flags & TCP_CFG_F_NO_CSUM_OFFLOAD))
438 #define tcp_zero_rwnd_sent(tc) ((tc)->flags & TCP_CONN_ZERO_RWND_SENT)
439 #define tcp_zero_rwnd_sent_on(tc) (tc)->flags |= TCP_CONN_ZERO_RWND_SENT
440 #define tcp_zero_rwnd_sent_off(tc) (tc)->flags &= ~TCP_CONN_ZERO_RWND_SENT
442 always_inline tcp_connection_t *
443 tcp_get_connection_from_transport (transport_connection_t * tconn)
445 return (tcp_connection_t *) tconn;
448 typedef tw_timer_wheel_16t_2w_512sl_t tcp_timer_wheel_t;
450 #endif /* SRC_VNET_TCP_TCP_TYPES_H_ */
453 * fd.io coding-style-patch-verification: ON
456 * eval: (c-set-style "gnu")