Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 SUSE LLC. |
| 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 | #include <vnet/sctp/sctp.h> |
| 16 | #include <vnet/sctp/sctp_debug.h> |
| 17 | |
| 18 | sctp_main_t sctp_main; |
| 19 | |
| 20 | static u32 |
| 21 | sctp_connection_bind (u32 session_index, transport_endpoint_t * tep) |
| 22 | { |
| 23 | sctp_main_t *tm = &sctp_main; |
| 24 | sctp_connection_t *listener; |
| 25 | void *iface_ip; |
| 26 | |
| 27 | pool_get (tm->listener_pool, listener); |
| 28 | memset (listener, 0, sizeof (*listener)); |
| 29 | |
| 30 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].parent = listener; |
| 31 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index = |
| 32 | listener - tm->listener_pool; |
| 33 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.lcl_port = tep->port; |
| 34 | |
| 35 | /* If we are provided a sw_if_index, bind using one of its IPs */ |
| 36 | if (ip_is_zero (&tep->ip, 1) && tep->sw_if_index != ENDPOINT_INVALID_INDEX) |
| 37 | { |
| 38 | if ((iface_ip = ip_interface_get_first_ip (tep->sw_if_index, |
| 39 | tep->is_ip4))) |
| 40 | ip_set (&tep->ip, iface_ip, tep->is_ip4); |
| 41 | } |
| 42 | ip_copy (&listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.lcl_ip, |
| 43 | &tep->ip, tep->is_ip4); |
| 44 | |
| 45 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.is_ip4 = tep->is_ip4; |
| 46 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto = |
| 47 | TRANSPORT_PROTO_SCTP; |
| 48 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_s_index = session_index; |
| 49 | listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.fib_index = |
| 50 | tep->fib_index; |
| 51 | listener->state = SCTP_STATE_CLOSED; |
| 52 | |
| 53 | sctp_connection_timers_init (listener); |
| 54 | |
| 55 | return listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index; |
| 56 | } |
| 57 | |
| 58 | u32 |
| 59 | sctp_session_bind (u32 session_index, transport_endpoint_t * tep) |
| 60 | { |
| 61 | return sctp_connection_bind (session_index, tep); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | sctp_connection_unbind (u32 listener_index) |
| 66 | { |
| 67 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 68 | sctp_connection_t *tc; |
| 69 | |
| 70 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 71 | |
| 72 | /* Poison the entry */ |
| 73 | if (CLIB_DEBUG > 0) |
| 74 | memset (tc, 0xFA, sizeof (*tc)); |
| 75 | |
| 76 | pool_put_index (tm->listener_pool, listener_index); |
| 77 | } |
| 78 | |
| 79 | u32 |
| 80 | sctp_session_unbind (u32 listener_index) |
| 81 | { |
| 82 | sctp_connection_unbind (listener_index); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | sctp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add) |
| 88 | { |
| 89 | sctp_main_t *tm = &sctp_main; |
| 90 | if (is_ip4) |
| 91 | tm->punt_unknown4 = is_add; |
| 92 | else |
| 93 | tm->punt_unknown6 = is_add; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | sctp_alloc_custom_local_endpoint (sctp_main_t * tm, ip46_address_t * lcl_addr, |
| 98 | u16 * lcl_port, u8 is_ip4) |
| 99 | { |
| 100 | int index, port; |
| 101 | if (is_ip4) |
| 102 | { |
| 103 | index = tm->last_v4_address_rotor++; |
| 104 | if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses)) |
| 105 | tm->last_v4_address_rotor = 0; |
| 106 | lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | index = tm->last_v6_address_rotor++; |
| 111 | if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses)) |
| 112 | tm->last_v6_address_rotor = 0; |
| 113 | clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index], |
| 114 | sizeof (ip6_address_t)); |
| 115 | } |
| 116 | port = transport_alloc_local_port (TRANSPORT_PROTO_SCTP, lcl_addr); |
| 117 | if (port < 1) |
| 118 | { |
| 119 | clib_warning ("Failed to allocate src port"); |
| 120 | return -1; |
| 121 | } |
| 122 | *lcl_port = port; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Initialize all connection timers as invalid |
| 128 | */ |
| 129 | void |
| 130 | sctp_connection_timers_init (sctp_connection_t * tc) |
| 131 | { |
| 132 | int i, j; |
| 133 | |
| 134 | /* Set all to invalid */ |
| 135 | for (i = 0; i < MAX_SCTP_CONNECTIONS; i++) |
| 136 | for (j = 0; j < SCTP_N_TIMERS; j++) |
| 137 | { |
| 138 | tc->sub_conn[i].timers[j] = SCTP_TIMER_HANDLE_INVALID; |
| 139 | } |
| 140 | |
| 141 | tc->rto = SCTP_RTO_INIT; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Stop all connection timers |
| 146 | */ |
| 147 | void |
| 148 | sctp_connection_timers_reset (sctp_connection_t * tc) |
| 149 | { |
| 150 | int i, j; |
| 151 | for (i = 0; i < MAX_SCTP_CONNECTIONS; i++) |
| 152 | { |
| 153 | for (j = 0; j < SCTP_N_TIMERS; j++) |
| 154 | sctp_timer_reset (tc, i, j); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | const char *sctp_fsm_states[] = { |
| 159 | #define _(sym, str) str, |
| 160 | foreach_sctp_fsm_state |
| 161 | #undef _ |
| 162 | }; |
| 163 | |
| 164 | u8 * |
| 165 | format_sctp_state (u8 * s, va_list * args) |
| 166 | { |
| 167 | u32 state = va_arg (*args, u32); |
| 168 | |
| 169 | if (state < SCTP_N_STATES) |
| 170 | s = format (s, "%s", sctp_fsm_states[state]); |
| 171 | else |
| 172 | s = format (s, "UNKNOWN (%d (0x%x))", state, state); |
| 173 | return s; |
| 174 | } |
| 175 | |
| 176 | u8 * |
| 177 | format_sctp_connection_id (u8 * s, va_list * args) |
| 178 | { |
| 179 | /* |
| 180 | sctp_connection_t *tc = va_arg (*args, sctp_connection_t *); |
| 181 | if (!tc) |
| 182 | return s; |
| 183 | if (tc->c_is_ip4) |
| 184 | { |
| 185 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 186 | format_ip4_address, &tc->c_lcl_ip4, |
| 187 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address, |
| 188 | &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T", |
| 193 | format_ip6_address, &tc->c_lcl_ip6, |
| 194 | clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address, |
| 195 | &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port)); |
| 196 | } |
| 197 | */ |
| 198 | return s; |
| 199 | } |
| 200 | |
| 201 | u8 * |
| 202 | format_sctp_connection (u8 * s, va_list * args) |
| 203 | { |
| 204 | sctp_connection_t *tc = va_arg (*args, sctp_connection_t *); |
| 205 | u32 verbose = va_arg (*args, u32); |
| 206 | |
| 207 | if (!tc) |
| 208 | return s; |
| 209 | s = format (s, "%-50U", format_sctp_connection_id, tc); |
| 210 | if (verbose) |
| 211 | { |
| 212 | s = format (s, "%-15U", format_sctp_state, tc->state); |
| 213 | } |
| 214 | |
| 215 | return s; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Initialize connection send variables. |
| 220 | */ |
| 221 | void |
| 222 | sctp_init_snd_vars (sctp_connection_t * tc) |
| 223 | { |
| 224 | u32 time_now; |
| 225 | |
| 226 | /* |
| 227 | * We use the time to randomize iss and for setting up the initial |
| 228 | * timestamp. Make sure it's updated otherwise syn and ack in the |
| 229 | * handshake may make it look as if time has flown in the opposite |
| 230 | * direction for us. |
| 231 | */ |
| 232 | sctp_set_time_now (vlib_get_thread_index ()); |
| 233 | time_now = sctp_time_now (); |
| 234 | |
| 235 | tc->iss = random_u32 (&time_now); |
| 236 | tc->snd_una = tc->iss; |
| 237 | tc->snd_nxt = tc->iss + 1; |
| 238 | tc->snd_una_max = tc->snd_nxt; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Update max segment size we're able to process. |
| 243 | * |
| 244 | * The value is constrained by our interface's MTU and IP options. It is |
| 245 | * also what we advertise to our peer. |
| 246 | */ |
| 247 | void |
| 248 | sctp_update_rcv_mss (sctp_connection_t * tc) |
| 249 | { |
| 250 | /* TODO find our iface MTU */ |
| 251 | tc->a_rwnd = DEFAULT_A_RWND - sizeof (sctp_full_hdr_t); |
| 252 | tc->rcv_opts.a_rwnd = tc->a_rwnd; |
| 253 | tc->rcv_a_rwnd = tc->a_rwnd; /* This will be updated by our congestion algos */ |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | sctp_init_mss (sctp_connection_t * tc) |
| 258 | { |
| 259 | SCTP_DBG ("CONN_INDEX = %u", |
| 260 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index); |
| 261 | |
| 262 | u16 default_a_rwnd = 536; |
| 263 | sctp_update_rcv_mss (tc); |
| 264 | |
| 265 | /* TODO cache mss and consider PMTU discovery */ |
| 266 | tc->snd_a_rwnd = clib_min (tc->rcv_opts.a_rwnd, tc->a_rwnd); |
| 267 | |
| 268 | if (tc->snd_a_rwnd < sizeof (sctp_full_hdr_t)) |
| 269 | { |
| 270 | SCTP_ADV_DBG ("tc->snd_a_rwnd < sizeof(sctp_full_hdr_t)"); |
| 271 | /* Assume that at least the min default mss works */ |
| 272 | tc->snd_a_rwnd = default_a_rwnd; |
| 273 | tc->rcv_opts.a_rwnd = default_a_rwnd; |
| 274 | } |
| 275 | |
| 276 | ASSERT (tc->snd_a_rwnd > sizeof (sctp_full_hdr_t)); |
| 277 | } |
| 278 | |
| 279 | /** Initialize sctp connection variables |
| 280 | * |
| 281 | * Should be called after having received a msg from the peer, i.e., a SYN or |
| 282 | * a SYNACK, such that connection options have already been exchanged. */ |
| 283 | void |
| 284 | sctp_connection_init_vars (sctp_connection_t * tc) |
| 285 | { |
| 286 | sctp_init_mss (tc); |
| 287 | sctp_init_snd_vars (tc); |
| 288 | } |
| 289 | |
| 290 | always_inline sctp_connection_t * |
| 291 | sctp_sub_connection_add (u8 thread_index) |
| 292 | { |
| 293 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 294 | sctp_connection_t *tc = tm->connections[thread_index]; |
| 295 | |
| 296 | tc->sub_conn[tc->next_avail_sub_conn].connection.c_index = |
| 297 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index; |
| 298 | tc->sub_conn[tc->next_avail_sub_conn].connection.thread_index = |
| 299 | thread_index; |
| 300 | tc->sub_conn[tc->next_avail_sub_conn].parent = tc; |
| 301 | |
| 302 | tc->next_avail_sub_conn += 1; |
| 303 | |
| 304 | return tc; |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | sctp_sub_connection_add_ip4 (u8 thread_index, |
| 309 | sctp_ipv4_addr_param_t * ipv4_addr) |
| 310 | { |
| 311 | sctp_connection_t *tc = sctp_sub_connection_add (thread_index); |
| 312 | |
| 313 | clib_memcpy (&tc->sub_conn[tc->next_avail_sub_conn].connection.lcl_ip.ip4, |
| 314 | &ipv4_addr->address, sizeof (ipv4_addr->address)); |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | sctp_sub_connection_add_ip6 (u8 thread_index, |
| 319 | sctp_ipv6_addr_param_t * ipv6_addr) |
| 320 | { |
| 321 | sctp_connection_t *tc = sctp_sub_connection_add (thread_index); |
| 322 | |
| 323 | clib_memcpy (&tc->sub_conn[tc->next_avail_sub_conn].connection.lcl_ip.ip6, |
| 324 | &ipv6_addr->address, sizeof (ipv6_addr->address)); |
| 325 | } |
| 326 | |
| 327 | sctp_connection_t * |
| 328 | sctp_connection_new (u8 thread_index) |
| 329 | { |
| 330 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 331 | sctp_connection_t *tc; |
| 332 | |
| 333 | pool_get (tm->connections[thread_index], tc); |
| 334 | memset (tc, 0, sizeof (*tc)); |
| 335 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].parent = tc; |
| 336 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index = |
| 337 | tc - tm->connections[thread_index]; |
| 338 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_thread_index = thread_index; |
| 339 | tc->local_tag = 0; |
| 340 | tc->next_avail_sub_conn = 1; |
| 341 | |
| 342 | return tc; |
| 343 | } |
| 344 | |
| 345 | sctp_connection_t * |
| 346 | sctp_half_open_connection_new (u8 thread_index) |
| 347 | { |
| 348 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 349 | sctp_connection_t *tc = 0; |
| 350 | ASSERT (vlib_get_thread_index () == 0); |
| 351 | pool_get (tm->half_open_connections, tc); |
| 352 | memset (tc, 0, sizeof (*tc)); |
| 353 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index = |
| 354 | tc - tm->half_open_connections; |
| 355 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].parent = tc; |
| 356 | return tc; |
| 357 | } |
| 358 | |
| 359 | static inline int |
| 360 | sctp_connection_open (transport_endpoint_t * rmt) |
| 361 | { |
| 362 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 363 | sctp_connection_t *tc; |
| 364 | ip46_address_t lcl_addr; |
| 365 | u16 lcl_port; |
| 366 | uword thread_id; |
| 367 | int rv; |
| 368 | |
| 369 | u8 idx = sctp_pick_conn_idx_on_state (SCTP_STATE_CLOSED); |
| 370 | |
| 371 | /* |
| 372 | * Allocate local endpoint |
| 373 | */ |
| 374 | if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses)) |
| 375 | || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses))) |
| 376 | rv = sctp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port, |
| 377 | rmt->is_ip4); |
| 378 | else |
| 379 | rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_SCTP, |
| 380 | rmt, &lcl_addr, &lcl_port); |
| 381 | |
| 382 | if (rv) |
| 383 | return -1; |
| 384 | |
| 385 | /* |
| 386 | * Create connection and send INIT CHUNK |
| 387 | */ |
| 388 | thread_id = vlib_get_thread_index (); |
| 389 | ASSERT (thread_id == 0); |
| 390 | |
| 391 | clib_spinlock_lock_if_init (&tm->half_open_lock); |
| 392 | tc = sctp_half_open_connection_new (thread_id); |
| 393 | |
| 394 | transport_connection_t *t_conn = &tc->sub_conn[idx].connection; |
| 395 | ip_copy (&t_conn->rmt_ip, &rmt->ip, rmt->is_ip4); |
| 396 | ip_copy (&t_conn->lcl_ip, &lcl_addr, rmt->is_ip4); |
| 397 | tc->sub_conn[idx].parent = tc; |
| 398 | t_conn->rmt_port = rmt->port; |
| 399 | t_conn->lcl_port = clib_host_to_net_u16 (lcl_port); |
| 400 | t_conn->is_ip4 = rmt->is_ip4; |
| 401 | t_conn->proto = TRANSPORT_PROTO_SCTP; |
| 402 | t_conn->fib_index = rmt->fib_index; |
| 403 | |
| 404 | sctp_connection_timers_init (tc); |
| 405 | /* The other connection vars will be initialized after INIT_ACK chunk received */ |
| 406 | sctp_init_snd_vars (tc); |
| 407 | |
| 408 | sctp_send_init (tc); |
| 409 | |
| 410 | clib_spinlock_unlock_if_init (&tm->half_open_lock); |
| 411 | |
| 412 | return tc->sub_conn[idx].connection.c_index; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Cleans up connection state. |
| 417 | * |
| 418 | * No notifications. |
| 419 | */ |
| 420 | void |
| 421 | sctp_connection_cleanup (sctp_connection_t * tc) |
| 422 | { |
| 423 | sctp_main_t *tm = &sctp_main; |
| 424 | u8 i; |
| 425 | |
| 426 | /* Cleanup local endpoint if this was an active connect */ |
| 427 | for (i = 0; i < MAX_SCTP_CONNECTIONS; i++) |
| 428 | transport_endpoint_cleanup (TRANSPORT_PROTO_SCTP, |
| 429 | &tc->sub_conn[i].connection.lcl_ip, |
| 430 | tc->sub_conn[i].connection.lcl_port); |
| 431 | |
| 432 | /* Check if connection is not yet fully established */ |
| 433 | if (tc->state == SCTP_STATE_COOKIE_WAIT) |
| 434 | { |
| 435 | |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | int thread_index = |
| 440 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.thread_index; |
| 441 | |
| 442 | /* Make sure all timers are cleared */ |
| 443 | sctp_connection_timers_reset (tc); |
| 444 | |
| 445 | /* Poison the entry */ |
| 446 | if (CLIB_DEBUG > 0) |
| 447 | memset (tc, 0xFA, sizeof (*tc)); |
| 448 | pool_put (tm->connections[thread_index], tc); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | sctp_session_open (transport_endpoint_t * tep) |
| 454 | { |
| 455 | return sctp_connection_open (tep); |
| 456 | } |
| 457 | |
| 458 | u16 |
| 459 | sctp_check_outstanding_data_chunks (sctp_connection_t * tc) |
| 460 | { |
| 461 | return 0; /* Indicates no more data to be read/sent */ |
| 462 | } |
| 463 | |
| 464 | void |
| 465 | sctp_connection_close (sctp_connection_t * tc) |
| 466 | { |
| 467 | SCTP_DBG ("Closing connection %u...", |
| 468 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index); |
| 469 | |
| 470 | tc->state = SCTP_STATE_SHUTDOWN_PENDING; |
| 471 | |
| 472 | sctp_send_shutdown (tc); |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | sctp_session_close (u32 conn_index, u32 thread_index) |
| 477 | { |
| 478 | ASSERT (thread_index == 0); |
| 479 | |
| 480 | sctp_connection_t *tc; |
| 481 | tc = sctp_connection_get (conn_index, thread_index); |
| 482 | sctp_connection_close (tc); |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | sctp_session_cleanup (u32 conn_index, u32 thread_index) |
| 487 | { |
| 488 | sctp_connection_t *tc; |
| 489 | tc = sctp_connection_get (conn_index, thread_index); |
| 490 | sctp_connection_timers_reset (tc); |
| 491 | |
| 492 | /* Wait for the session tx events to clear */ |
| 493 | tc->state = SCTP_STATE_CLOSED; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Update snd_mss to reflect the effective segment size that we can send |
| 498 | */ |
| 499 | void |
| 500 | sctp_update_snd_mss (sctp_connection_t * tc) |
| 501 | { |
| 502 | /* The overhead for the sctp_header_t and sctp_chunks_common_hdr_t |
| 503 | * (the sum equals to sctp_full_hdr_t) is already taken into account |
| 504 | * for the tc->a_rwnd computation. |
| 505 | * So let's not account it again here. |
| 506 | */ |
| 507 | tc->snd_hdr_length = |
| 508 | sizeof (sctp_payload_data_chunk_t) - sizeof (sctp_full_hdr_t); |
| 509 | tc->snd_a_rwnd = |
| 510 | clib_min (tc->a_rwnd, tc->rcv_opts.a_rwnd) - tc->snd_hdr_length; |
| 511 | |
| 512 | SCTP_DBG ("tc->snd_a_rwnd = %u, tc->snd_hdr_length = %u ", |
| 513 | tc->snd_a_rwnd, tc->snd_hdr_length); |
| 514 | |
| 515 | ASSERT (tc->snd_a_rwnd > 0); |
| 516 | } |
| 517 | |
| 518 | u16 |
| 519 | sctp_session_send_mss (transport_connection_t * trans_conn) |
| 520 | { |
| 521 | SCTP_DBG ("CONN_INDEX: %u", trans_conn->c_index); |
| 522 | |
| 523 | sctp_connection_t *tc = sctp_get_connection_from_transport (trans_conn); |
| 524 | |
| 525 | if (trans_conn == NULL) |
| 526 | { |
| 527 | SCTP_DBG ("trans_conn == NULL"); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | if (tc == NULL) |
| 532 | { |
| 533 | SCTP_DBG ("tc == NULL"); |
| 534 | return 0; |
| 535 | } |
| 536 | /* Ensure snd_mss does accurately reflect the amount of data we can push |
| 537 | * in a segment. This also makes sure that options are updated according to |
| 538 | * the current state of the connection. */ |
| 539 | sctp_update_snd_mss (tc); |
| 540 | |
| 541 | return tc->snd_a_rwnd; |
| 542 | } |
| 543 | |
| 544 | u16 |
| 545 | sctp_snd_space (sctp_connection_t * sctp_conn) |
| 546 | { |
| 547 | /* TODO: This requires a real implementation */ |
| 548 | if (sctp_conn == NULL) |
| 549 | { |
| 550 | SCTP_DBG ("sctp_conn == NULL"); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | if (sctp_conn->state != SCTP_STATE_ESTABLISHED) |
| 555 | { |
| 556 | SCTP_DBG_STATE_MACHINE |
| 557 | ("Trying to send DATA while not in SCTP_STATE_ESTABLISHED"); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | return sctp_conn->snd_a_rwnd; |
| 562 | } |
| 563 | |
| 564 | u32 |
| 565 | sctp_session_send_space (transport_connection_t * trans_conn) |
| 566 | { |
| 567 | SCTP_DBG ("CONN_INDEX: %u", trans_conn->c_index); |
| 568 | |
| 569 | sctp_connection_t *tc = sctp_get_connection_from_transport (trans_conn); |
| 570 | |
| 571 | return sctp_snd_space (tc); |
| 572 | } |
| 573 | |
| 574 | u32 |
| 575 | sctp_session_tx_fifo_offset (transport_connection_t * trans_conn) |
| 576 | { |
| 577 | SCTP_DBG ("CONN_INDEX: %u", trans_conn->c_index); |
| 578 | |
| 579 | sctp_connection_t *tc = sctp_get_connection_from_transport (trans_conn); |
| 580 | |
| 581 | if (tc == NULL) |
| 582 | { |
| 583 | SCTP_DBG ("tc == NULL"); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* This still works if fast retransmit is on */ |
| 588 | return (tc->snd_nxt - tc->snd_una); |
| 589 | } |
| 590 | |
| 591 | transport_connection_t * |
| 592 | sctp_session_get_transport (u32 conn_index, u32 thread_index) |
| 593 | { |
| 594 | sctp_connection_t *tc = sctp_connection_get (conn_index, thread_index); |
| 595 | return &tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection; |
| 596 | } |
| 597 | |
| 598 | transport_connection_t * |
| 599 | sctp_session_get_listener (u32 listener_index) |
| 600 | { |
| 601 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 602 | sctp_connection_t *tc; |
| 603 | tc = pool_elt_at_index (tm->listener_pool, listener_index); |
| 604 | return &tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection; |
| 605 | } |
| 606 | |
| 607 | u8 * |
| 608 | format_sctp_session (u8 * s, va_list * args) |
| 609 | { |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | u8 * |
| 614 | format_sctp_listener_session (u8 * s, va_list * args) |
| 615 | { |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | void |
| 620 | sctp_timer_init_handler (u32 conn_index) |
| 621 | { |
| 622 | sctp_connection_t *tc; |
| 623 | |
| 624 | tc = sctp_connection_get (conn_index, vlib_get_thread_index ()); |
| 625 | /* note: the connection may have already disappeared */ |
| 626 | if (PREDICT_FALSE (tc == 0)) |
| 627 | return; |
| 628 | ASSERT (tc->state == SCTP_STATE_COOKIE_ECHOED); |
| 629 | /* Start cleanup. App wasn't notified yet so use delete notify as |
| 630 | * opposed to delete to cleanup session layer state. */ |
| 631 | stream_session_delete_notify (&tc-> |
| 632 | sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection); |
| 633 | tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].timers[SCTP_TIMER_T1_INIT] = |
| 634 | SCTP_TIMER_HANDLE_INVALID; |
| 635 | |
| 636 | sctp_connection_cleanup (tc); |
| 637 | } |
| 638 | |
| 639 | /* *INDENT OFF* */ |
| 640 | static timer_expiration_handler *sctp_timer_expiration_handlers[SCTP_N_TIMERS] |
| 641 | = { |
| 642 | sctp_timer_init_handler |
| 643 | }; |
| 644 | |
| 645 | /* *INDENT ON* */ |
| 646 | |
| 647 | static void |
| 648 | sctp_expired_timers_dispatch (u32 * expired_timers) |
| 649 | { |
| 650 | int i; |
| 651 | u32 connection_index, timer_id; |
| 652 | |
| 653 | for (i = 0; i < vec_len (expired_timers); i++) |
| 654 | { |
| 655 | /* Get session index and timer id */ |
| 656 | connection_index = expired_timers[i] & 0x0FFFFFFF; |
| 657 | timer_id = expired_timers[i] >> 28; |
| 658 | |
| 659 | /* Handle expiration */ |
| 660 | (*sctp_timer_expiration_handlers[timer_id]) (connection_index); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | void |
| 665 | sctp_initialize_timer_wheels (sctp_main_t * tm) |
| 666 | { |
| 667 | tw_timer_wheel_16t_2w_512sl_t *tw; |
| 668 | /* *INDENT-OFF* */ |
| 669 | foreach_vlib_main (({ |
| 670 | tw = &tm->timer_wheels[ii]; |
| 671 | tw_timer_wheel_init_16t_2w_512sl (tw, sctp_expired_timers_dispatch, |
| 672 | 100e-3 /* timer period 100ms */ , ~0); |
| 673 | tw->last_run_time = vlib_time_now (this_vlib_main); |
| 674 | })); |
| 675 | /* *INDENT-ON* */ |
| 676 | } |
| 677 | |
| 678 | clib_error_t * |
| 679 | sctp_main_enable (vlib_main_t * vm) |
| 680 | { |
| 681 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 682 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 683 | clib_error_t *error = 0; |
| 684 | u32 num_threads; |
| 685 | int thread; |
| 686 | sctp_connection_t *tc __attribute__ ((unused)); |
| 687 | u32 preallocated_connections_per_thread; |
| 688 | |
| 689 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 690 | return error; |
| 691 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 692 | return error; |
| 693 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 694 | return error; |
| 695 | |
| 696 | /* |
| 697 | * Registrations |
| 698 | */ |
| 699 | |
| 700 | ip4_register_protocol (IP_PROTOCOL_SCTP, sctp4_input_node.index); |
| 701 | ip6_register_protocol (IP_PROTOCOL_SCTP, sctp6_input_node.index); |
| 702 | |
| 703 | /* |
| 704 | * Initialize data structures |
| 705 | */ |
| 706 | |
| 707 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 708 | vec_validate (tm->connections, num_threads - 1); |
| 709 | |
| 710 | /* |
| 711 | * Preallocate connections. Assume that thread 0 won't |
| 712 | * use preallocated threads when running multi-core |
| 713 | */ |
| 714 | if (num_threads == 1) |
| 715 | { |
| 716 | thread = 0; |
| 717 | preallocated_connections_per_thread = tm->preallocated_connections; |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | thread = 1; |
| 722 | preallocated_connections_per_thread = |
| 723 | tm->preallocated_connections / (num_threads - 1); |
| 724 | } |
| 725 | for (; thread < num_threads; thread++) |
| 726 | { |
| 727 | if (preallocated_connections_per_thread) |
| 728 | pool_init_fixed (tm->connections[thread], |
| 729 | preallocated_connections_per_thread); |
| 730 | } |
| 731 | |
| 732 | /* Initialize per worker thread tx buffers (used for control messages) */ |
| 733 | vec_validate (tm->tx_buffers, num_threads - 1); |
| 734 | |
| 735 | /* Initialize timer wheels */ |
| 736 | vec_validate (tm->timer_wheels, num_threads - 1); |
| 737 | sctp_initialize_timer_wheels (tm); |
| 738 | |
| 739 | /* Initialize clocks per tick for SCTP timestamp. Used to compute |
| 740 | * monotonically increasing timestamps. */ |
| 741 | tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock |
| 742 | / SCTP_TSTAMP_RESOLUTION; |
| 743 | |
| 744 | if (num_threads > 1) |
| 745 | { |
| 746 | } |
| 747 | |
| 748 | vec_validate (tm->tx_frames[0], num_threads - 1); |
| 749 | vec_validate (tm->tx_frames[1], num_threads - 1); |
| 750 | vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1); |
| 751 | vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1); |
| 752 | |
| 753 | tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size |
| 754 | (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 755 | |
| 756 | vec_validate (tm->time_now, num_threads - 1); |
| 757 | return error; |
| 758 | } |
| 759 | |
| 760 | clib_error_t * |
| 761 | sctp_enable_disable (vlib_main_t * vm, u8 is_en) |
| 762 | { |
| 763 | if (is_en) |
| 764 | { |
| 765 | if (sctp_main.is_enabled) |
| 766 | return 0; |
| 767 | |
| 768 | return sctp_main_enable (vm); |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | sctp_main.is_enabled = 0; |
| 773 | } |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | transport_connection_t * |
| 779 | sctp_half_open_session_get_transport (u32 conn_index) |
| 780 | { |
| 781 | sctp_connection_t *tc = sctp_half_open_connection_get (conn_index); |
| 782 | return &tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection; |
| 783 | } |
| 784 | |
| 785 | u8 * |
| 786 | format_sctp_half_open (u8 * s, va_list * args) |
| 787 | { |
| 788 | u32 tci = va_arg (*args, u32); |
| 789 | sctp_connection_t *tc = sctp_half_open_connection_get (tci); |
| 790 | return format (s, "%U", format_sctp_connection_id, tc); |
| 791 | } |
| 792 | |
| 793 | /* *INDENT OFF* */ |
| 794 | const static transport_proto_vft_t sctp_proto = { |
| 795 | .enable = sctp_enable_disable, |
| 796 | .bind = sctp_session_bind, |
| 797 | .unbind = sctp_session_unbind, |
| 798 | .open = sctp_session_open, |
| 799 | .close = sctp_session_close, |
| 800 | .cleanup = sctp_session_cleanup, |
| 801 | .push_header = sctp_push_header, |
| 802 | .send_mss = sctp_session_send_mss, |
| 803 | .send_space = sctp_session_send_space, |
| 804 | .tx_fifo_offset = NULL, //sctp_session_tx_fifo_offset, |
| 805 | .get_connection = sctp_session_get_transport, |
| 806 | .get_listener = sctp_session_get_listener, |
| 807 | .get_half_open = sctp_half_open_session_get_transport, |
| 808 | .format_connection = format_sctp_session, |
| 809 | .format_listener = format_sctp_listener_session, |
| 810 | .format_half_open = format_sctp_half_open, |
| 811 | }; |
| 812 | |
| 813 | /* *INDENT ON* */ |
| 814 | |
| 815 | clib_error_t * |
| 816 | sctp_init (vlib_main_t * vm) |
| 817 | { |
| 818 | sctp_main_t *tm = vnet_get_sctp_main (); |
| 819 | ip_main_t *im = &ip_main; |
| 820 | ip_protocol_info_t *pi; |
| 821 | /* Session layer, and by implication SCTP, are disabled by default */ |
| 822 | tm->is_enabled = 0; |
| 823 | |
| 824 | /* Register with IP for header parsing */ |
| 825 | pi = ip_get_protocol_info (im, IP_PROTOCOL_SCTP); |
| 826 | if (pi == 0) |
| 827 | return clib_error_return (0, "SCTP protocol info AWOL"); |
| 828 | pi->format_header = format_sctp_header; |
| 829 | pi->unformat_pg_edit = unformat_pg_sctp_header; |
| 830 | |
| 831 | /* Register as transport with session layer */ |
| 832 | transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto, |
| 833 | FIB_PROTOCOL_IP4, sctp4_output_node.index); |
| 834 | transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto, |
| 835 | FIB_PROTOCOL_IP6, sctp6_output_node.index); |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | VLIB_INIT_FUNCTION (sctp_init); |
| 841 | |
| 842 | /* |
| 843 | * fd.io coding-style-patch-verification: ON |
| 844 | * |
| 845 | * Local Variables: |
| 846 | * eval: (c-set-style "gnu") |
| 847 | * End: |
| 848 | */ |