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