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