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