Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/tcp/tcp.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/fib/fib.h> |
| 19 | #include <math.h> |
| 20 | |
| 21 | tcp_main_t tcp_main; |
| 22 | |
| 23 | static u32 |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 24 | tcp_connection_bind (u32 session_index, ip46_address_t * ip, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 25 | u16 port_host_byte_order, u8 is_ip4) |
| 26 | { |
| 27 | tcp_main_t *tm = &tcp_main; |
| 28 | tcp_connection_t *listener; |
| 29 | |
| 30 | pool_get (tm->listener_pool, listener); |
| 31 | memset (listener, 0, sizeof (*listener)); |
| 32 | |
| 33 | listener->c_c_index = listener - tm->listener_pool; |
| 34 | listener->c_lcl_port = clib_host_to_net_u16 (port_host_byte_order); |
| 35 | |
| 36 | if (is_ip4) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 37 | { |
| 38 | listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32; |
| 39 | listener->c_is_ip4 = 1; |
| 40 | listener->c_proto = SESSION_TYPE_IP4_TCP; |
| 41 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | else |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 43 | { |
| 44 | clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t)); |
| 45 | listener->c_proto = SESSION_TYPE_IP6_TCP; |
| 46 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 47 | |
| 48 | listener->c_s_index = session_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | listener->state = TCP_STATE_LISTEN; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 50 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 51 | tcp_connection_timers_init (listener); |
| 52 | |
| 53 | TCP_EVT_DBG (TCP_EVT_BIND, listener); |
| 54 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | return listener->c_c_index; |
| 56 | } |
| 57 | |
| 58 | u32 |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 59 | tcp_session_bind_ip4 (u32 session_index, ip46_address_t * ip, |
| 60 | u16 port_host_byte_order) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 61 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 62 | return tcp_connection_bind (session_index, ip, port_host_byte_order, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | u32 |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 66 | tcp_session_bind_ip6 (u32 session_index, ip46_address_t * ip, |
| 67 | u16 port_host_byte_order) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 68 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 69 | return tcp_connection_bind (session_index, ip, port_host_byte_order, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 73 | tcp_connection_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 74 | { |
| 75 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 76 | TCP_EVT_DBG (TCP_EVT_UNBIND, |
| 77 | pool_elt_at_index (tm->listener_pool, listener_index)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 78 | pool_put_index (tm->listener_pool, listener_index); |
| 79 | } |
| 80 | |
| 81 | u32 |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 82 | tcp_session_unbind (u32 listener_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 83 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 84 | tcp_connection_unbind (listener_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | transport_connection_t * |
| 89 | tcp_session_get_listener (u32 listener_index) |
| 90 | { |
| 91 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 92 | tcp_connection_t *tc; |
| 93 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 94 | return &tc->connection; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Cleans up connection state. |
| 99 | * |
| 100 | * No notifications. |
| 101 | */ |
| 102 | void |
| 103 | tcp_connection_cleanup (tcp_connection_t * tc) |
| 104 | { |
| 105 | tcp_main_t *tm = &tcp_main; |
| 106 | u32 tepi; |
| 107 | transport_endpoint_t *tep; |
| 108 | |
| 109 | /* Cleanup local endpoint if this was an active connect */ |
| 110 | tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip, |
| 111 | tc->c_lcl_port); |
| 112 | |
| 113 | /*XXX lock */ |
| 114 | if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 115 | { |
| 116 | tep = pool_elt_at_index (tm->local_endpoints, tepi); |
| 117 | transport_endpoint_table_del (&tm->local_endpoints_table, tep); |
| 118 | pool_put (tm->local_endpoints, tep); |
| 119 | } |
| 120 | |
| 121 | /* Make sure all timers are cleared */ |
| 122 | tcp_connection_timers_reset (tc); |
| 123 | |
| 124 | /* Check if half-open */ |
| 125 | if (tc->state == TCP_STATE_SYN_SENT) |
| 126 | pool_put (tm->half_open_connections, tc); |
| 127 | else |
| 128 | pool_put (tm->connections[tc->c_thread_index], tc); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Connection removal. |
| 133 | * |
| 134 | * This should be called only once connection enters CLOSED state. Note |
| 135 | * that it notifies the session of the removal event, so if the goal is to |
| 136 | * just remove the connection, call tcp_connection_cleanup instead. |
| 137 | */ |
| 138 | void |
| 139 | tcp_connection_del (tcp_connection_t * tc) |
| 140 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 141 | TCP_EVT_DBG (TCP_EVT_DELETE, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 142 | stream_session_delete_notify (&tc->connection); |
| 143 | tcp_connection_cleanup (tc); |
| 144 | } |
| 145 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 146 | /** Notify session that connection has been reset. |
| 147 | * |
| 148 | * Switch state to closed and wait for session to call cleanup. |
| 149 | */ |
| 150 | void |
| 151 | tcp_connection_reset (tcp_connection_t * tc) |
| 152 | { |
| 153 | if (tc->state == TCP_STATE_CLOSED) |
| 154 | return; |
| 155 | |
| 156 | tc->state = TCP_STATE_CLOSED; |
| 157 | stream_session_reset_notify (&tc->connection); |
| 158 | } |
| 159 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 160 | /** |
| 161 | * Begin connection closing procedure. |
| 162 | * |
| 163 | * If at the end the connection is not in CLOSED state, it is not removed. |
| 164 | * Instead, we rely on on TCP to advance through state machine to either |
| 165 | * 1) LAST_ACK (passive close) whereby when the last ACK is received |
| 166 | * tcp_connection_del is called. This notifies session of the delete and |
| 167 | * calls cleanup. |
| 168 | * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers |
| 169 | * and cleanup is called. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 170 | * |
| 171 | * N.B. Half-close connections are not supported |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 172 | */ |
| 173 | void |
| 174 | tcp_connection_close (tcp_connection_t * tc) |
| 175 | { |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 176 | TCP_EVT_DBG (TCP_EVT_CLOSE, tc); |
| 177 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | /* Send FIN if needed */ |
| 179 | if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD |
| 180 | || tc->state == TCP_STATE_CLOSE_WAIT) |
| 181 | tcp_send_fin (tc); |
| 182 | |
| 183 | /* Switch state */ |
| 184 | if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD) |
| 185 | tc->state = TCP_STATE_FIN_WAIT_1; |
| 186 | else if (tc->state == TCP_STATE_SYN_SENT) |
| 187 | tc->state = TCP_STATE_CLOSED; |
| 188 | else if (tc->state == TCP_STATE_CLOSE_WAIT) |
| 189 | tc->state = TCP_STATE_LAST_ACK; |
| 190 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 191 | /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */ |
| 192 | if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID |
| 193 | && tc->state == TCP_STATE_CLOSED) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 194 | tcp_connection_del (tc); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | tcp_session_close (u32 conn_index, u32 thread_index) |
| 199 | { |
| 200 | tcp_connection_t *tc; |
| 201 | tc = tcp_connection_get (conn_index, thread_index); |
| 202 | tcp_connection_close (tc); |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | tcp_session_cleanup (u32 conn_index, u32 thread_index) |
| 207 | { |
| 208 | tcp_connection_t *tc; |
| 209 | tc = tcp_connection_get (conn_index, thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 210 | |
| 211 | /* Wait for the session tx events to clear */ |
| 212 | tc->state = TCP_STATE_CLOSED; |
| 213 | tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void * |
| 217 | ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4) |
| 218 | { |
| 219 | ip_lookup_main_t *lm4 = &ip4_main.lookup_main; |
| 220 | ip_lookup_main_t *lm6 = &ip6_main.lookup_main; |
| 221 | ip_interface_address_t *ia = 0; |
| 222 | |
| 223 | if (is_ip4) |
| 224 | { |
| 225 | /* *INDENT-OFF* */ |
| 226 | foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ , |
| 227 | ({ |
| 228 | return ip_interface_address_get_address (lm4, ia); |
| 229 | })); |
| 230 | /* *INDENT-ON* */ |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | /* *INDENT-OFF* */ |
| 235 | foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ , |
| 236 | ({ |
| 237 | return ip_interface_address_get_address (lm6, ia); |
| 238 | })); |
| 239 | /* *INDENT-ON* */ |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 245 | #define PORT_MASK ((1 << 16)- 1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 246 | /** |
| 247 | * Allocate local port and add if successful add entry to local endpoint |
| 248 | * table to mark the pair as used. |
| 249 | */ |
| 250 | u16 |
| 251 | tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip) |
| 252 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 253 | transport_endpoint_t *tep; |
| 254 | u32 time_now, tei; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 255 | u16 min = 1024, max = 65535; /* XXX configurable ? */ |
| 256 | int tries; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 257 | |
| 258 | tries = max - min; |
| 259 | time_now = tcp_time_now (); |
| 260 | |
| 261 | /* Start at random point or max */ |
| 262 | pool_get (tm->local_endpoints, tep); |
| 263 | clib_memcpy (&tep->ip, ip, sizeof (*ip)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 264 | |
| 265 | /* Search for first free slot */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 266 | for (; tries >= 0; tries--) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 267 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 268 | u16 port = 0; |
| 269 | |
| 270 | /* Find a port in the specified range */ |
| 271 | while (1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 272 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 273 | port = random_u32 (&time_now) & PORT_MASK; |
| 274 | if (PREDICT_TRUE (port >= min && port < max)) |
| 275 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 278 | tep->port = port; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 279 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 280 | /* Look it up */ |
| 281 | tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip, |
| 282 | tep->port); |
| 283 | /* If not found, we're done */ |
| 284 | if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 285 | { |
| 286 | transport_endpoint_table_add (&tm->local_endpoints_table, tep, |
| 287 | tep - tm->local_endpoints); |
| 288 | return tep->port; |
| 289 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 290 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 291 | /* No free ports */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 292 | pool_put (tm->local_endpoints, tep); |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Initialize all connection timers as invalid |
| 298 | */ |
| 299 | void |
| 300 | tcp_connection_timers_init (tcp_connection_t * tc) |
| 301 | { |
| 302 | int i; |
| 303 | |
| 304 | /* Set all to invalid */ |
| 305 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 306 | { |
| 307 | tc->timers[i] = TCP_TIMER_HANDLE_INVALID; |
| 308 | } |
| 309 | |
| 310 | tc->rto = TCP_RTO_INIT; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Stop all connection timers |
| 315 | */ |
| 316 | void |
| 317 | tcp_connection_timers_reset (tcp_connection_t * tc) |
| 318 | { |
| 319 | int i; |
| 320 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 321 | { |
| 322 | tcp_timer_reset (tc, i); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** Initialize tcp connection variables |
| 327 | * |
| 328 | * Should be called after having received a msg from the peer, i.e., a SYN or |
| 329 | * a SYNACK, such that connection options have already been exchanged. */ |
| 330 | void |
| 331 | tcp_connection_init_vars (tcp_connection_t * tc) |
| 332 | { |
| 333 | tcp_connection_timers_init (tc); |
| 334 | tcp_set_snd_mss (tc); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 335 | scoreboard_init (&tc->sack_sb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 336 | tcp_cc_init (tc); |
| 337 | } |
| 338 | |
| 339 | int |
| 340 | tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4) |
| 341 | { |
| 342 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 343 | tcp_connection_t *tc; |
| 344 | fib_prefix_t prefix; |
| 345 | u32 fei, sw_if_index; |
| 346 | ip46_address_t lcl_addr; |
| 347 | u16 lcl_port; |
| 348 | |
| 349 | /* |
| 350 | * Find the local address and allocate port |
| 351 | */ |
| 352 | memset (&lcl_addr, 0, sizeof (lcl_addr)); |
| 353 | |
| 354 | /* Find a FIB path to the destination */ |
| 355 | clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr)); |
| 356 | prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 357 | prefix.fp_len = is_ip4 ? 32 : 128; |
| 358 | |
| 359 | fei = fib_table_lookup (0, &prefix); |
| 360 | |
| 361 | /* Couldn't find route to destination. Bail out. */ |
| 362 | if (fei == FIB_NODE_INDEX_INVALID) |
| 363 | return -1; |
| 364 | |
| 365 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 366 | |
| 367 | if (sw_if_index == (u32) ~ 0) |
| 368 | return -1; |
| 369 | |
| 370 | if (is_ip4) |
| 371 | { |
| 372 | ip4_address_t *ip4; |
| 373 | ip4 = ip_interface_get_first_ip (sw_if_index, 1); |
| 374 | lcl_addr.ip4.as_u32 = ip4->as_u32; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | ip6_address_t *ip6; |
| 379 | ip6 = ip_interface_get_first_ip (sw_if_index, 0); |
| 380 | clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6)); |
| 381 | } |
| 382 | |
| 383 | /* Allocate source port */ |
| 384 | lcl_port = tcp_allocate_local_port (tm, &lcl_addr); |
| 385 | if (lcl_port < 1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 386 | { |
| 387 | clib_warning ("Failed to allocate src port"); |
| 388 | return -1; |
| 389 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | * Create connection and send SYN |
| 393 | */ |
| 394 | |
| 395 | pool_get (tm->half_open_connections, tc); |
| 396 | memset (tc, 0, sizeof (*tc)); |
| 397 | |
| 398 | clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t)); |
| 399 | clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t)); |
| 400 | tc->c_rmt_port = clib_host_to_net_u16 (rmt_port); |
| 401 | tc->c_lcl_port = clib_host_to_net_u16 (lcl_port); |
| 402 | tc->c_c_index = tc - tm->half_open_connections; |
| 403 | tc->c_is_ip4 = is_ip4; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 404 | tc->c_proto = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 405 | |
| 406 | /* The other connection vars will be initialized after SYN ACK */ |
| 407 | tcp_connection_timers_init (tc); |
| 408 | |
| 409 | tcp_send_syn (tc); |
| 410 | |
| 411 | tc->state = TCP_STATE_SYN_SENT; |
| 412 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 413 | TCP_EVT_DBG (TCP_EVT_OPEN, tc); |
| 414 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 415 | return tc->c_c_index; |
| 416 | } |
| 417 | |
| 418 | int |
| 419 | tcp_session_open_ip4 (ip46_address_t * addr, u16 port) |
| 420 | { |
| 421 | return tcp_connection_open (addr, port, 1); |
| 422 | } |
| 423 | |
| 424 | int |
| 425 | tcp_session_open_ip6 (ip46_address_t * addr, u16 port) |
| 426 | { |
| 427 | return tcp_connection_open (addr, port, 0); |
| 428 | } |
| 429 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 430 | const char *tcp_dbg_evt_str[] = { |
| 431 | #define _(sym, str) str, |
| 432 | foreach_tcp_dbg_evt |
| 433 | #undef _ |
| 434 | }; |
| 435 | |
| 436 | const char *tcp_fsm_states[] = { |
| 437 | #define _(sym, str) str, |
| 438 | foreach_tcp_fsm_state |
| 439 | #undef _ |
| 440 | }; |
| 441 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 442 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 443 | format_tcp_state (u8 * s, va_list * args) |
| 444 | { |
| 445 | tcp_state_t *state = va_arg (*args, tcp_state_t *); |
| 446 | |
| 447 | if (*state < TCP_N_STATES) |
| 448 | s = format (s, "%s", tcp_fsm_states[*state]); |
| 449 | else |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 450 | s = format (s, "UNKNOWN (%d (0x%x))", *state, *state); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 451 | |
| 452 | return s; |
| 453 | } |
| 454 | |
| 455 | const char *tcp_conn_timers[] = { |
| 456 | #define _(sym, str) str, |
| 457 | foreach_tcp_timer |
| 458 | #undef _ |
| 459 | }; |
| 460 | |
| 461 | u8 * |
| 462 | format_tcp_timers (u8 * s, va_list * args) |
| 463 | { |
| 464 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 465 | int i, last = 0; |
| 466 | |
| 467 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 468 | if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID) |
| 469 | last = i; |
| 470 | |
| 471 | s = format (s, "["); |
| 472 | for (i = 0; i < last; i++) |
| 473 | { |
| 474 | if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID) |
| 475 | s = format (s, "%s,", tcp_conn_timers[i]); |
| 476 | } |
| 477 | |
| 478 | if (last > 0) |
| 479 | s = format (s, "%s]", tcp_conn_timers[i]); |
| 480 | else |
| 481 | s = format (s, "]"); |
| 482 | |
| 483 | return s; |
| 484 | } |
| 485 | |
| 486 | u8 * |
| 487 | format_tcp_connection (u8 * s, va_list * args) |
| 488 | { |
| 489 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 490 | if (!tc) |
| 491 | return s; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 492 | if (tc->c_is_ip4) |
| 493 | { |
| 494 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 495 | format_ip4_address, &tc->c_lcl_ip4, |
| 496 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address, |
| 497 | &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 502 | format_ip6_address, &tc->c_lcl_ip6, |
| 503 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address, |
| 504 | &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 505 | } |
| 506 | |
| 507 | return s; |
| 508 | } |
| 509 | |
| 510 | u8 * |
| 511 | format_tcp_connection_verbose (u8 * s, va_list * args) |
| 512 | { |
| 513 | tcp_connection_t *tc = va_arg (*args, tcp_connection_t *); |
| 514 | s = format (s, "%U %U %U", format_tcp_connection, tc, format_tcp_state, |
| 515 | &tc->state, format_tcp_timers, tc); |
| 516 | return s; |
| 517 | } |
| 518 | |
| 519 | u8 * |
| 520 | format_tcp_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 521 | { |
| 522 | u32 tci = va_arg (*args, u32); |
| 523 | u32 thread_index = va_arg (*args, u32); |
| 524 | tcp_connection_t *tc; |
| 525 | |
| 526 | tc = tcp_connection_get (tci, thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 527 | if (tc) |
| 528 | return format (s, "%U", format_tcp_connection, tc); |
| 529 | else |
| 530 | return format (s, "empty"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 534 | format_tcp_listener_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 535 | { |
| 536 | u32 tci = va_arg (*args, u32); |
| 537 | tcp_connection_t *tc = tcp_listener_get (tci); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 538 | return format (s, "%U", format_tcp_connection, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | u8 * |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 542 | format_tcp_half_open_session (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 543 | { |
| 544 | u32 tci = va_arg (*args, u32); |
| 545 | tcp_connection_t *tc = tcp_half_open_connection_get (tci); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 546 | return format (s, "%U", format_tcp_connection, tc); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | transport_connection_t * |
| 550 | tcp_session_get_transport (u32 conn_index, u32 thread_index) |
| 551 | { |
| 552 | tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index); |
| 553 | return &tc->connection; |
| 554 | } |
| 555 | |
| 556 | transport_connection_t * |
| 557 | tcp_half_open_session_get_transport (u32 conn_index) |
| 558 | { |
| 559 | tcp_connection_t *tc = tcp_half_open_connection_get (conn_index); |
| 560 | return &tc->connection; |
| 561 | } |
| 562 | |
| 563 | u16 |
| 564 | tcp_session_send_mss (transport_connection_t * trans_conn) |
| 565 | { |
| 566 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
| 567 | return tc->snd_mss; |
| 568 | } |
| 569 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 570 | /** |
| 571 | * Compute tx window session is allowed to fill. |
| 572 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 573 | u32 |
| 574 | tcp_session_send_space (transport_connection_t * trans_conn) |
| 575 | { |
Florin Coras | 4269098 | 2017-05-02 12:40:39 -0700 | [diff] [blame] | 576 | u32 snd_space, chunk; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 577 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 578 | |
| 579 | /* If we haven't gotten dupacks or if we did and have gotten sacked bytes |
| 580 | * then we can still send */ |
| 581 | if (PREDICT_TRUE (tcp_in_fastrecovery (tc) == 0 |
| 582 | && (tc->rcv_dupacks == 0 |
| 583 | || tc->sack_sb.last_sacked_bytes))) |
| 584 | { |
Florin Coras | 4269098 | 2017-05-02 12:40:39 -0700 | [diff] [blame] | 585 | chunk = tc->snd_wnd > tc->snd_mss ? tc->snd_mss : tc->snd_wnd; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 586 | snd_space = tcp_available_snd_space (tc); |
| 587 | |
| 588 | /* If we can't write at least a segment, don't try at all */ |
Florin Coras | 4269098 | 2017-05-02 12:40:39 -0700 | [diff] [blame] | 589 | if (chunk == 0 || snd_space < chunk) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 590 | return 0; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 591 | |
| 592 | /* round down to mss multiple */ |
Florin Coras | 4269098 | 2017-05-02 12:40:39 -0700 | [diff] [blame] | 593 | return snd_space - (snd_space % chunk); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* If in fast recovery, send 1 SMSS if wnd allows */ |
| 597 | if (tcp_in_fastrecovery (tc) && tcp_available_snd_space (tc) |
| 598 | && tcp_fastrecovery_sent_1_smss (tc)) |
| 599 | { |
| 600 | tcp_fastrecovery_1_smss_on (tc); |
| 601 | return tc->snd_mss; |
| 602 | } |
| 603 | |
| 604 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | u32 |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 608 | tcp_session_tx_fifo_offset (transport_connection_t * trans_conn) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 609 | { |
| 610 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 611 | |
| 612 | ASSERT (seq_geq (tc->snd_nxt, tc->snd_una)); |
| 613 | |
| 614 | /* This still works if fast retransmit is on */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 615 | return (tc->snd_nxt - tc->snd_una); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* *INDENT-OFF* */ |
| 619 | const static transport_proto_vft_t tcp4_proto = { |
| 620 | .bind = tcp_session_bind_ip4, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 621 | .unbind = tcp_session_unbind, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 622 | .push_header = tcp_push_header, |
| 623 | .get_connection = tcp_session_get_transport, |
| 624 | .get_listener = tcp_session_get_listener, |
| 625 | .get_half_open = tcp_half_open_session_get_transport, |
| 626 | .open = tcp_session_open_ip4, |
| 627 | .close = tcp_session_close, |
| 628 | .cleanup = tcp_session_cleanup, |
| 629 | .send_mss = tcp_session_send_mss, |
| 630 | .send_space = tcp_session_send_space, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 631 | .tx_fifo_offset = tcp_session_tx_fifo_offset, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 632 | .format_connection = format_tcp_session, |
| 633 | .format_listener = format_tcp_listener_session, |
| 634 | .format_half_open = format_tcp_half_open_session, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 635 | }; |
| 636 | |
| 637 | const static transport_proto_vft_t tcp6_proto = { |
| 638 | .bind = tcp_session_bind_ip6, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 639 | .unbind = tcp_session_unbind, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 640 | .push_header = tcp_push_header, |
| 641 | .get_connection = tcp_session_get_transport, |
| 642 | .get_listener = tcp_session_get_listener, |
| 643 | .get_half_open = tcp_half_open_session_get_transport, |
| 644 | .open = tcp_session_open_ip6, |
| 645 | .close = tcp_session_close, |
| 646 | .cleanup = tcp_session_cleanup, |
| 647 | .send_mss = tcp_session_send_mss, |
| 648 | .send_space = tcp_session_send_space, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 649 | .tx_fifo_offset = tcp_session_tx_fifo_offset, |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 650 | .format_connection = format_tcp_session, |
| 651 | .format_listener = format_tcp_listener_session, |
| 652 | .format_half_open = format_tcp_half_open_session, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 653 | }; |
| 654 | /* *INDENT-ON* */ |
| 655 | |
| 656 | void |
| 657 | tcp_timer_keep_handler (u32 conn_index) |
| 658 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 659 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 660 | tcp_connection_t *tc; |
| 661 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 662 | tc = tcp_connection_get (conn_index, thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 663 | tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID; |
| 664 | |
| 665 | tcp_connection_close (tc); |
| 666 | } |
| 667 | |
| 668 | void |
| 669 | tcp_timer_establish_handler (u32 conn_index) |
| 670 | { |
| 671 | tcp_connection_t *tc; |
| 672 | u8 sst; |
| 673 | |
| 674 | tc = tcp_half_open_connection_get (conn_index); |
| 675 | tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID; |
| 676 | |
| 677 | ASSERT (tc->state == TCP_STATE_SYN_SENT); |
| 678 | |
| 679 | sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP; |
| 680 | stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ ); |
| 681 | |
| 682 | tcp_connection_cleanup (tc); |
| 683 | } |
| 684 | |
| 685 | void |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 686 | tcp_timer_waitclose_handler (u32 conn_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 687 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 688 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 689 | tcp_connection_t *tc; |
| 690 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 691 | tc = tcp_connection_get (conn_index, thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 692 | tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID; |
| 693 | |
| 694 | /* Session didn't come back with a close(). Send FIN either way |
| 695 | * and switch to LAST_ACK. */ |
| 696 | if (tc->state == TCP_STATE_CLOSE_WAIT) |
| 697 | { |
| 698 | if (tc->flags & TCP_CONN_FINSNT) |
| 699 | { |
| 700 | clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!"); |
| 701 | } |
| 702 | |
| 703 | tcp_send_fin (tc); |
| 704 | tc->state = TCP_STATE_LAST_ACK; |
| 705 | |
| 706 | /* Make sure we don't wait in LAST ACK forever */ |
| 707 | tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME); |
| 708 | |
| 709 | /* Don't delete the connection yet */ |
| 710 | return; |
| 711 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 712 | |
| 713 | tcp_connection_del (tc); |
| 714 | } |
| 715 | |
| 716 | /* *INDENT-OFF* */ |
| 717 | static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] = |
| 718 | { |
| 719 | tcp_timer_retransmit_handler, |
| 720 | tcp_timer_delack_handler, |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 721 | tcp_timer_persist_handler, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 722 | tcp_timer_keep_handler, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 723 | tcp_timer_waitclose_handler, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 724 | tcp_timer_retransmit_syn_handler, |
| 725 | tcp_timer_establish_handler |
| 726 | }; |
| 727 | /* *INDENT-ON* */ |
| 728 | |
| 729 | static void |
| 730 | tcp_expired_timers_dispatch (u32 * expired_timers) |
| 731 | { |
| 732 | int i; |
| 733 | u32 connection_index, timer_id; |
| 734 | |
| 735 | for (i = 0; i < vec_len (expired_timers); i++) |
| 736 | { |
| 737 | /* Get session index and timer id */ |
| 738 | connection_index = expired_timers[i] & 0x0FFFFFFF; |
| 739 | timer_id = expired_timers[i] >> 28; |
| 740 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 741 | TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id); |
| 742 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 743 | /* Handle expiration */ |
| 744 | (*timer_expiration_handlers[timer_id]) (connection_index); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | void |
| 749 | tcp_initialize_timer_wheels (tcp_main_t * tm) |
| 750 | { |
| 751 | tw_timer_wheel_16t_2w_512sl_t *tw; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 752 | /* *INDENT-OFF* */ |
| 753 | foreach_vlib_main (({ |
| 754 | tw = &tm->timer_wheels[ii]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 755 | tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch, |
| 756 | 100e-3 /* timer period 100ms */ , ~0); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 757 | tw->last_run_time = vlib_time_now (this_vlib_main); |
| 758 | })); |
| 759 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | clib_error_t * |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 763 | tcp_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 764 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 765 | tcp_main_t *tm = vnet_get_tcp_main (); |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 766 | ip_protocol_info_t *pi; |
| 767 | ip_main_t *im = &ip_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 768 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 769 | clib_error_t *error = 0; |
| 770 | u32 num_threads; |
| 771 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 772 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 773 | return error; |
| 774 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 775 | return error; |
| 776 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 777 | return error; |
| 778 | |
| 779 | /* |
| 780 | * Registrations |
| 781 | */ |
| 782 | |
| 783 | /* Register with IP */ |
| 784 | pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP); |
| 785 | if (pi == 0) |
| 786 | return clib_error_return (0, "TCP protocol info AWOL"); |
| 787 | pi->format_header = format_tcp_header; |
| 788 | pi->unformat_pg_edit = unformat_pg_tcp_header; |
| 789 | |
| 790 | ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index); |
| 791 | |
| 792 | /* Register as transport with URI */ |
| 793 | session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto); |
| 794 | session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto); |
| 795 | |
| 796 | /* |
| 797 | * Initialize data structures |
| 798 | */ |
| 799 | |
| 800 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 801 | vec_validate (tm->connections, num_threads - 1); |
| 802 | |
| 803 | /* Initialize per worker thread tx buffers (used for control messages) */ |
| 804 | vec_validate (tm->tx_buffers, num_threads - 1); |
| 805 | |
| 806 | /* Initialize timer wheels */ |
| 807 | vec_validate (tm->timer_wheels, num_threads - 1); |
| 808 | tcp_initialize_timer_wheels (tm); |
| 809 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 810 | // vec_validate (tm->delack_connections, num_threads - 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 811 | |
| 812 | /* Initialize clocks per tick for TCP timestamp. Used to compute |
| 813 | * monotonically increasing timestamps. */ |
| 814 | tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock |
| 815 | / TCP_TSTAMP_RESOLUTION; |
| 816 | |
| 817 | clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table", |
| 818 | 200000 /* $$$$ config parameter nbuckets */ , |
| 819 | (64 << 20) /*$$$ config parameter table size */ ); |
| 820 | |
| 821 | return error; |
| 822 | } |
| 823 | |
Florin Coras | a0b34a7 | 2017-03-07 01:20:52 -0800 | [diff] [blame] | 824 | clib_error_t * |
| 825 | vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en) |
| 826 | { |
| 827 | if (is_en) |
| 828 | { |
| 829 | if (tcp_main.is_enabled) |
| 830 | return 0; |
| 831 | |
| 832 | return tcp_main_enable (vm); |
| 833 | } |
| 834 | else |
| 835 | { |
| 836 | tcp_main.is_enabled = 0; |
| 837 | } |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | clib_error_t * |
| 843 | tcp_init (vlib_main_t * vm) |
| 844 | { |
| 845 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 846 | |
| 847 | tm->vlib_main = vm; |
| 848 | tm->vnet_main = vnet_get_main (); |
| 849 | tm->is_enabled = 0; |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 854 | VLIB_INIT_FUNCTION (tcp_init); |
| 855 | |
| 856 | /* |
| 857 | * fd.io coding-style-patch-verification: ON |
| 858 | * |
| 859 | * Local Variables: |
| 860 | * eval: (c-set-style "gnu") |
| 861 | * End: |
| 862 | */ |