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