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 | |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 16 | /** |
| 17 | * @file |
| 18 | * @brief TCP host stack utilities |
| 19 | */ |
| 20 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 21 | #include <vnet/tcp/tcp.h> |
| 22 | #include <vnet/session/session.h> |
| 23 | #include <vnet/fib/fib.h> |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 24 | #include <vnet/dpo/load_balance.h> |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 25 | #include <vnet/dpo/receive_dpo.h> |
| 26 | #include <vnet/ip/ip6_neighbor.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | #include <math.h> |
| 28 | |
| 29 | tcp_main_t tcp_main; |
| 30 | |
| 31 | static u32 |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 32 | tcp_connection_bind (u32 session_index, transport_endpoint_t * lcl) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 33 | { |
| 34 | tcp_main_t *tm = &tcp_main; |
| 35 | tcp_connection_t *listener; |
| 36 | |
| 37 | pool_get (tm->listener_pool, listener); |
| 38 | memset (listener, 0, sizeof (*listener)); |
| 39 | |
| 40 | listener->c_c_index = listener - tm->listener_pool; |
Florin Coras | 0e49568 | 2017-09-19 22:27:18 -0700 | [diff] [blame] | 41 | listener->c_lcl_port = lcl->port; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 43 | if (lcl->is_ip4) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 44 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 45 | listener->c_lcl_ip4.as_u32 = lcl->ip.ip4.as_u32; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 46 | listener->c_is_ip4 = 1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 47 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | else |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 49 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 50 | clib_memcpy (&listener->c_lcl_ip6, &lcl->ip.ip6, |
| 51 | sizeof (ip6_address_t)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 52 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 53 | } |
| 54 | listener->c_transport_proto = TRANSPORT_PROTO_TCP; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | listener->c_s_index = session_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 56 | listener->state = TCP_STATE_LISTEN; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 57 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 58 | tcp_connection_timers_init (listener); |
| 59 | |
| 60 | TCP_EVT_DBG (TCP_EVT_BIND, listener); |
| 61 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 62 | return listener->c_c_index; |
| 63 | } |
| 64 | |
| 65 | u32 |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 66 | tcp_session_bind (u32 session_index, transport_endpoint_t * tep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 67 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 68 | return tcp_connection_bind (session_index, tep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 72 | tcp_connection_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 73 | { |
| 74 | tcp_main_t *tm = vnet_get_tcp_main (); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 75 | tcp_connection_t *tc; |
| 76 | |
| 77 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 78 | |
| 79 | TCP_EVT_DBG (TCP_EVT_UNBIND, tc); |
| 80 | |
| 81 | /* Poison the entry */ |
| 82 | if (CLIB_DEBUG > 0) |
| 83 | memset (tc, 0xFA, sizeof (*tc)); |
| 84 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 85 | pool_put_index (tm->listener_pool, listener_index); |
| 86 | } |
| 87 | |
| 88 | u32 |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 89 | tcp_session_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 90 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 91 | tcp_connection_unbind (listener_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | transport_connection_t * |
| 96 | tcp_session_get_listener (u32 listener_index) |
| 97 | { |
| 98 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 99 | tcp_connection_t *tc; |
| 100 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 101 | return &tc->connection; |
| 102 | } |
| 103 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 104 | always_inline void |
| 105 | transport_endpoint_del (u32 tepi) |
| 106 | { |
| 107 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 108 | clib_spinlock_lock_if_init (&tm->local_endpoints_lock); |
| 109 | pool_put_index (tm->local_endpoints, tepi); |
| 110 | clib_spinlock_unlock_if_init (&tm->local_endpoints_lock); |
| 111 | } |
| 112 | |
| 113 | always_inline transport_endpoint_t * |
| 114 | transport_endpoint_new (void) |
| 115 | { |
| 116 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 117 | transport_endpoint_t *tep; |
| 118 | pool_get (tm->local_endpoints, tep); |
| 119 | return tep; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Cleanup half-open connection |
| 124 | * |
| 125 | */ |
| 126 | void |
| 127 | tcp_half_open_connection_del (tcp_connection_t * tc) |
| 128 | { |
| 129 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 130 | clib_spinlock_lock_if_init (&tm->half_open_lock); |
| 131 | pool_put_index (tm->half_open_connections, tc->c_c_index); |
| 132 | if (CLIB_DEBUG) |
| 133 | memset (tc, 0xFA, sizeof (*tc)); |
| 134 | clib_spinlock_unlock_if_init (&tm->half_open_lock); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Try to cleanup half-open connection |
| 139 | * |
| 140 | * If called from a thread that doesn't own tc, the call won't have any |
| 141 | * effect. |
| 142 | * |
| 143 | * @param tc - connection to be cleaned up |
| 144 | * @return non-zero if cleanup failed. |
| 145 | */ |
| 146 | int |
| 147 | tcp_half_open_connection_cleanup (tcp_connection_t * tc) |
| 148 | { |
| 149 | /* Make sure this is the owning thread */ |
| 150 | if (tc->c_thread_index != vlib_get_thread_index ()) |
| 151 | return 1; |
| 152 | tcp_timer_reset (tc, TCP_TIMER_ESTABLISH); |
| 153 | tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN); |
| 154 | tcp_half_open_connection_del (tc); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | tcp_connection_t * |
| 159 | tcp_half_open_connection_new (void) |
| 160 | { |
| 161 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 162 | tcp_connection_t *tc = 0; |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 163 | ASSERT (vlib_get_thread_index () == 0); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 164 | pool_get (tm->half_open_connections, tc); |
| 165 | memset (tc, 0, sizeof (*tc)); |
| 166 | tc->c_c_index = tc - tm->half_open_connections; |
| 167 | return tc; |
| 168 | } |
| 169 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 170 | /** |
| 171 | * Cleans up connection state. |
| 172 | * |
| 173 | * No notifications. |
| 174 | */ |
| 175 | void |
| 176 | tcp_connection_cleanup (tcp_connection_t * tc) |
| 177 | { |
| 178 | tcp_main_t *tm = &tcp_main; |
| 179 | u32 tepi; |
| 180 | transport_endpoint_t *tep; |
| 181 | |
| 182 | /* Cleanup local endpoint if this was an active connect */ |
| 183 | tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip, |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 184 | clib_net_to_host_u16 (tc->c_lcl_port)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 185 | if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 186 | { |
| 187 | tep = pool_elt_at_index (tm->local_endpoints, tepi); |
| 188 | transport_endpoint_table_del (&tm->local_endpoints_table, tep); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 189 | transport_endpoint_del (tepi); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 192 | /* Check if connection is not yet fully established */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 193 | if (tc->state == TCP_STATE_SYN_SENT) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 194 | { |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 195 | /* Try to remove the half-open connection. If this is not the owning |
| 196 | * thread, tc won't be removed. Retransmit or establish timers will |
| 197 | * eventually expire and call again cleanup on the right thread. */ |
| 198 | tcp_half_open_connection_cleanup (tc); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 199 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 200 | else |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 201 | { |
| 202 | int thread_index = tc->c_thread_index; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 203 | |
| 204 | /* Make sure all timers are cleared */ |
| 205 | tcp_connection_timers_reset (tc); |
| 206 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 207 | /* Poison the entry */ |
| 208 | if (CLIB_DEBUG > 0) |
| 209 | memset (tc, 0xFA, sizeof (*tc)); |
| 210 | pool_put (tm->connections[thread_index], tc); |
| 211 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Connection removal. |
| 216 | * |
| 217 | * This should be called only once connection enters CLOSED state. Note |
| 218 | * that it notifies the session of the removal event, so if the goal is to |
| 219 | * just remove the connection, call tcp_connection_cleanup instead. |
| 220 | */ |
| 221 | void |
| 222 | tcp_connection_del (tcp_connection_t * tc) |
| 223 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 224 | TCP_EVT_DBG (TCP_EVT_DELETE, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 225 | stream_session_delete_notify (&tc->connection); |
| 226 | tcp_connection_cleanup (tc); |
| 227 | } |
| 228 | |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 229 | tcp_connection_t * |
| 230 | tcp_connection_new (u8 thread_index) |
| 231 | { |
| 232 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 233 | tcp_connection_t *tc; |
| 234 | |
| 235 | pool_get (tm->connections[thread_index], tc); |
| 236 | memset (tc, 0, sizeof (*tc)); |
| 237 | tc->c_c_index = tc - tm->connections[thread_index]; |
| 238 | tc->c_thread_index = thread_index; |
| 239 | return tc; |
| 240 | } |
| 241 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 242 | /** Notify session that connection has been reset. |
| 243 | * |
| 244 | * Switch state to closed and wait for session to call cleanup. |
| 245 | */ |
| 246 | void |
| 247 | tcp_connection_reset (tcp_connection_t * tc) |
| 248 | { |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 249 | TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc); |
Florin Coras | 11c0549 | 2017-05-10 12:29:14 -0700 | [diff] [blame] | 250 | switch (tc->state) |
| 251 | { |
| 252 | case TCP_STATE_SYN_RCVD: |
| 253 | /* Cleanup everything. App wasn't notified yet */ |
| 254 | stream_session_delete_notify (&tc->connection); |
| 255 | tcp_connection_cleanup (tc); |
| 256 | break; |
| 257 | case TCP_STATE_SYN_SENT: |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 258 | stream_session_connect_notify (&tc->connection, 1 /* fail */ ); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 259 | tcp_connection_cleanup (tc); |
| 260 | break; |
Florin Coras | 11c0549 | 2017-05-10 12:29:14 -0700 | [diff] [blame] | 261 | case TCP_STATE_ESTABLISHED: |
| 262 | case TCP_STATE_CLOSE_WAIT: |
| 263 | case TCP_STATE_FIN_WAIT_1: |
| 264 | case TCP_STATE_FIN_WAIT_2: |
| 265 | case TCP_STATE_CLOSING: |
| 266 | tc->state = TCP_STATE_CLOSED; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 267 | TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 268 | |
Florin Coras | 11c0549 | 2017-05-10 12:29:14 -0700 | [diff] [blame] | 269 | /* Make sure all timers are cleared */ |
| 270 | tcp_connection_timers_reset (tc); |
Florin Coras | 11c0549 | 2017-05-10 12:29:14 -0700 | [diff] [blame] | 271 | stream_session_reset_notify (&tc->connection); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 272 | |
| 273 | /* Wait for cleanup from session layer but not forever */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 274 | tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME); |
Florin Coras | 11c0549 | 2017-05-10 12:29:14 -0700 | [diff] [blame] | 275 | break; |
| 276 | case TCP_STATE_CLOSED: |
| 277 | return; |
| 278 | } |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 281 | /** |
| 282 | * Begin connection closing procedure. |
| 283 | * |
| 284 | * If at the end the connection is not in CLOSED state, it is not removed. |
| 285 | * Instead, we rely on on TCP to advance through state machine to either |
| 286 | * 1) LAST_ACK (passive close) whereby when the last ACK is received |
| 287 | * tcp_connection_del is called. This notifies session of the delete and |
| 288 | * calls cleanup. |
| 289 | * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers |
| 290 | * and cleanup is called. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 291 | * |
| 292 | * N.B. Half-close connections are not supported |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 293 | */ |
| 294 | void |
| 295 | tcp_connection_close (tcp_connection_t * tc) |
| 296 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 297 | TCP_EVT_DBG (TCP_EVT_CLOSE, tc); |
| 298 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 299 | /* Send/Program FIN if needed and switch state */ |
| 300 | switch (tc->state) |
| 301 | { |
| 302 | case TCP_STATE_SYN_SENT: |
| 303 | tc->state = TCP_STATE_CLOSED; |
| 304 | break; |
| 305 | case TCP_STATE_SYN_RCVD: |
| 306 | tcp_send_fin (tc); |
| 307 | tc->state = TCP_STATE_FIN_WAIT_1; |
| 308 | break; |
| 309 | case TCP_STATE_ESTABLISHED: |
| 310 | if (!stream_session_tx_fifo_max_dequeue (&tc->connection)) |
| 311 | tcp_send_fin (tc); |
| 312 | else |
| 313 | tc->flags |= TCP_CONN_FINPNDG; |
| 314 | tc->state = TCP_STATE_FIN_WAIT_1; |
| 315 | break; |
| 316 | case TCP_STATE_CLOSE_WAIT: |
| 317 | tcp_send_fin (tc); |
| 318 | tc->state = TCP_STATE_LAST_ACK; |
| 319 | break; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 320 | case TCP_STATE_FIN_WAIT_1: |
| 321 | break; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 322 | default: |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 323 | clib_warning ("state: %u", tc->state); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 324 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 325 | |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 326 | TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 328 | /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */ |
| 329 | if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID |
| 330 | && tc->state == TCP_STATE_CLOSED) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 331 | tcp_connection_del (tc); |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | tcp_session_close (u32 conn_index, u32 thread_index) |
| 336 | { |
| 337 | tcp_connection_t *tc; |
| 338 | tc = tcp_connection_get (conn_index, thread_index); |
| 339 | tcp_connection_close (tc); |
| 340 | } |
| 341 | |
| 342 | void |
| 343 | tcp_session_cleanup (u32 conn_index, u32 thread_index) |
| 344 | { |
| 345 | tcp_connection_t *tc; |
| 346 | tc = tcp_connection_get (conn_index, thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 347 | |
| 348 | /* Wait for the session tx events to clear */ |
| 349 | tc->state = TCP_STATE_CLOSED; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 350 | TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 351 | tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void * |
| 355 | ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4) |
| 356 | { |
| 357 | ip_lookup_main_t *lm4 = &ip4_main.lookup_main; |
| 358 | ip_lookup_main_t *lm6 = &ip6_main.lookup_main; |
| 359 | ip_interface_address_t *ia = 0; |
| 360 | |
| 361 | if (is_ip4) |
| 362 | { |
| 363 | /* *INDENT-OFF* */ |
| 364 | foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ , |
| 365 | ({ |
| 366 | return ip_interface_address_get_address (lm4, ia); |
| 367 | })); |
| 368 | /* *INDENT-ON* */ |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | /* *INDENT-OFF* */ |
| 373 | foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ , |
| 374 | ({ |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 375 | ip6_address_t *rv; |
| 376 | rv = ip_interface_address_get_address (lm6, ia); |
| 377 | /* Trying to use a link-local ip6 src address is a fool's errand */ |
| 378 | if (!ip6_address_is_link_local_unicast (rv)) |
| 379 | return rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | })); |
| 381 | /* *INDENT-ON* */ |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 387 | #define PORT_MASK ((1 << 16)- 1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 388 | /** |
| 389 | * Allocate local port and add if successful add entry to local endpoint |
| 390 | * table to mark the pair as used. |
| 391 | */ |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 392 | int |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 393 | tcp_allocate_local_port (ip46_address_t * ip) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 394 | { |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 395 | tcp_main_t *tm = vnet_get_tcp_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 396 | transport_endpoint_t *tep; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 397 | u32 tei; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 398 | u16 min = 1024, max = 65535; /* XXX configurable ? */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 399 | int tries, limit; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 400 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 401 | limit = max - min; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 402 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 403 | /* Only support active opens from thread 0 */ |
| 404 | ASSERT (vlib_get_thread_index () == 0); |
| 405 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 406 | /* Search for first free slot */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 407 | for (tries = 0; tries < limit; tries++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 408 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 409 | u16 port = 0; |
| 410 | |
| 411 | /* Find a port in the specified range */ |
| 412 | while (1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 413 | { |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 414 | port = random_u32 (&tm->port_allocator_seed) & PORT_MASK; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 415 | if (PREDICT_TRUE (port >= min && port < max)) |
| 416 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 419 | /* Look it up */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 420 | tei = transport_endpoint_lookup (&tm->local_endpoints_table, ip, port); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 421 | /* If not found, we're done */ |
| 422 | if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 423 | { |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 424 | clib_spinlock_lock_if_init (&tm->local_endpoints_lock); |
| 425 | tep = transport_endpoint_new (); |
| 426 | clib_memcpy (&tep->ip, ip, sizeof (*ip)); |
| 427 | tep->port = port; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 428 | transport_endpoint_table_add (&tm->local_endpoints_table, tep, |
| 429 | tep - tm->local_endpoints); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 430 | clib_spinlock_unlock_if_init (&tm->local_endpoints_lock); |
| 431 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 432 | return tep->port; |
| 433 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 434 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Initialize all connection timers as invalid |
| 440 | */ |
| 441 | void |
| 442 | tcp_connection_timers_init (tcp_connection_t * tc) |
| 443 | { |
| 444 | int i; |
| 445 | |
| 446 | /* Set all to invalid */ |
| 447 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 448 | { |
| 449 | tc->timers[i] = TCP_TIMER_HANDLE_INVALID; |
| 450 | } |
| 451 | |
| 452 | tc->rto = TCP_RTO_INIT; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Stop all connection timers |
| 457 | */ |
| 458 | void |
| 459 | tcp_connection_timers_reset (tcp_connection_t * tc) |
| 460 | { |
| 461 | int i; |
| 462 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 463 | { |
| 464 | tcp_timer_reset (tc, i); |
| 465 | } |
| 466 | } |
| 467 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 468 | #if 0 |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 469 | typedef struct ip4_tcp_hdr |
| 470 | { |
| 471 | ip4_header_t ip; |
| 472 | tcp_header_t tcp; |
| 473 | } ip4_tcp_hdr_t; |
| 474 | |
| 475 | typedef struct ip6_tcp_hdr |
| 476 | { |
| 477 | ip6_header_t ip; |
| 478 | tcp_header_t tcp; |
| 479 | } ip6_tcp_hdr_t; |
| 480 | |
| 481 | static void |
| 482 | tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo, |
| 483 | dpo_id_t * result) |
| 484 | { |
| 485 | const dpo_id_t *choice; |
| 486 | load_balance_t *lb; |
| 487 | int hash; |
| 488 | |
| 489 | lb = load_balance_get (dpo->dpoi_index); |
| 490 | if (tc->c_is_ip4) |
| 491 | { |
| 492 | ip4_tcp_hdr_t hdr; |
| 493 | memset (&hdr, 0, sizeof (hdr)); |
| 494 | hdr.ip.protocol = IP_PROTOCOL_TCP; |
| 495 | hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32; |
| 496 | hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32; |
| 497 | hdr.tcp.src_port = tc->c_lcl_port; |
| 498 | hdr.tcp.dst_port = tc->c_rmt_port; |
| 499 | hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config); |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | ip6_tcp_hdr_t hdr; |
| 504 | memset (&hdr, 0, sizeof (hdr)); |
| 505 | hdr.ip.protocol = IP_PROTOCOL_TCP; |
| 506 | clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6, |
| 507 | sizeof (ip6_address_t)); |
| 508 | clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6, |
| 509 | sizeof (ip6_address_t)); |
| 510 | hdr.tcp.src_port = tc->c_lcl_port; |
| 511 | hdr.tcp.dst_port = tc->c_rmt_port; |
| 512 | hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config); |
| 513 | } |
| 514 | choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1); |
| 515 | dpo_copy (result, choice); |
| 516 | } |
| 517 | |
| 518 | fib_node_index_t |
| 519 | tcp_lookup_rmt_in_fib (tcp_connection_t * tc) |
| 520 | { |
| 521 | fib_prefix_t prefix; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 522 | u32 fib_index; |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 523 | |
| 524 | clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr)); |
| 525 | prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 526 | prefix.fp_len = tc->c_is_ip4 ? 32 : 128; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 527 | fib_index = fib_table_find (prefix.fp_proto, tc->c_vrf); |
| 528 | return fib_table_lookup (fib_index, &prefix); |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int |
| 532 | tcp_connection_stack_on_fib_entry (tcp_connection_t * tc) |
| 533 | { |
| 534 | dpo_id_t choice = DPO_INVALID; |
| 535 | u32 output_node_index; |
| 536 | fib_entry_t *fe; |
| 537 | |
| 538 | fe = fib_entry_get (tc->c_rmt_fei); |
| 539 | if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE) |
| 540 | return -1; |
| 541 | |
| 542 | tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice); |
| 543 | |
| 544 | output_node_index = |
| 545 | tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index; |
| 546 | dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /** Stack tcp connection on peer's fib entry. |
| 551 | * |
| 552 | * This ultimately populates the dpo the connection will use to send packets. |
| 553 | */ |
| 554 | static void |
| 555 | tcp_connection_fib_attach (tcp_connection_t * tc) |
| 556 | { |
| 557 | tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc); |
| 558 | |
| 559 | ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID); |
| 560 | |
| 561 | tcp_connection_stack_on_fib_entry (tc); |
| 562 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 563 | #endif /* 0 */ |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 564 | |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 565 | /** |
| 566 | * Initialize connection send variables. |
| 567 | */ |
| 568 | void |
| 569 | tcp_init_snd_vars (tcp_connection_t * tc) |
| 570 | { |
| 571 | u32 time_now; |
| 572 | |
| 573 | /* Set random initial sequence */ |
| 574 | time_now = tcp_time_now (); |
| 575 | tc->iss = random_u32 (&time_now); |
| 576 | tc->snd_una = tc->iss; |
| 577 | tc->snd_nxt = tc->iss + 1; |
| 578 | tc->snd_una_max = tc->snd_nxt; |
| 579 | } |
| 580 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | /** Initialize tcp connection variables |
| 582 | * |
| 583 | * Should be called after having received a msg from the peer, i.e., a SYN or |
| 584 | * a SYNACK, such that connection options have already been exchanged. */ |
| 585 | void |
| 586 | tcp_connection_init_vars (tcp_connection_t * tc) |
| 587 | { |
| 588 | tcp_connection_timers_init (tc); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 589 | tcp_init_mss (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 590 | scoreboard_init (&tc->sack_sb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 591 | tcp_cc_init (tc); |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 592 | if (tc->state == TCP_STATE_SYN_RCVD) |
| 593 | tcp_init_snd_vars (tc); |
| 594 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 595 | // tcp_connection_fib_attach (tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | int |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 599 | tcp_connection_open (transport_endpoint_t * rmt) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 600 | { |
| 601 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 602 | tcp_connection_t *tc; |
| 603 | fib_prefix_t prefix; |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 604 | fib_node_index_t fei; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 605 | u32 sw_if_index, fib_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 606 | ip46_address_t lcl_addr; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 607 | int lcl_port; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * Find the local address and allocate port |
| 611 | */ |
| 612 | memset (&lcl_addr, 0, sizeof (lcl_addr)); |
| 613 | |
| 614 | /* Find a FIB path to the destination */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 615 | clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip)); |
| 616 | prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 617 | prefix.fp_len = rmt->is_ip4 ? 32 : 128; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 618 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 619 | fib_index = fib_table_find (prefix.fp_proto, rmt->vrf); |
tjanciga | ffef404 | 2017-08-24 11:57:21 +0200 | [diff] [blame] | 620 | if (fib_index == (u32) ~ 0) |
| 621 | { |
| 622 | clib_warning ("no fib table"); |
| 623 | return -1; |
| 624 | } |
| 625 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 626 | fei = fib_table_lookup (fib_index, &prefix); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 627 | |
| 628 | /* Couldn't find route to destination. Bail out. */ |
| 629 | if (fei == FIB_NODE_INDEX_INVALID) |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 630 | { |
| 631 | clib_warning ("no route to destination"); |
| 632 | return -1; |
| 633 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 634 | |
| 635 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 636 | |
| 637 | if (sw_if_index == (u32) ~ 0) |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 638 | { |
| 639 | clib_warning ("no resolving interface for %U", format_ip46_address, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 640 | &rmt->ip, IP46_TYPE_IP4); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 641 | return -1; |
| 642 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 643 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 644 | if (rmt->is_ip4) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 645 | { |
| 646 | ip4_address_t *ip4; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 647 | int index; |
| 648 | if (vec_len (tm->ip4_src_addresses)) |
| 649 | { |
| 650 | index = tm->last_v4_address_rotor++; |
| 651 | if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses)) |
| 652 | tm->last_v4_address_rotor = 0; |
| 653 | lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32; |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | ip4 = ip_interface_get_first_ip (sw_if_index, 1); |
| 658 | lcl_addr.ip4.as_u32 = ip4->as_u32; |
| 659 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 660 | } |
| 661 | else |
| 662 | { |
| 663 | ip6_address_t *ip6; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 664 | int index; |
| 665 | |
| 666 | if (vec_len (tm->ip6_src_addresses)) |
| 667 | { |
| 668 | index = tm->last_v6_address_rotor++; |
| 669 | if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses)) |
| 670 | tm->last_v6_address_rotor = 0; |
| 671 | clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index], |
| 672 | sizeof (*ip6)); |
| 673 | } |
| 674 | else |
| 675 | { |
| 676 | ip6 = ip_interface_get_first_ip (sw_if_index, 0); |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 677 | if (ip6 == 0) |
| 678 | { |
| 679 | clib_warning ("no routable ip6 addresses on %U", |
| 680 | format_vnet_sw_if_index_name, vnet_get_main (), |
| 681 | sw_if_index); |
| 682 | return -1; |
| 683 | } |
| 684 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 685 | clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6)); |
| 686 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Allocate source port */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 690 | lcl_port = tcp_allocate_local_port (&lcl_addr); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 691 | if (lcl_port < 1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 692 | { |
| 693 | clib_warning ("Failed to allocate src port"); |
| 694 | return -1; |
| 695 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Create connection and send SYN |
| 699 | */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 700 | clib_spinlock_lock_if_init (&tm->half_open_lock); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 701 | tc = tcp_half_open_connection_new (); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 702 | clib_memcpy (&tc->c_rmt_ip, &rmt->ip, sizeof (ip46_address_t)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 703 | clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t)); |
Florin Coras | 0e49568 | 2017-09-19 22:27:18 -0700 | [diff] [blame] | 704 | tc->c_rmt_port = rmt->port; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 705 | tc->c_lcl_port = clib_host_to_net_u16 (lcl_port); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 706 | tc->c_is_ip4 = rmt->is_ip4; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 707 | tc->c_transport_proto = TRANSPORT_PROTO_TCP; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 708 | tc->c_vrf = rmt->vrf; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 709 | /* The other connection vars will be initialized after SYN ACK */ |
| 710 | tcp_connection_timers_init (tc); |
| 711 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 712 | TCP_EVT_DBG (TCP_EVT_OPEN, tc); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 713 | tc->state = TCP_STATE_SYN_SENT; |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 714 | tcp_init_snd_vars (tc); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 715 | tcp_send_syn (tc); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 716 | clib_spinlock_unlock_if_init (&tm->half_open_lock); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 717 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 718 | return tc->c_c_index; |
| 719 | } |
| 720 | |
| 721 | int |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 722 | tcp_session_open (transport_endpoint_t * tep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 723 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 724 | return tcp_connection_open (tep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 725 | } |
| 726 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 727 | const char *tcp_dbg_evt_str[] = { |
| 728 | #define _(sym, str) str, |
| 729 | foreach_tcp_dbg_evt |
| 730 | #undef _ |
| 731 | }; |
| 732 | |
| 733 | const char *tcp_fsm_states[] = { |
| 734 | #define _(sym, str) str, |
| 735 | foreach_tcp_fsm_state |
| 736 | #undef _ |
| 737 | }; |
| 738 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 739 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 740 | format_tcp_state (u8 * s, va_list * args) |
| 741 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 742 | u32 state = va_arg (*args, u32); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 743 | |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 744 | if (state < TCP_N_STATES) |
| 745 | s = format (s, "%s", tcp_fsm_states[state]); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 746 | else |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 747 | s = format (s, "UNKNOWN (%d (0x%x))", state, state); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 748 | return s; |
| 749 | } |
| 750 | |
| 751 | const char *tcp_conn_timers[] = { |
| 752 | #define _(sym, str) str, |
| 753 | foreach_tcp_timer |
| 754 | #undef _ |
| 755 | }; |
| 756 | |
| 757 | u8 * |
| 758 | format_tcp_timers (u8 * s, va_list * args) |
| 759 | { |
| 760 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 761 | int i, last = -1; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 762 | |
| 763 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 764 | if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID) |
| 765 | last = i; |
| 766 | |
| 767 | s = format (s, "["); |
| 768 | for (i = 0; i < last; i++) |
| 769 | { |
| 770 | if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID) |
| 771 | s = format (s, "%s,", tcp_conn_timers[i]); |
| 772 | } |
| 773 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 774 | if (last >= 0) |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 775 | s = format (s, "%s]", tcp_conn_timers[i]); |
| 776 | else |
| 777 | s = format (s, "]"); |
| 778 | |
| 779 | return s; |
| 780 | } |
| 781 | |
| 782 | u8 * |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 783 | format_tcp_congestion_status (u8 * s, va_list * args) |
| 784 | { |
| 785 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 786 | if (tcp_in_recovery (tc)) |
| 787 | s = format (s, "recovery"); |
| 788 | else if (tcp_in_fastrecovery (tc)) |
| 789 | s = format (s, "fastrecovery"); |
| 790 | else |
| 791 | s = format (s, "none"); |
| 792 | return s; |
| 793 | } |
| 794 | |
| 795 | u8 * |
| 796 | format_tcp_vars (u8 * s, va_list * args) |
| 797 | { |
| 798 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 799 | s = format (s, " snd_una %u snd_nxt %u snd_una_max %u", |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 800 | tc->snd_una - tc->iss, tc->snd_nxt - tc->iss, |
| 801 | tc->snd_una_max - tc->iss); |
| 802 | s = format (s, " rcv_nxt %u rcv_las %u\n", |
| 803 | tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs); |
| 804 | s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n", |
| 805 | tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs, |
| 806 | tc->snd_wl2 - tc->iss); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 807 | s = format (s, " flight size %u send space %u rcv_wnd_av %d\n", |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 808 | tcp_flight_size (tc), tcp_available_output_snd_space (tc), |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 809 | tcp_rcv_wnd_available (tc)); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 810 | s = format (s, " cong %U ", format_tcp_congestion_status, tc); |
| 811 | s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n", |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 812 | tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 813 | s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u", |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 814 | tc->prev_ssthresh, tc->snd_congestion - tc->iss, |
| 815 | tc->rcv_dupacks); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 816 | s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss); |
| 817 | s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr, |
| 818 | tc->tsecr_last_ack); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 819 | s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto, |
| 820 | tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts); |
| 821 | s = format (s, "rtt_seq %u\n", tc->rtt_seq); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 822 | s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent, |
| 823 | tcp_time_now () - tc->tsval_recent_age); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 824 | s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb, |
| 825 | tc); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 826 | if (vec_len (tc->snd_sacks)) |
| 827 | s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc); |
| 828 | |
| 829 | return s; |
| 830 | } |
| 831 | |
| 832 | u8 * |
| 833 | format_tcp_connection_id (u8 * s, va_list * args) |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 834 | { |
| 835 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 836 | if (!tc) |
| 837 | return s; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 838 | if (tc->c_is_ip4) |
| 839 | { |
| 840 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 841 | format_ip4_address, &tc->c_lcl_ip4, |
| 842 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address, |
| 843 | &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 848 | format_ip6_address, &tc->c_lcl_ip6, |
| 849 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address, |
| 850 | &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 851 | } |
| 852 | |
| 853 | return s; |
| 854 | } |
| 855 | |
| 856 | u8 * |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 857 | format_tcp_connection (u8 * s, va_list * args) |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 858 | { |
| 859 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 860 | u32 verbose = va_arg (*args, u32); |
| 861 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 862 | if (!tc) |
| 863 | return s; |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 864 | s = format (s, "%-50U", format_tcp_connection_id, tc); |
| 865 | if (verbose) |
| 866 | { |
| 867 | s = format (s, "%-15U", format_tcp_state, tc->state); |
| 868 | if (verbose > 1) |
| 869 | s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc); |
| 870 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 871 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 872 | return s; |
| 873 | } |
| 874 | |
| 875 | u8 * |
| 876 | format_tcp_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 877 | { |
| 878 | u32 tci = va_arg (*args, u32); |
| 879 | u32 thread_index = va_arg (*args, u32); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 880 | u32 verbose = va_arg (*args, u32); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 881 | tcp_connection_t *tc; |
| 882 | |
| 883 | tc = tcp_connection_get (tci, thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 884 | if (tc) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 885 | s = format (s, "%U", format_tcp_connection, tc, verbose); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 886 | else |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 887 | s = format (s, "empty\n"); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 888 | return s; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 892 | format_tcp_listener_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 893 | { |
| 894 | u32 tci = va_arg (*args, u32); |
| 895 | tcp_connection_t *tc = tcp_listener_get (tci); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 896 | return format (s, "%U", format_tcp_connection_id, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 900 | format_tcp_half_open_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 901 | { |
| 902 | u32 tci = va_arg (*args, u32); |
| 903 | tcp_connection_t *tc = tcp_half_open_connection_get (tci); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 904 | return format (s, "%U", format_tcp_connection_id, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 905 | } |
| 906 | |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 907 | u8 * |
| 908 | format_tcp_sacks (u8 * s, va_list * args) |
| 909 | { |
| 910 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 911 | sack_block_t *sacks = tc->snd_sacks; |
| 912 | sack_block_t *block; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 913 | int i, len = 0; |
| 914 | |
| 915 | len = vec_len (sacks); |
| 916 | for (i = 0; i < len - 1; i++) |
| 917 | { |
| 918 | block = &sacks[i]; |
| 919 | s = format (s, " start %u end %u\n", block->start - tc->irs, |
| 920 | block->end - tc->irs); |
| 921 | } |
| 922 | if (len) |
| 923 | { |
| 924 | block = &sacks[len - 1]; |
| 925 | s = format (s, " start %u end %u", block->start - tc->irs, |
| 926 | block->end - tc->irs); |
| 927 | } |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 928 | return s; |
| 929 | } |
| 930 | |
| 931 | u8 * |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 932 | format_tcp_rcv_sacks (u8 * s, va_list * args) |
| 933 | { |
| 934 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 935 | sack_block_t *sacks = tc->rcv_opts.sacks; |
| 936 | sack_block_t *block; |
| 937 | int i, len = 0; |
| 938 | |
| 939 | len = vec_len (sacks); |
| 940 | for (i = 0; i < len - 1; i++) |
| 941 | { |
| 942 | block = &sacks[i]; |
| 943 | s = format (s, " start %u end %u\n", block->start - tc->iss, |
| 944 | block->end - tc->iss); |
| 945 | } |
| 946 | if (len) |
| 947 | { |
| 948 | block = &sacks[len - 1]; |
| 949 | s = format (s, " start %u end %u", block->start - tc->iss, |
| 950 | block->end - tc->iss); |
| 951 | } |
| 952 | return s; |
| 953 | } |
| 954 | |
| 955 | u8 * |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 956 | format_tcp_sack_hole (u8 * s, va_list * args) |
| 957 | { |
| 958 | sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 959 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 960 | if (tc) |
| 961 | s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss); |
| 962 | else |
| 963 | s = format (s, " [%u, %u]", hole->start, hole->end); |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 964 | return s; |
| 965 | } |
| 966 | |
| 967 | u8 * |
| 968 | format_tcp_scoreboard (u8 * s, va_list * args) |
| 969 | { |
| 970 | sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 971 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 972 | sack_scoreboard_hole_t *hole; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 973 | s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n", |
| 974 | sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes); |
| 975 | s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n", |
| 976 | sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv); |
| 977 | s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u", |
| 978 | sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt); |
| 979 | |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 980 | hole = scoreboard_first_hole (sb); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 981 | if (hole) |
| 982 | s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail); |
| 983 | |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 984 | while (hole) |
| 985 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 986 | s = format (s, "%U", format_tcp_sack_hole, hole, tc); |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 987 | hole = scoreboard_next_hole (sb, hole); |
| 988 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 989 | |
Florin Coras | 06d1101 | 2017-05-17 14:21:51 -0700 | [diff] [blame] | 990 | return s; |
| 991 | } |
| 992 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 993 | transport_connection_t * |
| 994 | tcp_session_get_transport (u32 conn_index, u32 thread_index) |
| 995 | { |
| 996 | tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index); |
| 997 | return &tc->connection; |
| 998 | } |
| 999 | |
| 1000 | transport_connection_t * |
| 1001 | tcp_half_open_session_get_transport (u32 conn_index) |
| 1002 | { |
| 1003 | tcp_connection_t *tc = tcp_half_open_connection_get (conn_index); |
| 1004 | return &tc->connection; |
| 1005 | } |
| 1006 | |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1007 | /** |
| 1008 | * Compute maximum segment size for session layer. |
| 1009 | * |
| 1010 | * Since the result needs to be the actual data length, it first computes |
| 1011 | * the tcp options to be used in the next burst and subtracts their |
| 1012 | * length from the connection's snd_mss. |
| 1013 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1014 | u16 |
| 1015 | tcp_session_send_mss (transport_connection_t * trans_conn) |
| 1016 | { |
| 1017 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1018 | |
| 1019 | /* Ensure snd_mss does accurately reflect the amount of data we can push |
| 1020 | * in a segment. This also makes sure that options are updated according to |
| 1021 | * the current state of the connection. */ |
| 1022 | tcp_update_snd_mss (tc); |
| 1023 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1024 | return tc->snd_mss; |
| 1025 | } |
| 1026 | |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1027 | always_inline u32 |
| 1028 | tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space) |
| 1029 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1030 | if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss)) |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1031 | { |
Florin Coras | db84e57 | 2017-05-09 18:54:52 -0700 | [diff] [blame] | 1032 | return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0; |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1035 | /* If not snd_wnd constrained and we can't write at least a segment, |
| 1036 | * don't try at all */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1037 | if (PREDICT_FALSE (snd_space < tc->snd_mss)) |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 1038 | return snd_space < tc->cwnd ? 0 : snd_space; |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1039 | |
| 1040 | /* round down to mss multiple */ |
| 1041 | return snd_space - (snd_space % tc->snd_mss); |
| 1042 | } |
| 1043 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1044 | /** |
| 1045 | * Compute tx window session is allowed to fill. |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1046 | * |
| 1047 | * Takes into account available send space, snd_mss and the congestion |
| 1048 | * state of the connection. If possible, the value returned is a multiple |
| 1049 | * of snd_mss. |
| 1050 | * |
| 1051 | * @param tc tcp connection |
| 1052 | * @return number of bytes session is allowed to write |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1053 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1054 | u32 |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1055 | tcp_snd_space (tcp_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1056 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1057 | int snd_space, snt_limited; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1058 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1059 | if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1060 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1061 | snd_space = tcp_available_output_snd_space (tc); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1062 | |
| 1063 | /* If we haven't gotten dupacks or if we did and have gotten sacked |
| 1064 | * bytes then we can still send as per Limited Transmit (RFC3042) */ |
| 1065 | if (PREDICT_FALSE (tc->rcv_dupacks != 0 |
| 1066 | && (tcp_opts_sack_permitted (tc) |
| 1067 | && tc->sack_sb.last_sacked_bytes == 0))) |
| 1068 | { |
| 1069 | if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt) |
| 1070 | tc->limited_transmit = tc->snd_nxt; |
| 1071 | ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt)); |
| 1072 | |
| 1073 | snt_limited = tc->snd_nxt - tc->limited_transmit; |
| 1074 | snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0); |
| 1075 | } |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1076 | return tcp_round_snd_space (tc, snd_space); |
| 1077 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1078 | |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1079 | if (tcp_in_recovery (tc)) |
| 1080 | { |
| 1081 | tc->snd_nxt = tc->snd_una_max; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1082 | snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1083 | - (tc->snd_una_max - tc->snd_congestion); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 1084 | if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1085 | return 0; |
Florin Coras | 3af90fc | 2017-05-03 21:09:42 -0700 | [diff] [blame] | 1086 | return tcp_round_snd_space (tc, snd_space); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1089 | /* RFC 5681: When previously unsent data is available and the new value of |
| 1090 | * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS |
| 1091 | * bytes of previously unsent data. */ |
| 1092 | if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1093 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1094 | if (tcp_available_output_snd_space (tc) < tc->snd_mss) |
| 1095 | return 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1096 | tcp_fastrecovery_1_smss_on (tc); |
| 1097 | return tc->snd_mss; |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | u32 |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1104 | tcp_session_send_space (transport_connection_t * trans_conn) |
| 1105 | { |
| 1106 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1107 | return clib_min (tcp_snd_space (tc), |
| 1108 | tc->snd_wnd - (tc->snd_nxt - tc->snd_una)); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 1111 | i32 |
| 1112 | tcp_rcv_wnd_available (tcp_connection_t * tc) |
| 1113 | { |
| 1114 | return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las); |
| 1115 | } |
| 1116 | |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 1117 | u32 |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1118 | tcp_session_tx_fifo_offset (transport_connection_t * trans_conn) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1119 | { |
| 1120 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1121 | |
| 1122 | ASSERT (seq_geq (tc->snd_nxt, tc->snd_una)); |
| 1123 | |
| 1124 | /* This still works if fast retransmit is on */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1125 | return (tc->snd_nxt - tc->snd_una); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* *INDENT-OFF* */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1129 | const static transport_proto_vft_t tcp_proto = { |
| 1130 | .bind = tcp_session_bind, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1131 | .unbind = tcp_session_unbind, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1132 | .push_header = tcp_push_header, |
| 1133 | .get_connection = tcp_session_get_transport, |
| 1134 | .get_listener = tcp_session_get_listener, |
| 1135 | .get_half_open = tcp_half_open_session_get_transport, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1136 | .open = tcp_session_open, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1137 | .close = tcp_session_close, |
| 1138 | .cleanup = tcp_session_cleanup, |
| 1139 | .send_mss = tcp_session_send_mss, |
| 1140 | .send_space = tcp_session_send_space, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1141 | .tx_fifo_offset = tcp_session_tx_fifo_offset, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1142 | .format_connection = format_tcp_session, |
| 1143 | .format_listener = format_tcp_listener_session, |
| 1144 | .format_half_open = format_tcp_half_open_session, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1145 | }; |
| 1146 | /* *INDENT-ON* */ |
| 1147 | |
| 1148 | void |
| 1149 | tcp_timer_keep_handler (u32 conn_index) |
| 1150 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1151 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1152 | tcp_connection_t *tc; |
| 1153 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1154 | tc = tcp_connection_get (conn_index, thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1155 | tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID; |
| 1156 | |
| 1157 | tcp_connection_close (tc); |
| 1158 | } |
| 1159 | |
| 1160 | void |
| 1161 | tcp_timer_establish_handler (u32 conn_index) |
| 1162 | { |
| 1163 | tcp_connection_t *tc; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1164 | |
| 1165 | tc = tcp_half_open_connection_get (conn_index); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 1166 | if (tc) |
| 1167 | { |
| 1168 | ASSERT (tc->state == TCP_STATE_SYN_SENT); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 1169 | stream_session_connect_notify (&tc->connection, 1 /* fail */ ); |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 1170 | TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 1171 | } |
| 1172 | else |
| 1173 | { |
| 1174 | tc = tcp_connection_get (conn_index, vlib_get_thread_index ()); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1175 | /* note: the connection may have already disappeared */ |
| 1176 | if (PREDICT_FALSE (tc == 0)) |
| 1177 | return; |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 1178 | TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 1179 | ASSERT (tc->state == TCP_STATE_SYN_RCVD); |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 1180 | /* Start cleanup. App wasn't notified yet so use delete notify as |
| 1181 | * opposed to delete to cleanup session layer state. */ |
| 1182 | stream_session_delete_notify (&tc->connection); |
Florin Coras | ab0289a | 2017-08-14 11:25:25 -0700 | [diff] [blame] | 1183 | } |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 1184 | tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1185 | tcp_connection_cleanup (tc); |
| 1186 | } |
| 1187 | |
| 1188 | void |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1189 | tcp_timer_waitclose_handler (u32 conn_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1190 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1191 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1192 | tcp_connection_t *tc; |
| 1193 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1194 | tc = tcp_connection_get (conn_index, thread_index); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1195 | if (!tc) |
| 1196 | return; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1197 | tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID; |
| 1198 | |
| 1199 | /* Session didn't come back with a close(). Send FIN either way |
| 1200 | * and switch to LAST_ACK. */ |
| 1201 | if (tc->state == TCP_STATE_CLOSE_WAIT) |
| 1202 | { |
| 1203 | if (tc->flags & TCP_CONN_FINSNT) |
| 1204 | { |
| 1205 | clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!"); |
| 1206 | } |
| 1207 | |
| 1208 | tcp_send_fin (tc); |
| 1209 | tc->state = TCP_STATE_LAST_ACK; |
| 1210 | |
| 1211 | /* Make sure we don't wait in LAST ACK forever */ |
| 1212 | tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME); |
| 1213 | |
| 1214 | /* Don't delete the connection yet */ |
| 1215 | return; |
| 1216 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1217 | |
| 1218 | tcp_connection_del (tc); |
| 1219 | } |
| 1220 | |
| 1221 | /* *INDENT-OFF* */ |
| 1222 | static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] = |
| 1223 | { |
| 1224 | tcp_timer_retransmit_handler, |
| 1225 | tcp_timer_delack_handler, |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1226 | tcp_timer_persist_handler, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1227 | tcp_timer_keep_handler, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1228 | tcp_timer_waitclose_handler, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1229 | tcp_timer_retransmit_syn_handler, |
| 1230 | tcp_timer_establish_handler |
| 1231 | }; |
| 1232 | /* *INDENT-ON* */ |
| 1233 | |
| 1234 | static void |
| 1235 | tcp_expired_timers_dispatch (u32 * expired_timers) |
| 1236 | { |
| 1237 | int i; |
| 1238 | u32 connection_index, timer_id; |
| 1239 | |
| 1240 | for (i = 0; i < vec_len (expired_timers); i++) |
| 1241 | { |
| 1242 | /* Get session index and timer id */ |
| 1243 | connection_index = expired_timers[i] & 0x0FFFFFFF; |
| 1244 | timer_id = expired_timers[i] >> 28; |
| 1245 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1246 | TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id); |
| 1247 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1248 | /* Handle expiration */ |
| 1249 | (*timer_expiration_handlers[timer_id]) (connection_index); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | void |
| 1254 | tcp_initialize_timer_wheels (tcp_main_t * tm) |
| 1255 | { |
| 1256 | tw_timer_wheel_16t_2w_512sl_t *tw; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1257 | /* *INDENT-OFF* */ |
| 1258 | foreach_vlib_main (({ |
| 1259 | tw = &tm->timer_wheels[ii]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1260 | tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch, |
| 1261 | 100e-3 /* timer period 100ms */ , ~0); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1262 | tw->last_run_time = vlib_time_now (this_vlib_main); |
| 1263 | })); |
| 1264 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | clib_error_t * |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1268 | tcp_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1269 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1270 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1271 | ip_protocol_info_t *pi; |
| 1272 | ip_main_t *im = &ip_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1273 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 1274 | clib_error_t *error = 0; |
| 1275 | u32 num_threads; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1276 | int thread; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1277 | tcp_connection_t *tc __attribute__ ((unused)); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1278 | u32 preallocated_connections_per_thread; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1279 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1280 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 1281 | return error; |
| 1282 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 1283 | return error; |
| 1284 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 1285 | return error; |
| 1286 | |
| 1287 | /* |
| 1288 | * Registrations |
| 1289 | */ |
| 1290 | |
| 1291 | /* Register with IP */ |
| 1292 | pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP); |
| 1293 | if (pi == 0) |
| 1294 | return clib_error_return (0, "TCP protocol info AWOL"); |
| 1295 | pi->format_header = format_tcp_header; |
| 1296 | pi->unformat_pg_edit = unformat_pg_tcp_header; |
| 1297 | |
| 1298 | ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index); |
root | c9d1c5b | 2017-08-15 12:58:31 -0400 | [diff] [blame] | 1299 | ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1300 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1301 | /* Register as transport with session layer */ |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1302 | session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto); |
| 1303 | session_register_transport (TRANSPORT_PROTO_TCP, 0, &tcp_proto); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1304 | |
| 1305 | /* |
| 1306 | * Initialize data structures |
| 1307 | */ |
| 1308 | |
| 1309 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 1310 | vec_validate (tm->connections, num_threads - 1); |
| 1311 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1312 | /* |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1313 | * Preallocate connections. Assume that thread 0 won't |
| 1314 | * use preallocated threads when running multi-core |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1315 | */ |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1316 | if (num_threads == 1) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1317 | { |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1318 | thread = 0; |
| 1319 | preallocated_connections_per_thread = tm->preallocated_connections; |
| 1320 | } |
| 1321 | else |
| 1322 | { |
| 1323 | thread = 1; |
| 1324 | preallocated_connections_per_thread = |
| 1325 | tm->preallocated_connections / (num_threads - 1); |
| 1326 | } |
| 1327 | for (; thread < num_threads; thread++) |
| 1328 | { |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1329 | if (preallocated_connections_per_thread) |
| 1330 | pool_init_fixed (tm->connections[thread], |
| 1331 | preallocated_connections_per_thread); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1335 | * Use a preallocated half-open connection pool? |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1336 | */ |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1337 | if (tm->preallocated_half_open_connections) |
| 1338 | pool_init_fixed (tm->half_open_connections, |
| 1339 | tm->preallocated_half_open_connections); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1340 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1341 | /* Initialize per worker thread tx buffers (used for control messages) */ |
| 1342 | vec_validate (tm->tx_buffers, num_threads - 1); |
| 1343 | |
| 1344 | /* Initialize timer wheels */ |
| 1345 | vec_validate (tm->timer_wheels, num_threads - 1); |
| 1346 | tcp_initialize_timer_wheels (tm); |
| 1347 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1348 | /* Initialize clocks per tick for TCP timestamp. Used to compute |
| 1349 | * monotonically increasing timestamps. */ |
| 1350 | tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock |
| 1351 | / TCP_TSTAMP_RESOLUTION; |
| 1352 | |
Dave Barach | d84ba85 | 2017-08-22 17:56:46 -0400 | [diff] [blame] | 1353 | if (tm->local_endpoints_table_buckets == 0) |
| 1354 | tm->local_endpoints_table_buckets = 250000; |
| 1355 | if (tm->local_endpoints_table_memory == 0) |
| 1356 | tm->local_endpoints_table_memory = 512 << 20; |
| 1357 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1358 | clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table", |
Dave Barach | d84ba85 | 2017-08-22 17:56:46 -0400 | [diff] [blame] | 1359 | tm->local_endpoints_table_buckets, |
| 1360 | tm->local_endpoints_table_memory); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1361 | |
| 1362 | /* Initialize [port-allocator] random number seed */ |
| 1363 | tm->port_allocator_seed = (u32) clib_cpu_time_now (); |
| 1364 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1365 | if (num_threads > 1) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 1366 | { |
| 1367 | clib_spinlock_init (&tm->half_open_lock); |
| 1368 | clib_spinlock_init (&tm->local_endpoints_lock); |
| 1369 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1370 | |
| 1371 | vec_validate (tm->tx_frames[0], num_threads - 1); |
| 1372 | vec_validate (tm->tx_frames[1], num_threads - 1); |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 1373 | vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1); |
| 1374 | vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1375 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1376 | tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size |
| 1377 | (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Florin Coras | 82d3ec8 | 2017-08-14 08:10:42 -0700 | [diff] [blame] | 1378 | |
| 1379 | vec_validate (tm->time_now, num_threads - 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1380 | return error; |
| 1381 | } |
| 1382 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1383 | clib_error_t * |
| 1384 | vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en) |
| 1385 | { |
| 1386 | if (is_en) |
| 1387 | { |
| 1388 | if (tcp_main.is_enabled) |
| 1389 | return 0; |
| 1390 | |
| 1391 | return tcp_main_enable (vm); |
| 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | tcp_main.is_enabled = 0; |
| 1396 | } |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 1401 | void |
| 1402 | tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add) |
| 1403 | { |
| 1404 | tcp_main_t *tm = &tcp_main; |
| 1405 | if (is_ip4) |
| 1406 | tm->punt_unknown4 = is_add; |
| 1407 | else |
| 1408 | tm->punt_unknown6 = is_add; |
| 1409 | } |
| 1410 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1411 | clib_error_t * |
| 1412 | tcp_init (vlib_main_t * vm) |
| 1413 | { |
| 1414 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1415 | tm->is_enabled = 0; |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1416 | tcp_api_reference (); |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 1417 | return 0; |
| 1418 | } |
| 1419 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1420 | VLIB_INIT_FUNCTION (tcp_init); |
| 1421 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1422 | static clib_error_t * |
| 1423 | tcp_config_fn (vlib_main_t * vm, unformat_input_t * input) |
| 1424 | { |
| 1425 | tcp_main_t *tm = vnet_get_tcp_main (); |
Dave Barach | d84ba85 | 2017-08-22 17:56:46 -0400 | [diff] [blame] | 1426 | u64 tmp; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1427 | |
| 1428 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1429 | { |
| 1430 | if (unformat |
| 1431 | (input, "preallocated-connections %d", |
| 1432 | &tm->preallocated_connections)) |
| 1433 | ; |
| 1434 | else if (unformat (input, "preallocated-half-open-connections %d", |
| 1435 | &tm->preallocated_half_open_connections)) |
| 1436 | ; |
Dave Barach | d84ba85 | 2017-08-22 17:56:46 -0400 | [diff] [blame] | 1437 | else if (unformat (input, "local-endpoints-table-memory %U", |
| 1438 | unformat_memory_size, &tmp)) |
| 1439 | { |
| 1440 | if (tmp >= 0x100000000) |
| 1441 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 1442 | tmp, tmp); |
| 1443 | tm->local_endpoints_table_memory = tmp; |
| 1444 | } |
| 1445 | else if (unformat (input, "local-endpoints-table-buckets %d", |
| 1446 | &tm->local_endpoints_table_buckets)) |
| 1447 | ; |
| 1448 | |
| 1449 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1450 | else |
| 1451 | return clib_error_return (0, "unknown input `%U'", |
| 1452 | format_unformat_error, input); |
| 1453 | } |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp"); |
| 1458 | |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1459 | |
| 1460 | /** |
| 1461 | * \brief Configure an ipv4 source address range |
| 1462 | * @param vm vlib_main_t pointer |
| 1463 | * @param start first ipv4 address in the source address range |
| 1464 | * @param end last ipv4 address in the source address range |
| 1465 | * @param table_id VRF / table ID, 0 for the default FIB |
| 1466 | * @return 0 if all OK, else an error indication from api_errno.h |
| 1467 | */ |
| 1468 | |
| 1469 | int |
| 1470 | tcp_configure_v4_source_address_range (vlib_main_t * vm, |
| 1471 | ip4_address_t * start, |
| 1472 | ip4_address_t * end, u32 table_id) |
| 1473 | { |
| 1474 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1475 | vnet_main_t *vnm = vnet_get_main (); |
| 1476 | u32 start_host_byte_order, end_host_byte_order; |
| 1477 | fib_prefix_t prefix; |
| 1478 | vnet_sw_interface_t *si; |
| 1479 | fib_node_index_t fei; |
| 1480 | u32 fib_index = 0; |
| 1481 | u32 sw_if_index; |
| 1482 | int rv; |
| 1483 | int vnet_proxy_arp_add_del (ip4_address_t * lo_addr, |
| 1484 | ip4_address_t * hi_addr, u32 fib_index, |
| 1485 | int is_del); |
| 1486 | |
| 1487 | memset (&prefix, 0, sizeof (prefix)); |
| 1488 | |
| 1489 | fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id); |
| 1490 | |
| 1491 | if (fib_index == ~0) |
| 1492 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 1493 | |
| 1494 | start_host_byte_order = clib_net_to_host_u32 (start->as_u32); |
| 1495 | end_host_byte_order = clib_net_to_host_u32 (end->as_u32); |
| 1496 | |
| 1497 | /* sanity check for reversed args or some such */ |
| 1498 | if ((end_host_byte_order - start_host_byte_order) > (10 << 10)) |
| 1499 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 1500 | |
| 1501 | /* Lookup the last address, to identify the interface involved */ |
| 1502 | prefix.fp_len = 32; |
| 1503 | prefix.fp_proto = FIB_PROTOCOL_IP4; |
| 1504 | memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t)); |
| 1505 | |
| 1506 | fei = fib_table_lookup (fib_index, &prefix); |
| 1507 | |
| 1508 | /* Couldn't find route to destination. Bail out. */ |
| 1509 | if (fei == FIB_NODE_INDEX_INVALID) |
| 1510 | return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB; |
| 1511 | |
| 1512 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 1513 | |
| 1514 | /* Enable proxy arp on the interface */ |
| 1515 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 1516 | si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP; |
| 1517 | |
| 1518 | /* Configure proxy arp across the range */ |
| 1519 | rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ ); |
| 1520 | |
| 1521 | if (rv) |
| 1522 | return rv; |
| 1523 | |
| 1524 | do |
| 1525 | { |
| 1526 | dpo_id_t dpo = DPO_INVALID; |
| 1527 | |
| 1528 | vec_add1 (tm->ip4_src_addresses, start[0]); |
| 1529 | |
| 1530 | /* Add local adjacencies for the range */ |
| 1531 | |
| 1532 | receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ , |
| 1533 | NULL, &dpo); |
| 1534 | prefix.fp_len = 32; |
| 1535 | prefix.fp_proto = FIB_PROTOCOL_IP4; |
| 1536 | prefix.fp_addr.ip4.as_u32 = start->as_u32; |
| 1537 | |
| 1538 | fib_table_entry_special_dpo_update (fib_index, |
| 1539 | &prefix, |
| 1540 | FIB_SOURCE_API, |
| 1541 | FIB_ENTRY_FLAG_EXCLUSIVE, &dpo); |
| 1542 | dpo_reset (&dpo); |
| 1543 | |
| 1544 | start_host_byte_order++; |
| 1545 | start->as_u32 = clib_host_to_net_u32 (start_host_byte_order); |
| 1546 | } |
| 1547 | while (start_host_byte_order <= end_host_byte_order); |
| 1548 | |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * \brief Configure an ipv6 source address range |
| 1554 | * @param vm vlib_main_t pointer |
| 1555 | * @param start first ipv6 address in the source address range |
| 1556 | * @param end last ipv6 address in the source address range |
| 1557 | * @param table_id VRF / table ID, 0 for the default FIB |
| 1558 | * @return 0 if all OK, else an error indication from api_errno.h |
| 1559 | */ |
| 1560 | |
| 1561 | int |
| 1562 | tcp_configure_v6_source_address_range (vlib_main_t * vm, |
| 1563 | ip6_address_t * start, |
| 1564 | ip6_address_t * end, u32 table_id) |
| 1565 | { |
| 1566 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1567 | fib_prefix_t prefix; |
| 1568 | u32 fib_index = 0; |
| 1569 | fib_node_index_t fei; |
| 1570 | u32 sw_if_index; |
| 1571 | |
| 1572 | memset (&prefix, 0, sizeof (prefix)); |
| 1573 | |
| 1574 | fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id); |
| 1575 | |
| 1576 | if (fib_index == ~0) |
| 1577 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 1578 | |
| 1579 | while (1) |
| 1580 | { |
| 1581 | int i; |
| 1582 | ip6_address_t tmp; |
| 1583 | dpo_id_t dpo = DPO_INVALID; |
| 1584 | |
| 1585 | /* Remember this address */ |
| 1586 | vec_add1 (tm->ip6_src_addresses, start[0]); |
| 1587 | |
| 1588 | /* Lookup the prefix, to identify the interface involved */ |
| 1589 | prefix.fp_len = 128; |
| 1590 | prefix.fp_proto = FIB_PROTOCOL_IP6; |
| 1591 | memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t)); |
| 1592 | |
| 1593 | fei = fib_table_lookup (fib_index, &prefix); |
| 1594 | |
| 1595 | /* Couldn't find route to destination. Bail out. */ |
| 1596 | if (fei == FIB_NODE_INDEX_INVALID) |
| 1597 | return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB; |
| 1598 | |
| 1599 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 1600 | |
| 1601 | if (sw_if_index == (u32) ~ 0) |
| 1602 | return VNET_API_ERROR_NO_MATCHING_INTERFACE; |
| 1603 | |
| 1604 | /* Add a proxy neighbor discovery entry for this address */ |
| 1605 | ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ ); |
| 1606 | |
| 1607 | /* Add a receive adjacency for this address */ |
| 1608 | receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ , |
| 1609 | NULL, &dpo); |
| 1610 | |
| 1611 | fib_table_entry_special_dpo_update (fib_index, |
| 1612 | &prefix, |
| 1613 | FIB_SOURCE_API, |
| 1614 | FIB_ENTRY_FLAG_EXCLUSIVE, &dpo); |
| 1615 | dpo_reset (&dpo); |
| 1616 | |
| 1617 | /* Done with the entire range? */ |
| 1618 | if (!memcmp (start, end, sizeof (start[0]))) |
| 1619 | break; |
| 1620 | |
| 1621 | /* Increment the address. DGMS. */ |
| 1622 | tmp = start[0]; |
| 1623 | for (i = 15; i >= 0; i--) |
| 1624 | { |
| 1625 | tmp.as_u8[i] += 1; |
| 1626 | if (tmp.as_u8[i] != 0) |
| 1627 | break; |
| 1628 | } |
| 1629 | start[0] = tmp; |
| 1630 | } |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1634 | static clib_error_t * |
| 1635 | tcp_src_address (vlib_main_t * vm, |
| 1636 | unformat_input_t * input, vlib_cli_command_t * cmd_arg) |
| 1637 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1638 | ip4_address_t v4start, v4end; |
| 1639 | ip6_address_t v6start, v6end; |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1640 | u32 table_id = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1641 | int v4set = 0; |
| 1642 | int v6set = 0; |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1643 | int rv; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1644 | |
| 1645 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1646 | { |
| 1647 | if (unformat (input, "%U - %U", unformat_ip4_address, &v4start, |
| 1648 | unformat_ip4_address, &v4end)) |
| 1649 | v4set = 1; |
| 1650 | else if (unformat (input, "%U", unformat_ip4_address, &v4start)) |
| 1651 | { |
| 1652 | memcpy (&v4end, &v4start, sizeof (v4start)); |
| 1653 | v4set = 1; |
| 1654 | } |
| 1655 | else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start, |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1656 | unformat_ip6_address, &v6end)) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1657 | v6set = 1; |
| 1658 | else if (unformat (input, "%U", unformat_ip6_address, &v6start)) |
| 1659 | { |
Yoann Desmouceaux | 6b297aa | 2017-09-20 10:34:22 +0200 | [diff] [blame] | 1660 | memcpy (&v6end, &v6start, sizeof (v6start)); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1661 | v6set = 1; |
| 1662 | } |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1663 | else if (unformat (input, "fib-table %d", &table_id)) |
| 1664 | ; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1665 | else |
| 1666 | break; |
| 1667 | } |
| 1668 | |
| 1669 | if (!v4set && !v6set) |
| 1670 | return clib_error_return (0, "at least one v4 or v6 address required"); |
| 1671 | |
| 1672 | if (v4set) |
| 1673 | { |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1674 | rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end, |
| 1675 | table_id); |
| 1676 | switch (rv) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1677 | { |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1678 | case 0: |
| 1679 | break; |
| 1680 | |
| 1681 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 1682 | return clib_error_return (0, "Invalid table-id %d", table_id); |
| 1683 | |
| 1684 | case VNET_API_ERROR_INVALID_ARGUMENT: |
| 1685 | return clib_error_return (0, "Invalid address range %U - %U", |
| 1686 | format_ip4_address, &v4start, |
| 1687 | format_ip4_address, &v4end); |
| 1688 | default: |
| 1689 | return clib_error_return (0, "error %d", rv); |
| 1690 | break; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1691 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1692 | } |
| 1693 | if (v6set) |
| 1694 | { |
Dave Barach | 3bbcfab | 2017-08-15 19:03:44 -0400 | [diff] [blame] | 1695 | rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end, |
| 1696 | table_id); |
| 1697 | switch (rv) |
| 1698 | { |
| 1699 | case 0: |
| 1700 | break; |
| 1701 | |
| 1702 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 1703 | return clib_error_return (0, "Invalid table-id %d", table_id); |
| 1704 | |
| 1705 | default: |
| 1706 | return clib_error_return (0, "error %d", rv); |
| 1707 | break; |
| 1708 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1709 | } |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | /* *INDENT-OFF* */ |
| 1714 | VLIB_CLI_COMMAND (tcp_src_address_command, static) = |
| 1715 | { |
| 1716 | .path = "tcp src-address", |
| 1717 | .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range", |
| 1718 | .function = tcp_src_address, |
| 1719 | }; |
| 1720 | /* *INDENT-ON* */ |
| 1721 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 1722 | static u8 * |
| 1723 | tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb) |
| 1724 | { |
| 1725 | #if TCP_SCOREBOARD_TRACE |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1726 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 1727 | scoreboard_trace_elt_t *block; |
| 1728 | int i = 0; |
| 1729 | |
| 1730 | if (!sb->trace) |
| 1731 | return s; |
| 1732 | |
| 1733 | s = format (s, "scoreboard trace:"); |
| 1734 | vec_foreach (block, sb->trace) |
| 1735 | { |
| 1736 | s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end, |
| 1737 | block->ack, block->snd_una_max, block->group); |
| 1738 | if ((++i % 3) == 0) |
| 1739 | s = format (s, "\n"); |
| 1740 | } |
| 1741 | return s; |
| 1742 | #else |
| 1743 | return 0; |
| 1744 | #endif |
| 1745 | } |
| 1746 | |
| 1747 | static clib_error_t * |
| 1748 | tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1749 | vlib_cli_command_t * cmd_arg) |
| 1750 | { |
| 1751 | transport_connection_t *tconn = 0; |
| 1752 | tcp_connection_t *tc; |
| 1753 | u8 *s = 0; |
| 1754 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1755 | { |
| 1756 | if (unformat (input, "%U", unformat_transport_connection, &tconn, |
| 1757 | TRANSPORT_PROTO_TCP)) |
| 1758 | ; |
| 1759 | else |
| 1760 | return clib_error_return (0, "unknown input `%U'", |
| 1761 | format_unformat_error, input); |
| 1762 | } |
| 1763 | |
| 1764 | if (!TCP_SCOREBOARD_TRACE) |
| 1765 | { |
| 1766 | vlib_cli_output (vm, "scoreboard tracing not enabled"); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | tc = tcp_get_connection_from_transport (tconn); |
| 1771 | s = tcp_scoreboard_dump_trace (s, &tc->sack_sb); |
| 1772 | vlib_cli_output (vm, "%v", s); |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | /* *INDENT-OFF* */ |
| 1777 | VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) = |
| 1778 | { |
| 1779 | .path = "show tcp scoreboard trace", |
| 1780 | .short_help = "show tcp scoreboard trace <connection>", |
| 1781 | .function = tcp_show_scoreboard_trace_fn, |
| 1782 | }; |
| 1783 | /* *INDENT-ON* */ |
| 1784 | |
| 1785 | u8 * |
| 1786 | tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose) |
| 1787 | { |
| 1788 | int i, trace_len; |
| 1789 | scoreboard_trace_elt_t *trace; |
| 1790 | u32 next_ack, left, group, has_new_ack = 0; |
| 1791 | tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc; |
| 1792 | sack_block_t *block; |
| 1793 | |
| 1794 | if (!tc) |
| 1795 | return s; |
| 1796 | |
| 1797 | memset (dummy_tc, 0, sizeof (*dummy_tc)); |
| 1798 | tcp_connection_timers_init (dummy_tc); |
| 1799 | scoreboard_init (&dummy_tc->sack_sb); |
| 1800 | dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK; |
| 1801 | |
| 1802 | #if TCP_SCOREBOARD_TRACE |
| 1803 | trace = tc->sack_sb.trace; |
| 1804 | trace_len = vec_len (tc->sack_sb.trace); |
| 1805 | #else |
| 1806 | trace = 0; |
| 1807 | trace_len = 0; |
| 1808 | #endif |
| 1809 | |
| 1810 | for (i = 0; i < trace_len; i++) |
| 1811 | { |
| 1812 | if (trace[i].ack != 0) |
| 1813 | { |
| 1814 | dummy_tc->snd_una = trace[i].ack - 1448; |
| 1815 | dummy_tc->snd_una_max = trace[i].ack; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | left = 0; |
| 1820 | while (left < trace_len) |
| 1821 | { |
| 1822 | group = trace[left].group; |
| 1823 | vec_reset_length (dummy_tc->rcv_opts.sacks); |
| 1824 | has_new_ack = 0; |
| 1825 | while (trace[left].group == group) |
| 1826 | { |
| 1827 | if (trace[left].ack != 0) |
| 1828 | { |
| 1829 | if (verbose) |
| 1830 | s = format (s, "Adding ack %u, snd_una_max %u, segs: ", |
| 1831 | trace[left].ack, trace[left].snd_una_max); |
| 1832 | dummy_tc->snd_una_max = trace[left].snd_una_max; |
| 1833 | next_ack = trace[left].ack; |
| 1834 | has_new_ack = 1; |
| 1835 | } |
| 1836 | else |
| 1837 | { |
| 1838 | if (verbose) |
| 1839 | s = format (s, "[%u, %u], ", trace[left].start, |
| 1840 | trace[left].end); |
| 1841 | vec_add2 (dummy_tc->rcv_opts.sacks, block, 1); |
| 1842 | block->start = trace[left].start; |
| 1843 | block->end = trace[left].end; |
| 1844 | } |
| 1845 | left++; |
| 1846 | } |
| 1847 | |
| 1848 | /* Push segments */ |
| 1849 | tcp_rcv_sacks (dummy_tc, next_ack); |
| 1850 | if (has_new_ack) |
| 1851 | dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv; |
| 1852 | |
| 1853 | if (verbose) |
| 1854 | s = format (s, "result: %U", format_tcp_scoreboard, |
| 1855 | &dummy_tc->sack_sb); |
| 1856 | |
| 1857 | } |
| 1858 | s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb); |
| 1859 | |
| 1860 | return s; |
| 1861 | } |
| 1862 | |
| 1863 | static clib_error_t * |
| 1864 | tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1865 | vlib_cli_command_t * cmd_arg) |
| 1866 | { |
| 1867 | transport_connection_t *tconn = 0; |
| 1868 | tcp_connection_t *tc = 0; |
| 1869 | u8 *str = 0; |
| 1870 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1871 | { |
| 1872 | if (unformat (input, "%U", unformat_transport_connection, &tconn, |
| 1873 | TRANSPORT_PROTO_TCP)) |
| 1874 | ; |
| 1875 | else |
| 1876 | return clib_error_return (0, "unknown input `%U'", |
| 1877 | format_unformat_error, input); |
| 1878 | } |
| 1879 | |
| 1880 | if (!TCP_SCOREBOARD_TRACE) |
| 1881 | { |
| 1882 | vlib_cli_output (vm, "scoreboard tracing not enabled"); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | tc = tcp_get_connection_from_transport (tconn); |
| 1887 | if (!tc) |
| 1888 | { |
| 1889 | vlib_cli_output (vm, "connection not found"); |
| 1890 | return 0; |
| 1891 | } |
| 1892 | str = tcp_scoreboard_replay (str, tc, 1); |
| 1893 | vlib_cli_output (vm, "%v", str); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* *INDENT-OFF* */ |
| 1898 | VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) = |
| 1899 | { |
| 1900 | .path = "tcp replay scoreboard", |
| 1901 | .short_help = "tcp replay scoreboard <connection>", |
| 1902 | .function = tcp_scoreboard_trace_fn, |
| 1903 | }; |
| 1904 | /* *INDENT-ON* */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1905 | |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 1906 | static clib_error_t * |
| 1907 | show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1908 | vlib_cli_command_t * cmd_arg) |
| 1909 | { |
| 1910 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 1911 | if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1912 | return clib_error_return (0, "unknown input `%U'", format_unformat_error, |
| 1913 | input); |
| 1914 | vlib_cli_output (vm, "IPv4 TCP punt: %s", |
| 1915 | tm->punt_unknown4 ? "enabled" : "disabled"); |
| 1916 | vlib_cli_output (vm, "IPv6 TCP punt: %s", |
| 1917 | tm->punt_unknown6 ? "enabled" : "disabled"); |
| 1918 | return 0; |
| 1919 | } |
| 1920 | /* *INDENT-OFF* */ |
| 1921 | VLIB_CLI_COMMAND (show_tcp_punt_command, static) = |
| 1922 | { |
| 1923 | .path = "show tcp punt", |
| 1924 | .short_help = "show tcp punt", |
| 1925 | .function = show_tcp_punt_fn, |
| 1926 | }; |
| 1927 | /* *INDENT-ON* */ |
| 1928 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1929 | /* |
| 1930 | * fd.io coding-style-patch-verification: ON |
| 1931 | * |
| 1932 | * Local Variables: |
| 1933 | * eval: (c-set-style "gnu") |
| 1934 | * End: |
| 1935 | */ |