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 <vppinfra/sparse_vec.h> |
| 17 | #include <vnet/tcp/tcp_packet.h> |
| 18 | #include <vnet/tcp/tcp.h> |
| 19 | #include <vnet/session/session.h> |
| 20 | #include <math.h> |
| 21 | |
| 22 | static char *tcp_error_strings[] = { |
| 23 | #define tcp_error(n,s) s, |
| 24 | #include <vnet/tcp/tcp_error.def> |
| 25 | #undef tcp_error |
| 26 | }; |
| 27 | |
| 28 | /* All TCP nodes have the same outgoing arcs */ |
| 29 | #define foreach_tcp_state_next \ |
| 30 | _ (DROP, "error-drop") \ |
| 31 | _ (TCP4_OUTPUT, "tcp4-output") \ |
| 32 | _ (TCP6_OUTPUT, "tcp6-output") |
| 33 | |
| 34 | typedef enum _tcp_established_next |
| 35 | { |
| 36 | #define _(s,n) TCP_ESTABLISHED_NEXT_##s, |
| 37 | foreach_tcp_state_next |
| 38 | #undef _ |
| 39 | TCP_ESTABLISHED_N_NEXT, |
| 40 | } tcp_established_next_t; |
| 41 | |
| 42 | typedef enum _tcp_rcv_process_next |
| 43 | { |
| 44 | #define _(s,n) TCP_RCV_PROCESS_NEXT_##s, |
| 45 | foreach_tcp_state_next |
| 46 | #undef _ |
| 47 | TCP_RCV_PROCESS_N_NEXT, |
| 48 | } tcp_rcv_process_next_t; |
| 49 | |
| 50 | typedef enum _tcp_syn_sent_next |
| 51 | { |
| 52 | #define _(s,n) TCP_SYN_SENT_NEXT_##s, |
| 53 | foreach_tcp_state_next |
| 54 | #undef _ |
| 55 | TCP_SYN_SENT_N_NEXT, |
| 56 | } tcp_syn_sent_next_t; |
| 57 | |
| 58 | typedef enum _tcp_listen_next |
| 59 | { |
| 60 | #define _(s,n) TCP_LISTEN_NEXT_##s, |
| 61 | foreach_tcp_state_next |
| 62 | #undef _ |
| 63 | TCP_LISTEN_N_NEXT, |
| 64 | } tcp_listen_next_t; |
| 65 | |
| 66 | /* Generic, state independent indices */ |
| 67 | typedef enum _tcp_state_next |
| 68 | { |
| 69 | #define _(s,n) TCP_NEXT_##s, |
| 70 | foreach_tcp_state_next |
| 71 | #undef _ |
| 72 | TCP_STATE_N_NEXT, |
| 73 | } tcp_state_next_t; |
| 74 | |
| 75 | #define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \ |
| 76 | : TCP_NEXT_TCP6_OUTPUT) |
| 77 | |
| 78 | vlib_node_registration_t tcp4_established_node; |
| 79 | vlib_node_registration_t tcp6_established_node; |
| 80 | |
| 81 | /** |
| 82 | * Validate segment sequence number. As per RFC793: |
| 83 | * |
| 84 | * Segment Receive Test |
| 85 | * Length Window |
| 86 | * ------- ------- ------------------------------------------- |
| 87 | * 0 0 SEG.SEQ = RCV.NXT |
| 88 | * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND |
| 89 | * >0 0 not acceptable |
| 90 | * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND |
| 91 | * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND |
| 92 | * |
| 93 | * This ultimately consists in checking if segment falls within the window. |
| 94 | * The one important difference compared to RFC793 is that we use rcv_las, |
| 95 | * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the |
| 96 | * peer's reference when computing our receive window. |
| 97 | * |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 98 | * This: |
| 99 | * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las) |
| 100 | * however, is too strict when we have retransmits. Instead we just check that |
| 101 | * the seq is not beyond the right edge and that the end of the segment is not |
| 102 | * less than the left edge. |
| 103 | * |
| 104 | * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so |
| 105 | * use rcv_nxt in the right edge window test instead of rcv_las. |
| 106 | * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 107 | */ |
| 108 | always_inline u8 |
| 109 | tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq) |
| 110 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 111 | return (seq_geq (end_seq, tc->rcv_las) |
| 112 | && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
| 116 | tcp_options_parse (tcp_header_t * th, tcp_options_t * to) |
| 117 | { |
| 118 | const u8 *data; |
| 119 | u8 opt_len, opts_len, kind; |
| 120 | int j; |
| 121 | sack_block_t b; |
| 122 | |
| 123 | opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t); |
| 124 | data = (const u8 *) (th + 1); |
| 125 | |
| 126 | /* Zero out all flags but those set in SYN */ |
| 127 | to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE); |
| 128 | |
| 129 | for (; opts_len > 0; opts_len -= opt_len, data += opt_len) |
| 130 | { |
| 131 | kind = data[0]; |
| 132 | |
| 133 | /* Get options length */ |
| 134 | if (kind == TCP_OPTION_EOL) |
| 135 | break; |
| 136 | else if (kind == TCP_OPTION_NOOP) |
| 137 | opt_len = 1; |
| 138 | else |
| 139 | { |
| 140 | /* broken options */ |
| 141 | if (opts_len < 2) |
| 142 | break; |
| 143 | opt_len = data[1]; |
| 144 | |
| 145 | /* weird option length */ |
| 146 | if (opt_len < 2 || opt_len > opts_len) |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | /* Parse options */ |
| 151 | switch (kind) |
| 152 | { |
| 153 | case TCP_OPTION_MSS: |
| 154 | if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th)) |
| 155 | { |
| 156 | to->flags |= TCP_OPTS_FLAG_MSS; |
| 157 | to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2)); |
| 158 | } |
| 159 | break; |
| 160 | case TCP_OPTION_WINDOW_SCALE: |
| 161 | if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th)) |
| 162 | { |
| 163 | to->flags |= TCP_OPTS_FLAG_WSCALE; |
| 164 | to->wscale = data[2]; |
| 165 | if (to->wscale > TCP_MAX_WND_SCALE) |
| 166 | { |
| 167 | clib_warning ("Illegal window scaling value: %d", |
| 168 | to->wscale); |
| 169 | to->wscale = TCP_MAX_WND_SCALE; |
| 170 | } |
| 171 | } |
| 172 | break; |
| 173 | case TCP_OPTION_TIMESTAMP: |
| 174 | if (opt_len == TCP_OPTION_LEN_TIMESTAMP) |
| 175 | { |
| 176 | to->flags |= TCP_OPTS_FLAG_TSTAMP; |
| 177 | to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2)); |
| 178 | to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6)); |
| 179 | } |
| 180 | break; |
| 181 | case TCP_OPTION_SACK_PERMITTED: |
| 182 | if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th)) |
| 183 | to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED; |
| 184 | break; |
| 185 | case TCP_OPTION_SACK_BLOCK: |
| 186 | /* If SACK permitted was not advertised or a SYN, break */ |
| 187 | if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th)) |
| 188 | break; |
| 189 | |
| 190 | /* If too short or not correctly formatted, break */ |
| 191 | if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK)) |
| 192 | break; |
| 193 | |
| 194 | to->flags |= TCP_OPTS_FLAG_SACK; |
| 195 | to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK; |
| 196 | vec_reset_length (to->sacks); |
| 197 | for (j = 0; j < to->n_sack_blocks; j++) |
| 198 | { |
| 199 | b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j)); |
| 200 | b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j)); |
| 201 | vec_add1 (to->sacks, b); |
| 202 | } |
| 203 | break; |
| 204 | default: |
| 205 | /* Nothing to see here */ |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 211 | /** |
| 212 | * RFC1323: Check against wrapped sequence numbers (PAWS). If we have |
| 213 | * timestamp to echo and it's less than tsval_recent, drop segment |
| 214 | * but still send an ACK in order to retain TCP's mechanism for detecting |
| 215 | * and recovering from half-open connections |
| 216 | * |
| 217 | * Or at least that's what the theory says. It seems that this might not work |
| 218 | * very well with packet reordering and fast retransmit. XXX |
| 219 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 220 | always_inline int |
| 221 | tcp_segment_check_paws (tcp_connection_t * tc) |
| 222 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 223 | return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent |
| 224 | && timestamp_lt (tc->opt.tsval, tc->tsval_recent); |
| 225 | } |
| 226 | |
| 227 | /** |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 228 | * Update tsval recent |
| 229 | */ |
| 230 | always_inline void |
| 231 | tcp_update_timestamp (tcp_connection_t * tc, u32 seq, u32 seq_end) |
| 232 | { |
| 233 | /* |
| 234 | * RFC1323: If Last.ACK.sent falls within the range of sequence numbers |
| 235 | * of an incoming segment: |
| 236 | * SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN |
| 237 | * then the TSval from the segment is copied to TS.Recent; |
| 238 | * otherwise, the TSval is ignored. |
| 239 | */ |
| 240 | if (tcp_opts_tstamp (&tc->opt) && tc->tsval_recent |
| 241 | && seq_leq (seq, tc->rcv_las) && seq_leq (tc->rcv_las, seq_end)) |
| 242 | { |
| 243 | tc->tsval_recent = tc->opt.tsval; |
| 244 | tc->tsval_recent_age = tcp_time_now (); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 249 | * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19 |
| 250 | * |
| 251 | * It first verifies if segment has a wrapped sequence number (PAWS) and then |
| 252 | * does the processing associated to the first four steps (ignoring security |
| 253 | * and precedence): sequence number, rst bit and syn bit checks. |
| 254 | * |
| 255 | * @return 0 if segments passes validation. |
| 256 | */ |
| 257 | static int |
| 258 | tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0, |
| 259 | vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0) |
| 260 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 261 | if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0))) |
| 262 | return -1; |
| 263 | |
| 264 | tcp_options_parse (th0, &tc0->opt); |
| 265 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 266 | if (tcp_segment_check_paws (tc0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 267 | { |
| 268 | clib_warning ("paws failed"); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 269 | TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number, |
| 270 | vnet_buffer (b0)->tcp.seq_end); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 271 | |
| 272 | /* If it just so happens that a segment updates tsval_recent for a |
| 273 | * segment over 24 days old, invalidate tsval_recent. */ |
| 274 | if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE, |
| 275 | tcp_time_now ())) |
| 276 | { |
| 277 | /* Age isn't reset until we get a valid tsval (bsd inspired) */ |
| 278 | tc0->tsval_recent = 0; |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 279 | clib_warning ("paws failed - really old segment. REALLY?"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
| 283 | /* Drop after ack if not rst */ |
| 284 | if (!tcp_rst (th0)) |
| 285 | { |
| 286 | tcp_make_ack (tc0, b0); |
| 287 | *next0 = tcp_next_output (tc0->c_is_ip4); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 288 | TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 289 | return -1; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* 1st: check sequence number */ |
| 295 | if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number, |
| 296 | vnet_buffer (b0)->tcp.seq_end)) |
| 297 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 298 | /* If our window is 0 and the packet is in sequence, let it pass |
| 299 | * through for ack processing. It should be dropped later.*/ |
| 300 | if (tc0->rcv_wnd == 0 |
| 301 | && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 302 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 303 | /* TODO Should segment be tagged? */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 304 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 305 | else |
| 306 | { |
| 307 | /* If not RST, send dup ack */ |
| 308 | if (!tcp_rst (th0)) |
| 309 | { |
| 310 | tcp_make_ack (tc0, b0); |
| 311 | *next0 = tcp_next_output (tc0->c_is_ip4); |
| 312 | TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0); |
| 313 | } |
| 314 | return -1; |
| 315 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* 2nd: check the RST bit */ |
| 319 | if (tcp_rst (th0)) |
| 320 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 321 | tcp_connection_reset (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | /* 3rd: check security and precedence (skip) */ |
| 326 | |
| 327 | /* 4th: check the SYN bit */ |
| 328 | if (tcp_syn (th0)) |
| 329 | { |
| 330 | tcp_send_reset (b0, tc0->c_is_ip4); |
| 331 | return -1; |
| 332 | } |
| 333 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 334 | /* If segment in window, save timestamp */ |
| 335 | tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number, |
| 336 | vnet_buffer (b0)->tcp.seq_end); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | always_inline int |
| 342 | tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0) |
| 343 | { |
| 344 | /* SND.UNA =< SEG.ACK =< SND.NXT */ |
| 345 | return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number) |
| 346 | && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt)); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298 |
| 351 | * |
| 352 | * Note that although the original article, srtt and rttvar are scaled |
| 353 | * to minimize round-off errors, here we don't. Instead, we rely on |
| 354 | * better precision time measurements. |
| 355 | * |
| 356 | * TODO support us rtt resolution |
| 357 | */ |
| 358 | static void |
| 359 | tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt) |
| 360 | { |
| 361 | int err; |
| 362 | |
| 363 | if (tc->srtt != 0) |
| 364 | { |
| 365 | err = mrtt - tc->srtt; |
| 366 | tc->srtt += err >> 3; |
| 367 | |
| 368 | /* XXX Drop in RTT results in RTTVAR increase and bigger RTO. |
| 369 | * The increase should be bound */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 370 | tc->rttvar += ((int) clib_abs (err) - (int) tc->rttvar) >> 2; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 371 | } |
| 372 | else |
| 373 | { |
| 374 | /* First measurement. */ |
| 375 | tc->srtt = mrtt; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 376 | tc->rttvar = mrtt >> 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | /** Update RTT estimate and RTO timer |
| 381 | * |
| 382 | * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK |
| 383 | * timing. Middle boxes are known to fiddle with TCP options so we |
| 384 | * should give higher priority to ACK timing. |
| 385 | * |
| 386 | * return 1 if valid rtt 0 otherwise |
| 387 | */ |
| 388 | static int |
| 389 | tcp_update_rtt (tcp_connection_t * tc, u32 ack) |
| 390 | { |
| 391 | u32 mrtt = 0; |
| 392 | |
| 393 | /* Karn's rule, part 1. Don't use retransmitted segments to estimate |
| 394 | * RTT because they're ambiguous. */ |
| 395 | if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff) |
| 396 | { |
| 397 | mrtt = tcp_time_now () - tc->rtt_ts; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* As per RFC7323 TSecr can be used for RTTM only if the segment advances |
| 401 | * snd_una, i.e., the left side of the send window: |
| 402 | * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't |
| 403 | * try to update rtt for dupacks */ |
| 404 | else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked) |
| 405 | { |
| 406 | mrtt = tcp_time_now () - tc->opt.tsecr; |
| 407 | } |
| 408 | |
| 409 | /* Ignore dubious measurements */ |
| 410 | if (mrtt == 0 || mrtt > TCP_RTT_MAX) |
| 411 | return 0; |
| 412 | |
| 413 | tcp_estimate_rtt (tc, mrtt); |
| 414 | |
| 415 | tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX); |
| 416 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 417 | /* Allow measuring of RTT and make sure boff is 0 */ |
| 418 | tc->rtt_seq = 0; |
| 419 | tc->rto_boff = 0; |
| 420 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Dequeue bytes that have been acked and while at it update RTT estimates. |
| 426 | */ |
| 427 | static void |
| 428 | tcp_dequeue_acked (tcp_connection_t * tc, u32 ack) |
| 429 | { |
| 430 | /* Dequeue the newly ACKed bytes */ |
| 431 | stream_session_dequeue_drop (&tc->connection, tc->bytes_acked); |
| 432 | |
| 433 | /* Update rtt and rto */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 434 | tcp_update_rtt (tc, ack); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 437 | /** |
| 438 | * Check if dupack as per RFC5681 Sec. 2 |
| 439 | * |
| 440 | * This works only if called before updating snd_wnd. |
| 441 | * */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 442 | always_inline u8 |
| 443 | tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd) |
| 444 | { |
| 445 | return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una) |
| 446 | && seq_gt (tc->snd_una_max, tc->snd_una) |
| 447 | && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number) |
| 448 | && (new_snd_wnd == tc->snd_wnd)); |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole) |
| 453 | { |
| 454 | sack_scoreboard_hole_t *next, *prev; |
| 455 | |
| 456 | if (hole->next != TCP_INVALID_SACK_HOLE_INDEX) |
| 457 | { |
| 458 | next = pool_elt_at_index (sb->holes, hole->next); |
| 459 | next->prev = hole->prev; |
| 460 | } |
| 461 | |
| 462 | if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX) |
| 463 | { |
| 464 | prev = pool_elt_at_index (sb->holes, hole->prev); |
| 465 | prev->next = hole->next; |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | sb->head = hole->next; |
| 470 | } |
| 471 | |
| 472 | pool_put (sb->holes, hole); |
| 473 | } |
| 474 | |
| 475 | sack_scoreboard_hole_t * |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 476 | scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 477 | u32 start, u32 end) |
| 478 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 479 | sack_scoreboard_hole_t *hole, *next, *prev; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 480 | u32 hole_index; |
| 481 | |
| 482 | pool_get (sb->holes, hole); |
| 483 | memset (hole, 0, sizeof (*hole)); |
| 484 | |
| 485 | hole->start = start; |
| 486 | hole->end = end; |
| 487 | hole_index = hole - sb->holes; |
| 488 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 489 | prev = scoreboard_get_hole (sb, prev_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 490 | if (prev) |
| 491 | { |
| 492 | hole->prev = prev - sb->holes; |
| 493 | hole->next = prev->next; |
| 494 | |
| 495 | if ((next = scoreboard_next_hole (sb, hole))) |
| 496 | next->prev = hole_index; |
| 497 | |
| 498 | prev->next = hole_index; |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | sb->head = hole_index; |
| 503 | hole->prev = TCP_INVALID_SACK_HOLE_INDEX; |
| 504 | hole->next = TCP_INVALID_SACK_HOLE_INDEX; |
| 505 | } |
| 506 | |
| 507 | return hole; |
| 508 | } |
| 509 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 510 | void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 511 | tcp_rcv_sacks (tcp_connection_t * tc, u32 ack) |
| 512 | { |
| 513 | sack_scoreboard_t *sb = &tc->sack_sb; |
| 514 | sack_block_t *blk, tmp; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 515 | sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole; |
| 516 | u32 blk_index = 0, old_sacked_bytes, hole_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 517 | int i, j; |
| 518 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 519 | sb->last_sacked_bytes = 0; |
| 520 | sb->snd_una_adv = 0; |
| 521 | old_sacked_bytes = sb->sacked_bytes; |
| 522 | |
| 523 | if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 524 | return; |
| 525 | |
| 526 | /* Remove invalid blocks */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 527 | blk = tc->opt.sacks; |
| 528 | while (blk < vec_end (tc->opt.sacks)) |
| 529 | { |
| 530 | if (seq_lt (blk->start, blk->end) |
| 531 | && seq_gt (blk->start, tc->snd_una) |
| 532 | && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt)) |
| 533 | { |
| 534 | blk++; |
| 535 | continue; |
| 536 | } |
| 537 | vec_del1 (tc->opt.sacks, blk - tc->opt.sacks); |
| 538 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 539 | |
| 540 | /* Add block for cumulative ack */ |
| 541 | if (seq_gt (ack, tc->snd_una)) |
| 542 | { |
| 543 | tmp.start = tc->snd_una; |
| 544 | tmp.end = ack; |
| 545 | vec_add1 (tc->opt.sacks, tmp); |
| 546 | } |
| 547 | |
| 548 | if (vec_len (tc->opt.sacks) == 0) |
| 549 | return; |
| 550 | |
| 551 | /* Make sure blocks are ordered */ |
| 552 | for (i = 0; i < vec_len (tc->opt.sacks); i++) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 553 | for (j = i + 1; j < vec_len (tc->opt.sacks); j++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 554 | if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start)) |
| 555 | { |
| 556 | tmp = tc->opt.sacks[i]; |
| 557 | tc->opt.sacks[i] = tc->opt.sacks[j]; |
| 558 | tc->opt.sacks[j] = tmp; |
| 559 | } |
| 560 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 561 | if (sb->head == TCP_INVALID_SACK_HOLE_INDEX) |
| 562 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 563 | /* If no holes, insert the first that covers all outstanding bytes */ |
| 564 | last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX, |
| 565 | tc->snd_una, tc->snd_una_max); |
| 566 | sb->tail = scoreboard_hole_index (sb, last_hole); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | /* If we have holes but snd_una_max is beyond the last hole, update |
| 571 | * last hole end */ |
| 572 | tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1]; |
| 573 | last_hole = scoreboard_last_hole (sb); |
| 574 | if (seq_gt (tc->snd_una_max, sb->max_byte_sacked) |
| 575 | && seq_gt (tc->snd_una_max, last_hole->end)) |
| 576 | last_hole->end = tc->snd_una_max; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* Walk the holes with the SACK blocks */ |
| 580 | hole = pool_elt_at_index (sb->holes, sb->head); |
| 581 | while (hole && blk_index < vec_len (tc->opt.sacks)) |
| 582 | { |
| 583 | blk = &tc->opt.sacks[blk_index]; |
| 584 | |
| 585 | if (seq_leq (blk->start, hole->start)) |
| 586 | { |
| 587 | /* Block covers hole. Remove hole */ |
| 588 | if (seq_geq (blk->end, hole->end)) |
| 589 | { |
| 590 | next_hole = scoreboard_next_hole (sb, hole); |
| 591 | |
| 592 | /* Byte accounting */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 593 | if (seq_leq (hole->end, ack)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 594 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 595 | /* Bytes lost because snd_wnd left edge advances */ |
| 596 | if (next_hole && seq_leq (next_hole->start, ack)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 597 | sb->sacked_bytes -= next_hole->start - hole->end; |
| 598 | else |
| 599 | sb->sacked_bytes -= ack - hole->end; |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | sb->sacked_bytes += scoreboard_hole_bytes (hole); |
| 604 | } |
| 605 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 606 | /* snd_una needs to be advanced */ |
| 607 | if (seq_geq (ack, hole->end)) |
| 608 | { |
| 609 | if (next_hole && seq_lt (ack, next_hole->start)) |
| 610 | sb->snd_una_adv = next_hole->start - ack; |
| 611 | else |
| 612 | sb->snd_una_adv = sb->max_byte_sacked - ack; |
| 613 | |
| 614 | /* all these can be delivered */ |
| 615 | sb->sacked_bytes -= sb->snd_una_adv; |
| 616 | } |
| 617 | |
| 618 | /* About to remove last hole */ |
| 619 | if (hole == last_hole) |
| 620 | { |
| 621 | sb->tail = hole->prev; |
| 622 | last_hole = scoreboard_last_hole (sb); |
| 623 | /* keep track of max byte sacked in case the last hole |
| 624 | * is acked */ |
| 625 | if (seq_gt (hole->end, sb->max_byte_sacked)) |
| 626 | sb->max_byte_sacked = hole->end; |
| 627 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 628 | scoreboard_remove_hole (sb, hole); |
| 629 | hole = next_hole; |
| 630 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 631 | /* Partial 'head' overlap */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 632 | else |
| 633 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 634 | if (seq_gt (blk->end, hole->start)) |
| 635 | { |
| 636 | sb->sacked_bytes += blk->end - hole->start; |
| 637 | hole->start = blk->end; |
| 638 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 639 | blk_index++; |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | /* Hole must be split */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 645 | if (seq_lt (blk->end, hole->end)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 646 | { |
| 647 | sb->sacked_bytes += blk->end - blk->start; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 648 | hole_index = scoreboard_hole_index (sb, hole); |
| 649 | new_hole = scoreboard_insert_hole (sb, hole_index, blk->end, |
| 650 | hole->end); |
| 651 | |
| 652 | /* Pool might've moved */ |
| 653 | hole = scoreboard_get_hole (sb, hole_index); |
| 654 | hole->end = blk->start; |
| 655 | |
| 656 | /* New or split of tail */ |
| 657 | if ((last_hole->end == new_hole->end) |
| 658 | || seq_lt (last_hole->end, new_hole->start)) |
| 659 | { |
| 660 | last_hole = new_hole; |
| 661 | sb->tail = scoreboard_hole_index (sb, new_hole); |
| 662 | } |
| 663 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 664 | blk_index++; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 665 | hole = scoreboard_next_hole (sb, hole); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 666 | } |
| 667 | else |
| 668 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 669 | sb->sacked_bytes += hole->end - blk->start; |
| 670 | hole->end = blk->start; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 671 | hole = scoreboard_next_hole (sb, hole); |
| 672 | } |
| 673 | } |
| 674 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 675 | |
| 676 | sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv |
| 677 | - old_sacked_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** Update snd_wnd |
| 681 | * |
| 682 | * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set |
| 683 | * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */ |
| 684 | static void |
| 685 | tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd) |
| 686 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 687 | if (seq_lt (tc->snd_wl1, seq) |
| 688 | || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 689 | { |
| 690 | tc->snd_wnd = snd_wnd; |
| 691 | tc->snd_wl1 = seq; |
| 692 | tc->snd_wl2 = ack; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 693 | TCP_EVT_DBG (TCP_EVT_SND_WND, tc); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 694 | |
| 695 | /* Set probe timer if we just got 0 wnd */ |
| 696 | if (tc->snd_wnd < tc->snd_mss |
| 697 | && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST)) |
| 698 | tcp_persist_timer_set (tc); |
| 699 | else |
| 700 | tcp_persist_timer_reset (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 704 | void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 705 | tcp_cc_congestion (tcp_connection_t * tc) |
| 706 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 707 | tc->snd_congestion = tc->snd_nxt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 708 | tc->cc_algo->congestion (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 709 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 712 | void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 713 | tcp_cc_recover (tcp_connection_t * tc) |
| 714 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 715 | /* TODO: check if time to recover was small. It might be that RTO popped |
| 716 | * too soon. |
| 717 | */ |
| 718 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 719 | tc->cc_algo->recovered (tc); |
| 720 | |
| 721 | tc->rtx_bytes = 0; |
| 722 | tc->rcv_dupacks = 0; |
| 723 | tc->snd_nxt = tc->snd_una; |
| 724 | |
| 725 | tc->cc_algo->rcv_ack (tc); |
| 726 | tc->tsecr_last_ack = tc->opt.tsecr; |
| 727 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 728 | tcp_cong_recovery_off (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 729 | |
| 730 | TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static void |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 734 | tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 735 | { |
| 736 | u8 partial_ack; |
| 737 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 738 | if (tcp_in_cong_recovery (tc)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 739 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 740 | partial_ack = seq_lt (tc->snd_una, tc->snd_congestion); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 741 | if (!partial_ack) |
| 742 | { |
| 743 | /* Clear retransmitted bytes. */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 744 | tcp_cc_recover (tc); |
| 745 | } |
| 746 | else |
| 747 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 748 | TCP_EVT_DBG (TCP_EVT_CC_PACK, tc); |
| 749 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 750 | /* Clear retransmitted bytes. XXX should we clear all? */ |
| 751 | tc->rtx_bytes = 0; |
| 752 | tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK); |
| 753 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 754 | /* In case snd_nxt is still in the past and output tries to |
| 755 | * shove some new bytes */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 756 | tc->snd_nxt = tc->snd_una_max; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 757 | |
| 758 | /* XXX need proper RFC6675 support */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 759 | if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 760 | { |
| 761 | tcp_fast_retransmit (tc); |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | /* Retransmit first unacked segment */ |
| 766 | tcp_retransmit_first_unacked (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 767 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | tc->cc_algo->rcv_ack (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 773 | tc->tsecr_last_ack = tc->opt.tsecr; |
| 774 | tc->rcv_dupacks = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 775 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void |
| 779 | tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack) |
| 780 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 781 | // ASSERT (seq_geq(tc->snd_una, ack)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 782 | |
| 783 | tc->rcv_dupacks++; |
| 784 | if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD) |
| 785 | { |
| 786 | /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */ |
| 787 | if (tc->opt.tsecr != tc->tsecr_last_ack) |
| 788 | { |
| 789 | tc->rcv_dupacks = 0; |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | tcp_fastrecovery_on (tc); |
| 794 | |
| 795 | /* Handle congestion and dupack */ |
| 796 | tcp_cc_congestion (tc); |
| 797 | tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK); |
| 798 | |
| 799 | tcp_fast_retransmit (tc); |
| 800 | |
| 801 | /* Post retransmit update cwnd to ssthresh and account for the |
| 802 | * three segments that have left the network and should've been |
| 803 | * buffered at the receiver */ |
| 804 | tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss; |
| 805 | } |
| 806 | else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD) |
| 807 | { |
| 808 | ASSERT (tcp_in_fastrecovery (tc)); |
| 809 | |
| 810 | tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | void |
| 815 | tcp_cc_init (tcp_connection_t * tc) |
| 816 | { |
| 817 | tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO); |
| 818 | tc->cc_algo->init (tc); |
| 819 | } |
| 820 | |
| 821 | static int |
| 822 | tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b, |
| 823 | tcp_header_t * th, u32 * next, u32 * error) |
| 824 | { |
| 825 | u32 new_snd_wnd; |
| 826 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 827 | /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 828 | if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)) |
| 829 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 830 | /* If we have outstanding data and this is within the window, accept it, |
| 831 | * probably retransmit has timed out. Otherwise ACK segment and then |
| 832 | * drop it */ |
| 833 | if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max)) |
| 834 | { |
| 835 | tcp_make_ack (tc, b); |
| 836 | *next = tcp_next_output (tc->c_is_ip4); |
| 837 | *error = TCP_ERROR_ACK_INVALID; |
| 838 | TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0, |
| 839 | vnet_buffer (b)->tcp.ack_number); |
| 840 | return -1; |
| 841 | } |
| 842 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 843 | TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2, |
| 844 | vnet_buffer (b)->tcp.ack_number); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 845 | |
| 846 | tc->snd_nxt = vnet_buffer (b)->tcp.ack_number; |
| 847 | *error = TCP_ERROR_ACK_FUTURE; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 850 | /* If old ACK, probably it's an old dupack */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 851 | if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una)) |
| 852 | { |
| 853 | *error = TCP_ERROR_ACK_OLD; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 854 | TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1, |
| 855 | vnet_buffer (b)->tcp.ack_number); |
| 856 | if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD) |
| 857 | { |
| 858 | TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc); |
| 859 | tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number); |
| 860 | } |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 861 | /* Don't drop yet */ |
| 862 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (tcp_opts_sack_permitted (&tc->opt)) |
| 866 | tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number); |
| 867 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 868 | new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 869 | |
| 870 | if (tcp_ack_is_dupack (tc, b, new_snd_wnd)) |
| 871 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 872 | TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 873 | tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number); |
| 874 | *error = TCP_ERROR_ACK_DUP; |
| 875 | return -1; |
| 876 | } |
| 877 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 878 | /* |
| 879 | * Valid ACK |
| 880 | */ |
| 881 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 882 | tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 883 | tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 884 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 885 | /* Dequeue ACKed data and update RTT */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 886 | tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 887 | tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number, |
| 888 | vnet_buffer (b)->tcp.ack_number, new_snd_wnd); |
| 889 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 890 | /* If some of our sent bytes have been acked, update cc and retransmit |
| 891 | * timer. */ |
| 892 | if (tc->bytes_acked) |
| 893 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 894 | TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 895 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 896 | /* Updates congestion control (slow start/congestion avoidance) */ |
| 897 | tcp_cc_rcv_ack (tc, b); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 898 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 899 | /* If everything has been acked, stop retransmit timer |
| 900 | * otherwise update */ |
| 901 | if (tc->snd_una == tc->snd_una_max) |
| 902 | tcp_retransmit_timer_reset (tc); |
| 903 | else |
| 904 | tcp_retransmit_timer_update (tc); |
| 905 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * Build SACK list as per RFC2018. |
| 912 | * |
| 913 | * Makes sure the first block contains the segment that generated the current |
| 914 | * ACK and the following ones are the ones most recently reported in SACK |
| 915 | * blocks. |
| 916 | * |
| 917 | * @param tc TCP connection for which the SACK list is updated |
| 918 | * @param start Start sequence number of the newest SACK block |
| 919 | * @param end End sequence of the newest SACK block |
| 920 | */ |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 921 | void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 922 | tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end) |
| 923 | { |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 924 | sack_block_t *new_list = 0, *block = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 925 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 926 | |
| 927 | /* If the first segment is ooo add it to the list. Last write might've moved |
| 928 | * rcv_nxt over the first segment. */ |
| 929 | if (seq_lt (tc->rcv_nxt, start)) |
| 930 | { |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 931 | vec_add2 (new_list, block, 1); |
| 932 | block->start = start; |
| 933 | block->end = end; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* Find the blocks still worth keeping. */ |
| 937 | for (i = 0; i < vec_len (tc->snd_sacks); i++) |
| 938 | { |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 939 | /* Discard if rcv_nxt advanced beyond current block */ |
| 940 | if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 941 | continue; |
| 942 | |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 943 | /* Merge or drop if segment overlapped by the new segment */ |
| 944 | if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start) |
| 945 | && seq_leq (tc->snd_sacks[i].start, new_list[0].end))) |
| 946 | { |
| 947 | if (seq_lt (tc->snd_sacks[i].start, new_list[0].start)) |
| 948 | new_list[0].start = tc->snd_sacks[i].start; |
| 949 | if (seq_lt (new_list[0].end, tc->snd_sacks[i].end)) |
| 950 | new_list[0].end = tc->snd_sacks[i].end; |
| 951 | continue; |
| 952 | } |
| 953 | |
| 954 | /* Save to new SACK list if we have space. */ |
| 955 | if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS) |
| 956 | { |
| 957 | vec_add1 (new_list, tc->snd_sacks[i]); |
| 958 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 959 | } |
| 960 | |
Florin Coras | 45d3496 | 2017-04-25 00:05:27 -0700 | [diff] [blame] | 961 | ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 962 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 963 | /* Replace old vector with new one */ |
| 964 | vec_free (tc->snd_sacks); |
| 965 | tc->snd_sacks = new_list; |
| 966 | } |
| 967 | |
| 968 | /** Enqueue data for delivery to application */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 969 | always_inline int |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 970 | tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b, |
| 971 | u16 data_len) |
| 972 | { |
| 973 | int written; |
| 974 | |
| 975 | /* Pure ACK. Update rcv_nxt and be done. */ |
| 976 | if (PREDICT_FALSE (data_len == 0)) |
| 977 | { |
| 978 | tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end; |
| 979 | return TCP_ERROR_PURE_ACK; |
| 980 | } |
| 981 | |
| 982 | written = stream_session_enqueue_data (&tc->connection, |
| 983 | vlib_buffer_get_current (b), |
| 984 | data_len, 1 /* queue event */ ); |
| 985 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 986 | TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written); |
| 987 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 988 | /* Update rcv_nxt */ |
| 989 | if (PREDICT_TRUE (written == data_len)) |
| 990 | { |
| 991 | tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end; |
| 992 | } |
| 993 | /* If more data written than expected, account for out-of-order bytes. */ |
| 994 | else if (written > data_len) |
| 995 | { |
| 996 | tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len; |
| 997 | |
| 998 | /* Send ACK confirming the update */ |
| 999 | tc->flags |= TCP_CONN_SNDACK; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1000 | } |
| 1001 | else if (written > 0) |
| 1002 | { |
| 1003 | /* We've written something but FIFO is probably full now */ |
| 1004 | tc->rcv_nxt += written; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1005 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1006 | /* Depending on how fast the app is, all remaining buffers in burst will |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1007 | * not be enqueued. Inform peer */ |
| 1008 | tc->flags |= TCP_CONN_SNDACK; |
| 1009 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1010 | return TCP_ERROR_PARTIALLY_ENQUEUED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1011 | } |
| 1012 | else |
| 1013 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1014 | tc->flags |= TCP_CONN_SNDACK; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1015 | return TCP_ERROR_FIFO_FULL; |
| 1016 | } |
| 1017 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1018 | /* Update SACK list if need be */ |
| 1019 | if (tcp_opts_sack_permitted (&tc->opt)) |
| 1020 | { |
| 1021 | /* Remove SACK blocks that have been delivered */ |
| 1022 | tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt); |
| 1023 | } |
| 1024 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1025 | return TCP_ERROR_ENQUEUED; |
| 1026 | } |
| 1027 | |
| 1028 | /** Enqueue out-of-order data */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1029 | always_inline int |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1030 | tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b, |
| 1031 | u16 data_len) |
| 1032 | { |
| 1033 | stream_session_t *s0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1034 | int rv; |
| 1035 | |
| 1036 | /* Pure ACK. Do nothing */ |
| 1037 | if (PREDICT_FALSE (data_len == 0)) |
| 1038 | { |
| 1039 | return TCP_ERROR_PURE_ACK; |
| 1040 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1041 | |
| 1042 | s0 = stream_session_get (tc->c_s_index, tc->c_thread_index); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1043 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1044 | /* Enqueue out-of-order data with absolute offset */ |
| 1045 | rv = svm_fifo_enqueue_with_offset (s0->server_rx_fifo, |
| 1046 | vnet_buffer (b)->tcp.seq_number, |
| 1047 | data_len, vlib_buffer_get_current (b)); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1048 | |
| 1049 | /* Nothing written */ |
| 1050 | if (rv) |
| 1051 | { |
| 1052 | TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0); |
| 1053 | return TCP_ERROR_FIFO_FULL; |
| 1054 | } |
| 1055 | |
| 1056 | TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1057 | |
| 1058 | /* Update SACK list if in use */ |
| 1059 | if (tcp_opts_sack_permitted (&tc->opt)) |
| 1060 | { |
| 1061 | ooo_segment_t *newest; |
| 1062 | u32 start, end; |
| 1063 | |
| 1064 | /* Get the newest segment from the fifo */ |
| 1065 | newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1066 | start = ooo_segment_offset (s0->server_rx_fifo, newest); |
| 1067 | end = ooo_segment_end_offset (s0->server_rx_fifo, newest); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1068 | |
| 1069 | tcp_update_sack_list (tc, start, end); |
| 1070 | } |
| 1071 | |
| 1072 | return TCP_ERROR_ENQUEUED; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1076 | * Check if ACK could be delayed. If ack can be delayed, it should return |
| 1077 | * true for a full frame. If we're always acking return 0. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1078 | */ |
| 1079 | always_inline int |
| 1080 | tcp_can_delack (tcp_connection_t * tc) |
| 1081 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1082 | /* Send ack if ... */ |
| 1083 | if (TCP_ALWAYS_ACK |
| 1084 | /* just sent a rcv wnd 0 */ |
| 1085 | || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0 |
| 1086 | /* constrained to send ack */ |
| 1087 | || (tc->flags & TCP_CONN_SNDACK) != 0 |
| 1088 | /* we're almost out of tx wnd */ |
| 1089 | || tcp_available_snd_space (tc) < 2 * tc->snd_mss) |
| 1090 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1091 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1092 | return 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static int |
| 1096 | tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b, |
| 1097 | u16 n_data_bytes, u32 * next0) |
| 1098 | { |
| 1099 | u32 error = 0; |
| 1100 | |
| 1101 | /* Handle out-of-order data */ |
| 1102 | if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt)) |
| 1103 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1104 | /* Old sequence numbers allowed through because they overlapped |
| 1105 | * the rx window */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1106 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1107 | if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1108 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1109 | error = TCP_ERROR_SEGMENT_OLD; |
| 1110 | *next0 = TCP_NEXT_DROP; |
| 1111 | goto done; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1114 | error = tcp_session_enqueue_ooo (tc, b, n_data_bytes); |
| 1115 | |
| 1116 | /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open |
| 1117 | * cwnd on remote peer when congested 2) acks leaving should have the |
| 1118 | * latest rcv_wnd since the burst may eaten up all of it, so only the |
| 1119 | * old ones could be filtered. |
| 1120 | */ |
| 1121 | |
| 1122 | /* RFC2581: Send DUPACK for fast retransmit */ |
| 1123 | tcp_make_ack (tc, b); |
| 1124 | *next0 = tcp_next_output (tc->c_is_ip4); |
| 1125 | |
| 1126 | /* Mark as DUPACK. We may filter these in output if |
| 1127 | * the burst fills the holes. */ |
| 1128 | if (n_data_bytes) |
| 1129 | vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK; |
| 1130 | |
| 1131 | TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1132 | goto done; |
| 1133 | } |
| 1134 | |
| 1135 | /* In order data, enqueue. Fifo figures out by itself if any out-of-order |
| 1136 | * segments can be enqueued after fifo tail offset changes. */ |
| 1137 | error = tcp_session_enqueue_data (tc, b, n_data_bytes); |
| 1138 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1139 | if (n_data_bytes == 0) |
| 1140 | { |
| 1141 | *next0 = TCP_NEXT_DROP; |
| 1142 | goto done; |
| 1143 | } |
| 1144 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1145 | /* Check if ACK can be delayed */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1146 | if (tcp_can_delack (tc)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1147 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1148 | if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK)) |
| 1149 | tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1150 | goto done; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1151 | } |
| 1152 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1153 | *next0 = tcp_next_output (tc->c_is_ip4); |
| 1154 | tcp_make_ack (tc, b); |
| 1155 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1156 | done: |
| 1157 | return error; |
| 1158 | } |
| 1159 | |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1160 | typedef struct |
| 1161 | { |
| 1162 | tcp_header_t tcp_header; |
| 1163 | tcp_connection_t tcp_connection; |
| 1164 | } tcp_rx_trace_t; |
| 1165 | |
| 1166 | u8 * |
| 1167 | format_tcp_rx_trace (u8 * s, va_list * args) |
| 1168 | { |
| 1169 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 1170 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 1171 | tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *); |
| 1172 | uword indent = format_get_indent (s); |
| 1173 | |
| 1174 | s = format (s, "%U\n%U%U", |
| 1175 | format_tcp_header, &t->tcp_header, 128, |
| 1176 | format_white_space, indent, |
| 1177 | format_tcp_connection_verbose, &t->tcp_connection); |
| 1178 | |
| 1179 | return s; |
| 1180 | } |
| 1181 | |
| 1182 | u8 * |
| 1183 | format_tcp_rx_trace_short (u8 * s, va_list * args) |
| 1184 | { |
| 1185 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 1186 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 1187 | tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *); |
| 1188 | |
| 1189 | s = format (s, "%d -> %d (%U)", |
| 1190 | clib_net_to_host_u16 (t->tcp_header.src_port), |
| 1191 | clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state, |
| 1192 | &t->tcp_connection.state); |
| 1193 | |
| 1194 | return s; |
| 1195 | } |
| 1196 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1197 | void |
| 1198 | tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0, |
| 1199 | tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4) |
| 1200 | { |
| 1201 | if (tc0) |
| 1202 | { |
| 1203 | clib_memcpy (&t0->tcp_connection, tc0, sizeof (t0->tcp_connection)); |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | th0 = tcp_buffer_hdr (b0); |
| 1208 | } |
| 1209 | clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header)); |
| 1210 | } |
| 1211 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1212 | always_inline void |
| 1213 | tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1214 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1215 | if (PREDICT_TRUE (!val)) |
| 1216 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1217 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1218 | if (is_ip4) |
| 1219 | vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val); |
| 1220 | else |
| 1221 | vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | always_inline uword |
| 1225 | tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1226 | vlib_frame_t * from_frame, int is_ip4) |
| 1227 | { |
| 1228 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1229 | u32 my_thread_index = vm->thread_index, errors = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1230 | tcp_main_t *tm = vnet_get_tcp_main (); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1231 | u8 is_fin = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1232 | |
| 1233 | from = vlib_frame_vector_args (from_frame); |
| 1234 | n_left_from = from_frame->n_vectors; |
| 1235 | |
| 1236 | next_index = node->cached_next_index; |
| 1237 | |
| 1238 | while (n_left_from > 0) |
| 1239 | { |
| 1240 | u32 n_left_to_next; |
| 1241 | |
| 1242 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1243 | |
| 1244 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1245 | { |
| 1246 | u32 bi0; |
| 1247 | vlib_buffer_t *b0; |
| 1248 | tcp_header_t *th0 = 0; |
| 1249 | tcp_connection_t *tc0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1250 | u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED; |
| 1251 | |
| 1252 | bi0 = from[0]; |
| 1253 | to_next[0] = bi0; |
| 1254 | from += 1; |
| 1255 | to_next += 1; |
| 1256 | n_left_from -= 1; |
| 1257 | n_left_to_next -= 1; |
| 1258 | |
| 1259 | b0 = vlib_get_buffer (vm, bi0); |
| 1260 | tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index, |
| 1261 | my_thread_index); |
| 1262 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1263 | if (PREDICT_FALSE (tc0 == 0)) |
| 1264 | { |
| 1265 | error0 = TCP_ERROR_INVALID_CONNECTION; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1266 | goto done; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1269 | th0 = tcp_buffer_hdr (b0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1270 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1271 | is_fin = (th0->flags & TCP_FLAG_FIN) != 0; |
| 1272 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1273 | /* SYNs, FINs and data consume sequence numbers */ |
| 1274 | vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1275 | + tcp_is_syn (th0) + is_fin + vnet_buffer (b0)->tcp.data_len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1276 | |
| 1277 | /* TODO header prediction fast path */ |
| 1278 | |
| 1279 | /* 1-4: check SEQ, RST, SYN */ |
| 1280 | if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0))) |
| 1281 | { |
| 1282 | error0 = TCP_ERROR_SEGMENT_INVALID; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1283 | TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0, |
| 1284 | vnet_buffer (b0)->tcp.seq_number, |
| 1285 | vnet_buffer (b0)->tcp.seq_end); |
| 1286 | goto done; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /* 5: check the ACK field */ |
| 1290 | if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0)) |
| 1291 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1292 | goto done; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* 6: check the URG bit TODO */ |
| 1296 | |
| 1297 | /* 7: process the segment text */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1298 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1299 | vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset); |
| 1300 | error0 = tcp_segment_rcv (tm, tc0, b0, |
| 1301 | vnet_buffer (b0)->tcp.data_len, &next0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1302 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1303 | /* N.B. buffer is rewritten if segment is ooo. Thus, th0 becomes a |
| 1304 | * dangling reference. */ |
| 1305 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1306 | /* 8: check the FIN bit */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1307 | if (is_fin) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1308 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1309 | /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead |
| 1310 | * wait for session to call close. To avoid lingering |
| 1311 | * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1312 | tc0->state = TCP_STATE_CLOSE_WAIT; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1313 | TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1314 | stream_session_disconnect_notify (&tc0->connection); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1315 | tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1316 | } |
| 1317 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1318 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1319 | b0->error = node->errors[error0]; |
| 1320 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1321 | { |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1322 | tcp_rx_trace_t *t0 = |
| 1323 | vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 1324 | tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1328 | n_left_to_next, bi0, next0); |
| 1329 | } |
| 1330 | |
| 1331 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1332 | } |
| 1333 | |
| 1334 | errors = session_manager_flush_enqueue_events (my_thread_index); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1335 | tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1336 | |
| 1337 | return from_frame->n_vectors; |
| 1338 | } |
| 1339 | |
| 1340 | static uword |
| 1341 | tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1342 | vlib_frame_t * from_frame) |
| 1343 | { |
| 1344 | return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 1345 | } |
| 1346 | |
| 1347 | static uword |
| 1348 | tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1349 | vlib_frame_t * from_frame) |
| 1350 | { |
| 1351 | return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 1352 | } |
| 1353 | |
| 1354 | /* *INDENT-OFF* */ |
| 1355 | VLIB_REGISTER_NODE (tcp4_established_node) = |
| 1356 | { |
| 1357 | .function = tcp4_established, |
| 1358 | .name = "tcp4-established", |
| 1359 | /* Takes a vector of packets. */ |
| 1360 | .vector_size = sizeof (u32), |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1361 | .n_errors = TCP_N_ERROR, |
| 1362 | .error_strings = tcp_error_strings, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1363 | .n_next_nodes = TCP_ESTABLISHED_N_NEXT, |
| 1364 | .next_nodes = |
| 1365 | { |
| 1366 | #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n, |
| 1367 | foreach_tcp_state_next |
| 1368 | #undef _ |
| 1369 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1370 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1371 | }; |
| 1372 | /* *INDENT-ON* */ |
| 1373 | |
| 1374 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established); |
| 1375 | |
| 1376 | /* *INDENT-OFF* */ |
| 1377 | VLIB_REGISTER_NODE (tcp6_established_node) = |
| 1378 | { |
| 1379 | .function = tcp6_established, |
| 1380 | .name = "tcp6-established", |
| 1381 | /* Takes a vector of packets. */ |
| 1382 | .vector_size = sizeof (u32), |
| 1383 | .n_errors = TCP_N_ERROR, |
| 1384 | .error_strings = tcp_error_strings, |
| 1385 | .n_next_nodes = TCP_ESTABLISHED_N_NEXT, |
| 1386 | .next_nodes = |
| 1387 | { |
| 1388 | #define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n, |
| 1389 | foreach_tcp_state_next |
| 1390 | #undef _ |
| 1391 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1392 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1393 | }; |
| 1394 | /* *INDENT-ON* */ |
| 1395 | |
| 1396 | |
| 1397 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established); |
| 1398 | |
| 1399 | vlib_node_registration_t tcp4_syn_sent_node; |
| 1400 | vlib_node_registration_t tcp6_syn_sent_node; |
| 1401 | |
| 1402 | always_inline uword |
| 1403 | tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1404 | vlib_frame_t * from_frame, int is_ip4) |
| 1405 | { |
| 1406 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1407 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1408 | u32 my_thread_index = vm->thread_index, errors = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1409 | u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP; |
| 1410 | |
| 1411 | from = vlib_frame_vector_args (from_frame); |
| 1412 | n_left_from = from_frame->n_vectors; |
| 1413 | |
| 1414 | next_index = node->cached_next_index; |
| 1415 | |
| 1416 | while (n_left_from > 0) |
| 1417 | { |
| 1418 | u32 n_left_to_next; |
| 1419 | |
| 1420 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1421 | |
| 1422 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1423 | { |
| 1424 | u32 bi0, ack0, seq0; |
| 1425 | vlib_buffer_t *b0; |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1426 | tcp_rx_trace_t *t0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1427 | tcp_header_t *tcp0 = 0; |
| 1428 | tcp_connection_t *tc0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1429 | tcp_connection_t *new_tc0; |
| 1430 | u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED; |
| 1431 | |
| 1432 | bi0 = from[0]; |
| 1433 | to_next[0] = bi0; |
| 1434 | from += 1; |
| 1435 | to_next += 1; |
| 1436 | n_left_from -= 1; |
| 1437 | n_left_to_next -= 1; |
| 1438 | |
| 1439 | b0 = vlib_get_buffer (vm, bi0); |
| 1440 | tc0 = |
| 1441 | tcp_half_open_connection_get (vnet_buffer (b0)-> |
| 1442 | tcp.connection_index); |
| 1443 | |
| 1444 | ack0 = vnet_buffer (b0)->tcp.ack_number; |
| 1445 | seq0 = vnet_buffer (b0)->tcp.seq_number; |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1446 | tcp0 = tcp_buffer_hdr (b0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1447 | |
| 1448 | if (PREDICT_FALSE |
| 1449 | (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0))) |
| 1450 | goto drop; |
| 1451 | |
| 1452 | /* SYNs, FINs and data consume sequence numbers */ |
| 1453 | vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0) |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1454 | + tcp_is_fin (tcp0) + vnet_buffer (b0)->tcp.data_len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1455 | |
| 1456 | /* |
| 1457 | * 1. check the ACK bit |
| 1458 | */ |
| 1459 | |
| 1460 | /* |
| 1461 | * If the ACK bit is set |
| 1462 | * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless |
| 1463 | * the RST bit is set, if so drop the segment and return) |
| 1464 | * <SEQ=SEG.ACK><CTL=RST> |
| 1465 | * and discard the segment. Return. |
| 1466 | * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable. |
| 1467 | */ |
| 1468 | if (tcp_ack (tcp0)) |
| 1469 | { |
| 1470 | if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt) |
| 1471 | { |
| 1472 | if (!tcp_rst (tcp0)) |
| 1473 | tcp_send_reset (b0, is_ip4); |
| 1474 | |
| 1475 | goto drop; |
| 1476 | } |
| 1477 | |
| 1478 | /* Make sure ACK is valid */ |
| 1479 | if (tc0->snd_una > ack0) |
| 1480 | goto drop; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * 2. check the RST bit |
| 1485 | */ |
| 1486 | |
| 1487 | if (tcp_rst (tcp0)) |
| 1488 | { |
| 1489 | /* If ACK is acceptable, signal client that peer is not |
| 1490 | * willing to accept connection and drop connection*/ |
| 1491 | if (tcp_ack (tcp0)) |
| 1492 | { |
| 1493 | stream_session_connect_notify (&tc0->connection, sst, |
| 1494 | 1 /* fail */ ); |
| 1495 | tcp_connection_cleanup (tc0); |
| 1496 | } |
| 1497 | goto drop; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * 3. check the security and precedence (skipped) |
| 1502 | */ |
| 1503 | |
| 1504 | /* |
| 1505 | * 4. check the SYN bit |
| 1506 | */ |
| 1507 | |
| 1508 | /* No SYN flag. Drop. */ |
| 1509 | if (!tcp_syn (tcp0)) |
| 1510 | goto drop; |
| 1511 | |
| 1512 | /* Stop connection establishment and retransmit timers */ |
| 1513 | tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH); |
| 1514 | tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN); |
| 1515 | |
| 1516 | /* Valid SYN or SYN-ACK. Move connection from half-open pool to |
| 1517 | * current thread pool. */ |
| 1518 | pool_get (tm->connections[my_thread_index], new_tc0); |
| 1519 | clib_memcpy (new_tc0, tc0, sizeof (*new_tc0)); |
| 1520 | |
| 1521 | new_tc0->c_thread_index = my_thread_index; |
| 1522 | |
| 1523 | /* Cleanup half-open connection XXX lock */ |
| 1524 | pool_put (tm->half_open_connections, tc0); |
| 1525 | |
| 1526 | new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end; |
| 1527 | new_tc0->irs = seq0; |
| 1528 | |
| 1529 | /* Parse options */ |
| 1530 | tcp_options_parse (tcp0, &new_tc0->opt); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1531 | |
| 1532 | if (tcp_opts_tstamp (&new_tc0->opt)) |
| 1533 | { |
| 1534 | new_tc0->tsval_recent = new_tc0->opt.tsval; |
| 1535 | new_tc0->tsval_recent_age = tcp_time_now (); |
| 1536 | } |
| 1537 | |
| 1538 | if (tcp_opts_wscale (&new_tc0->opt)) |
| 1539 | new_tc0->snd_wscale = new_tc0->opt.wscale; |
| 1540 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1541 | /* No scaling */ |
| 1542 | new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1543 | new_tc0->snd_wl1 = seq0; |
| 1544 | new_tc0->snd_wl2 = ack0; |
| 1545 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1546 | tcp_connection_init_vars (new_tc0); |
| 1547 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1548 | /* SYN-ACK: See if we can switch to ESTABLISHED state */ |
| 1549 | if (tcp_ack (tcp0)) |
| 1550 | { |
| 1551 | /* Our SYN is ACKed: we have iss < ack = snd_una */ |
| 1552 | |
| 1553 | /* TODO Dequeue acknowledged segments if we support Fast Open */ |
| 1554 | new_tc0->snd_una = ack0; |
| 1555 | new_tc0->state = TCP_STATE_ESTABLISHED; |
| 1556 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1557 | /* Make sure las is initialized for the wnd computation */ |
| 1558 | new_tc0->rcv_las = new_tc0->rcv_nxt; |
| 1559 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1560 | /* Notify app that we have connection */ |
| 1561 | stream_session_connect_notify (&new_tc0->connection, sst, 0); |
| 1562 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1563 | stream_session_init_fifos_pointers (&new_tc0->connection, |
| 1564 | new_tc0->irs + 1, |
| 1565 | new_tc0->iss + 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1566 | /* Make sure after data segment processing ACK is sent */ |
| 1567 | new_tc0->flags |= TCP_CONN_SNDACK; |
| 1568 | } |
| 1569 | /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */ |
| 1570 | else |
| 1571 | { |
| 1572 | new_tc0->state = TCP_STATE_SYN_RCVD; |
| 1573 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1574 | /* Notify app that we have connection */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1575 | stream_session_connect_notify (&new_tc0->connection, sst, 0); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1576 | stream_session_init_fifos_pointers (&new_tc0->connection, |
| 1577 | new_tc0->irs + 1, |
| 1578 | new_tc0->iss + 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1579 | tcp_make_synack (new_tc0, b0); |
| 1580 | next0 = tcp_next_output (is_ip4); |
| 1581 | |
| 1582 | goto drop; |
| 1583 | } |
| 1584 | |
| 1585 | /* Read data, if any */ |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1586 | if (vnet_buffer (b0)->tcp.data_len) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1587 | { |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1588 | vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset); |
| 1589 | error0 = tcp_segment_rcv (tm, new_tc0, b0, |
| 1590 | vnet_buffer (b0)->tcp.data_len, |
| 1591 | &next0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1592 | if (error0 == TCP_ERROR_PURE_ACK) |
| 1593 | error0 = TCP_ERROR_SYN_ACKS_RCVD; |
| 1594 | } |
| 1595 | else |
| 1596 | { |
| 1597 | tcp_make_ack (new_tc0, b0); |
| 1598 | next0 = tcp_next_output (new_tc0->c_is_ip4); |
| 1599 | } |
| 1600 | |
| 1601 | drop: |
| 1602 | |
| 1603 | b0->error = error0 ? node->errors[error0] : 0; |
| 1604 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1605 | { |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1606 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 1607 | clib_memcpy (&t0->tcp_header, tcp0, sizeof (t0->tcp_header)); |
| 1608 | clib_memcpy (&t0->tcp_connection, tc0, |
| 1609 | sizeof (t0->tcp_connection)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1613 | n_left_to_next, bi0, next0); |
| 1614 | } |
| 1615 | |
| 1616 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1617 | } |
| 1618 | |
| 1619 | errors = session_manager_flush_enqueue_events (my_thread_index); |
| 1620 | if (errors) |
| 1621 | { |
| 1622 | if (is_ip4) |
| 1623 | vlib_node_increment_counter (vm, tcp4_established_node.index, |
| 1624 | TCP_ERROR_EVENT_FIFO_FULL, errors); |
| 1625 | else |
| 1626 | vlib_node_increment_counter (vm, tcp6_established_node.index, |
| 1627 | TCP_ERROR_EVENT_FIFO_FULL, errors); |
| 1628 | } |
| 1629 | |
| 1630 | return from_frame->n_vectors; |
| 1631 | } |
| 1632 | |
| 1633 | static uword |
| 1634 | tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1635 | vlib_frame_t * from_frame) |
| 1636 | { |
| 1637 | return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 1638 | } |
| 1639 | |
| 1640 | static uword |
| 1641 | tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1642 | vlib_frame_t * from_frame) |
| 1643 | { |
| 1644 | return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 1645 | } |
| 1646 | |
| 1647 | /* *INDENT-OFF* */ |
| 1648 | VLIB_REGISTER_NODE (tcp4_syn_sent_node) = |
| 1649 | { |
| 1650 | .function = tcp4_syn_sent, |
| 1651 | .name = "tcp4-syn-sent", |
| 1652 | /* Takes a vector of packets. */ |
| 1653 | .vector_size = sizeof (u32), |
| 1654 | .n_errors = TCP_N_ERROR, |
| 1655 | .error_strings = tcp_error_strings, |
| 1656 | .n_next_nodes = TCP_SYN_SENT_N_NEXT, |
| 1657 | .next_nodes = |
| 1658 | { |
| 1659 | #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n, |
| 1660 | foreach_tcp_state_next |
| 1661 | #undef _ |
| 1662 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1663 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1664 | }; |
| 1665 | /* *INDENT-ON* */ |
| 1666 | |
| 1667 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent); |
| 1668 | |
| 1669 | /* *INDENT-OFF* */ |
| 1670 | VLIB_REGISTER_NODE (tcp6_syn_sent_node) = |
| 1671 | { |
| 1672 | .function = tcp6_syn_sent_rcv, |
| 1673 | .name = "tcp6-syn-sent", |
| 1674 | /* Takes a vector of packets. */ |
| 1675 | .vector_size = sizeof (u32), |
| 1676 | .n_errors = TCP_N_ERROR, |
| 1677 | .error_strings = tcp_error_strings, |
| 1678 | .n_next_nodes = TCP_SYN_SENT_N_NEXT, |
| 1679 | .next_nodes = |
| 1680 | { |
| 1681 | #define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n, |
| 1682 | foreach_tcp_state_next |
| 1683 | #undef _ |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 1684 | }, |
| 1685 | .format_trace = format_tcp_rx_trace_short, |
| 1686 | }; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1687 | /* *INDENT-ON* */ |
| 1688 | |
| 1689 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv); |
| 1690 | /** |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1691 | * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1692 | * as per RFC793 p. 64 |
| 1693 | */ |
| 1694 | always_inline uword |
| 1695 | tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1696 | vlib_frame_t * from_frame, int is_ip4) |
| 1697 | { |
| 1698 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1699 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1700 | u32 my_thread_index = vm->thread_index, errors = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1701 | |
| 1702 | from = vlib_frame_vector_args (from_frame); |
| 1703 | n_left_from = from_frame->n_vectors; |
| 1704 | |
| 1705 | next_index = node->cached_next_index; |
| 1706 | |
| 1707 | while (n_left_from > 0) |
| 1708 | { |
| 1709 | u32 n_left_to_next; |
| 1710 | |
| 1711 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1712 | |
| 1713 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1714 | { |
| 1715 | u32 bi0; |
| 1716 | vlib_buffer_t *b0; |
| 1717 | tcp_header_t *tcp0 = 0; |
| 1718 | tcp_connection_t *tc0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1719 | u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED; |
| 1720 | |
| 1721 | bi0 = from[0]; |
| 1722 | to_next[0] = bi0; |
| 1723 | from += 1; |
| 1724 | to_next += 1; |
| 1725 | n_left_from -= 1; |
| 1726 | n_left_to_next -= 1; |
| 1727 | |
| 1728 | b0 = vlib_get_buffer (vm, bi0); |
| 1729 | tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index, |
| 1730 | my_thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1731 | if (PREDICT_FALSE (tc0 == 0)) |
| 1732 | { |
| 1733 | error0 = TCP_ERROR_INVALID_CONNECTION; |
| 1734 | goto drop; |
| 1735 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1736 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1737 | tcp0 = tcp_buffer_hdr (b0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1738 | |
| 1739 | /* SYNs, FINs and data consume sequence numbers */ |
| 1740 | vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1741 | + tcp_is_syn (tcp0) + tcp_is_fin (tcp0) |
| 1742 | + vnet_buffer (b0)->tcp.data_len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * Special treatment for CLOSED |
| 1746 | */ |
| 1747 | switch (tc0->state) |
| 1748 | { |
| 1749 | case TCP_STATE_CLOSED: |
| 1750 | goto drop; |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * For all other states (except LISTEN) |
| 1756 | */ |
| 1757 | |
| 1758 | /* 1-4: check SEQ, RST, SYN */ |
| 1759 | if (PREDICT_FALSE |
| 1760 | (tcp_segment_validate (vm, tc0, b0, tcp0, &next0))) |
| 1761 | { |
| 1762 | error0 = TCP_ERROR_SEGMENT_INVALID; |
| 1763 | goto drop; |
| 1764 | } |
| 1765 | |
| 1766 | /* 5: check the ACK field */ |
| 1767 | switch (tc0->state) |
| 1768 | { |
| 1769 | case TCP_STATE_SYN_RCVD: |
| 1770 | /* |
| 1771 | * If the segment acknowledgment is not acceptable, form a |
| 1772 | * reset segment, |
| 1773 | * <SEQ=SEG.ACK><CTL=RST> |
| 1774 | * and send it. |
| 1775 | */ |
| 1776 | if (!tcp_rcv_ack_is_acceptable (tc0, b0)) |
| 1777 | { |
| 1778 | tcp_send_reset (b0, is_ip4); |
| 1779 | goto drop; |
| 1780 | } |
| 1781 | /* Switch state to ESTABLISHED */ |
| 1782 | tc0->state = TCP_STATE_ESTABLISHED; |
| 1783 | |
| 1784 | /* Initialize session variables */ |
| 1785 | tc0->snd_una = vnet_buffer (b0)->tcp.ack_number; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1786 | tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1787 | << tc0->opt.wscale; |
| 1788 | tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number; |
| 1789 | tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number; |
| 1790 | |
| 1791 | /* Shoulder tap the server */ |
| 1792 | stream_session_accept_notify (&tc0->connection); |
| 1793 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1794 | /* Reset SYN-ACK retransmit timer */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1795 | tcp_retransmit_timer_reset (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1796 | break; |
| 1797 | case TCP_STATE_ESTABLISHED: |
| 1798 | /* We can get packets in established state here because they |
| 1799 | * were enqueued before state change */ |
| 1800 | if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0)) |
| 1801 | goto drop; |
| 1802 | |
| 1803 | break; |
| 1804 | case TCP_STATE_FIN_WAIT_1: |
| 1805 | /* In addition to the processing for the ESTABLISHED state, if |
| 1806 | * our FIN is now acknowledged then enter FIN-WAIT-2 and |
| 1807 | * continue processing in that state. */ |
| 1808 | if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0)) |
| 1809 | goto drop; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1810 | |
| 1811 | /* If FIN is ACKed */ |
| 1812 | if (tc0->snd_una == tc0->snd_una_max) |
| 1813 | { |
| 1814 | tc0->state = TCP_STATE_FIN_WAIT_2; |
| 1815 | /* Stop all timers, 2MSL will be set lower */ |
| 1816 | tcp_connection_timers_reset (tc0); |
| 1817 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1818 | break; |
| 1819 | case TCP_STATE_FIN_WAIT_2: |
| 1820 | /* In addition to the processing for the ESTABLISHED state, if |
| 1821 | * the retransmission queue is empty, the user's CLOSE can be |
| 1822 | * acknowledged ("ok") but do not delete the TCB. */ |
| 1823 | if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0)) |
| 1824 | goto drop; |
| 1825 | /* check if rtx queue is empty and ack CLOSE TODO */ |
| 1826 | break; |
| 1827 | case TCP_STATE_CLOSE_WAIT: |
| 1828 | /* Do the same processing as for the ESTABLISHED state. */ |
| 1829 | if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0)) |
| 1830 | goto drop; |
| 1831 | break; |
| 1832 | case TCP_STATE_CLOSING: |
| 1833 | /* In addition to the processing for the ESTABLISHED state, if |
| 1834 | * the ACK acknowledges our FIN then enter the TIME-WAIT state, |
| 1835 | * otherwise ignore the segment. */ |
| 1836 | if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0)) |
| 1837 | goto drop; |
| 1838 | |
| 1839 | /* XXX test that send queue empty */ |
| 1840 | tc0->state = TCP_STATE_TIME_WAIT; |
| 1841 | goto drop; |
| 1842 | |
| 1843 | break; |
| 1844 | case TCP_STATE_LAST_ACK: |
| 1845 | /* The only thing that can arrive in this state is an |
| 1846 | * acknowledgment of our FIN. If our FIN is now acknowledged, |
| 1847 | * delete the TCB, enter the CLOSED state, and return. */ |
| 1848 | |
| 1849 | if (!tcp_rcv_ack_is_acceptable (tc0, b0)) |
| 1850 | goto drop; |
| 1851 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1852 | tc0->state = TCP_STATE_CLOSED; |
| 1853 | |
| 1854 | /* Don't delete the connection/session yet. Instead, wait a |
| 1855 | * reasonable amount of time until the pipes are cleared. In |
| 1856 | * particular, this makes sure that we won't have dead sessions |
| 1857 | * when processing events on the tx path */ |
| 1858 | tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME); |
| 1859 | |
| 1860 | /* Stop retransmit */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1861 | tcp_retransmit_timer_reset (tc0); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1862 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1863 | goto drop; |
| 1864 | |
| 1865 | break; |
| 1866 | case TCP_STATE_TIME_WAIT: |
| 1867 | /* The only thing that can arrive in this state is a |
| 1868 | * retransmission of the remote FIN. Acknowledge it, and restart |
| 1869 | * the 2 MSL timeout. */ |
| 1870 | |
| 1871 | /* TODO */ |
| 1872 | goto drop; |
| 1873 | break; |
| 1874 | default: |
| 1875 | ASSERT (0); |
| 1876 | } |
| 1877 | |
| 1878 | /* 6: check the URG bit TODO */ |
| 1879 | |
| 1880 | /* 7: process the segment text */ |
| 1881 | switch (tc0->state) |
| 1882 | { |
| 1883 | case TCP_STATE_ESTABLISHED: |
| 1884 | case TCP_STATE_FIN_WAIT_1: |
| 1885 | case TCP_STATE_FIN_WAIT_2: |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1886 | vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset); |
| 1887 | error0 = tcp_segment_rcv (tm, tc0, b0, |
| 1888 | vnet_buffer (b0)->tcp.data_len, |
| 1889 | &next0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1890 | break; |
| 1891 | case TCP_STATE_CLOSE_WAIT: |
| 1892 | case TCP_STATE_CLOSING: |
| 1893 | case TCP_STATE_LAST_ACK: |
| 1894 | case TCP_STATE_TIME_WAIT: |
| 1895 | /* This should not occur, since a FIN has been received from the |
| 1896 | * remote side. Ignore the segment text. */ |
| 1897 | break; |
| 1898 | } |
| 1899 | |
| 1900 | /* 8: check the FIN bit */ |
| 1901 | if (!tcp_fin (tcp0)) |
| 1902 | goto drop; |
| 1903 | |
| 1904 | switch (tc0->state) |
| 1905 | { |
| 1906 | case TCP_STATE_ESTABLISHED: |
| 1907 | case TCP_STATE_SYN_RCVD: |
| 1908 | /* Send FIN-ACK notify app and enter CLOSE-WAIT */ |
| 1909 | tcp_connection_timers_reset (tc0); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1910 | tcp_make_fin (tc0, b0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1911 | next0 = tcp_next_output (tc0->c_is_ip4); |
| 1912 | stream_session_disconnect_notify (&tc0->connection); |
| 1913 | tc0->state = TCP_STATE_CLOSE_WAIT; |
| 1914 | break; |
| 1915 | case TCP_STATE_CLOSE_WAIT: |
| 1916 | case TCP_STATE_CLOSING: |
| 1917 | case TCP_STATE_LAST_ACK: |
| 1918 | /* move along .. */ |
| 1919 | break; |
| 1920 | case TCP_STATE_FIN_WAIT_1: |
| 1921 | tc0->state = TCP_STATE_TIME_WAIT; |
| 1922 | tcp_connection_timers_reset (tc0); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1923 | tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1924 | break; |
| 1925 | case TCP_STATE_FIN_WAIT_2: |
| 1926 | /* Got FIN, send ACK! */ |
| 1927 | tc0->state = TCP_STATE_TIME_WAIT; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1928 | tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1929 | tcp_make_ack (tc0, b0); |
| 1930 | next0 = tcp_next_output (is_ip4); |
| 1931 | break; |
| 1932 | case TCP_STATE_TIME_WAIT: |
| 1933 | /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait |
| 1934 | * timeout. |
| 1935 | */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1936 | tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1937 | break; |
| 1938 | } |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1939 | TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1940 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1941 | drop: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1942 | b0->error = error0 ? node->errors[error0] : 0; |
| 1943 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1944 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1945 | { |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 1946 | tcp_rx_trace_t *t0 = |
| 1947 | vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 1948 | tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1952 | n_left_to_next, bi0, next0); |
| 1953 | } |
| 1954 | |
| 1955 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1956 | } |
| 1957 | |
| 1958 | errors = session_manager_flush_enqueue_events (my_thread_index); |
| 1959 | if (errors) |
| 1960 | { |
| 1961 | if (is_ip4) |
| 1962 | vlib_node_increment_counter (vm, tcp4_established_node.index, |
| 1963 | TCP_ERROR_EVENT_FIFO_FULL, errors); |
| 1964 | else |
| 1965 | vlib_node_increment_counter (vm, tcp6_established_node.index, |
| 1966 | TCP_ERROR_EVENT_FIFO_FULL, errors); |
| 1967 | } |
| 1968 | |
| 1969 | return from_frame->n_vectors; |
| 1970 | } |
| 1971 | |
| 1972 | static uword |
| 1973 | tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1974 | vlib_frame_t * from_frame) |
| 1975 | { |
| 1976 | return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 1977 | } |
| 1978 | |
| 1979 | static uword |
| 1980 | tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1981 | vlib_frame_t * from_frame) |
| 1982 | { |
| 1983 | return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 1984 | } |
| 1985 | |
| 1986 | /* *INDENT-OFF* */ |
| 1987 | VLIB_REGISTER_NODE (tcp4_rcv_process_node) = |
| 1988 | { |
| 1989 | .function = tcp4_rcv_process, |
| 1990 | .name = "tcp4-rcv-process", |
| 1991 | /* Takes a vector of packets. */ |
| 1992 | .vector_size = sizeof (u32), |
| 1993 | .n_errors = TCP_N_ERROR, |
| 1994 | .error_strings = tcp_error_strings, |
| 1995 | .n_next_nodes = TCP_RCV_PROCESS_N_NEXT, |
| 1996 | .next_nodes = |
| 1997 | { |
| 1998 | #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n, |
| 1999 | foreach_tcp_state_next |
| 2000 | #undef _ |
| 2001 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2002 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2003 | }; |
| 2004 | /* *INDENT-ON* */ |
| 2005 | |
| 2006 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process); |
| 2007 | |
| 2008 | /* *INDENT-OFF* */ |
| 2009 | VLIB_REGISTER_NODE (tcp6_rcv_process_node) = |
| 2010 | { |
| 2011 | .function = tcp6_rcv_process, |
| 2012 | .name = "tcp6-rcv-process", |
| 2013 | /* Takes a vector of packets. */ |
| 2014 | .vector_size = sizeof (u32), |
| 2015 | .n_errors = TCP_N_ERROR, |
| 2016 | .error_strings = tcp_error_strings, |
| 2017 | .n_next_nodes = TCP_RCV_PROCESS_N_NEXT, |
| 2018 | .next_nodes = |
| 2019 | { |
| 2020 | #define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n, |
| 2021 | foreach_tcp_state_next |
| 2022 | #undef _ |
| 2023 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2024 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2025 | }; |
| 2026 | /* *INDENT-ON* */ |
| 2027 | |
| 2028 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process); |
| 2029 | |
| 2030 | vlib_node_registration_t tcp4_listen_node; |
| 2031 | vlib_node_registration_t tcp6_listen_node; |
| 2032 | |
| 2033 | /** |
| 2034 | * LISTEN state processing as per RFC 793 p. 65 |
| 2035 | */ |
| 2036 | always_inline uword |
| 2037 | tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2038 | vlib_frame_t * from_frame, int is_ip4) |
| 2039 | { |
| 2040 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2041 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2042 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 2043 | u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP; |
| 2044 | |
| 2045 | from = vlib_frame_vector_args (from_frame); |
| 2046 | n_left_from = from_frame->n_vectors; |
| 2047 | |
| 2048 | next_index = node->cached_next_index; |
| 2049 | |
| 2050 | while (n_left_from > 0) |
| 2051 | { |
| 2052 | u32 n_left_to_next; |
| 2053 | |
| 2054 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 2055 | |
| 2056 | while (n_left_from > 0 && n_left_to_next > 0) |
| 2057 | { |
| 2058 | u32 bi0; |
| 2059 | vlib_buffer_t *b0; |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2060 | tcp_rx_trace_t *t0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2061 | tcp_header_t *th0 = 0; |
| 2062 | tcp_connection_t *lc0; |
| 2063 | ip4_header_t *ip40; |
| 2064 | ip6_header_t *ip60; |
| 2065 | tcp_connection_t *child0; |
| 2066 | u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP; |
| 2067 | |
| 2068 | bi0 = from[0]; |
| 2069 | to_next[0] = bi0; |
| 2070 | from += 1; |
| 2071 | to_next += 1; |
| 2072 | n_left_from -= 1; |
| 2073 | n_left_to_next -= 1; |
| 2074 | |
| 2075 | b0 = vlib_get_buffer (vm, bi0); |
| 2076 | lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index); |
| 2077 | |
| 2078 | if (is_ip4) |
| 2079 | { |
| 2080 | ip40 = vlib_buffer_get_current (b0); |
| 2081 | th0 = ip4_next_header (ip40); |
| 2082 | } |
| 2083 | else |
| 2084 | { |
| 2085 | ip60 = vlib_buffer_get_current (b0); |
| 2086 | th0 = ip6_next_header (ip60); |
| 2087 | } |
| 2088 | |
| 2089 | /* Create child session. For syn-flood protection use filter */ |
| 2090 | |
| 2091 | /* 1. first check for an RST */ |
| 2092 | if (tcp_rst (th0)) |
| 2093 | goto drop; |
| 2094 | |
| 2095 | /* 2. second check for an ACK */ |
| 2096 | if (tcp_ack (th0)) |
| 2097 | { |
| 2098 | tcp_send_reset (b0, is_ip4); |
| 2099 | goto drop; |
| 2100 | } |
| 2101 | |
| 2102 | /* 3. check for a SYN (did that already) */ |
| 2103 | |
| 2104 | /* Create child session and send SYN-ACK */ |
| 2105 | pool_get (tm->connections[my_thread_index], child0); |
| 2106 | memset (child0, 0, sizeof (*child0)); |
| 2107 | |
| 2108 | child0->c_c_index = child0 - tm->connections[my_thread_index]; |
| 2109 | child0->c_lcl_port = lc0->c_lcl_port; |
| 2110 | child0->c_rmt_port = th0->src_port; |
| 2111 | child0->c_is_ip4 = is_ip4; |
| 2112 | child0->c_thread_index = my_thread_index; |
| 2113 | |
| 2114 | if (is_ip4) |
| 2115 | { |
| 2116 | child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32; |
| 2117 | child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32; |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address, |
| 2122 | sizeof (ip6_address_t)); |
| 2123 | clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address, |
| 2124 | sizeof (ip6_address_t)); |
| 2125 | } |
| 2126 | |
| 2127 | if (stream_session_accept (&child0->connection, lc0->c_s_index, sst, |
| 2128 | 0 /* notify */ )) |
| 2129 | { |
| 2130 | error0 = TCP_ERROR_CREATE_SESSION_FAIL; |
| 2131 | goto drop; |
| 2132 | } |
| 2133 | |
| 2134 | tcp_options_parse (th0, &child0->opt); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2135 | |
| 2136 | child0->irs = vnet_buffer (b0)->tcp.seq_number; |
| 2137 | child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 2138 | child0->rcv_las = child0->rcv_nxt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2139 | child0->state = TCP_STATE_SYN_RCVD; |
| 2140 | |
| 2141 | /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK} |
| 2142 | * segments are used to initialize PAWS. */ |
| 2143 | if (tcp_opts_tstamp (&child0->opt)) |
| 2144 | { |
| 2145 | child0->tsval_recent = child0->opt.tsval; |
| 2146 | child0->tsval_recent_age = tcp_time_now (); |
| 2147 | } |
| 2148 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2149 | if (tcp_opts_wscale (&child0->opt)) |
| 2150 | child0->snd_wscale = child0->opt.wscale; |
| 2151 | |
| 2152 | /* No scaling */ |
| 2153 | child0->snd_wnd = clib_net_to_host_u16 (th0->window); |
| 2154 | child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number; |
| 2155 | child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number; |
| 2156 | |
| 2157 | tcp_connection_init_vars (child0); |
| 2158 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 2159 | TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0); |
| 2160 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2161 | /* Reuse buffer to make syn-ack and send */ |
| 2162 | tcp_make_synack (child0, b0); |
| 2163 | next0 = tcp_next_output (is_ip4); |
| 2164 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 2165 | /* Init fifo pointers after we have iss */ |
| 2166 | stream_session_init_fifos_pointers (&child0->connection, |
| 2167 | child0->irs + 1, |
| 2168 | child0->iss + 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2169 | drop: |
| 2170 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 2171 | { |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2172 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 2173 | clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header)); |
| 2174 | clib_memcpy (&t0->tcp_connection, lc0, |
| 2175 | sizeof (t0->tcp_connection)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2176 | } |
| 2177 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2178 | b0->error = node->errors[error0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2179 | |
| 2180 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 2181 | n_left_to_next, bi0, next0); |
| 2182 | } |
| 2183 | |
| 2184 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 2185 | } |
| 2186 | return from_frame->n_vectors; |
| 2187 | } |
| 2188 | |
| 2189 | static uword |
| 2190 | tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2191 | vlib_frame_t * from_frame) |
| 2192 | { |
| 2193 | return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 2194 | } |
| 2195 | |
| 2196 | static uword |
| 2197 | tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2198 | vlib_frame_t * from_frame) |
| 2199 | { |
| 2200 | return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 2201 | } |
| 2202 | |
| 2203 | /* *INDENT-OFF* */ |
| 2204 | VLIB_REGISTER_NODE (tcp4_listen_node) = |
| 2205 | { |
| 2206 | .function = tcp4_listen, |
| 2207 | .name = "tcp4-listen", |
| 2208 | /* Takes a vector of packets. */ |
| 2209 | .vector_size = sizeof (u32), |
| 2210 | .n_errors = TCP_N_ERROR, |
| 2211 | .error_strings = tcp_error_strings, |
| 2212 | .n_next_nodes = TCP_LISTEN_N_NEXT, |
| 2213 | .next_nodes = |
| 2214 | { |
| 2215 | #define _(s,n) [TCP_LISTEN_NEXT_##s] = n, |
| 2216 | foreach_tcp_state_next |
| 2217 | #undef _ |
| 2218 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2219 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2220 | }; |
| 2221 | /* *INDENT-ON* */ |
| 2222 | |
| 2223 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen); |
| 2224 | |
| 2225 | /* *INDENT-OFF* */ |
| 2226 | VLIB_REGISTER_NODE (tcp6_listen_node) = |
| 2227 | { |
| 2228 | .function = tcp6_listen, |
| 2229 | .name = "tcp6-listen", |
| 2230 | /* Takes a vector of packets. */ |
| 2231 | .vector_size = sizeof (u32), |
| 2232 | .n_errors = TCP_N_ERROR, |
| 2233 | .error_strings = tcp_error_strings, |
| 2234 | .n_next_nodes = TCP_LISTEN_N_NEXT, |
| 2235 | .next_nodes = |
| 2236 | { |
| 2237 | #define _(s,n) [TCP_LISTEN_NEXT_##s] = n, |
| 2238 | foreach_tcp_state_next |
| 2239 | #undef _ |
| 2240 | }, |
Clement Durand | 6cf260c | 2017-04-13 13:27:04 +0200 | [diff] [blame] | 2241 | .format_trace = format_tcp_rx_trace_short, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2242 | }; |
| 2243 | /* *INDENT-ON* */ |
| 2244 | |
| 2245 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen); |
| 2246 | |
| 2247 | vlib_node_registration_t tcp4_input_node; |
| 2248 | vlib_node_registration_t tcp6_input_node; |
| 2249 | |
| 2250 | typedef enum _tcp_input_next |
| 2251 | { |
| 2252 | TCP_INPUT_NEXT_DROP, |
| 2253 | TCP_INPUT_NEXT_LISTEN, |
| 2254 | TCP_INPUT_NEXT_RCV_PROCESS, |
| 2255 | TCP_INPUT_NEXT_SYN_SENT, |
| 2256 | TCP_INPUT_NEXT_ESTABLISHED, |
| 2257 | TCP_INPUT_NEXT_RESET, |
| 2258 | TCP_INPUT_N_NEXT |
| 2259 | } tcp_input_next_t; |
| 2260 | |
| 2261 | #define foreach_tcp4_input_next \ |
| 2262 | _ (DROP, "error-drop") \ |
| 2263 | _ (LISTEN, "tcp4-listen") \ |
| 2264 | _ (RCV_PROCESS, "tcp4-rcv-process") \ |
| 2265 | _ (SYN_SENT, "tcp4-syn-sent") \ |
| 2266 | _ (ESTABLISHED, "tcp4-established") \ |
| 2267 | _ (RESET, "tcp4-reset") |
| 2268 | |
| 2269 | #define foreach_tcp6_input_next \ |
| 2270 | _ (DROP, "error-drop") \ |
| 2271 | _ (LISTEN, "tcp6-listen") \ |
| 2272 | _ (RCV_PROCESS, "tcp6-rcv-process") \ |
| 2273 | _ (SYN_SENT, "tcp6-syn-sent") \ |
| 2274 | _ (ESTABLISHED, "tcp6-established") \ |
| 2275 | _ (RESET, "tcp6-reset") |
| 2276 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2277 | #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN) |
| 2278 | |
| 2279 | always_inline uword |
| 2280 | tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2281 | vlib_frame_t * from_frame, int is_ip4) |
| 2282 | { |
| 2283 | u32 n_left_from, next_index, *from, *to_next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2284 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2285 | tcp_main_t *tm = vnet_get_tcp_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2286 | |
| 2287 | from = vlib_frame_vector_args (from_frame); |
| 2288 | n_left_from = from_frame->n_vectors; |
| 2289 | |
| 2290 | next_index = node->cached_next_index; |
| 2291 | |
| 2292 | while (n_left_from > 0) |
| 2293 | { |
| 2294 | u32 n_left_to_next; |
| 2295 | |
| 2296 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 2297 | |
| 2298 | while (n_left_from > 0 && n_left_to_next > 0) |
| 2299 | { |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2300 | int n_advance_bytes0, n_data_bytes0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2301 | u32 bi0; |
| 2302 | vlib_buffer_t *b0; |
| 2303 | tcp_header_t *tcp0 = 0; |
| 2304 | tcp_connection_t *tc0; |
| 2305 | ip4_header_t *ip40; |
| 2306 | ip6_header_t *ip60; |
| 2307 | u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP; |
| 2308 | u8 flags0; |
| 2309 | |
| 2310 | bi0 = from[0]; |
| 2311 | to_next[0] = bi0; |
| 2312 | from += 1; |
| 2313 | to_next += 1; |
| 2314 | n_left_from -= 1; |
| 2315 | n_left_to_next -= 1; |
| 2316 | |
| 2317 | b0 = vlib_get_buffer (vm, bi0); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 2318 | vnet_buffer (b0)->tcp.flags = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2319 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2320 | /* Checksum computed by ipx_local no need to compute again */ |
| 2321 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2322 | if (is_ip4) |
| 2323 | { |
| 2324 | ip40 = vlib_buffer_get_current (b0); |
| 2325 | tcp0 = ip4_next_header (ip40); |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2326 | n_advance_bytes0 = (ip4_header_bytes (ip40) |
| 2327 | + tcp_header_bytes (tcp0)); |
| 2328 | n_data_bytes0 = clib_net_to_host_u16 (ip40->length) |
| 2329 | - n_advance_bytes0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2330 | |
| 2331 | /* lookup session */ |
| 2332 | tc0 = |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2333 | (tcp_connection_t *) |
| 2334 | stream_session_lookup_transport4 (&ip40->dst_address, |
| 2335 | &ip40->src_address, |
| 2336 | tcp0->dst_port, |
| 2337 | tcp0->src_port, |
| 2338 | SESSION_TYPE_IP4_TCP, |
| 2339 | my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2340 | } |
| 2341 | else |
| 2342 | { |
| 2343 | ip60 = vlib_buffer_get_current (b0); |
| 2344 | tcp0 = ip6_next_header (ip60); |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2345 | n_advance_bytes0 = tcp_header_bytes (tcp0); |
| 2346 | n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length) |
| 2347 | - n_advance_bytes0; |
| 2348 | n_advance_bytes0 += sizeof (ip60[0]); |
| 2349 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2350 | tc0 = |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2351 | (tcp_connection_t *) |
| 2352 | stream_session_lookup_transport6 (&ip60->src_address, |
| 2353 | &ip60->dst_address, |
| 2354 | tcp0->src_port, |
| 2355 | tcp0->dst_port, |
| 2356 | SESSION_TYPE_IP6_TCP, |
| 2357 | my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2358 | } |
| 2359 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2360 | /* Length check */ |
| 2361 | if (PREDICT_FALSE (n_advance_bytes0 < 0)) |
| 2362 | { |
| 2363 | error0 = TCP_ERROR_LENGTH; |
| 2364 | goto done; |
| 2365 | } |
| 2366 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2367 | /* Session exists */ |
| 2368 | if (PREDICT_TRUE (0 != tc0)) |
| 2369 | { |
| 2370 | /* Save connection index */ |
| 2371 | vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index; |
| 2372 | vnet_buffer (b0)->tcp.seq_number = |
| 2373 | clib_net_to_host_u32 (tcp0->seq_number); |
| 2374 | vnet_buffer (b0)->tcp.ack_number = |
| 2375 | clib_net_to_host_u32 (tcp0->ack_number); |
| 2376 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2377 | vnet_buffer (b0)->tcp.hdr_offset = (u8 *) tcp0 |
| 2378 | - (u8 *) vlib_buffer_get_current (b0); |
| 2379 | vnet_buffer (b0)->tcp.data_offset = n_advance_bytes0; |
| 2380 | vnet_buffer (b0)->tcp.data_len = n_data_bytes0; |
| 2381 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2382 | flags0 = tcp0->flags & filter_flags; |
| 2383 | next0 = tm->dispatch_table[tc0->state][flags0].next; |
| 2384 | error0 = tm->dispatch_table[tc0->state][flags0].error; |
| 2385 | |
| 2386 | if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH)) |
| 2387 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 2388 | tcp_state_t state0 = tc0->state; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2389 | /* Overload tcp flags to store state */ |
| 2390 | vnet_buffer (b0)->tcp.flags = tc0->state; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 2391 | clib_warning ("disp error state %U flags %U", |
| 2392 | format_tcp_state, &state0, |
Dave Barach | 6368151 | 2017-04-20 17:50:39 -0400 | [diff] [blame] | 2393 | format_tcp_flags, (int) flags0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | else |
| 2397 | { |
| 2398 | /* Send reset */ |
| 2399 | next0 = TCP_INPUT_NEXT_RESET; |
| 2400 | error0 = TCP_ERROR_NO_LISTENER; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2401 | } |
| 2402 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2403 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2404 | b0->error = error0 ? node->errors[error0] : 0; |
| 2405 | |
| 2406 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 2407 | { |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 2408 | tcp_rx_trace_t *t0 = |
| 2409 | vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 2410 | tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 2414 | n_left_to_next, bi0, next0); |
| 2415 | } |
| 2416 | |
| 2417 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 2418 | } |
| 2419 | |
| 2420 | return from_frame->n_vectors; |
| 2421 | } |
| 2422 | |
| 2423 | static uword |
| 2424 | tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2425 | vlib_frame_t * from_frame) |
| 2426 | { |
| 2427 | return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 2428 | } |
| 2429 | |
| 2430 | static uword |
| 2431 | tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2432 | vlib_frame_t * from_frame) |
| 2433 | { |
| 2434 | return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 2435 | } |
| 2436 | |
| 2437 | /* *INDENT-OFF* */ |
| 2438 | VLIB_REGISTER_NODE (tcp4_input_node) = |
| 2439 | { |
| 2440 | .function = tcp4_input, |
| 2441 | .name = "tcp4-input", |
| 2442 | /* Takes a vector of packets. */ |
| 2443 | .vector_size = sizeof (u32), |
| 2444 | .n_errors = TCP_N_ERROR, |
| 2445 | .error_strings = tcp_error_strings, |
| 2446 | .n_next_nodes = TCP_INPUT_N_NEXT, |
| 2447 | .next_nodes = |
| 2448 | { |
| 2449 | #define _(s,n) [TCP_INPUT_NEXT_##s] = n, |
| 2450 | foreach_tcp4_input_next |
| 2451 | #undef _ |
| 2452 | }, |
| 2453 | .format_buffer = format_tcp_header, |
| 2454 | .format_trace = format_tcp_rx_trace, |
| 2455 | }; |
| 2456 | /* *INDENT-ON* */ |
| 2457 | |
| 2458 | VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input); |
| 2459 | |
| 2460 | /* *INDENT-OFF* */ |
| 2461 | VLIB_REGISTER_NODE (tcp6_input_node) = |
| 2462 | { |
| 2463 | .function = tcp6_input, |
| 2464 | .name = "tcp6-input", |
| 2465 | /* Takes a vector of packets. */ |
| 2466 | .vector_size = sizeof (u32), |
| 2467 | .n_errors = TCP_N_ERROR, |
| 2468 | .error_strings = tcp_error_strings, |
| 2469 | .n_next_nodes = TCP_INPUT_N_NEXT, |
| 2470 | .next_nodes = |
| 2471 | { |
| 2472 | #define _(s,n) [TCP_INPUT_NEXT_##s] = n, |
| 2473 | foreach_tcp6_input_next |
| 2474 | #undef _ |
| 2475 | }, |
| 2476 | .format_buffer = format_tcp_header, |
| 2477 | .format_trace = format_tcp_rx_trace, |
| 2478 | }; |
| 2479 | /* *INDENT-ON* */ |
| 2480 | |
| 2481 | VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2482 | |
| 2483 | static void |
| 2484 | tcp_dispatch_table_init (tcp_main_t * tm) |
| 2485 | { |
| 2486 | int i, j; |
| 2487 | for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++) |
| 2488 | for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++) |
| 2489 | { |
| 2490 | tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP; |
| 2491 | tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH; |
| 2492 | } |
| 2493 | |
| 2494 | #define _(t,f,n,e) \ |
| 2495 | do { \ |
| 2496 | tm->dispatch_table[TCP_STATE_##t][f].next = (n); \ |
| 2497 | tm->dispatch_table[TCP_STATE_##t][f].error = (e); \ |
| 2498 | } while (0) |
| 2499 | |
| 2500 | /* SYNs for new connections -> tcp-listen. */ |
| 2501 | _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE); |
| 2502 | /* ACK for for a SYN-ACK -> tcp-rcv-process. */ |
| 2503 | _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 2504 | _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2505 | /* SYN-ACK for a SYN */ |
| 2506 | _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, |
| 2507 | TCP_ERROR_NONE); |
| 2508 | _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE); |
| 2509 | _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE); |
| 2510 | _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, |
| 2511 | TCP_ERROR_NONE); |
| 2512 | /* ACK for for established connection -> tcp-established. */ |
| 2513 | _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE); |
| 2514 | /* FIN for for established connection -> tcp-established. */ |
| 2515 | _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE); |
| 2516 | _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, |
| 2517 | TCP_ERROR_NONE); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 2518 | _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2519 | /* ACK or FIN-ACK to our FIN */ |
| 2520 | _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
| 2521 | _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, |
| 2522 | TCP_ERROR_NONE); |
| 2523 | /* FIN in reply to our FIN from the other side */ |
| 2524 | _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
| 2525 | /* FIN confirming that the peer (app) has closed */ |
| 2526 | _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
| 2527 | _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, |
| 2528 | TCP_ERROR_NONE); |
| 2529 | _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 2530 | _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2531 | #undef _ |
| 2532 | } |
| 2533 | |
| 2534 | clib_error_t * |
| 2535 | tcp_input_init (vlib_main_t * vm) |
| 2536 | { |
| 2537 | clib_error_t *error = 0; |
| 2538 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 2539 | |
| 2540 | if ((error = vlib_call_init_function (vm, tcp_init))) |
| 2541 | return error; |
| 2542 | |
| 2543 | /* Initialize dispatch table. */ |
| 2544 | tcp_dispatch_table_init (tm); |
| 2545 | |
| 2546 | return error; |
| 2547 | } |
| 2548 | |
| 2549 | VLIB_INIT_FUNCTION (tcp_input_init); |
| 2550 | |
| 2551 | /* |
| 2552 | * fd.io coding-style-patch-verification: ON |
| 2553 | * |
| 2554 | * Local Variables: |
| 2555 | * eval: (c-set-style "gnu") |
| 2556 | * End: |
| 2557 | */ |