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 |
| 24 | tcp_connection_bind (vlib_main_t * vm, u32 session_index, ip46_address_t * ip, |
| 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) |
| 37 | listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32; |
| 38 | else |
| 39 | clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t)); |
| 40 | |
| 41 | listener->c_s_index = session_index; |
| 42 | listener->c_proto = SESSION_TYPE_IP4_TCP; |
| 43 | listener->state = TCP_STATE_LISTEN; |
| 44 | listener->c_is_ip4 = 1; |
| 45 | |
| 46 | return listener->c_c_index; |
| 47 | } |
| 48 | |
| 49 | u32 |
| 50 | tcp_session_bind_ip4 (vlib_main_t * vm, u32 session_index, |
| 51 | ip46_address_t * ip, u16 port_host_byte_order) |
| 52 | { |
| 53 | return tcp_connection_bind (vm, session_index, ip, port_host_byte_order, 1); |
| 54 | } |
| 55 | |
| 56 | u32 |
| 57 | tcp_session_bind_ip6 (vlib_main_t * vm, u32 session_index, |
| 58 | ip46_address_t * ip, u16 port_host_byte_order) |
| 59 | { |
| 60 | return tcp_connection_bind (vm, session_index, ip, port_host_byte_order, 0); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | tcp_session_unbind (u32 listener_index) |
| 66 | { |
| 67 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 68 | pool_put_index (tm->listener_pool, listener_index); |
| 69 | } |
| 70 | |
| 71 | u32 |
| 72 | tcp_session_unbind_ip4 (vlib_main_t * vm, u32 listener_index) |
| 73 | { |
| 74 | tcp_session_unbind (listener_index); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | u32 |
| 79 | tcp_session_unbind_ip6 (vlib_main_t * vm, u32 listener_index) |
| 80 | { |
| 81 | tcp_session_unbind (listener_index); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | transport_connection_t * |
| 86 | tcp_session_get_listener (u32 listener_index) |
| 87 | { |
| 88 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 89 | tcp_connection_t *tc; |
| 90 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 91 | return &tc->connection; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Cleans up connection state. |
| 96 | * |
| 97 | * No notifications. |
| 98 | */ |
| 99 | void |
| 100 | tcp_connection_cleanup (tcp_connection_t * tc) |
| 101 | { |
| 102 | tcp_main_t *tm = &tcp_main; |
| 103 | u32 tepi; |
| 104 | transport_endpoint_t *tep; |
| 105 | |
| 106 | /* Cleanup local endpoint if this was an active connect */ |
| 107 | tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip, |
| 108 | tc->c_lcl_port); |
| 109 | |
| 110 | /*XXX lock */ |
| 111 | if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 112 | { |
| 113 | tep = pool_elt_at_index (tm->local_endpoints, tepi); |
| 114 | transport_endpoint_table_del (&tm->local_endpoints_table, tep); |
| 115 | pool_put (tm->local_endpoints, tep); |
| 116 | } |
| 117 | |
| 118 | /* Make sure all timers are cleared */ |
| 119 | tcp_connection_timers_reset (tc); |
| 120 | |
| 121 | /* Check if half-open */ |
| 122 | if (tc->state == TCP_STATE_SYN_SENT) |
| 123 | pool_put (tm->half_open_connections, tc); |
| 124 | else |
| 125 | pool_put (tm->connections[tc->c_thread_index], tc); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Connection removal. |
| 130 | * |
| 131 | * This should be called only once connection enters CLOSED state. Note |
| 132 | * that it notifies the session of the removal event, so if the goal is to |
| 133 | * just remove the connection, call tcp_connection_cleanup instead. |
| 134 | */ |
| 135 | void |
| 136 | tcp_connection_del (tcp_connection_t * tc) |
| 137 | { |
| 138 | stream_session_delete_notify (&tc->connection); |
| 139 | tcp_connection_cleanup (tc); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Begin connection closing procedure. |
| 144 | * |
| 145 | * If at the end the connection is not in CLOSED state, it is not removed. |
| 146 | * Instead, we rely on on TCP to advance through state machine to either |
| 147 | * 1) LAST_ACK (passive close) whereby when the last ACK is received |
| 148 | * tcp_connection_del is called. This notifies session of the delete and |
| 149 | * calls cleanup. |
| 150 | * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers |
| 151 | * and cleanup is called. |
| 152 | */ |
| 153 | void |
| 154 | tcp_connection_close (tcp_connection_t * tc) |
| 155 | { |
| 156 | /* Send FIN if needed */ |
| 157 | if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD |
| 158 | || tc->state == TCP_STATE_CLOSE_WAIT) |
| 159 | tcp_send_fin (tc); |
| 160 | |
| 161 | /* Switch state */ |
| 162 | if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD) |
| 163 | tc->state = TCP_STATE_FIN_WAIT_1; |
| 164 | else if (tc->state == TCP_STATE_SYN_SENT) |
| 165 | tc->state = TCP_STATE_CLOSED; |
| 166 | else if (tc->state == TCP_STATE_CLOSE_WAIT) |
| 167 | tc->state = TCP_STATE_LAST_ACK; |
| 168 | |
| 169 | /* Half-close connections are not supported XXX */ |
| 170 | |
| 171 | if (tc->state == TCP_STATE_CLOSED) |
| 172 | tcp_connection_del (tc); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | tcp_session_close (u32 conn_index, u32 thread_index) |
| 177 | { |
| 178 | tcp_connection_t *tc; |
| 179 | tc = tcp_connection_get (conn_index, thread_index); |
| 180 | tcp_connection_close (tc); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | tcp_session_cleanup (u32 conn_index, u32 thread_index) |
| 185 | { |
| 186 | tcp_connection_t *tc; |
| 187 | tc = tcp_connection_get (conn_index, thread_index); |
| 188 | tcp_connection_cleanup (tc); |
| 189 | } |
| 190 | |
| 191 | void * |
| 192 | ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4) |
| 193 | { |
| 194 | ip_lookup_main_t *lm4 = &ip4_main.lookup_main; |
| 195 | ip_lookup_main_t *lm6 = &ip6_main.lookup_main; |
| 196 | ip_interface_address_t *ia = 0; |
| 197 | |
| 198 | if (is_ip4) |
| 199 | { |
| 200 | /* *INDENT-OFF* */ |
| 201 | foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ , |
| 202 | ({ |
| 203 | return ip_interface_address_get_address (lm4, ia); |
| 204 | })); |
| 205 | /* *INDENT-ON* */ |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | /* *INDENT-OFF* */ |
| 210 | foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ , |
| 211 | ({ |
| 212 | return ip_interface_address_get_address (lm6, ia); |
| 213 | })); |
| 214 | /* *INDENT-ON* */ |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Allocate local port and add if successful add entry to local endpoint |
| 222 | * table to mark the pair as used. |
| 223 | */ |
| 224 | u16 |
| 225 | tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip) |
| 226 | { |
| 227 | u8 unique = 0; |
| 228 | transport_endpoint_t *tep; |
| 229 | u32 time_now, tei; |
| 230 | u16 min = 1024, max = 65535, tries; /* XXX configurable ? */ |
| 231 | |
| 232 | tries = max - min; |
| 233 | time_now = tcp_time_now (); |
| 234 | |
| 235 | /* Start at random point or max */ |
| 236 | pool_get (tm->local_endpoints, tep); |
| 237 | clib_memcpy (&tep->ip, ip, sizeof (*ip)); |
| 238 | tep->port = random_u32 (&time_now) << 16; |
| 239 | tep->port = tep->port < min ? max : tep->port; |
| 240 | |
| 241 | /* Search for first free slot */ |
| 242 | while (tries) |
| 243 | { |
| 244 | tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip, |
| 245 | tep->port); |
| 246 | if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX) |
| 247 | { |
| 248 | unique = 1; |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | tep->port--; |
| 253 | |
| 254 | if (tep->port < min) |
| 255 | tep->port = max; |
| 256 | |
| 257 | tries--; |
| 258 | } |
| 259 | |
| 260 | if (unique) |
| 261 | { |
| 262 | transport_endpoint_table_add (&tm->local_endpoints_table, tep, |
| 263 | tep - tm->local_endpoints); |
| 264 | |
| 265 | return tep->port; |
| 266 | } |
| 267 | |
| 268 | /* Failed */ |
| 269 | pool_put (tm->local_endpoints, tep); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Initialize all connection timers as invalid |
| 275 | */ |
| 276 | void |
| 277 | tcp_connection_timers_init (tcp_connection_t * tc) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | /* Set all to invalid */ |
| 282 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 283 | { |
| 284 | tc->timers[i] = TCP_TIMER_HANDLE_INVALID; |
| 285 | } |
| 286 | |
| 287 | tc->rto = TCP_RTO_INIT; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Stop all connection timers |
| 292 | */ |
| 293 | void |
| 294 | tcp_connection_timers_reset (tcp_connection_t * tc) |
| 295 | { |
| 296 | int i; |
| 297 | for (i = 0; i < TCP_N_TIMERS; i++) |
| 298 | { |
| 299 | tcp_timer_reset (tc, i); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** Initialize tcp connection variables |
| 304 | * |
| 305 | * Should be called after having received a msg from the peer, i.e., a SYN or |
| 306 | * a SYNACK, such that connection options have already been exchanged. */ |
| 307 | void |
| 308 | tcp_connection_init_vars (tcp_connection_t * tc) |
| 309 | { |
| 310 | tcp_connection_timers_init (tc); |
| 311 | tcp_set_snd_mss (tc); |
| 312 | tc->sack_sb.head = TCP_INVALID_SACK_HOLE_INDEX; |
| 313 | tcp_cc_init (tc); |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4) |
| 318 | { |
| 319 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 320 | tcp_connection_t *tc; |
| 321 | fib_prefix_t prefix; |
| 322 | u32 fei, sw_if_index; |
| 323 | ip46_address_t lcl_addr; |
| 324 | u16 lcl_port; |
| 325 | |
| 326 | /* |
| 327 | * Find the local address and allocate port |
| 328 | */ |
| 329 | memset (&lcl_addr, 0, sizeof (lcl_addr)); |
| 330 | |
| 331 | /* Find a FIB path to the destination */ |
| 332 | clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr)); |
| 333 | prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 334 | prefix.fp_len = is_ip4 ? 32 : 128; |
| 335 | |
| 336 | fei = fib_table_lookup (0, &prefix); |
| 337 | |
| 338 | /* Couldn't find route to destination. Bail out. */ |
| 339 | if (fei == FIB_NODE_INDEX_INVALID) |
| 340 | return -1; |
| 341 | |
| 342 | sw_if_index = fib_entry_get_resolving_interface (fei); |
| 343 | |
| 344 | if (sw_if_index == (u32) ~ 0) |
| 345 | return -1; |
| 346 | |
| 347 | if (is_ip4) |
| 348 | { |
| 349 | ip4_address_t *ip4; |
| 350 | ip4 = ip_interface_get_first_ip (sw_if_index, 1); |
| 351 | lcl_addr.ip4.as_u32 = ip4->as_u32; |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | ip6_address_t *ip6; |
| 356 | ip6 = ip_interface_get_first_ip (sw_if_index, 0); |
| 357 | clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6)); |
| 358 | } |
| 359 | |
| 360 | /* Allocate source port */ |
| 361 | lcl_port = tcp_allocate_local_port (tm, &lcl_addr); |
| 362 | if (lcl_port < 1) |
| 363 | return -1; |
| 364 | |
| 365 | /* |
| 366 | * Create connection and send SYN |
| 367 | */ |
| 368 | |
| 369 | pool_get (tm->half_open_connections, tc); |
| 370 | memset (tc, 0, sizeof (*tc)); |
| 371 | |
| 372 | clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t)); |
| 373 | clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t)); |
| 374 | tc->c_rmt_port = clib_host_to_net_u16 (rmt_port); |
| 375 | tc->c_lcl_port = clib_host_to_net_u16 (lcl_port); |
| 376 | tc->c_c_index = tc - tm->half_open_connections; |
| 377 | tc->c_is_ip4 = is_ip4; |
| 378 | |
| 379 | /* The other connection vars will be initialized after SYN ACK */ |
| 380 | tcp_connection_timers_init (tc); |
| 381 | |
| 382 | tcp_send_syn (tc); |
| 383 | |
| 384 | tc->state = TCP_STATE_SYN_SENT; |
| 385 | |
| 386 | return tc->c_c_index; |
| 387 | } |
| 388 | |
| 389 | int |
| 390 | tcp_session_open_ip4 (ip46_address_t * addr, u16 port) |
| 391 | { |
| 392 | return tcp_connection_open (addr, port, 1); |
| 393 | } |
| 394 | |
| 395 | int |
| 396 | tcp_session_open_ip6 (ip46_address_t * addr, u16 port) |
| 397 | { |
| 398 | return tcp_connection_open (addr, port, 0); |
| 399 | } |
| 400 | |
| 401 | u8 * |
| 402 | format_tcp_session_ip4 (u8 * s, va_list * args) |
| 403 | { |
| 404 | u32 tci = va_arg (*args, u32); |
| 405 | u32 thread_index = va_arg (*args, u32); |
| 406 | tcp_connection_t *tc; |
| 407 | |
| 408 | tc = tcp_connection_get (tci, thread_index); |
| 409 | |
| 410 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address, |
| 411 | &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port), |
| 412 | format_ip4_address, &tc->c_rmt_ip4, |
| 413 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 414 | |
| 415 | return s; |
| 416 | } |
| 417 | |
| 418 | u8 * |
| 419 | format_tcp_session_ip6 (u8 * s, va_list * args) |
| 420 | { |
| 421 | u32 tci = va_arg (*args, u32); |
| 422 | u32 thread_index = va_arg (*args, u32); |
| 423 | tcp_connection_t *tc = tcp_connection_get (tci, thread_index); |
| 424 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address, |
| 425 | &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port), |
| 426 | format_ip6_address, &tc->c_rmt_ip6, |
| 427 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 428 | return s; |
| 429 | } |
| 430 | |
| 431 | u8 * |
| 432 | format_tcp_listener_session_ip4 (u8 * s, va_list * args) |
| 433 | { |
| 434 | u32 tci = va_arg (*args, u32); |
| 435 | tcp_connection_t *tc = tcp_listener_get (tci); |
| 436 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address, |
| 437 | &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port), |
| 438 | format_ip4_address, &tc->c_rmt_ip4, |
| 439 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 440 | return s; |
| 441 | } |
| 442 | |
| 443 | u8 * |
| 444 | format_tcp_listener_session_ip6 (u8 * s, va_list * args) |
| 445 | { |
| 446 | u32 tci = va_arg (*args, u32); |
| 447 | tcp_connection_t *tc = tcp_listener_get (tci); |
| 448 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address, |
| 449 | &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port), |
| 450 | format_ip6_address, &tc->c_rmt_ip6, |
| 451 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 452 | return s; |
| 453 | } |
| 454 | |
| 455 | u8 * |
| 456 | format_tcp_half_open_session_ip4 (u8 * s, va_list * args) |
| 457 | { |
| 458 | u32 tci = va_arg (*args, u32); |
| 459 | tcp_connection_t *tc = tcp_half_open_connection_get (tci); |
| 460 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address, |
| 461 | &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port), |
| 462 | format_ip4_address, &tc->c_rmt_ip4, |
| 463 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 464 | return s; |
| 465 | } |
| 466 | |
| 467 | u8 * |
| 468 | format_tcp_half_open_session_ip6 (u8 * s, va_list * args) |
| 469 | { |
| 470 | u32 tci = va_arg (*args, u32); |
| 471 | tcp_connection_t *tc = tcp_half_open_connection_get (tci); |
| 472 | s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address, |
| 473 | &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port), |
| 474 | format_ip6_address, &tc->c_rmt_ip6, |
| 475 | clib_net_to_host_u16 (tc->c_rmt_port)); |
| 476 | return s; |
| 477 | } |
| 478 | |
| 479 | transport_connection_t * |
| 480 | tcp_session_get_transport (u32 conn_index, u32 thread_index) |
| 481 | { |
| 482 | tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index); |
| 483 | return &tc->connection; |
| 484 | } |
| 485 | |
| 486 | transport_connection_t * |
| 487 | tcp_half_open_session_get_transport (u32 conn_index) |
| 488 | { |
| 489 | tcp_connection_t *tc = tcp_half_open_connection_get (conn_index); |
| 490 | return &tc->connection; |
| 491 | } |
| 492 | |
| 493 | u16 |
| 494 | tcp_session_send_mss (transport_connection_t * trans_conn) |
| 495 | { |
| 496 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
| 497 | return tc->snd_mss; |
| 498 | } |
| 499 | |
| 500 | u32 |
| 501 | tcp_session_send_space (transport_connection_t * trans_conn) |
| 502 | { |
| 503 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
| 504 | return tcp_available_snd_space (tc); |
| 505 | } |
| 506 | |
| 507 | u32 |
| 508 | tcp_session_rx_fifo_offset (transport_connection_t * trans_conn) |
| 509 | { |
| 510 | tcp_connection_t *tc = (tcp_connection_t *) trans_conn; |
| 511 | return (tc->snd_una_max - tc->snd_una); |
| 512 | } |
| 513 | |
| 514 | /* *INDENT-OFF* */ |
| 515 | const static transport_proto_vft_t tcp4_proto = { |
| 516 | .bind = tcp_session_bind_ip4, |
| 517 | .unbind = tcp_session_unbind_ip4, |
| 518 | .push_header = tcp_push_header, |
| 519 | .get_connection = tcp_session_get_transport, |
| 520 | .get_listener = tcp_session_get_listener, |
| 521 | .get_half_open = tcp_half_open_session_get_transport, |
| 522 | .open = tcp_session_open_ip4, |
| 523 | .close = tcp_session_close, |
| 524 | .cleanup = tcp_session_cleanup, |
| 525 | .send_mss = tcp_session_send_mss, |
| 526 | .send_space = tcp_session_send_space, |
| 527 | .rx_fifo_offset = tcp_session_rx_fifo_offset, |
| 528 | .format_connection = format_tcp_session_ip4, |
| 529 | .format_listener = format_tcp_listener_session_ip4, |
| 530 | .format_half_open = format_tcp_half_open_session_ip4 |
| 531 | }; |
| 532 | |
| 533 | const static transport_proto_vft_t tcp6_proto = { |
| 534 | .bind = tcp_session_bind_ip6, |
| 535 | .unbind = tcp_session_unbind_ip6, |
| 536 | .push_header = tcp_push_header, |
| 537 | .get_connection = tcp_session_get_transport, |
| 538 | .get_listener = tcp_session_get_listener, |
| 539 | .get_half_open = tcp_half_open_session_get_transport, |
| 540 | .open = tcp_session_open_ip6, |
| 541 | .close = tcp_session_close, |
| 542 | .cleanup = tcp_session_cleanup, |
| 543 | .send_mss = tcp_session_send_mss, |
| 544 | .send_space = tcp_session_send_space, |
| 545 | .rx_fifo_offset = tcp_session_rx_fifo_offset, |
| 546 | .format_connection = format_tcp_session_ip6, |
| 547 | .format_listener = format_tcp_listener_session_ip6, |
| 548 | .format_half_open = format_tcp_half_open_session_ip6 |
| 549 | }; |
| 550 | /* *INDENT-ON* */ |
| 551 | |
| 552 | void |
| 553 | tcp_timer_keep_handler (u32 conn_index) |
| 554 | { |
| 555 | u32 cpu_index = os_get_cpu_number (); |
| 556 | tcp_connection_t *tc; |
| 557 | |
| 558 | tc = tcp_connection_get (conn_index, cpu_index); |
| 559 | tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID; |
| 560 | |
| 561 | tcp_connection_close (tc); |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | tcp_timer_establish_handler (u32 conn_index) |
| 566 | { |
| 567 | tcp_connection_t *tc; |
| 568 | u8 sst; |
| 569 | |
| 570 | tc = tcp_half_open_connection_get (conn_index); |
| 571 | tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID; |
| 572 | |
| 573 | ASSERT (tc->state == TCP_STATE_SYN_SENT); |
| 574 | |
| 575 | sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP; |
| 576 | stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ ); |
| 577 | |
| 578 | tcp_connection_cleanup (tc); |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | tcp_timer_2msl_handler (u32 conn_index) |
| 583 | { |
| 584 | u32 cpu_index = os_get_cpu_number (); |
| 585 | tcp_connection_t *tc; |
| 586 | |
| 587 | tc = tcp_connection_get (conn_index, cpu_index); |
| 588 | tc->timers[TCP_TIMER_2MSL] = TCP_TIMER_HANDLE_INVALID; |
| 589 | |
| 590 | tcp_connection_del (tc); |
| 591 | } |
| 592 | |
| 593 | /* *INDENT-OFF* */ |
| 594 | static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] = |
| 595 | { |
| 596 | tcp_timer_retransmit_handler, |
| 597 | tcp_timer_delack_handler, |
| 598 | 0, |
| 599 | tcp_timer_keep_handler, |
| 600 | tcp_timer_2msl_handler, |
| 601 | tcp_timer_retransmit_syn_handler, |
| 602 | tcp_timer_establish_handler |
| 603 | }; |
| 604 | /* *INDENT-ON* */ |
| 605 | |
| 606 | static void |
| 607 | tcp_expired_timers_dispatch (u32 * expired_timers) |
| 608 | { |
| 609 | int i; |
| 610 | u32 connection_index, timer_id; |
| 611 | |
| 612 | for (i = 0; i < vec_len (expired_timers); i++) |
| 613 | { |
| 614 | /* Get session index and timer id */ |
| 615 | connection_index = expired_timers[i] & 0x0FFFFFFF; |
| 616 | timer_id = expired_timers[i] >> 28; |
| 617 | |
| 618 | /* Handle expiration */ |
| 619 | (*timer_expiration_handlers[timer_id]) (connection_index); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | void |
| 624 | tcp_initialize_timer_wheels (tcp_main_t * tm) |
| 625 | { |
| 626 | tw_timer_wheel_16t_2w_512sl_t *tw; |
| 627 | vec_foreach (tw, tm->timer_wheels) |
| 628 | { |
| 629 | tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch, |
| 630 | 100e-3 /* timer period 100ms */ , ~0); |
| 631 | tw->last_run_time = vlib_time_now (tm->vlib_main); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | clib_error_t * |
| 636 | tcp_init (vlib_main_t * vm) |
| 637 | { |
| 638 | ip_main_t *im = &ip_main; |
| 639 | ip_protocol_info_t *pi; |
| 640 | tcp_main_t *tm = vnet_get_tcp_main (); |
| 641 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 642 | clib_error_t *error = 0; |
| 643 | u32 num_threads; |
| 644 | |
| 645 | tm->vlib_main = vm; |
| 646 | tm->vnet_main = vnet_get_main (); |
| 647 | |
| 648 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 649 | return error; |
| 650 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 651 | return error; |
| 652 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 653 | return error; |
| 654 | |
| 655 | /* |
| 656 | * Registrations |
| 657 | */ |
| 658 | |
| 659 | /* Register with IP */ |
| 660 | pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP); |
| 661 | if (pi == 0) |
| 662 | return clib_error_return (0, "TCP protocol info AWOL"); |
| 663 | pi->format_header = format_tcp_header; |
| 664 | pi->unformat_pg_edit = unformat_pg_tcp_header; |
| 665 | |
| 666 | ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index); |
| 667 | |
| 668 | /* Register as transport with URI */ |
| 669 | session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto); |
| 670 | session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto); |
| 671 | |
| 672 | /* |
| 673 | * Initialize data structures |
| 674 | */ |
| 675 | |
| 676 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 677 | vec_validate (tm->connections, num_threads - 1); |
| 678 | |
| 679 | /* Initialize per worker thread tx buffers (used for control messages) */ |
| 680 | vec_validate (tm->tx_buffers, num_threads - 1); |
| 681 | |
| 682 | /* Initialize timer wheels */ |
| 683 | vec_validate (tm->timer_wheels, num_threads - 1); |
| 684 | tcp_initialize_timer_wheels (tm); |
| 685 | |
| 686 | vec_validate (tm->delack_connections, num_threads - 1); |
| 687 | |
| 688 | /* Initialize clocks per tick for TCP timestamp. Used to compute |
| 689 | * monotonically increasing timestamps. */ |
| 690 | tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock |
| 691 | / TCP_TSTAMP_RESOLUTION; |
| 692 | |
| 693 | clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table", |
| 694 | 200000 /* $$$$ config parameter nbuckets */ , |
| 695 | (64 << 20) /*$$$ config parameter table size */ ); |
| 696 | |
| 697 | return error; |
| 698 | } |
| 699 | |
| 700 | VLIB_INIT_FUNCTION (tcp_init); |
| 701 | |
| 702 | /* |
| 703 | * fd.io coding-style-patch-verification: ON |
| 704 | * |
| 705 | * Local Variables: |
| 706 | * eval: (c-set-style "gnu") |
| 707 | * End: |
| 708 | */ |