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