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