Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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 Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 25 | #include <vnet/tcp/tcp_debug.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 27 | #define TCP_TICK 0.001 /**< TCP tick period (s) */ |
| 28 | #define THZ (u32) (1/TCP_TICK) /**< TCP tick frequency */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 29 | #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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 33 | #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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 37 | |
| 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 | |
| 52 | typedef 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 | |
| 60 | format_function_t format_tcp_state; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 61 | format_function_t format_tcp_flags; |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 62 | format_function_t format_tcp_sacks; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 63 | |
| 64 | /** TCP timers */ |
| 65 | #define foreach_tcp_timer \ |
| 66 | _(RETRANSMIT, "RETRANSMIT") \ |
| 67 | _(DELACK, "DELAYED ACK") \ |
| 68 | _(PERSIST, "PERSIST") \ |
| 69 | _(KEEP, "KEEP") \ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 70 | _(WAITCLOSE, "WAIT CLOSE") \ |
| 71 | _(RETRANSMIT_SYN, "RETRANSMIT SYN") \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 72 | _(ESTABLISH, "ESTABLISH") |
| 73 | |
| 74 | typedef 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 | |
| 82 | typedef void (timer_expiration_handler) (u32 index); |
| 83 | |
| 84 | extern timer_expiration_handler tcp_timer_delack_handler; |
| 85 | extern timer_expiration_handler tcp_timer_retransmit_handler; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 86 | extern timer_expiration_handler tcp_timer_persist_handler; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 87 | extern 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 Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 97 | #define TCP_CLOSEWAIT_TIME 1 /* 0.1s */ |
| 98 | #define TCP_CLEANUP_TIME 5 /* 0.5s Time to wait before cleanup */ |
Florin Coras | 5921f98 | 2017-04-03 18:00:00 -0700 | [diff] [blame] | 99 | #define TCP_TIMER_PERSIST_MIN 2 /* 0.2s */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 100 | |
| 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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 106 | /** TCP connection flags */ |
| 107 | #define foreach_tcp_connection_flag \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 108 | _(SNDACK, "Send ACK") \ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 109 | _(FINSNT, "FIN sent") \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 110 | _(SENT_RCV_WND0, "Sent 0 receive window") \ |
| 111 | _(RECOVERY, "Recovery on") \ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 112 | _(FAST_RECOVERY, "Fast Recovery on") \ |
| 113 | _(FR_1_SMSS, "Sent 1 SMSS") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 114 | |
| 115 | typedef 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 | |
| 123 | typedef 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 | |
| 136 | enum |
| 137 | { |
| 138 | #define _(f) TCP_BUF_BIT_##f, |
| 139 | foreach_tcp_buf_flag |
| 140 | #undef _ |
| 141 | TCP_N_BUF_BITS, |
| 142 | }; |
| 143 | |
| 144 | enum |
| 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 | |
| 154 | typedef 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 | |
| 162 | typedef struct _sack_scoreboard |
| 163 | { |
| 164 | sack_scoreboard_hole_t *holes; /**< Pool of holes */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 165 | u32 head; /**< Index of first entry */ |
| 166 | u32 tail; /**< Index of last entry */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 167 | u32 sacked_bytes; /**< Number of bytes sacked in sb */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 168 | 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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 171 | } sack_scoreboard_t; |
| 172 | |
| 173 | typedef enum _tcp_cc_algorithm_type |
| 174 | { |
| 175 | TCP_CC_NEWRENO, |
| 176 | } tcp_cc_algorithm_type_e; |
| 177 | |
| 178 | typedef struct _tcp_cc_algorithm tcp_cc_algorithm_t; |
| 179 | |
| 180 | typedef enum _tcp_cc_ack_t |
| 181 | { |
| 182 | TCP_CC_ACK, |
| 183 | TCP_CC_DUPACK, |
| 184 | TCP_CC_PARTIALACK |
| 185 | } tcp_cc_ack_t; |
| 186 | |
| 187 | typedef 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 Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 214 | 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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 217 | 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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 225 | u16 rcv_dupacks; /**< Number of DUPACKs received */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 226 | 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 Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 234 | u32 tsecr_last_ack; /**< Timestamp echoed to us in last healthy ACK */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 235 | u32 snd_congestion; /**< snd_una_max when congestion is detected */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 236 | 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 Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 246 | u16 snd_mss; /**< Effective send max seg (data) size */ |
| 247 | u16 mss; /**< Our max seg size that includes options */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 248 | } tcp_connection_t; |
| 249 | |
| 250 | struct _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 Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 261 | #define tcp_recovery_on(tc) (tc)->flags |= TCP_CONN_RECOVERY |
| 262 | #define tcp_recovery_off(tc) (tc)->flags &= ~TCP_CONN_RECOVERY |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 263 | #define tcp_in_fastrecovery(tc) ((tc)->flags & TCP_CONN_FAST_RECOVERY) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 264 | #define tcp_in_recovery(tc) ((tc)->flags & (TCP_CONN_RECOVERY)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 265 | #define tcp_in_slowstart(tc) (tc->cwnd < tc->ssthresh) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 266 | #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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 269 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 270 | #define tcp_in_cong_recovery(tc) ((tc)->flags & \ |
| 271 | (TCP_CONN_FAST_RECOVERY | TCP_CONN_RECOVERY)) |
| 272 | |
| 273 | always_inline void |
| 274 | tcp_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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 280 | typedef enum |
| 281 | { |
| 282 | TCP_IP4, |
| 283 | TCP_IP6, |
| 284 | TCP_N_AF, |
| 285 | } tcp_af_t; |
| 286 | |
| 287 | typedef 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 | |
| 295 | typedef struct _tcp_lookup_dispatch |
| 296 | { |
| 297 | u8 next, error; |
| 298 | } tcp_lookup_dispatch_t; |
| 299 | |
| 300 | typedef 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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 320 | // /* Convenience per worker-thread vector of connections to DELACK */ |
| 321 | // u32 **delack_connections; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 322 | |
| 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 Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 335 | /* Flag that indicates if stack is on or off */ |
| 336 | u8 is_enabled; |
| 337 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 338 | /* 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 | |
| 345 | extern tcp_main_t tcp_main; |
| 346 | extern vlib_node_registration_t tcp4_input_node; |
| 347 | extern vlib_node_registration_t tcp6_input_node; |
| 348 | extern vlib_node_registration_t tcp4_output_node; |
| 349 | extern vlib_node_registration_t tcp6_output_node; |
| 350 | |
| 351 | always_inline tcp_main_t * |
| 352 | vnet_get_tcp_main () |
| 353 | { |
| 354 | return &tcp_main; |
| 355 | } |
| 356 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 357 | always_inline tcp_header_t * |
| 358 | tcp_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 Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 365 | clib_error_t *vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en); |
| 366 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 367 | always_inline tcp_connection_t * |
| 368 | tcp_connection_get (u32 conn_index, u32 thread_index) |
| 369 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 370 | if (pool_is_free_index (tcp_main.connections[thread_index], conn_index)) |
| 371 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 372 | return pool_elt_at_index (tcp_main.connections[thread_index], conn_index); |
| 373 | } |
| 374 | |
| 375 | always_inline tcp_connection_t * |
| 376 | tcp_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 | |
| 385 | void tcp_connection_close (tcp_connection_t * tc); |
| 386 | void tcp_connection_cleanup (tcp_connection_t * tc); |
| 387 | void tcp_connection_del (tcp_connection_t * tc); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 388 | void tcp_connection_reset (tcp_connection_t * tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 389 | |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame^] | 390 | u8 *format_tcp_connection_id (u8 * s, va_list * args); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 391 | u8 *format_tcp_connection (u8 * s, va_list * args); |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 392 | u8 *format_tcp_scoreboard (u8 * s, va_list * args); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 393 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 394 | always_inline tcp_connection_t * |
| 395 | tcp_listener_get (u32 tli) |
| 396 | { |
| 397 | return pool_elt_at_index (tcp_main.listener_pool, tli); |
| 398 | } |
| 399 | |
| 400 | always_inline tcp_connection_t * |
| 401 | tcp_half_open_connection_get (u32 conn_index) |
| 402 | { |
| 403 | return pool_elt_at_index (tcp_main.half_open_connections, conn_index); |
| 404 | } |
| 405 | |
| 406 | void tcp_make_ack (tcp_connection_t * ts, vlib_buffer_t * b); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 407 | void tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 408 | void tcp_make_synack (tcp_connection_t * ts, vlib_buffer_t * b); |
| 409 | void tcp_send_reset (vlib_buffer_t * pkt, u8 is_ip4); |
| 410 | void tcp_send_syn (tcp_connection_t * tc); |
| 411 | void tcp_send_fin (tcp_connection_t * tc); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 412 | void tcp_init_mss (tcp_connection_t * tc); |
| 413 | void tcp_update_snd_mss (tcp_connection_t * tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 414 | |
| 415 | always_inline u32 |
| 416 | tcp_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 | |
| 431 | always_inline u32 |
| 432 | tcp_flight_size (const tcp_connection_t * tc) |
| 433 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 434 | 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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Initial cwnd as per RFC5681 |
| 448 | */ |
| 449 | always_inline u32 |
| 450 | tcp_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 | |
| 460 | always_inline u32 |
| 461 | tcp_loss_wnd (const tcp_connection_t * tc) |
| 462 | { |
| 463 | return tc->snd_mss; |
| 464 | } |
| 465 | |
| 466 | always_inline u32 |
| 467 | tcp_available_wnd (const tcp_connection_t * tc) |
| 468 | { |
| 469 | return clib_min (tc->cwnd, tc->snd_wnd); |
| 470 | } |
| 471 | |
| 472 | always_inline u32 |
| 473 | tcp_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 Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame^] | 484 | u32 tcp_rcv_wnd_available (tcp_connection_t * tc); |
| 485 | u32 tcp_snd_space (tcp_connection_t * tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 486 | void tcp_update_rcv_wnd (tcp_connection_t * tc); |
| 487 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 488 | void tcp_retransmit_first_unacked (tcp_connection_t * tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 489 | void tcp_fast_retransmit (tcp_connection_t * tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 490 | void tcp_cc_congestion (tcp_connection_t * tc); |
| 491 | void tcp_cc_recover (tcp_connection_t * tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 492 | |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 493 | /* Made public for unit testing only */ |
| 494 | void tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end); |
| 495 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 496 | always_inline u32 |
| 497 | tcp_time_now (void) |
| 498 | { |
| 499 | return clib_cpu_time_now () * tcp_main.tstamp_ticks_per_clock; |
| 500 | } |
| 501 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 502 | always_inline void |
| 503 | tcp_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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | u32 tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b); |
| 510 | |
| 511 | u32 |
| 512 | tcp_prepare_retransmit_segment (tcp_connection_t * tc, vlib_buffer_t * b, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 513 | u32 offset, u32 max_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 514 | |
| 515 | void tcp_connection_timers_init (tcp_connection_t * tc); |
| 516 | void tcp_connection_timers_reset (tcp_connection_t * tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 517 | void tcp_connection_init_vars (tcp_connection_t * tc); |
| 518 | |
| 519 | always_inline void |
| 520 | tcp_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 | |
| 527 | always_inline void |
| 528 | tcp_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 | |
| 535 | always_inline void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 536 | tcp_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 | |
| 546 | always_inline void |
| 547 | tcp_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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 557 | /* XXX Switch retransmit to faster TW */ |
| 558 | always_inline void |
| 559 | tcp_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 | |
| 565 | always_inline void |
| 566 | tcp_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 | |
| 572 | always_inline void |
| 573 | tcp_retransmit_timer_reset (tcp_connection_t * tc) |
| 574 | { |
| 575 | tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT); |
| 576 | } |
| 577 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 578 | always_inline void |
| 579 | tcp_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 Coras | 5921f98 | 2017-04-03 18:00:00 -0700 | [diff] [blame] | 583 | clib_max (tc->rto * TCP_TO_TIMER_TICK, |
| 584 | TCP_TIMER_PERSIST_MIN)); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | always_inline void |
| 588 | tcp_persist_timer_update (tcp_connection_t * tc) |
| 589 | { |
| 590 | tcp_timer_update (tc, TCP_TIMER_PERSIST, |
Florin Coras | 5921f98 | 2017-04-03 18:00:00 -0700 | [diff] [blame] | 591 | clib_max (tc->rto * TCP_TO_TIMER_TICK, |
| 592 | TCP_TIMER_PERSIST_MIN)); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | always_inline void |
| 596 | tcp_persist_timer_reset (tcp_connection_t * tc) |
| 597 | { |
| 598 | tcp_timer_reset (tc, TCP_TIMER_PERSIST); |
| 599 | } |
| 600 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 601 | always_inline u8 |
| 602 | tcp_timer_is_active (tcp_connection_t * tc, tcp_timers_e timer) |
| 603 | { |
| 604 | return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID; |
| 605 | } |
| 606 | |
| 607 | void |
| 608 | scoreboard_remove_hole (sack_scoreboard_t * sb, |
| 609 | sack_scoreboard_hole_t * hole); |
| 610 | |
| 611 | always_inline sack_scoreboard_hole_t * |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 612 | scoreboard_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 | |
| 619 | always_inline sack_scoreboard_hole_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 620 | scoreboard_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 | |
| 627 | always_inline sack_scoreboard_hole_t * |
| 628 | scoreboard_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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 635 | always_inline sack_scoreboard_hole_t * |
| 636 | scoreboard_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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 643 | always_inline void |
| 644 | scoreboard_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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 651 | sb->sacked_bytes = 0; |
| 652 | sb->last_sacked_bytes = 0; |
| 653 | sb->snd_una_adv = 0; |
| 654 | sb->max_byte_sacked = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | always_inline u32 |
| 658 | scoreboard_hole_bytes (sack_scoreboard_hole_t * hole) |
| 659 | { |
| 660 | return hole->end - hole->start; |
| 661 | } |
| 662 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 663 | always_inline u32 |
| 664 | scoreboard_hole_index (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole) |
| 665 | { |
| 666 | return hole - sb->holes; |
| 667 | } |
| 668 | |
| 669 | always_inline void |
| 670 | scoreboard_init (sack_scoreboard_t * sb) |
| 671 | { |
| 672 | sb->head = TCP_INVALID_SACK_HOLE_INDEX; |
| 673 | sb->tail = TCP_INVALID_SACK_HOLE_INDEX; |
| 674 | } |
| 675 | |
| 676 | void tcp_rcv_sacks (tcp_connection_t * tc, u32 ack); |
| 677 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 678 | always_inline void |
| 679 | tcp_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 | |
| 688 | always_inline tcp_cc_algorithm_t * |
| 689 | tcp_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 | |
| 695 | void 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 | */ |
| 712 | always_inline void * |
| 713 | vlib_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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 736 | * @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 | */ |
| 747 | always_inline void * |
| 748 | vlib_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 | */ |