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