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 | #include <vnet/tcp/tcp.h> |
| 17 | #include <vnet/lisp-cp/packets.h> |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 18 | #include <math.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | |
| 20 | vlib_node_registration_t tcp4_output_node; |
| 21 | vlib_node_registration_t tcp6_output_node; |
| 22 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 23 | typedef enum _tcp_output_next |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | { |
| 25 | TCP_OUTPUT_NEXT_DROP, |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 26 | TCP_OUTPUT_NEXT_IP_LOOKUP, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | TCP_OUTPUT_N_NEXT |
| 28 | } tcp_output_next_t; |
| 29 | |
| 30 | #define foreach_tcp4_output_next \ |
| 31 | _ (DROP, "error-drop") \ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 32 | _ (IP_LOOKUP, "ip4-lookup") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 33 | |
| 34 | #define foreach_tcp6_output_next \ |
| 35 | _ (DROP, "error-drop") \ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 36 | _ (IP_LOOKUP, "ip6-lookup") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 37 | |
| 38 | static char *tcp_error_strings[] = { |
| 39 | #define tcp_error(n,s) s, |
| 40 | #include <vnet/tcp/tcp_error.def> |
| 41 | #undef tcp_error |
| 42 | }; |
| 43 | |
| 44 | typedef struct |
| 45 | { |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 46 | tcp_header_t tcp_header; |
| 47 | tcp_connection_t tcp_connection; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | } tcp_tx_trace_t; |
| 49 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 50 | u16 dummy_mtu = 1460; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 51 | |
| 52 | u8 * |
| 53 | format_tcp_tx_trace (u8 * s, va_list * args) |
| 54 | { |
| 55 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 56 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 57 | tcp_tx_trace_t *t = va_arg (*args, tcp_tx_trace_t *); |
| 58 | uword indent = format_get_indent (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 59 | |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 60 | s = format (s, "%U\n%U%U", |
| 61 | format_tcp_header, &t->tcp_header, 128, |
| 62 | format_white_space, indent, |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 63 | format_tcp_connection, &t->tcp_connection, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 64 | |
| 65 | return s; |
| 66 | } |
| 67 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 68 | static u8 |
| 69 | tcp_window_compute_scale (u32 available_space) |
| 70 | { |
| 71 | u8 wnd_scale = 0; |
| 72 | while (wnd_scale < TCP_MAX_WND_SCALE |
| 73 | && (available_space >> wnd_scale) > TCP_WND_MAX) |
| 74 | wnd_scale++; |
| 75 | return wnd_scale; |
| 76 | } |
| 77 | |
| 78 | /** |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 79 | * Update max segment size we're able to process. |
| 80 | * |
| 81 | * The value is constrained by our interface's MTU and IP options. It is |
| 82 | * also what we advertise to our peer. |
| 83 | */ |
| 84 | void |
| 85 | tcp_update_rcv_mss (tcp_connection_t * tc) |
| 86 | { |
| 87 | /* TODO find our iface MTU */ |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 88 | tc->mss = dummy_mtu - sizeof (tcp_header_t); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * TCP's initial window |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 93 | */ |
| 94 | always_inline u32 |
| 95 | tcp_initial_wnd_unscaled (tcp_connection_t * tc) |
| 96 | { |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 97 | /* RFC 6928 recommends the value lower. However at the time our connections |
| 98 | * are initialized, fifos may not be allocated. Therefore, advertise the |
| 99 | * smallest possible unscaled window size and update once fifos are |
| 100 | * assigned to the session. |
| 101 | */ |
| 102 | /* |
| 103 | tcp_update_rcv_mss (tc); |
| 104 | TCP_IW_N_SEGMENTS * tc->mss; |
| 105 | */ |
| 106 | return TCP_MIN_RX_FIFO_SIZE; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 110 | * Compute initial window and scale factor. As per RFC1323, window field in |
| 111 | * SYN and SYN-ACK segments is never scaled. |
| 112 | */ |
| 113 | u32 |
| 114 | tcp_initial_window_to_advertise (tcp_connection_t * tc) |
| 115 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 116 | u32 max_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 117 | |
| 118 | /* Initial wnd for SYN. Fifos are not allocated yet. |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 119 | * Use some predefined value. For SYN-ACK we still want the |
| 120 | * scale to be computed in the same way */ |
| 121 | max_fifo = TCP_MAX_RX_FIFO_SIZE; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 122 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 123 | tc->rcv_wscale = tcp_window_compute_scale (max_fifo); |
| 124 | tc->rcv_wnd = tcp_initial_wnd_unscaled (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 125 | |
| 126 | return clib_min (tc->rcv_wnd, TCP_WND_MAX); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Compute and return window to advertise, scaled as per RFC1323 |
| 131 | */ |
| 132 | u32 |
| 133 | tcp_window_to_advertise (tcp_connection_t * tc, tcp_state_t state) |
| 134 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 135 | if (state < TCP_STATE_ESTABLISHED) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 136 | return tcp_initial_window_to_advertise (tc); |
| 137 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 138 | tcp_update_rcv_wnd (tc); |
| 139 | |
| 140 | if (tc->rcv_wnd == 0) |
| 141 | { |
| 142 | tc->flags |= TCP_CONN_SENT_RCV_WND0; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | tc->flags &= ~TCP_CONN_SENT_RCV_WND0; |
| 147 | } |
| 148 | |
| 149 | return tc->rcv_wnd >> tc->rcv_wscale; |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | tcp_update_rcv_wnd (tcp_connection_t * tc) |
| 154 | { |
| 155 | i32 observed_wnd; |
| 156 | u32 available_space, max_fifo, wnd; |
| 157 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 158 | /* |
| 159 | * Figure out how much space we have available |
| 160 | */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 161 | available_space = stream_session_max_rx_enqueue (&tc->connection); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 162 | max_fifo = stream_session_rx_fifo_size (&tc->connection); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 163 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 164 | ASSERT (tc->rcv_opts.mss < max_fifo); |
| 165 | if (available_space < tc->rcv_opts.mss && available_space < max_fifo >> 3) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 166 | available_space = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 167 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 168 | /* |
| 169 | * Use the above and what we know about what we've previously advertised |
| 170 | * to compute the new window |
| 171 | */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 172 | observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las); |
| 173 | if (observed_wnd < 0) |
| 174 | observed_wnd = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 175 | |
| 176 | /* Bad. Thou shalt not shrink */ |
| 177 | if (available_space < observed_wnd) |
| 178 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 179 | wnd = observed_wnd; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 180 | TCP_EVT_DBG (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 181 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 182 | else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 183 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 184 | wnd = available_space; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 187 | /* Make sure we have a multiple of rcv_wscale */ |
| 188 | if (wnd && tc->rcv_wscale) |
| 189 | { |
| 190 | wnd &= ~(1 << tc->rcv_wscale); |
| 191 | if (wnd == 0) |
| 192 | wnd = 1 << tc->rcv_wscale; |
| 193 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 194 | |
| 195 | tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Write TCP options to segment. |
| 200 | */ |
| 201 | u32 |
| 202 | tcp_options_write (u8 * data, tcp_options_t * opts) |
| 203 | { |
| 204 | u32 opts_len = 0; |
| 205 | u32 buf, seq_len = 4; |
| 206 | |
| 207 | if (tcp_opts_mss (opts)) |
| 208 | { |
| 209 | *data++ = TCP_OPTION_MSS; |
| 210 | *data++ = TCP_OPTION_LEN_MSS; |
| 211 | buf = clib_host_to_net_u16 (opts->mss); |
| 212 | clib_memcpy (data, &buf, sizeof (opts->mss)); |
| 213 | data += sizeof (opts->mss); |
| 214 | opts_len += TCP_OPTION_LEN_MSS; |
| 215 | } |
| 216 | |
| 217 | if (tcp_opts_wscale (opts)) |
| 218 | { |
| 219 | *data++ = TCP_OPTION_WINDOW_SCALE; |
| 220 | *data++ = TCP_OPTION_LEN_WINDOW_SCALE; |
| 221 | *data++ = opts->wscale; |
| 222 | opts_len += TCP_OPTION_LEN_WINDOW_SCALE; |
| 223 | } |
| 224 | |
| 225 | if (tcp_opts_sack_permitted (opts)) |
| 226 | { |
| 227 | *data++ = TCP_OPTION_SACK_PERMITTED; |
| 228 | *data++ = TCP_OPTION_LEN_SACK_PERMITTED; |
| 229 | opts_len += TCP_OPTION_LEN_SACK_PERMITTED; |
| 230 | } |
| 231 | |
| 232 | if (tcp_opts_tstamp (opts)) |
| 233 | { |
| 234 | *data++ = TCP_OPTION_TIMESTAMP; |
| 235 | *data++ = TCP_OPTION_LEN_TIMESTAMP; |
| 236 | buf = clib_host_to_net_u32 (opts->tsval); |
| 237 | clib_memcpy (data, &buf, sizeof (opts->tsval)); |
| 238 | data += sizeof (opts->tsval); |
| 239 | buf = clib_host_to_net_u32 (opts->tsecr); |
| 240 | clib_memcpy (data, &buf, sizeof (opts->tsecr)); |
| 241 | data += sizeof (opts->tsecr); |
| 242 | opts_len += TCP_OPTION_LEN_TIMESTAMP; |
| 243 | } |
| 244 | |
| 245 | if (tcp_opts_sack (opts)) |
| 246 | { |
| 247 | int i; |
| 248 | u32 n_sack_blocks = clib_min (vec_len (opts->sacks), |
| 249 | TCP_OPTS_MAX_SACK_BLOCKS); |
| 250 | |
| 251 | if (n_sack_blocks != 0) |
| 252 | { |
| 253 | *data++ = TCP_OPTION_SACK_BLOCK; |
| 254 | *data++ = 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK; |
| 255 | for (i = 0; i < n_sack_blocks; i++) |
| 256 | { |
| 257 | buf = clib_host_to_net_u32 (opts->sacks[i].start); |
| 258 | clib_memcpy (data, &buf, seq_len); |
| 259 | data += seq_len; |
| 260 | buf = clib_host_to_net_u32 (opts->sacks[i].end); |
| 261 | clib_memcpy (data, &buf, seq_len); |
| 262 | data += seq_len; |
| 263 | } |
| 264 | opts_len += 2 + n_sack_blocks * TCP_OPTION_LEN_SACK_BLOCK; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* Terminate TCP options */ |
| 269 | if (opts_len % 4) |
| 270 | { |
| 271 | *data++ = TCP_OPTION_EOL; |
| 272 | opts_len += TCP_OPTION_LEN_EOL; |
| 273 | } |
| 274 | |
| 275 | /* Pad with zeroes to a u32 boundary */ |
| 276 | while (opts_len % 4) |
| 277 | { |
| 278 | *data++ = TCP_OPTION_NOOP; |
| 279 | opts_len += TCP_OPTION_LEN_NOOP; |
| 280 | } |
| 281 | return opts_len; |
| 282 | } |
| 283 | |
| 284 | always_inline int |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 285 | tcp_make_syn_options (tcp_options_t * opts, u8 wnd_scale) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 286 | { |
| 287 | u8 len = 0; |
| 288 | |
| 289 | opts->flags |= TCP_OPTS_FLAG_MSS; |
| 290 | opts->mss = dummy_mtu; /*XXX discover that */ |
| 291 | len += TCP_OPTION_LEN_MSS; |
| 292 | |
| 293 | opts->flags |= TCP_OPTS_FLAG_WSCALE; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 294 | opts->wscale = wnd_scale; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 295 | len += TCP_OPTION_LEN_WINDOW_SCALE; |
| 296 | |
| 297 | opts->flags |= TCP_OPTS_FLAG_TSTAMP; |
| 298 | opts->tsval = tcp_time_now (); |
| 299 | opts->tsecr = 0; |
| 300 | len += TCP_OPTION_LEN_TIMESTAMP; |
| 301 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 302 | if (TCP_USE_SACKS) |
| 303 | { |
| 304 | opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED; |
| 305 | len += TCP_OPTION_LEN_SACK_PERMITTED; |
| 306 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | |
| 308 | /* Align to needed boundary */ |
| 309 | len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN; |
| 310 | return len; |
| 311 | } |
| 312 | |
| 313 | always_inline int |
| 314 | tcp_make_synack_options (tcp_connection_t * tc, tcp_options_t * opts) |
| 315 | { |
| 316 | u8 len = 0; |
| 317 | |
| 318 | opts->flags |= TCP_OPTS_FLAG_MSS; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 319 | opts->mss = tc->mss; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 320 | len += TCP_OPTION_LEN_MSS; |
| 321 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 322 | if (tcp_opts_wscale (&tc->rcv_opts)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 323 | { |
| 324 | opts->flags |= TCP_OPTS_FLAG_WSCALE; |
| 325 | opts->wscale = tc->rcv_wscale; |
| 326 | len += TCP_OPTION_LEN_WINDOW_SCALE; |
| 327 | } |
| 328 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 329 | if (tcp_opts_tstamp (&tc->rcv_opts)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 330 | { |
| 331 | opts->flags |= TCP_OPTS_FLAG_TSTAMP; |
| 332 | opts->tsval = tcp_time_now (); |
| 333 | opts->tsecr = tc->tsval_recent; |
| 334 | len += TCP_OPTION_LEN_TIMESTAMP; |
| 335 | } |
| 336 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 337 | if (tcp_opts_sack_permitted (&tc->rcv_opts)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 338 | { |
| 339 | opts->flags |= TCP_OPTS_FLAG_SACK_PERMITTED; |
| 340 | len += TCP_OPTION_LEN_SACK_PERMITTED; |
| 341 | } |
| 342 | |
| 343 | /* Align to needed boundary */ |
| 344 | len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN; |
| 345 | return len; |
| 346 | } |
| 347 | |
| 348 | always_inline int |
| 349 | tcp_make_established_options (tcp_connection_t * tc, tcp_options_t * opts) |
| 350 | { |
| 351 | u8 len = 0; |
| 352 | |
| 353 | opts->flags = 0; |
| 354 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 355 | if (tcp_opts_tstamp (&tc->rcv_opts)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 356 | { |
| 357 | opts->flags |= TCP_OPTS_FLAG_TSTAMP; |
| 358 | opts->tsval = tcp_time_now (); |
| 359 | opts->tsecr = tc->tsval_recent; |
| 360 | len += TCP_OPTION_LEN_TIMESTAMP; |
| 361 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 362 | if (tcp_opts_sack_permitted (&tc->rcv_opts)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 363 | { |
| 364 | if (vec_len (tc->snd_sacks)) |
| 365 | { |
| 366 | opts->flags |= TCP_OPTS_FLAG_SACK; |
| 367 | opts->sacks = tc->snd_sacks; |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 368 | opts->n_sack_blocks = clib_min (vec_len (tc->snd_sacks), |
| 369 | TCP_OPTS_MAX_SACK_BLOCKS); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 370 | len += 2 + TCP_OPTION_LEN_SACK_BLOCK * opts->n_sack_blocks; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* Align to needed boundary */ |
| 375 | len += (TCP_OPTS_ALIGN - len % TCP_OPTS_ALIGN) % TCP_OPTS_ALIGN; |
| 376 | return len; |
| 377 | } |
| 378 | |
| 379 | always_inline int |
| 380 | tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts, |
| 381 | tcp_state_t state) |
| 382 | { |
| 383 | switch (state) |
| 384 | { |
| 385 | case TCP_STATE_ESTABLISHED: |
| 386 | case TCP_STATE_FIN_WAIT_1: |
| 387 | return tcp_make_established_options (tc, opts); |
| 388 | case TCP_STATE_SYN_RCVD: |
| 389 | return tcp_make_synack_options (tc, opts); |
| 390 | case TCP_STATE_SYN_SENT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 391 | return tcp_make_syn_options (opts, tc->rcv_wscale); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 392 | default: |
| 393 | clib_warning ("Not handled!"); |
| 394 | return 0; |
| 395 | } |
| 396 | } |
| 397 | |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 398 | /** |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 399 | * Update snd_mss to reflect the effective segment size that we can send |
| 400 | * by taking into account all TCP options, including SACKs |
| 401 | */ |
| 402 | void |
| 403 | tcp_update_snd_mss (tcp_connection_t * tc) |
| 404 | { |
| 405 | /* Compute options to be used for connection. These may be reused when |
| 406 | * sending data or to compute the effective mss (snd_mss) */ |
| 407 | tc->snd_opts_len = |
| 408 | tcp_make_options (tc, &tc->snd_opts, TCP_STATE_ESTABLISHED); |
| 409 | |
| 410 | /* XXX check if MTU has been updated */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 411 | tc->snd_mss = clib_min (tc->mss, tc->rcv_opts.mss) - tc->snd_opts_len; |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 412 | ASSERT (tc->snd_mss > 0); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void |
| 416 | tcp_init_mss (tcp_connection_t * tc) |
| 417 | { |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 418 | u16 default_min_mss = 536; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 419 | tcp_update_rcv_mss (tc); |
| 420 | |
| 421 | /* TODO cache mss and consider PMTU discovery */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 422 | tc->snd_mss = clib_min (tc->rcv_opts.mss, tc->mss); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 423 | |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 424 | if (tc->snd_mss < 45) |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 425 | { |
| 426 | clib_warning ("snd mss is 0"); |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 427 | /* Assume that at least the min default mss works */ |
| 428 | tc->snd_mss = default_min_mss; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 429 | tc->rcv_opts.mss = default_min_mss; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* We should have enough space for 40 bytes of options */ |
| 433 | ASSERT (tc->snd_mss > 45); |
| 434 | |
| 435 | /* If we use timestamp option, account for it */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 436 | if (tcp_opts_tstamp (&tc->rcv_opts)) |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 437 | tc->snd_mss -= TCP_OPTION_LEN_TIMESTAMP; |
| 438 | } |
| 439 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 440 | always_inline int |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 441 | tcp_alloc_tx_buffers (tcp_main_t * tm, u8 thread_index, u32 n_free_buffers) |
| 442 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 443 | vec_validate (tm->tx_buffers[thread_index], |
| 444 | vec_len (tm->tx_buffers[thread_index]) + n_free_buffers - 1); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 445 | _vec_len (tm->tx_buffers[thread_index]) = |
| 446 | vlib_buffer_alloc_from_free_list (vlib_get_main (), |
| 447 | tm->tx_buffers[thread_index], |
| 448 | n_free_buffers, |
| 449 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 450 | /* buffer shortage, report failure */ |
| 451 | if (vec_len (tm->tx_buffers[thread_index]) == 0) |
| 452 | { |
| 453 | clib_warning ("out of buffers"); |
| 454 | return -1; |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | always_inline int |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 460 | tcp_get_free_buffer_index (tcp_main_t * tm, u32 * bidx) |
| 461 | { |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 462 | u32 *my_tx_buffers; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 463 | u32 thread_index = vlib_get_thread_index (); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 464 | if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0)) |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 465 | { |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 466 | if (tcp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE)) |
| 467 | return -1; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 468 | } |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 469 | my_tx_buffers = tm->tx_buffers[thread_index]; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 470 | *bidx = my_tx_buffers[_vec_len (my_tx_buffers) - 1]; |
| 471 | _vec_len (my_tx_buffers) -= 1; |
| 472 | return 0; |
| 473 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 474 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 475 | always_inline void |
| 476 | tcp_return_buffer (tcp_main_t * tm) |
| 477 | { |
| 478 | u32 *my_tx_buffers; |
| 479 | u32 thread_index = vlib_get_thread_index (); |
| 480 | my_tx_buffers = tm->tx_buffers[thread_index]; |
| 481 | _vec_len (my_tx_buffers) += 1; |
| 482 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 483 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 484 | always_inline void * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 485 | tcp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b) |
| 486 | { |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 487 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 488 | vlib_buffer_free_one (vm, b->next_buffer); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 489 | b->flags = 0; |
| 490 | b->current_data = 0; |
| 491 | b->current_length = 0; |
| 492 | b->total_length_not_including_first_buffer = 0; |
| 493 | vnet_buffer (b)->tcp.flags = 0; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 494 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 495 | /* Leave enough space for headers */ |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 496 | return vlib_buffer_make_headroom (b, MAX_HDRS_LEN); |
| 497 | } |
| 498 | |
| 499 | always_inline void * |
| 500 | tcp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b) |
| 501 | { |
| 502 | ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0); |
| 503 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 504 | b->total_length_not_including_first_buffer = 0; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 505 | vnet_buffer (b)->tcp.flags = 0; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 506 | |
| 507 | /* Leave enough space for headers */ |
| 508 | return vlib_buffer_make_headroom (b, MAX_HDRS_LEN); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Prepare ACK |
| 513 | */ |
| 514 | void |
| 515 | tcp_make_ack_i (tcp_connection_t * tc, vlib_buffer_t * b, tcp_state_t state, |
| 516 | u8 flags) |
| 517 | { |
| 518 | tcp_options_t _snd_opts, *snd_opts = &_snd_opts; |
| 519 | u8 tcp_opts_len, tcp_hdr_opts_len; |
| 520 | tcp_header_t *th; |
| 521 | u16 wnd; |
| 522 | |
| 523 | wnd = tcp_window_to_advertise (tc, state); |
| 524 | |
| 525 | /* Make and write options */ |
| 526 | tcp_opts_len = tcp_make_established_options (tc, snd_opts); |
| 527 | tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t); |
| 528 | |
| 529 | th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt, |
| 530 | tc->rcv_nxt, tcp_hdr_opts_len, flags, wnd); |
| 531 | |
| 532 | tcp_options_write ((u8 *) (th + 1), snd_opts); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 533 | vnet_buffer (b)->tcp.connection_index = tc->c_c_index; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Convert buffer to ACK |
| 538 | */ |
| 539 | void |
| 540 | tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b) |
| 541 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 542 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 543 | |
| 544 | tcp_reuse_buffer (vm, b); |
| 545 | tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 546 | TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 547 | vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK; |
| 548 | tc->rcv_las = tc->rcv_nxt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Convert buffer to FIN-ACK |
| 553 | */ |
| 554 | void |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 555 | tcp_make_fin (tcp_connection_t * tc, vlib_buffer_t * b) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 556 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 557 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 558 | u8 flags = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 559 | |
| 560 | tcp_reuse_buffer (vm, b); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 561 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 562 | flags = TCP_FLAG_FIN | TCP_FLAG_ACK; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 563 | tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, flags); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | |
| 565 | /* Reset flags, make sure ack is sent */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 566 | vnet_buffer (b)->tcp.flags &= ~TCP_BUF_FLAG_DUPACK; |
| 567 | |
| 568 | tc->snd_nxt += 1; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Convert buffer to SYN-ACK |
| 573 | */ |
| 574 | void |
| 575 | tcp_make_synack (tcp_connection_t * tc, vlib_buffer_t * b) |
| 576 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 577 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 578 | tcp_options_t _snd_opts, *snd_opts = &_snd_opts; |
| 579 | u8 tcp_opts_len, tcp_hdr_opts_len; |
| 580 | tcp_header_t *th; |
| 581 | u16 initial_wnd; |
| 582 | u32 time_now; |
| 583 | |
| 584 | memset (snd_opts, 0, sizeof (*snd_opts)); |
| 585 | |
| 586 | tcp_reuse_buffer (vm, b); |
| 587 | |
| 588 | /* Set random initial sequence */ |
| 589 | time_now = tcp_time_now (); |
| 590 | |
| 591 | tc->iss = random_u32 (&time_now); |
| 592 | tc->snd_una = tc->iss; |
| 593 | tc->snd_nxt = tc->iss + 1; |
| 594 | tc->snd_una_max = tc->snd_nxt; |
| 595 | |
| 596 | initial_wnd = tcp_initial_window_to_advertise (tc); |
| 597 | |
| 598 | /* Make and write options */ |
| 599 | tcp_opts_len = tcp_make_synack_options (tc, snd_opts); |
| 600 | tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t); |
| 601 | |
| 602 | th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss, |
| 603 | tc->rcv_nxt, tcp_hdr_opts_len, |
| 604 | TCP_FLAG_SYN | TCP_FLAG_ACK, initial_wnd); |
| 605 | |
| 606 | tcp_options_write ((u8 *) (th + 1), snd_opts); |
| 607 | |
| 608 | vnet_buffer (b)->tcp.connection_index = tc->c_c_index; |
| 609 | vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_ACK; |
| 610 | |
| 611 | /* Init retransmit timer */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 612 | tcp_retransmit_timer_set (tc); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 613 | TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | always_inline void |
| 617 | tcp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, |
| 618 | u8 is_ip4) |
| 619 | { |
| 620 | u32 *to_next, next_index; |
| 621 | vlib_frame_t *f; |
| 622 | |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 623 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 624 | b->error = 0; |
| 625 | |
| 626 | /* Default FIB for now */ |
| 627 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
| 628 | |
| 629 | /* Send to IP lookup */ |
| 630 | next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index; |
| 631 | f = vlib_get_frame_to_node (vm, next_index); |
| 632 | |
| 633 | /* Enqueue the packet */ |
| 634 | to_next = vlib_frame_vector_args (f); |
| 635 | to_next[0] = bi; |
| 636 | f->n_vectors = 1; |
| 637 | vlib_put_frame_to_node (vm, next_index, f); |
| 638 | } |
| 639 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 640 | always_inline void |
| 641 | tcp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, |
| 642 | u8 is_ip4, u8 flush) |
| 643 | { |
| 644 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 645 | u32 thread_index = vlib_get_thread_index (); |
| 646 | u32 *to_next, next_index; |
| 647 | vlib_frame_t *f; |
| 648 | |
| 649 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 650 | b->error = 0; |
| 651 | |
| 652 | /* Decide where to send the packet */ |
| 653 | next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index; |
| 654 | |
| 655 | /* Initialize the trajectory trace, if configured */ |
| 656 | if (VLIB_BUFFER_TRACE_TRAJECTORY > 0) |
| 657 | { |
| 658 | b->pre_data[0] = 1; |
| 659 | b->pre_data[1] = next_index; |
| 660 | } |
| 661 | |
| 662 | /* Get frame to v4/6 output node */ |
| 663 | f = tm->tx_frames[!is_ip4][thread_index]; |
| 664 | if (!f) |
| 665 | { |
| 666 | f = vlib_get_frame_to_node (vm, next_index); |
| 667 | ASSERT (f); |
| 668 | tm->tx_frames[!is_ip4][thread_index] = f; |
| 669 | } |
| 670 | to_next = vlib_frame_vector_args (f); |
| 671 | to_next[f->n_vectors] = bi; |
| 672 | f->n_vectors += 1; |
| 673 | if (flush || f->n_vectors == VLIB_FRAME_SIZE) |
| 674 | { |
| 675 | vlib_put_frame_to_node (vm, next_index, f); |
| 676 | tm->tx_frames[!is_ip4][thread_index] = 0; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | always_inline void |
| 681 | tcp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4) |
| 682 | { |
| 683 | tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 0); |
| 684 | } |
| 685 | |
| 686 | always_inline void |
| 687 | tcp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, |
| 688 | u8 is_ip4) |
| 689 | { |
| 690 | tcp_enqueue_to_output_i (vm, b, bi, is_ip4, 1); |
| 691 | } |
| 692 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 693 | int |
| 694 | tcp_make_reset_in_place (vlib_main_t * vm, vlib_buffer_t * b0, |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 695 | tcp_state_t state, u8 thread_index, u8 is_ip4) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 696 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 697 | ip4_header_t *ih4; |
| 698 | ip6_header_t *ih6; |
| 699 | tcp_header_t *th0; |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 700 | ip4_address_t src_ip40, dst_ip40; |
| 701 | ip6_address_t src_ip60, dst_ip60; |
| 702 | u16 src_port, dst_port; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 703 | u32 tmp; |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 704 | u32 seq, ack; |
| 705 | u8 flags; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 706 | |
| 707 | /* Find IP and TCP headers */ |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 708 | th0 = tcp_buffer_hdr (b0); |
| 709 | |
| 710 | /* Save src and dst ip */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 711 | if (is_ip4) |
| 712 | { |
| 713 | ih4 = vlib_buffer_get_current (b0); |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 714 | ASSERT ((ih4->ip_version_and_header_length & 0xF0) == 0x40); |
| 715 | src_ip40.as_u32 = ih4->src_address.as_u32; |
| 716 | dst_ip40.as_u32 = ih4->dst_address.as_u32; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 717 | } |
| 718 | else |
| 719 | { |
| 720 | ih6 = vlib_buffer_get_current (b0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 721 | ASSERT ((ih6->ip_version_traffic_class_and_flow_label & 0xF0) == 0x60); |
| 722 | clib_memcpy (&src_ip60, &ih6->src_address, sizeof (ip6_address_t)); |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 723 | clib_memcpy (&dst_ip60, &ih6->dst_address, sizeof (ip6_address_t)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 724 | } |
| 725 | |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 726 | src_port = th0->src_port; |
| 727 | dst_port = th0->dst_port; |
| 728 | |
| 729 | /* Try to determine what/why we're actually resetting */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 730 | if (state == TCP_STATE_CLOSED) |
| 731 | { |
| 732 | if (!tcp_syn (th0)) |
| 733 | return -1; |
| 734 | |
| 735 | tmp = clib_net_to_host_u32 (th0->seq_number); |
| 736 | |
| 737 | /* Got a SYN for no listener. */ |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 738 | flags = TCP_FLAG_RST | TCP_FLAG_ACK; |
| 739 | ack = clib_host_to_net_u32 (tmp + 1); |
| 740 | seq = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 741 | } |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 742 | else |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 743 | { |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 744 | flags = TCP_FLAG_RST; |
| 745 | seq = th0->ack_number; |
| 746 | ack = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 747 | } |
| 748 | |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 749 | tcp_reuse_buffer (vm, b0); |
| 750 | th0 = vlib_buffer_push_tcp_net_order (b0, dst_port, src_port, seq, ack, |
| 751 | sizeof (tcp_header_t), flags, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 752 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 753 | if (is_ip4) |
| 754 | { |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 755 | ih4 = vlib_buffer_push_ip4 (vm, b0, &dst_ip40, &src_ip40, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 756 | IP_PROTOCOL_TCP, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 757 | th0->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ih4); |
| 758 | } |
| 759 | else |
| 760 | { |
| 761 | int bogus = ~0; |
Florin Coras | dc629cd | 2017-05-09 00:52:37 -0700 | [diff] [blame] | 762 | ih6 = vlib_buffer_push_ip6 (vm, b0, &dst_ip60, &src_ip60, |
| 763 | IP_PROTOCOL_TCP); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 764 | th0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ih6, &bogus); |
| 765 | ASSERT (!bogus); |
| 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Send reset without reusing existing buffer |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 773 | * |
| 774 | * It extracts connection info out of original packet |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 775 | */ |
| 776 | void |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 777 | tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt, u8 is_ip4) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 778 | { |
| 779 | vlib_buffer_t *b; |
| 780 | u32 bi; |
| 781 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 782 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 783 | u8 tcp_hdr_len, flags = 0; |
| 784 | tcp_header_t *th, *pkt_th; |
| 785 | u32 seq, ack; |
| 786 | ip4_header_t *ih4, *pkt_ih4; |
| 787 | ip6_header_t *ih6, *pkt_ih6; |
| 788 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 789 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 790 | return; |
| 791 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 792 | b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 793 | tcp_init_buffer (vm, b); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 794 | |
| 795 | /* Make and write options */ |
| 796 | tcp_hdr_len = sizeof (tcp_header_t); |
| 797 | |
| 798 | if (is_ip4) |
| 799 | { |
| 800 | pkt_ih4 = vlib_buffer_get_current (pkt); |
| 801 | pkt_th = ip4_next_header (pkt_ih4); |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | pkt_ih6 = vlib_buffer_get_current (pkt); |
| 806 | pkt_th = ip6_next_header (pkt_ih6); |
| 807 | } |
| 808 | |
| 809 | if (tcp_ack (pkt_th)) |
| 810 | { |
| 811 | flags = TCP_FLAG_RST; |
| 812 | seq = pkt_th->ack_number; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 813 | ack = (tc && tc->state >= TCP_STATE_SYN_RCVD) ? tc->rcv_nxt : 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 814 | } |
| 815 | else |
| 816 | { |
| 817 | flags = TCP_FLAG_RST | TCP_FLAG_ACK; |
| 818 | seq = 0; |
| 819 | ack = clib_host_to_net_u32 (vnet_buffer (pkt)->tcp.seq_end); |
| 820 | } |
| 821 | |
| 822 | th = vlib_buffer_push_tcp_net_order (b, pkt_th->dst_port, pkt_th->src_port, |
| 823 | seq, ack, tcp_hdr_len, flags, 0); |
| 824 | |
| 825 | /* Swap src and dst ip */ |
| 826 | if (is_ip4) |
| 827 | { |
| 828 | ASSERT ((pkt_ih4->ip_version_and_header_length & 0xF0) == 0x40); |
| 829 | ih4 = vlib_buffer_push_ip4 (vm, b, &pkt_ih4->dst_address, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 830 | &pkt_ih4->src_address, IP_PROTOCOL_TCP, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 831 | th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4); |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | int bogus = ~0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 836 | ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) == |
| 837 | 0x60); |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 838 | ih6 = vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address, |
| 839 | &pkt_ih6->src_address, IP_PROTOCOL_TCP); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 840 | th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus); |
| 841 | ASSERT (!bogus); |
| 842 | } |
| 843 | |
| 844 | tcp_enqueue_to_ip_lookup (vm, b, bi, is_ip4); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 845 | TCP_EVT_DBG (TCP_EVT_RST_SENT, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 848 | /** |
| 849 | * Build and set reset packet for connection |
| 850 | */ |
| 851 | void |
| 852 | tcp_send_reset (tcp_connection_t * tc) |
| 853 | { |
| 854 | vlib_main_t *vm = vlib_get_main (); |
| 855 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 856 | vlib_buffer_t *b; |
| 857 | u32 bi; |
| 858 | tcp_header_t *th; |
| 859 | u16 tcp_hdr_opts_len, advertise_wnd, opts_write_len; |
| 860 | u8 flags; |
| 861 | |
| 862 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 863 | return; |
| 864 | b = vlib_get_buffer (vm, bi); |
| 865 | tcp_init_buffer (vm, b); |
| 866 | |
| 867 | tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state); |
| 868 | tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t); |
| 869 | advertise_wnd = tcp_window_to_advertise (tc, TCP_STATE_ESTABLISHED); |
| 870 | flags = TCP_FLAG_RST; |
| 871 | th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt, |
| 872 | tc->rcv_nxt, tcp_hdr_opts_len, flags, |
| 873 | advertise_wnd); |
| 874 | opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts); |
| 875 | ASSERT (opts_write_len == tc->snd_opts_len); |
| 876 | vnet_buffer (b)->tcp.connection_index = tc->c_c_index; |
| 877 | tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4); |
| 878 | } |
| 879 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 880 | void |
| 881 | tcp_push_ip_hdr (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b) |
| 882 | { |
| 883 | tcp_header_t *th = vlib_buffer_get_current (b); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 884 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 885 | if (tc->c_is_ip4) |
| 886 | { |
| 887 | ip4_header_t *ih; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 888 | ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 889 | &tc->c_rmt_ip4, IP_PROTOCOL_TCP, 1); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 890 | th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 891 | } |
| 892 | else |
| 893 | { |
| 894 | ip6_header_t *ih; |
| 895 | int bogus = ~0; |
| 896 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 897 | ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 898 | &tc->c_rmt_ip6, IP_PROTOCOL_TCP); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 899 | th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 900 | ASSERT (!bogus); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * Send SYN |
| 906 | * |
| 907 | * Builds a SYN packet for a half-open connection and sends it to ipx_lookup. |
| 908 | * The packet is not forwarded through tcpx_output to avoid doing lookups |
| 909 | * in the half_open pool. |
| 910 | */ |
| 911 | void |
| 912 | tcp_send_syn (tcp_connection_t * tc) |
| 913 | { |
| 914 | vlib_buffer_t *b; |
| 915 | u32 bi; |
| 916 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 917 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 918 | u8 tcp_hdr_opts_len, tcp_opts_len; |
| 919 | tcp_header_t *th; |
| 920 | u32 time_now; |
| 921 | u16 initial_wnd; |
| 922 | tcp_options_t snd_opts; |
| 923 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 924 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 925 | return; |
| 926 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 927 | b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 928 | tcp_init_buffer (vm, b); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 929 | |
| 930 | /* Set random initial sequence */ |
| 931 | time_now = tcp_time_now (); |
| 932 | |
| 933 | tc->iss = random_u32 (&time_now); |
| 934 | tc->snd_una = tc->iss; |
| 935 | tc->snd_una_max = tc->snd_nxt = tc->iss + 1; |
| 936 | |
| 937 | initial_wnd = tcp_initial_window_to_advertise (tc); |
| 938 | |
| 939 | /* Make and write options */ |
| 940 | memset (&snd_opts, 0, sizeof (snd_opts)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 941 | tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 942 | tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t); |
| 943 | |
| 944 | th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss, |
| 945 | tc->rcv_nxt, tcp_hdr_opts_len, TCP_FLAG_SYN, |
| 946 | initial_wnd); |
| 947 | |
| 948 | tcp_options_write ((u8 *) (th + 1), &snd_opts); |
| 949 | |
| 950 | /* Measure RTT with this */ |
| 951 | tc->rtt_ts = tcp_time_now (); |
| 952 | tc->rtt_seq = tc->snd_nxt; |
| 953 | |
| 954 | /* Start retransmit trimer */ |
| 955 | tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, tc->rto * TCP_TO_TIMER_TICK); |
| 956 | tc->rto_boff = 0; |
| 957 | |
| 958 | /* Set the connection establishment timer */ |
| 959 | tcp_timer_set (tc, TCP_TIMER_ESTABLISH, TCP_ESTABLISH_TIME); |
| 960 | |
| 961 | tcp_push_ip_hdr (tm, tc, b); |
| 962 | tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 963 | TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 966 | /** |
| 967 | * Flush tx frame populated by retransmits and timer pops |
| 968 | */ |
| 969 | void |
| 970 | tcp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4) |
| 971 | { |
| 972 | if (tcp_main.tx_frames[!is_ip4][thread_index]) |
| 973 | { |
| 974 | u32 next_index; |
| 975 | next_index = is_ip4 ? tcp4_output_node.index : tcp6_output_node.index; |
| 976 | vlib_put_frame_to_node (vm, next_index, |
| 977 | tcp_main.tx_frames[!is_ip4][thread_index]); |
| 978 | tcp_main.tx_frames[!is_ip4][thread_index] = 0; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Flush both v4 and v6 tx frames for thread index |
| 984 | */ |
| 985 | void |
| 986 | tcp_flush_frames_to_output (u8 thread_index) |
| 987 | { |
| 988 | vlib_main_t *vm = vlib_get_main (); |
| 989 | tcp_flush_frame_to_output (vm, thread_index, 1); |
| 990 | tcp_flush_frame_to_output (vm, thread_index, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
| 994 | * Send FIN |
| 995 | */ |
| 996 | void |
| 997 | tcp_send_fin (tcp_connection_t * tc) |
| 998 | { |
| 999 | vlib_buffer_t *b; |
| 1000 | u32 bi; |
| 1001 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1002 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1003 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1004 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 1005 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1006 | b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1007 | /* buffer will be initialized by in tcp_make_fin */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1008 | tcp_make_fin (tc, b); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1009 | tcp_enqueue_to_output_now (vm, b, bi, tc->c_is_ip4); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1010 | tc->flags |= TCP_CONN_FINSNT; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1011 | tc->flags &= ~TCP_CONN_FINPNDG; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1012 | tcp_retransmit_timer_force_update (tc); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1013 | TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | always_inline u8 |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1017 | tcp_make_state_flags (tcp_connection_t * tc, tcp_state_t next_state) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1018 | { |
| 1019 | switch (next_state) |
| 1020 | { |
| 1021 | case TCP_STATE_ESTABLISHED: |
| 1022 | return TCP_FLAG_ACK; |
| 1023 | case TCP_STATE_SYN_RCVD: |
| 1024 | return TCP_FLAG_SYN | TCP_FLAG_ACK; |
| 1025 | case TCP_STATE_SYN_SENT: |
| 1026 | return TCP_FLAG_SYN; |
| 1027 | case TCP_STATE_LAST_ACK: |
| 1028 | case TCP_STATE_FIN_WAIT_1: |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1029 | if (tc->snd_nxt + 1 < tc->snd_una_max) |
| 1030 | return TCP_FLAG_ACK; |
| 1031 | else |
| 1032 | return TCP_FLAG_FIN; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1033 | default: |
| 1034 | clib_warning ("Shouldn't be here!"); |
| 1035 | } |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * Push TCP header and update connection variables |
| 1041 | */ |
| 1042 | static void |
| 1043 | tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b, |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1044 | tcp_state_t next_state, u8 compute_opts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1045 | { |
| 1046 | u32 advertise_wnd, data_len; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1047 | u8 tcp_hdr_opts_len, opts_write_len, flags; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1048 | tcp_header_t *th; |
| 1049 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1050 | data_len = b->current_length + b->total_length_not_including_first_buffer; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1051 | ASSERT (!b->total_length_not_including_first_buffer |
| 1052 | || (b->flags & VLIB_BUFFER_NEXT_PRESENT)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1053 | vnet_buffer (b)->tcp.flags = 0; |
| 1054 | |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1055 | if (compute_opts) |
| 1056 | tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state); |
| 1057 | |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1058 | tcp_hdr_opts_len = tc->snd_opts_len + sizeof (tcp_header_t); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1059 | advertise_wnd = tcp_window_to_advertise (tc, next_state); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1060 | flags = tcp_make_state_flags (tc, next_state); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1061 | |
| 1062 | /* Push header and options */ |
| 1063 | th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->snd_nxt, |
| 1064 | tc->rcv_nxt, tcp_hdr_opts_len, flags, |
| 1065 | advertise_wnd); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1066 | opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1067 | |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1068 | ASSERT (opts_write_len == tc->snd_opts_len); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1069 | vnet_buffer (b)->tcp.connection_index = tc->c_c_index; |
| 1070 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1071 | /* |
| 1072 | * Update connection variables |
| 1073 | */ |
| 1074 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1075 | tc->snd_nxt += data_len; |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1076 | tc->rcv_las = tc->rcv_nxt; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1077 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1078 | /* TODO this is updated in output as well ... */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1079 | if (seq_gt (tc->snd_nxt, tc->snd_una_max)) |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1080 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1081 | tc->snd_una_max = tc->snd_nxt; |
| 1082 | tcp_validate_txf_size (tc, tc->snd_una_max - tc->snd_una); |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1083 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1084 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1085 | TCP_EVT_DBG (TCP_EVT_PKTIZE, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1086 | } |
| 1087 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1088 | void |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1089 | tcp_send_ack (tcp_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1090 | { |
| 1091 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1092 | vlib_main_t *vm = vlib_get_main (); |
| 1093 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1094 | vlib_buffer_t *b; |
| 1095 | u32 bi; |
| 1096 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1097 | /* Get buffer */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1098 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 1099 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1100 | b = vlib_get_buffer (vm, bi); |
| 1101 | |
| 1102 | /* Fill in the ACK */ |
| 1103 | tcp_make_ack (tc, b); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1104 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
| 1105 | } |
| 1106 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1107 | /** |
| 1108 | * Delayed ack timer handler |
| 1109 | * |
| 1110 | * Sends delayed ACK when timer expires |
| 1111 | */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1112 | void |
| 1113 | tcp_timer_delack_handler (u32 index) |
| 1114 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1115 | u32 thread_index = vlib_get_thread_index (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1116 | tcp_connection_t *tc; |
| 1117 | |
| 1118 | tc = tcp_connection_get (index, thread_index); |
| 1119 | tc->timers[TCP_TIMER_DELACK] = TCP_TIMER_HANDLE_INVALID; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1120 | tcp_send_ack (tc); |
| 1121 | } |
| 1122 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1123 | /** |
| 1124 | * Build a retransmit segment |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1125 | * |
| 1126 | * @return the number of bytes in the segment or 0 if there's nothing to |
| 1127 | * retransmit |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1128 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1129 | u32 |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1130 | tcp_prepare_retransmit_segment (tcp_connection_t * tc, u32 offset, |
| 1131 | u32 max_deq_bytes, vlib_buffer_t ** b) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1132 | { |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1133 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1134 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1135 | int n_bytes = 0; |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1136 | u32 start, bi, available_bytes, seg_size; |
| 1137 | u8 *data; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1138 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1139 | ASSERT (tc->state >= TCP_STATE_ESTABLISHED); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1140 | ASSERT (max_deq_bytes != 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1141 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * Make sure we can retransmit something |
| 1144 | */ |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1145 | available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1146 | available_bytes -= offset; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1147 | if (!available_bytes) |
| 1148 | return 0; |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1149 | max_deq_bytes = clib_min (tc->snd_mss, max_deq_bytes); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1150 | max_deq_bytes = clib_min (available_bytes, max_deq_bytes); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* Start is beyond snd_congestion */ |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1153 | start = tc->snd_una + offset; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1154 | if (seq_geq (start, tc->snd_congestion)) |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1155 | goto done; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1156 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1157 | /* Don't overshoot snd_congestion */ |
| 1158 | if (seq_gt (start + max_deq_bytes, tc->snd_congestion)) |
| 1159 | { |
| 1160 | max_deq_bytes = tc->snd_congestion - start; |
| 1161 | if (max_deq_bytes == 0) |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1162 | goto done; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1165 | seg_size = max_deq_bytes + MAX_HDRS_LEN; |
| 1166 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1167 | /* |
| 1168 | * Prepare options |
| 1169 | */ |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1170 | tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state); |
| 1171 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1172 | /* |
| 1173 | * Allocate and fill in buffer(s) |
| 1174 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1175 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1176 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 1177 | return 0; |
| 1178 | *b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1179 | data = tcp_init_buffer (vm, *b); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1180 | |
| 1181 | /* Easy case, buffer size greater than mss */ |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1182 | if (PREDICT_TRUE (seg_size <= tm->bytes_per_buffer)) |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1183 | { |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1184 | n_bytes = stream_session_peek_bytes (&tc->connection, data, offset, |
| 1185 | max_deq_bytes); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1186 | ASSERT (n_bytes == max_deq_bytes); |
| 1187 | b[0]->current_length = n_bytes; |
| 1188 | tcp_push_hdr_i (tc, *b, tc->state, 0); |
| 1189 | } |
| 1190 | /* Split mss into multiple buffers */ |
| 1191 | else |
| 1192 | { |
| 1193 | u32 chain_bi = ~0, n_bufs_per_seg; |
| 1194 | u32 thread_index = vlib_get_thread_index (); |
| 1195 | u16 n_peeked, len_to_deq, available_bufs; |
| 1196 | vlib_buffer_t *chain_b, *prev_b; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1197 | int i; |
| 1198 | |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1199 | n_bufs_per_seg = ceil ((double) seg_size / tm->bytes_per_buffer); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1200 | |
| 1201 | /* Make sure we have enough buffers */ |
| 1202 | available_bufs = vec_len (tm->tx_buffers[thread_index]); |
| 1203 | if (n_bufs_per_seg > available_bufs) |
| 1204 | { |
| 1205 | if (tcp_alloc_tx_buffers (tm, thread_index, |
| 1206 | VLIB_FRAME_SIZE - available_bufs)) |
| 1207 | { |
| 1208 | tcp_return_buffer (tm); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1213 | n_bytes = stream_session_peek_bytes (&tc->connection, data, offset, |
| 1214 | tm->bytes_per_buffer - |
| 1215 | MAX_HDRS_LEN); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1216 | b[0]->current_length = n_bytes; |
| 1217 | b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1218 | b[0]->total_length_not_including_first_buffer = 0; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1219 | max_deq_bytes -= n_bytes; |
| 1220 | |
| 1221 | chain_b = *b; |
| 1222 | for (i = 1; i < n_bufs_per_seg; i++) |
| 1223 | { |
| 1224 | prev_b = chain_b; |
| 1225 | len_to_deq = clib_min (max_deq_bytes, tm->bytes_per_buffer); |
| 1226 | tcp_get_free_buffer_index (tm, &chain_bi); |
| 1227 | ASSERT (chain_bi != (u32) ~ 0); |
| 1228 | chain_b = vlib_get_buffer (vm, chain_bi); |
| 1229 | chain_b->current_data = 0; |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1230 | data = vlib_buffer_get_current (chain_b); |
| 1231 | n_peeked = stream_session_peek_bytes (&tc->connection, data, |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1232 | offset + n_bytes, len_to_deq); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1233 | ASSERT (n_peeked == len_to_deq); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1234 | n_bytes += n_peeked; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1235 | chain_b->current_length = n_peeked; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1236 | chain_b->flags = 0; |
| 1237 | chain_b->next_buffer = 0; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1238 | |
| 1239 | /* update previous buffer */ |
| 1240 | prev_b->next_buffer = chain_bi; |
| 1241 | prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1242 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1243 | max_deq_bytes -= n_peeked; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1244 | b[0]->total_length_not_including_first_buffer += n_peeked; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1245 | } |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1246 | |
| 1247 | tcp_push_hdr_i (tc, *b, tc->state, 0); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1250 | ASSERT (n_bytes > 0); |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 1251 | ASSERT (((*b)->current_data + (*b)->current_length) <= |
| 1252 | tm->bytes_per_buffer); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1253 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1254 | if (tcp_in_fastrecovery (tc)) |
| 1255 | tc->snd_rxt_bytes += n_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1256 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1257 | done: |
| 1258 | TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1259 | return n_bytes; |
| 1260 | } |
| 1261 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1262 | /** |
| 1263 | * Reset congestion control, switch cwnd to loss window and try again. |
| 1264 | */ |
| 1265 | static void |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1266 | tcp_rtx_timeout_cc (tcp_connection_t * tc) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1267 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1268 | tc->prev_ssthresh = tc->ssthresh; |
| 1269 | tc->prev_cwnd = tc->cwnd; |
| 1270 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1271 | /* Cleanly recover cc (also clears up fast retransmit) */ |
| 1272 | if (tcp_in_fastrecovery (tc)) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1273 | tcp_cc_fastrecovery_exit (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1274 | |
| 1275 | /* Start again from the beginning */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1276 | tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1277 | tc->cwnd = tcp_loss_wnd (tc); |
| 1278 | tc->snd_congestion = tc->snd_una_max; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1279 | |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1280 | tcp_recovery_on (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1283 | static void |
| 1284 | tcp_timer_retransmit_handler_i (u32 index, u8 is_syn) |
| 1285 | { |
| 1286 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1287 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1288 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1289 | tcp_connection_t *tc; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1290 | vlib_buffer_t *b = 0; |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1291 | u32 bi, n_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1292 | |
| 1293 | if (is_syn) |
| 1294 | { |
| 1295 | tc = tcp_half_open_connection_get (index); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1296 | tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1297 | } |
| 1298 | else |
| 1299 | { |
| 1300 | tc = tcp_connection_get (index, thread_index); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1301 | tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1304 | if (!tcp_in_recovery (tc) && tc->rto_boff > 0 |
| 1305 | && tc->state >= TCP_STATE_ESTABLISHED) |
| 1306 | { |
| 1307 | tc->rto_boff = 0; |
| 1308 | tcp_update_rto (tc); |
| 1309 | } |
| 1310 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1311 | /* Increment RTO backoff (also equal to number of retries) */ |
| 1312 | tc->rto_boff += 1; |
| 1313 | |
| 1314 | /* Go back to first un-acked byte */ |
| 1315 | tc->snd_nxt = tc->snd_una; |
| 1316 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1317 | if (tc->state >= TCP_STATE_ESTABLISHED) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1318 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1319 | /* Lost FIN, retransmit and return */ |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1320 | if (tcp_is_lost_fin (tc)) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1321 | { |
| 1322 | tcp_send_fin (tc); |
| 1323 | return; |
| 1324 | } |
| 1325 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1326 | /* First retransmit timeout */ |
| 1327 | if (tc->rto_boff == 1) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1328 | tcp_rtx_timeout_cc (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1329 | |
| 1330 | /* Exponential backoff */ |
| 1331 | tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX); |
| 1332 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1333 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1); |
| 1334 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1335 | /* Send one segment */ |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1336 | n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b); |
| 1337 | ASSERT (n_bytes); |
| 1338 | bi = vlib_get_buffer_index (vm, b); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1339 | /* TODO be less aggressive about this */ |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1340 | scoreboard_clear (&tc->sack_sb); |
| 1341 | |
| 1342 | if (n_bytes == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1343 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1344 | clib_warning ("could not retransmit anything"); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1345 | clib_warning ("%U", format_tcp_connection, tc, 2); |
| 1346 | |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1347 | /* Try again eventually */ |
| 1348 | tcp_retransmit_timer_set (tc); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1349 | ASSERT (0 || (tc->rto_boff > 1 |
| 1350 | && tc->snd_una == tc->snd_congestion)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1351 | return; |
| 1352 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1353 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1354 | /* For first retransmit, record timestamp (Eifel detection RFC3522) */ |
| 1355 | if (tc->rto_boff == 1) |
| 1356 | tc->snd_rxt_ts = tcp_time_now (); |
| 1357 | } |
| 1358 | /* Retransmit for SYN/SYNACK */ |
| 1359 | else if (tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_SYN_SENT) |
| 1360 | { |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1361 | /* Half-open connection actually moved to established but we were |
| 1362 | * waiting for syn retransmit to pop to call cleanup from the right |
| 1363 | * thread. */ |
| 1364 | if (tc->flags & TCP_CONN_HALF_OPEN_DONE) |
| 1365 | { |
| 1366 | ASSERT (tc->state == TCP_STATE_SYN_SENT); |
| 1367 | if (tcp_half_open_connection_cleanup (tc)) |
| 1368 | { |
| 1369 | clib_warning ("could not remove half-open connection"); |
| 1370 | ASSERT (0); |
| 1371 | } |
| 1372 | return; |
| 1373 | } |
| 1374 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1375 | /* Try without increasing RTO a number of times. If this fails, |
| 1376 | * start growing RTO exponentially */ |
| 1377 | if (tc->rto_boff > TCP_RTO_SYN_RETRIES) |
| 1378 | tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX); |
| 1379 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1380 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 1381 | return; |
| 1382 | b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1383 | tcp_init_buffer (vm, b); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1384 | tcp_push_hdr_i (tc, b, tc->state, 1); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1385 | |
| 1386 | /* Account for the SYN */ |
| 1387 | tc->snd_nxt += 1; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1388 | tc->rtt_ts = 0; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 1389 | TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc, |
| 1390 | (tc->state == TCP_STATE_SYN_SENT ? 0 : 1)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1391 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1392 | else |
| 1393 | { |
| 1394 | ASSERT (tc->state == TCP_STATE_CLOSED); |
| 1395 | clib_warning ("connection closed ..."); |
| 1396 | return; |
| 1397 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1398 | |
| 1399 | if (!is_syn) |
| 1400 | { |
| 1401 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
| 1402 | |
| 1403 | /* Re-enable retransmit timer */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1404 | tcp_retransmit_timer_set (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1405 | } |
| 1406 | else |
| 1407 | { |
| 1408 | ASSERT (tc->state == TCP_STATE_SYN_SENT); |
| 1409 | |
| 1410 | /* This goes straight to ipx_lookup */ |
| 1411 | tcp_push_ip_hdr (tm, tc, b); |
| 1412 | tcp_enqueue_to_ip_lookup (vm, b, bi, tc->c_is_ip4); |
| 1413 | |
| 1414 | /* Re-enable retransmit timer */ |
| 1415 | tcp_timer_set (tc, TCP_TIMER_RETRANSMIT_SYN, |
| 1416 | tc->rto * TCP_TO_TIMER_TICK); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | void |
| 1421 | tcp_timer_retransmit_handler (u32 index) |
| 1422 | { |
| 1423 | tcp_timer_retransmit_handler_i (index, 0); |
| 1424 | } |
| 1425 | |
| 1426 | void |
| 1427 | tcp_timer_retransmit_syn_handler (u32 index) |
| 1428 | { |
| 1429 | tcp_timer_retransmit_handler_i (index, 1); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1433 | * Got 0 snd_wnd from peer, try to do something about it. |
| 1434 | * |
| 1435 | */ |
| 1436 | void |
| 1437 | tcp_timer_persist_handler (u32 index) |
| 1438 | { |
| 1439 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1440 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1441 | u32 thread_index = vlib_get_thread_index (); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1442 | tcp_connection_t *tc; |
| 1443 | vlib_buffer_t *b; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1444 | u32 bi, old_snd_nxt, max_snd_bytes, available_bytes, offset; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1445 | int n_bytes = 0; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1446 | u8 *data; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1447 | |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 1448 | tc = tcp_connection_get_if_valid (index, thread_index); |
| 1449 | |
| 1450 | if (!tc) |
| 1451 | return; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1452 | |
| 1453 | /* Make sure timer handle is set to invalid */ |
| 1454 | tc->timers[TCP_TIMER_PERSIST] = TCP_TIMER_HANDLE_INVALID; |
| 1455 | |
| 1456 | /* Problem already solved or worse */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1457 | if (tc->state == TCP_STATE_CLOSED || tc->state > TCP_STATE_ESTABLISHED |
Florin Coras | 5095895 | 2017-08-29 14:50:13 -0700 | [diff] [blame] | 1458 | || tc->snd_wnd > tc->snd_mss || tcp_in_recovery (tc)) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1459 | return; |
| 1460 | |
Florin Coras | 5095895 | 2017-08-29 14:50:13 -0700 | [diff] [blame] | 1461 | available_bytes = stream_session_tx_fifo_max_dequeue (&tc->connection); |
| 1462 | offset = tc->snd_una_max - tc->snd_una; |
| 1463 | |
| 1464 | /* Reprogram persist if no new bytes available to send. We may have data |
| 1465 | * next time */ |
| 1466 | if (!available_bytes) |
| 1467 | { |
| 1468 | tcp_persist_timer_set (tc); |
| 1469 | return; |
| 1470 | } |
| 1471 | |
| 1472 | if (available_bytes <= offset) |
| 1473 | { |
| 1474 | ASSERT (tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT)); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1478 | /* Increment RTO backoff */ |
| 1479 | tc->rto_boff += 1; |
| 1480 | tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX); |
| 1481 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1482 | /* |
| 1483 | * Try to force the first unsent segment (or buffer) |
| 1484 | */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1485 | if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi))) |
| 1486 | return; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1487 | b = vlib_get_buffer (vm, bi); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1488 | data = tcp_init_buffer (vm, b); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1489 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1490 | tcp_validate_txf_size (tc, offset); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1491 | tc->snd_opts_len = tcp_make_options (tc, &tc->snd_opts, tc->state); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1492 | max_snd_bytes = clib_min (tc->snd_mss, tm->bytes_per_buffer - MAX_HDRS_LEN); |
| 1493 | n_bytes = stream_session_peek_bytes (&tc->connection, data, offset, |
| 1494 | max_snd_bytes); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1495 | b->current_length = n_bytes; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1496 | ASSERT (n_bytes != 0 && (tc->snd_nxt == tc->snd_una_max || tc->rto_boff > 1 |
| 1497 | || tcp_timer_is_active (tc, |
| 1498 | TCP_TIMER_RETRANSMIT))); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1499 | |
| 1500 | /* Allow updating of snd_una_max but don't update snd_nxt */ |
| 1501 | old_snd_nxt = tc->snd_nxt; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1502 | tcp_push_hdr_i (tc, b, tc->state, 0); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1503 | tc->snd_nxt = old_snd_nxt; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1504 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
| 1505 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1506 | /* Just sent new data, enable retransmit */ |
| 1507 | tcp_retransmit_timer_update (tc); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /** |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1511 | * Retransmit first unacked segment |
| 1512 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1513 | void |
| 1514 | tcp_retransmit_first_unacked (tcp_connection_t * tc) |
| 1515 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1516 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1517 | vlib_buffer_t *b; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1518 | u32 bi, old_snd_nxt, n_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1519 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1520 | old_snd_nxt = tc->snd_nxt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1521 | tc->snd_nxt = tc->snd_una; |
| 1522 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1523 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1524 | n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b); |
| 1525 | if (!n_bytes) |
| 1526 | return; |
| 1527 | bi = vlib_get_buffer_index (vm, b); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1528 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
Florin Coras | 5921f98 | 2017-04-03 18:00:00 -0700 | [diff] [blame] | 1529 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1530 | tc->snd_nxt = old_snd_nxt; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /** |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1534 | * Do fast retransmit with SACKs |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1535 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1536 | void |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1537 | tcp_fast_retransmit_sack (tcp_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1538 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1539 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1540 | u32 n_written = 0, offset, max_bytes; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1541 | vlib_buffer_t *b = 0; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1542 | sack_scoreboard_hole_t *hole; |
| 1543 | sack_scoreboard_t *sb; |
| 1544 | u32 bi, old_snd_nxt; |
| 1545 | int snd_space; |
| 1546 | u8 snd_limited = 0, can_rescue = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1547 | |
| 1548 | ASSERT (tcp_in_fastrecovery (tc)); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1549 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1550 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1551 | old_snd_nxt = tc->snd_nxt; |
| 1552 | sb = &tc->sack_sb; |
| 1553 | snd_space = tcp_available_snd_space (tc); |
| 1554 | |
| 1555 | hole = scoreboard_get_hole (sb, sb->cur_rxt_hole); |
| 1556 | while (hole && snd_space > 0) |
| 1557 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1558 | hole = scoreboard_next_rxt_hole (sb, hole, |
| 1559 | tcp_fastrecovery_sent_1_smss (tc), |
| 1560 | &can_rescue, &snd_limited); |
| 1561 | if (!hole) |
| 1562 | { |
| 1563 | if (!can_rescue || !(seq_lt (sb->rescue_rxt, tc->snd_una) |
| 1564 | || seq_gt (sb->rescue_rxt, |
| 1565 | tc->snd_congestion))) |
| 1566 | break; |
| 1567 | |
| 1568 | /* If rescue rxt undefined or less than snd_una then one segment of |
| 1569 | * up to SMSS octets that MUST include the highest outstanding |
| 1570 | * unSACKed sequence number SHOULD be returned, and RescueRxt set to |
| 1571 | * RecoveryPoint. HighRxt MUST NOT be updated. |
| 1572 | */ |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1573 | max_bytes = clib_min (tc->snd_mss, |
| 1574 | tc->snd_congestion - tc->snd_una); |
| 1575 | max_bytes = clib_min (max_bytes, snd_space); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1576 | offset = tc->snd_congestion - tc->snd_una - max_bytes; |
| 1577 | sb->rescue_rxt = tc->snd_congestion; |
| 1578 | tc->snd_nxt = tc->snd_una + offset; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1579 | n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes, |
| 1580 | &b); |
| 1581 | ASSERT (n_written); |
| 1582 | bi = vlib_get_buffer_index (vm, b); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1583 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
| 1584 | break; |
| 1585 | } |
| 1586 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1587 | max_bytes = clib_min (hole->end - sb->high_rxt, snd_space); |
| 1588 | max_bytes = snd_limited ? clib_min (max_bytes, tc->snd_mss) : max_bytes; |
| 1589 | if (max_bytes == 0) |
| 1590 | break; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1591 | offset = sb->high_rxt - tc->snd_una; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1592 | tc->snd_nxt = sb->high_rxt; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1593 | n_written = tcp_prepare_retransmit_segment (tc, offset, max_bytes, &b); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1594 | |
| 1595 | /* Nothing left to retransmit */ |
| 1596 | if (n_written == 0) |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1597 | break; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1598 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1599 | bi = vlib_get_buffer_index (vm, b); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1600 | sb->high_rxt += n_written; |
| 1601 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1602 | ASSERT (n_written <= snd_space); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1603 | snd_space -= n_written; |
| 1604 | } |
| 1605 | |
| 1606 | /* If window allows, send 1 SMSS of new data */ |
| 1607 | tc->snd_nxt = old_snd_nxt; |
| 1608 | } |
| 1609 | |
| 1610 | /** |
| 1611 | * Fast retransmit without SACK info |
| 1612 | */ |
| 1613 | void |
| 1614 | tcp_fast_retransmit_no_sack (tcp_connection_t * tc) |
| 1615 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1616 | vlib_main_t *vm = vlib_get_main (); |
| 1617 | u32 n_written = 0, offset = 0, bi, old_snd_nxt; |
| 1618 | int snd_space; |
| 1619 | vlib_buffer_t *b; |
| 1620 | |
| 1621 | ASSERT (tcp_in_fastrecovery (tc)); |
| 1622 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0); |
| 1623 | |
| 1624 | /* Start resending from first un-acked segment */ |
| 1625 | old_snd_nxt = tc->snd_nxt; |
| 1626 | tc->snd_nxt = tc->snd_una; |
| 1627 | snd_space = tcp_available_snd_space (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1628 | |
| 1629 | while (snd_space > 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1630 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1631 | offset += n_written; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1632 | n_written = tcp_prepare_retransmit_segment (tc, offset, snd_space, &b); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1633 | |
| 1634 | /* Nothing left to retransmit */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1635 | if (n_written == 0) |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1636 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1637 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1638 | bi = vlib_get_buffer_index (vm, b); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1639 | tcp_enqueue_to_output (vm, b, bi, tc->c_is_ip4); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1640 | snd_space -= n_written; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1641 | } |
| 1642 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1643 | /* Restore snd_nxt. If window allows, send 1 SMSS of new data */ |
| 1644 | tc->snd_nxt = old_snd_nxt; |
| 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * Do fast retransmit |
| 1649 | */ |
| 1650 | void |
| 1651 | tcp_fast_retransmit (tcp_connection_t * tc) |
| 1652 | { |
| 1653 | if (tcp_opts_sack_permitted (&tc->rcv_opts) |
| 1654 | && scoreboard_first_hole (&tc->sack_sb)) |
| 1655 | tcp_fast_retransmit_sack (tc); |
| 1656 | else |
| 1657 | tcp_fast_retransmit_no_sack (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | always_inline u32 |
| 1661 | tcp_session_has_ooo_data (tcp_connection_t * tc) |
| 1662 | { |
| 1663 | stream_session_t *s = |
| 1664 | stream_session_get (tc->c_s_index, tc->c_thread_index); |
| 1665 | return svm_fifo_has_ooo_data (s->server_rx_fifo); |
| 1666 | } |
| 1667 | |
| 1668 | always_inline uword |
| 1669 | tcp46_output_inline (vlib_main_t * vm, |
| 1670 | vlib_node_runtime_t * node, |
| 1671 | vlib_frame_t * from_frame, int is_ip4) |
| 1672 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1673 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1674 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1675 | |
| 1676 | from = vlib_frame_vector_args (from_frame); |
| 1677 | n_left_from = from_frame->n_vectors; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1678 | next_index = node->cached_next_index; |
Florin Coras | 82d3ec8 | 2017-08-14 08:10:42 -0700 | [diff] [blame] | 1679 | tcp_set_time_now (my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1680 | |
| 1681 | while (n_left_from > 0) |
| 1682 | { |
| 1683 | u32 n_left_to_next; |
| 1684 | |
| 1685 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1686 | |
| 1687 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1688 | { |
| 1689 | u32 bi0; |
| 1690 | vlib_buffer_t *b0; |
| 1691 | tcp_connection_t *tc0; |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1692 | tcp_tx_trace_t *t0; |
| 1693 | tcp_header_t *th0 = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1694 | u32 error0 = TCP_ERROR_PKTS_SENT, next0 = TCP_OUTPUT_NEXT_IP_LOOKUP; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1695 | |
| 1696 | bi0 = from[0]; |
| 1697 | to_next[0] = bi0; |
| 1698 | from += 1; |
| 1699 | to_next += 1; |
| 1700 | n_left_from -= 1; |
| 1701 | n_left_to_next -= 1; |
| 1702 | |
| 1703 | b0 = vlib_get_buffer (vm, bi0); |
| 1704 | tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index, |
| 1705 | my_thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1706 | if (PREDICT_FALSE (tc0 == 0 || tc0->state == TCP_STATE_CLOSED)) |
| 1707 | { |
| 1708 | error0 = TCP_ERROR_INVALID_CONNECTION; |
| 1709 | next0 = TCP_OUTPUT_NEXT_DROP; |
| 1710 | goto done; |
| 1711 | } |
| 1712 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1713 | th0 = vlib_buffer_get_current (b0); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1714 | TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1715 | |
| 1716 | if (is_ip4) |
| 1717 | { |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1718 | vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4, |
| 1719 | IP_PROTOCOL_TCP, 1); |
| 1720 | b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; |
Dave Barach | 2c0a4f4 | 2017-06-29 09:30:15 -0400 | [diff] [blame] | 1721 | vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data; |
| 1722 | th0->checksum = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1723 | } |
| 1724 | else |
| 1725 | { |
| 1726 | ip6_header_t *ih0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1727 | ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6, |
| 1728 | &tc0->c_rmt_ip6, IP_PROTOCOL_TCP); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1729 | b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; |
Dave Barach | 2c0a4f4 | 2017-06-29 09:30:15 -0400 | [diff] [blame] | 1730 | vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data; |
| 1731 | vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data; |
| 1732 | th0->checksum = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /* Filter out DUPACKs if there are no OOO segments left */ |
| 1736 | if (PREDICT_FALSE |
| 1737 | (vnet_buffer (b0)->tcp.flags & TCP_BUF_FLAG_DUPACK)) |
| 1738 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1739 | if (!tcp_session_has_ooo_data (tc0)) |
| 1740 | { |
| 1741 | error0 = TCP_ERROR_FILTERED_DUPACKS; |
| 1742 | next0 = TCP_OUTPUT_NEXT_DROP; |
| 1743 | goto done; |
| 1744 | } |
| 1745 | } |
| 1746 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1747 | /* Stop DELACK timer and fix flags */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1748 | tc0->flags &= ~(TCP_CONN_SNDACK); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1749 | tcp_timer_reset (tc0, TCP_TIMER_DELACK); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1750 | |
| 1751 | /* If not retransmitting |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1752 | * 1) update snd_una_max (SYN, SYNACK, FIN) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1753 | * 2) If we're not tracking an ACK, start tracking */ |
| 1754 | if (seq_lt (tc0->snd_una_max, tc0->snd_nxt)) |
| 1755 | { |
| 1756 | tc0->snd_una_max = tc0->snd_nxt; |
| 1757 | if (tc0->rtt_ts == 0) |
| 1758 | { |
| 1759 | tc0->rtt_ts = tcp_time_now (); |
| 1760 | tc0->rtt_seq = tc0->snd_nxt; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | /* Set the retransmit timer if not set already and not |
| 1765 | * doing a pure ACK */ |
| 1766 | if (!tcp_timer_is_active (tc0, TCP_TIMER_RETRANSMIT) |
| 1767 | && tc0->snd_nxt != tc0->snd_una) |
| 1768 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1769 | tcp_retransmit_timer_set (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1770 | tc0->rto_boff = 0; |
| 1771 | } |
| 1772 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1773 | #if 0 |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 1774 | /* Make sure we haven't lost route to our peer */ |
| 1775 | if (PREDICT_FALSE (tc0->last_fib_check |
| 1776 | < tc0->snd_opts.tsval + TCP_FIB_RECHECK_PERIOD)) |
| 1777 | { |
| 1778 | if (PREDICT_TRUE |
| 1779 | (tc0->c_rmt_fei == tcp_lookup_rmt_in_fib (tc0))) |
| 1780 | { |
| 1781 | tc0->last_fib_check = tc0->snd_opts.tsval; |
| 1782 | } |
| 1783 | else |
| 1784 | { |
| 1785 | clib_warning ("lost connection to peer"); |
| 1786 | tcp_connection_reset (tc0); |
| 1787 | goto done; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /* Use pre-computed dpo to set next node */ |
| 1792 | next0 = tc0->c_rmt_dpo.dpoi_next_node; |
| 1793 | vnet_buffer (b0)->ip.adj_index[VLIB_TX] = tc0->c_rmt_dpo.dpoi_index; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1794 | #endif |
| 1795 | |
| 1796 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
| 1797 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1798 | |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1799 | b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1800 | done: |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1801 | b0->error = node->errors[error0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1802 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1803 | { |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1804 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 1805 | if (th0) |
| 1806 | { |
| 1807 | clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header)); |
| 1808 | } |
| 1809 | else |
| 1810 | { |
| 1811 | memset (&t0->tcp_header, 0, sizeof (t0->tcp_header)); |
| 1812 | } |
| 1813 | clib_memcpy (&t0->tcp_connection, tc0, |
| 1814 | sizeof (t0->tcp_connection)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1818 | n_left_to_next, bi0, next0); |
| 1819 | } |
| 1820 | |
| 1821 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1822 | } |
| 1823 | |
| 1824 | return from_frame->n_vectors; |
| 1825 | } |
| 1826 | |
| 1827 | static uword |
| 1828 | tcp4_output (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1829 | vlib_frame_t * from_frame) |
| 1830 | { |
| 1831 | return tcp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 1832 | } |
| 1833 | |
| 1834 | static uword |
| 1835 | tcp6_output (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1836 | vlib_frame_t * from_frame) |
| 1837 | { |
| 1838 | return tcp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 1839 | } |
| 1840 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1841 | /* *INDENT-OFF* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1842 | VLIB_REGISTER_NODE (tcp4_output_node) = |
| 1843 | { |
| 1844 | .function = tcp4_output,.name = "tcp4-output", |
| 1845 | /* Takes a vector of packets. */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1846 | .vector_size = sizeof (u32), |
| 1847 | .n_errors = TCP_N_ERROR, |
| 1848 | .error_strings = tcp_error_strings, |
| 1849 | .n_next_nodes = TCP_OUTPUT_N_NEXT, |
| 1850 | .next_nodes = { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1851 | #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n, |
| 1852 | foreach_tcp4_output_next |
| 1853 | #undef _ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1854 | }, |
| 1855 | .format_buffer = format_tcp_header, |
| 1856 | .format_trace = format_tcp_tx_trace, |
| 1857 | }; |
| 1858 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1859 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1860 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_output_node, tcp4_output); |
| 1861 | |
| 1862 | /* *INDENT-OFF* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1863 | VLIB_REGISTER_NODE (tcp6_output_node) = |
| 1864 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1865 | .function = tcp6_output, |
| 1866 | .name = "tcp6-output", |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1867 | /* Takes a vector of packets. */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1868 | .vector_size = sizeof (u32), |
| 1869 | .n_errors = TCP_N_ERROR, |
| 1870 | .error_strings = tcp_error_strings, |
| 1871 | .n_next_nodes = TCP_OUTPUT_N_NEXT, |
| 1872 | .next_nodes = { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1873 | #define _(s,n) [TCP_OUTPUT_NEXT_##s] = n, |
| 1874 | foreach_tcp6_output_next |
| 1875 | #undef _ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1876 | }, |
| 1877 | .format_buffer = format_tcp_header, |
| 1878 | .format_trace = format_tcp_tx_trace, |
| 1879 | }; |
| 1880 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1881 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1882 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_output_node, tcp6_output); |
| 1883 | |
| 1884 | u32 |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1885 | tcp_push_header (transport_connection_t * tconn, vlib_buffer_t * b) |
| 1886 | { |
| 1887 | tcp_connection_t *tc; |
| 1888 | |
| 1889 | tc = (tcp_connection_t *) tconn; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1890 | tcp_push_hdr_i (tc, b, TCP_STATE_ESTABLISHED, 0); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1891 | ASSERT (seq_leq (tc->snd_una_max, tc->snd_una + tc->snd_wnd)); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1892 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1893 | if (tc->rtt_ts == 0 && !tcp_in_cong_recovery (tc)) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1894 | { |
| 1895 | tc->rtt_ts = tcp_time_now (); |
| 1896 | tc->rtt_seq = tc->snd_nxt; |
| 1897 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | typedef enum _tcp_reset_next |
| 1902 | { |
| 1903 | TCP_RESET_NEXT_DROP, |
| 1904 | TCP_RESET_NEXT_IP_LOOKUP, |
| 1905 | TCP_RESET_N_NEXT |
| 1906 | } tcp_reset_next_t; |
| 1907 | |
| 1908 | #define foreach_tcp4_reset_next \ |
| 1909 | _(DROP, "error-drop") \ |
| 1910 | _(IP_LOOKUP, "ip4-lookup") |
| 1911 | |
| 1912 | #define foreach_tcp6_reset_next \ |
| 1913 | _(DROP, "error-drop") \ |
| 1914 | _(IP_LOOKUP, "ip6-lookup") |
| 1915 | |
| 1916 | static uword |
| 1917 | tcp46_send_reset_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1918 | vlib_frame_t * from_frame, u8 is_ip4) |
| 1919 | { |
| 1920 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1921 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1922 | |
| 1923 | from = vlib_frame_vector_args (from_frame); |
| 1924 | n_left_from = from_frame->n_vectors; |
| 1925 | |
| 1926 | next_index = node->cached_next_index; |
| 1927 | |
| 1928 | while (n_left_from > 0) |
| 1929 | { |
| 1930 | u32 n_left_to_next; |
| 1931 | |
| 1932 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1933 | |
| 1934 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1935 | { |
| 1936 | u32 bi0; |
| 1937 | vlib_buffer_t *b0; |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1938 | tcp_tx_trace_t *t0; |
| 1939 | tcp_header_t *th0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1940 | u32 error0 = TCP_ERROR_RST_SENT, next0 = TCP_RESET_NEXT_IP_LOOKUP; |
| 1941 | |
| 1942 | bi0 = from[0]; |
| 1943 | to_next[0] = bi0; |
| 1944 | from += 1; |
| 1945 | to_next += 1; |
| 1946 | n_left_from -= 1; |
| 1947 | n_left_to_next -= 1; |
| 1948 | |
| 1949 | b0 = vlib_get_buffer (vm, bi0); |
| 1950 | |
| 1951 | if (tcp_make_reset_in_place (vm, b0, vnet_buffer (b0)->tcp.flags, |
| 1952 | my_thread_index, is_ip4)) |
| 1953 | { |
| 1954 | error0 = TCP_ERROR_LOOKUP_DROPS; |
| 1955 | next0 = TCP_RESET_NEXT_DROP; |
| 1956 | goto done; |
| 1957 | } |
| 1958 | |
| 1959 | /* Prepare to send to IP lookup */ |
| 1960 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0; |
| 1961 | next0 = TCP_RESET_NEXT_IP_LOOKUP; |
| 1962 | |
| 1963 | done: |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1964 | b0->error = node->errors[error0]; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1965 | b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1966 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1967 | { |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1968 | th0 = vlib_buffer_get_current (b0); |
| 1969 | if (is_ip4) |
| 1970 | th0 = ip4_next_header ((ip4_header_t *) th0); |
| 1971 | else |
| 1972 | th0 = ip6_next_header ((ip6_header_t *) th0); |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1973 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 1974 | clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1978 | n_left_to_next, bi0, next0); |
| 1979 | } |
| 1980 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1981 | } |
| 1982 | return from_frame->n_vectors; |
| 1983 | } |
| 1984 | |
| 1985 | static uword |
| 1986 | tcp4_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1987 | vlib_frame_t * from_frame) |
| 1988 | { |
| 1989 | return tcp46_send_reset_inline (vm, node, from_frame, 1); |
| 1990 | } |
| 1991 | |
| 1992 | static uword |
| 1993 | tcp6_send_reset (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1994 | vlib_frame_t * from_frame) |
| 1995 | { |
| 1996 | return tcp46_send_reset_inline (vm, node, from_frame, 0); |
| 1997 | } |
| 1998 | |
| 1999 | /* *INDENT-OFF* */ |
| 2000 | VLIB_REGISTER_NODE (tcp4_reset_node) = { |
| 2001 | .function = tcp4_send_reset, |
| 2002 | .name = "tcp4-reset", |
| 2003 | .vector_size = sizeof (u32), |
| 2004 | .n_errors = TCP_N_ERROR, |
| 2005 | .error_strings = tcp_error_strings, |
| 2006 | .n_next_nodes = TCP_RESET_N_NEXT, |
| 2007 | .next_nodes = { |
| 2008 | #define _(s,n) [TCP_RESET_NEXT_##s] = n, |
| 2009 | foreach_tcp4_reset_next |
| 2010 | #undef _ |
| 2011 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2012 | .format_trace = format_tcp_tx_trace, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2013 | }; |
| 2014 | /* *INDENT-ON* */ |
| 2015 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 2016 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_reset_node, tcp4_send_reset); |
| 2017 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2018 | /* *INDENT-OFF* */ |
| 2019 | VLIB_REGISTER_NODE (tcp6_reset_node) = { |
| 2020 | .function = tcp6_send_reset, |
| 2021 | .name = "tcp6-reset", |
| 2022 | .vector_size = sizeof (u32), |
| 2023 | .n_errors = TCP_N_ERROR, |
| 2024 | .error_strings = tcp_error_strings, |
| 2025 | .n_next_nodes = TCP_RESET_N_NEXT, |
| 2026 | .next_nodes = { |
| 2027 | #define _(s,n) [TCP_RESET_NEXT_##s] = n, |
| 2028 | foreach_tcp6_reset_next |
| 2029 | #undef _ |
| 2030 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2031 | .format_trace = format_tcp_tx_trace, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2032 | }; |
| 2033 | /* *INDENT-ON* */ |
| 2034 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 2035 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_reset_node, tcp6_send_reset); |
| 2036 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2037 | /* |
| 2038 | * fd.io coding-style-patch-verification: ON |
| 2039 | * |
| 2040 | * Local Variables: |
| 2041 | * eval: (c-set-style "gnu") |
| 2042 | * End: |
| 2043 | */ |