blob: d0e28c53fe7a5a97c007024821be3f83eaba7848 [file] [log] [blame]
Marco Varlese191a5942017-10-30 18:17:21 +01001/*
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
18sctp_main_t sctp_main;
19
20static u32
21sctp_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;
Florin Corasb0f662f2018-12-27 14:51:46 -080026 u32 mtu = 1460;
Marco Varlese191a5942017-10-30 18:17:21 +010027
28 pool_get (tm->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -040029 clib_memset (listener, 0, sizeof (*listener));
Marco Varlese191a5942017-10-30 18:17:21 +010030
Marco Varlesec7fe4f32018-03-05 15:12:29 +010031 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].subconn_idx =
32 SCTP_PRIMARY_PATH_IDX;
33 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].c_c_index =
Marco Varlese191a5942017-10-30 18:17:21 +010034 listener - tm->listener_pool;
Marco Varlesec7fe4f32018-03-05 15:12:29 +010035 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.lcl_port = tep->port;
Marco Varlese191a5942017-10-30 18:17:21 +010036
37 /* If we are provided a sw_if_index, bind using one of its IPs */
38 if (ip_is_zero (&tep->ip, 1) && tep->sw_if_index != ENDPOINT_INVALID_INDEX)
39 {
40 if ((iface_ip = ip_interface_get_first_ip (tep->sw_if_index,
41 tep->is_ip4)))
42 ip_set (&tep->ip, iface_ip, tep->is_ip4);
43 }
Marco Varlesec7fe4f32018-03-05 15:12:29 +010044 ip_copy (&listener->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.lcl_ip,
Marco Varlese191a5942017-10-30 18:17:21 +010045 &tep->ip, tep->is_ip4);
46
Florin Corasb0f662f2018-12-27 14:51:46 -080047 if (tep->sw_if_index != ENDPOINT_INVALID_INDEX)
48 mtu = tep->is_ip4 ? vnet_sw_interface_get_mtu (vnet_get_main (),
49 tep->sw_if_index,
50 VNET_MTU_IP4) :
51 vnet_sw_interface_get_mtu (vnet_get_main (), tep->sw_if_index,
52 VNET_MTU_IP6);
53
Ole Troand7231612018-06-07 10:17:57 +020054 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].PMTU = mtu;
Marco Varlesec7fe4f32018-03-05 15:12:29 +010055 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.is_ip4 = tep->is_ip4;
56 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.proto =
Marco Varlese191a5942017-10-30 18:17:21 +010057 TRANSPORT_PROTO_SCTP;
Marco Varlesec7fe4f32018-03-05 15:12:29 +010058 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].c_s_index = session_index;
59 listener->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.fib_index =
Marco Varlese191a5942017-10-30 18:17:21 +010060 tep->fib_index;
61 listener->state = SCTP_STATE_CLOSED;
62
63 sctp_connection_timers_init (listener);
64
Marco Varlesec7fe4f32018-03-05 15:12:29 +010065 return listener->sub_conn[SCTP_PRIMARY_PATH_IDX].c_c_index;
Marco Varlese191a5942017-10-30 18:17:21 +010066}
67
68u32
69sctp_session_bind (u32 session_index, transport_endpoint_t * tep)
70{
71 return sctp_connection_bind (session_index, tep);
72}
73
74static void
75sctp_connection_unbind (u32 listener_index)
76{
77 sctp_main_t *tm = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +010078 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +010079
Marco Varlese8ad6a2d2018-01-26 16:50:01 +010080 sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
Marco Varlese191a5942017-10-30 18:17:21 +010081
82 /* Poison the entry */
83 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -040084 clib_memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
Marco Varlese191a5942017-10-30 18:17:21 +010085
86 pool_put_index (tm->listener_pool, listener_index);
87}
88
89u32
90sctp_session_unbind (u32 listener_index)
91{
92 sctp_connection_unbind (listener_index);
93 return 0;
94}
95
96void
97sctp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
98{
99 sctp_main_t *tm = &sctp_main;
100 if (is_ip4)
101 tm->punt_unknown4 = is_add;
102 else
103 tm->punt_unknown6 = is_add;
104}
105
106static int
107sctp_alloc_custom_local_endpoint (sctp_main_t * tm, ip46_address_t * lcl_addr,
108 u16 * lcl_port, u8 is_ip4)
109{
110 int index, port;
111 if (is_ip4)
112 {
113 index = tm->last_v4_address_rotor++;
114 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
115 tm->last_v4_address_rotor = 0;
116 lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
117 }
118 else
119 {
120 index = tm->last_v6_address_rotor++;
121 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
122 tm->last_v6_address_rotor = 0;
123 clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index],
124 sizeof (ip6_address_t));
125 }
126 port = transport_alloc_local_port (TRANSPORT_PROTO_SCTP, lcl_addr);
127 if (port < 1)
128 {
129 clib_warning ("Failed to allocate src port");
130 return -1;
131 }
132 *lcl_port = port;
133 return 0;
134}
135
136/**
137 * Initialize all connection timers as invalid
138 */
139void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100140sctp_connection_timers_init (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100141{
142 int i, j;
143
144 /* Set all to invalid */
145 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100146 {
147 sctp_conn->sub_conn[i].RTO = SCTP_RTO_INIT;
Marco Varlese21c8baf2018-02-02 17:17:51 +0100148
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100149 for (j = 0; j < SCTP_N_TIMERS; j++)
150 {
151 sctp_conn->sub_conn[i].timers[j] = SCTP_TIMER_HANDLE_INVALID;
152 }
153 }
Marco Varlese191a5942017-10-30 18:17:21 +0100154}
155
156/**
157 * Stop all connection timers
158 */
159void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100160sctp_connection_timers_reset (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100161{
162 int i, j;
163 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
164 {
165 for (j = 0; j < SCTP_N_TIMERS; j++)
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100166 sctp_timer_reset (sctp_conn, i, j);
Marco Varlese191a5942017-10-30 18:17:21 +0100167 }
168}
169
170const char *sctp_fsm_states[] = {
171#define _(sym, str) str,
172 foreach_sctp_fsm_state
173#undef _
174};
175
176u8 *
177format_sctp_state (u8 * s, va_list * args)
178{
179 u32 state = va_arg (*args, u32);
180
181 if (state < SCTP_N_STATES)
182 s = format (s, "%s", sctp_fsm_states[state]);
183 else
184 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
185 return s;
186}
187
188u8 *
189format_sctp_connection_id (u8 * s, va_list * args)
190{
Marco Varlesef3ab4892018-02-19 15:23:13 +0100191 sctp_connection_t *sctp_conn = va_arg (*args, sctp_connection_t *);
192 if (!sctp_conn)
193 return s;
194
195 u8 i;
196 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
197 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800198 if (i > 0 && sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
199 continue;
Marco Varlesef3ab4892018-02-19 15:23:13 +0100200 if (sctp_conn->sub_conn[i].connection.is_ip4)
201 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800202 s = format (s, "[#%d][%s] %U:%d->%U:%d",
Marco Varlesef3ab4892018-02-19 15:23:13 +0100203 sctp_conn->sub_conn[i].connection.thread_index,
Florin Corasb0f662f2018-12-27 14:51:46 -0800204 "S",
Marco Varlesef3ab4892018-02-19 15:23:13 +0100205 format_ip4_address,
206 &sctp_conn->sub_conn[i].connection.lcl_ip.ip4,
207 clib_net_to_host_u16 (sctp_conn->sub_conn[i].
208 connection.lcl_port),
209 format_ip4_address,
210 &sctp_conn->sub_conn[i].connection.rmt_ip.ip4,
211 clib_net_to_host_u16 (sctp_conn->sub_conn[i].
212 connection.rmt_port));
213 }
214 else
215 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800216 s = format (s, "[#%d][%s] %U:%d->%U:%d",
Marco Varlesef3ab4892018-02-19 15:23:13 +0100217 sctp_conn->sub_conn[i].connection.thread_index,
Florin Corasb0f662f2018-12-27 14:51:46 -0800218 "S",
Marco Varlesef3ab4892018-02-19 15:23:13 +0100219 format_ip6_address,
220 &sctp_conn->sub_conn[i].connection.lcl_ip.ip6,
221 clib_net_to_host_u16 (sctp_conn->sub_conn[i].
222 connection.lcl_port),
223 format_ip6_address,
224 &sctp_conn->sub_conn[i].connection.rmt_ip.ip6,
225 clib_net_to_host_u16 (sctp_conn->sub_conn[i].
226 connection.rmt_port));
227 }
228 }
Marco Varlese191a5942017-10-30 18:17:21 +0100229 return s;
230}
231
232u8 *
233format_sctp_connection (u8 * s, va_list * args)
234{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100235 sctp_connection_t *sctp_conn = va_arg (*args, sctp_connection_t *);
Marco Varlese191a5942017-10-30 18:17:21 +0100236 u32 verbose = va_arg (*args, u32);
237
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100238 if (!sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100239 return s;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100240 s = format (s, "%-50U", format_sctp_connection_id, sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100241 if (verbose)
242 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100243 s = format (s, "%-15U", format_sctp_state, sctp_conn->state);
Florin Corasb0f662f2018-12-27 14:51:46 -0800244 if (verbose > 1)
245 s = format (s, "\n");
Marco Varlese191a5942017-10-30 18:17:21 +0100246 }
247
248 return s;
249}
250
251/**
252 * Initialize connection send variables.
253 */
254void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100255sctp_init_snd_vars (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100256{
257 u32 time_now;
Marco Varlese191a5942017-10-30 18:17:21 +0100258 /*
259 * We use the time to randomize iss and for setting up the initial
260 * timestamp. Make sure it's updated otherwise syn and ack in the
261 * handshake may make it look as if time has flown in the opposite
262 * direction for us.
263 */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100264
Marco Varlese191a5942017-10-30 18:17:21 +0100265 sctp_set_time_now (vlib_get_thread_index ());
266 time_now = sctp_time_now ();
267
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100268 sctp_conn->local_initial_tsn = random_u32 (&time_now);
Marco Varlesef3ab4892018-02-19 15:23:13 +0100269 sctp_conn->last_unacked_tsn = sctp_conn->local_initial_tsn;
270 sctp_conn->next_tsn = sctp_conn->local_initial_tsn + 1;
271
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100272 sctp_conn->remote_initial_tsn = 0x0;
273 sctp_conn->last_rcvd_tsn = sctp_conn->remote_initial_tsn;
Marco Varlese191a5942017-10-30 18:17:21 +0100274}
275
276always_inline sctp_connection_t *
277sctp_sub_connection_add (u8 thread_index)
278{
279 sctp_main_t *tm = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100280 sctp_connection_t *sctp_conn = tm->connections[thread_index];
Marco Varlese191a5942017-10-30 18:17:21 +0100281
Marco Varlese3c6a9762018-03-01 11:19:59 +0100282 u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100283
Marco Varlese3c6a9762018-03-01 11:19:59 +0100284 ASSERT (subconn_idx < MAX_SCTP_CONNECTIONS);
285
286 sctp_conn->sub_conn[subconn_idx].connection.c_index =
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100287 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.c_index;
Marco Varlese3c6a9762018-03-01 11:19:59 +0100288 sctp_conn->sub_conn[subconn_idx].connection.thread_index = thread_index;
289 sctp_conn->sub_conn[subconn_idx].subconn_idx = subconn_idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100290
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100291 return sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100292}
293
Marco Varlese3c6a9762018-03-01 11:19:59 +0100294u8
295sctp_sub_connection_add_ip4 (vlib_main_t * vm,
296 ip4_address_t * lcl_addr,
297 ip4_address_t * rmt_addr)
Marco Varlese191a5942017-10-30 18:17:21 +0100298{
Marco Varlese3c6a9762018-03-01 11:19:59 +0100299 sctp_connection_t *sctp_conn = sctp_sub_connection_add (vm->thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +0100300
Marco Varlese3c6a9762018-03-01 11:19:59 +0100301 u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
302
303 if (subconn_idx == MAX_SCTP_CONNECTIONS)
304 return SCTP_ERROR_MAX_CONNECTIONS;
305
306 clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.lcl_ip,
307 &lcl_addr, sizeof (lcl_addr));
308
309 clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.rmt_ip,
310 &rmt_addr, sizeof (rmt_addr));
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100311
312 sctp_conn->forming_association_changed = 1;
Marco Varlese3c6a9762018-03-01 11:19:59 +0100313
314 return SCTP_ERROR_NONE;
Marco Varlese191a5942017-10-30 18:17:21 +0100315}
316
Marco Varlese3c6a9762018-03-01 11:19:59 +0100317u8
Marco Varlese465c0872018-03-01 14:01:46 +0100318sctp_sub_connection_del_ip4 (ip4_address_t * lcl_addr,
319 ip4_address_t * rmt_addr)
320{
321 sctp_main_t *sctp_main = vnet_get_sctp_main ();
322
323 u32 thread_idx = vlib_get_thread_index ();
324 u8 i;
325
326 ASSERT (thread_idx == 0);
327
328 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
329 {
330 sctp_connection_t *sctp_conn = sctp_main->connections[thread_idx];
331 sctp_sub_connection_t *sub_conn =
332 &sctp_main->connections[thread_idx]->sub_conn[i];
333 ip46_address_t *lcl_ip =
334 &sctp_main->connections[thread_idx]->sub_conn[i].connection.lcl_ip;
335 ip46_address_t *rmt_ip =
336 &sctp_main->connections[thread_idx]->sub_conn[i].connection.rmt_ip;
337
338 if (!sub_conn->connection.is_ip4)
339 continue;
340 if (lcl_ip->ip4.as_u32 == lcl_addr->as_u32 &&
341 rmt_ip->ip4.as_u32 == rmt_addr->as_u32)
342 {
343 sub_conn->state = SCTP_SUBCONN_STATE_DOWN;
344 sctp_conn->forming_association_changed = 1;
345 break;
346 }
347 }
348 return SCTP_ERROR_NONE;
349}
350
351u8
Marco Varlese3c6a9762018-03-01 11:19:59 +0100352sctp_sub_connection_add_ip6 (vlib_main_t * vm,
353 ip6_address_t * lcl_addr,
354 ip6_address_t * rmt_addr)
Marco Varlese191a5942017-10-30 18:17:21 +0100355{
Marco Varlese3c6a9762018-03-01 11:19:59 +0100356 sctp_connection_t *sctp_conn = sctp_sub_connection_add (vm->thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +0100357
Marco Varlese3c6a9762018-03-01 11:19:59 +0100358 u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
359
360 if (subconn_idx == MAX_SCTP_CONNECTIONS)
361 return SCTP_ERROR_MAX_CONNECTIONS;
362
363 clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.lcl_ip,
364 &lcl_addr, sizeof (lcl_addr));
365
366 clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.rmt_ip,
367 &rmt_addr, sizeof (rmt_addr));
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100368
369 sctp_conn->forming_association_changed = 1;
Marco Varlese3c6a9762018-03-01 11:19:59 +0100370
371 return SCTP_ERROR_NONE;
Marco Varlese191a5942017-10-30 18:17:21 +0100372}
373
Marco Varlese465c0872018-03-01 14:01:46 +0100374u8
375sctp_sub_connection_del_ip6 (ip6_address_t * lcl_addr,
376 ip6_address_t * rmt_addr)
377{
378 sctp_main_t *sctp_main = vnet_get_sctp_main ();
379
380 u32 thread_idx = vlib_get_thread_index ();
381 u8 i;
382
383 ASSERT (thread_idx == 0);
384
385 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
386 {
387 sctp_connection_t *sctp_conn = sctp_main->connections[thread_idx];
388 sctp_sub_connection_t *sub_conn =
389 &sctp_main->connections[thread_idx]->sub_conn[i];
390 ip46_address_t *lcl_ip =
391 &sctp_main->connections[thread_idx]->sub_conn[i].connection.lcl_ip;
392 ip46_address_t *rmt_ip =
393 &sctp_main->connections[thread_idx]->sub_conn[i].connection.rmt_ip;
394
395 if (!sub_conn->connection.is_ip4)
396 continue;
397 if ((lcl_ip->ip6.as_u64[0] == lcl_addr->as_u64[0]
398 && lcl_ip->ip6.as_u64[1] == lcl_addr->as_u64[1])
399 && (rmt_ip->ip6.as_u64[0] == rmt_addr->as_u64[0]
400 && rmt_ip->ip6.as_u64[1] == rmt_addr->as_u64[1]))
401 {
402 sub_conn->state = SCTP_SUBCONN_STATE_DOWN;
403 sctp_conn->forming_association_changed = 1;
404 break;
405 }
406 }
407 return SCTP_ERROR_NONE;
408}
409
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100410u8
411sctp_configure (sctp_user_configuration_t config)
412{
413 sctp_main_t *sctp_main = vnet_get_sctp_main ();
414
415 u32 thread_idx = vlib_get_thread_index ();
416
417 sctp_main->connections[thread_idx]->conn_config.never_delay_sack =
418 config.never_delay_sack;
419 sctp_main->connections[thread_idx]->conn_config.never_bundle =
420 config.never_bundle;
421
422 return 0;
423}
424
Marco Varlese191a5942017-10-30 18:17:21 +0100425sctp_connection_t *
426sctp_connection_new (u8 thread_index)
427{
Marco Varlese15cc6a82018-02-21 12:39:52 +0100428 sctp_main_t *sctp_main = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100429 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100430
Marco Varlese15cc6a82018-02-21 12:39:52 +0100431 pool_get (sctp_main->connections[thread_index], sctp_conn);
Dave Barachb7b92992018-10-17 10:38:51 -0400432 clib_memset (sctp_conn, 0, sizeof (*sctp_conn));
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100433 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].subconn_idx =
434 SCTP_PRIMARY_PATH_IDX;
435 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].c_c_index =
Marco Varlese15cc6a82018-02-21 12:39:52 +0100436 sctp_conn - sctp_main->connections[thread_index];
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100437 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].c_thread_index = thread_index;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100438 sctp_conn->local_tag = 0;
Marco Varlese191a5942017-10-30 18:17:21 +0100439
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100440 return sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100441}
442
443sctp_connection_t *
444sctp_half_open_connection_new (u8 thread_index)
445{
446 sctp_main_t *tm = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100447 sctp_connection_t *sctp_conn = 0;
Marco Varlese191a5942017-10-30 18:17:21 +0100448 ASSERT (vlib_get_thread_index () == 0);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100449 pool_get (tm->half_open_connections, sctp_conn);
Dave Barachb7b92992018-10-17 10:38:51 -0400450 clib_memset (sctp_conn, 0, sizeof (*sctp_conn));
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100451 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].c_c_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100452 sctp_conn - tm->half_open_connections;
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100453 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].subconn_idx =
454 SCTP_PRIMARY_PATH_IDX;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100455 return sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100456}
457
458static inline int
Florin Coras5665ced2018-10-25 18:03:45 -0700459sctp_connection_open (transport_endpoint_cfg_t * rmt)
Marco Varlese191a5942017-10-30 18:17:21 +0100460{
461 sctp_main_t *tm = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100462 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100463 ip46_address_t lcl_addr;
464 u16 lcl_port;
465 uword thread_id;
Florin Corasb0f662f2018-12-27 14:51:46 -0800466 u32 mtu = 1460;
Marco Varlese191a5942017-10-30 18:17:21 +0100467 int rv;
468
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100469 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese191a5942017-10-30 18:17:21 +0100470
471 /*
472 * Allocate local endpoint
473 */
474 if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
475 || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
476 rv = sctp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
477 rmt->is_ip4);
478 else
479 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_SCTP,
480 rmt, &lcl_addr, &lcl_port);
481
482 if (rv)
483 return -1;
484
485 /*
486 * Create connection and send INIT CHUNK
487 */
488 thread_id = vlib_get_thread_index ();
489 ASSERT (thread_id == 0);
490
491 clib_spinlock_lock_if_init (&tm->half_open_lock);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100492 sctp_conn = sctp_half_open_connection_new (thread_id);
Florin Corasb0f662f2018-12-27 14:51:46 -0800493 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
494 mtu = rmt->is_ip4 ? vnet_sw_interface_get_mtu (vnet_get_main (),
495 rmt->peer.sw_if_index,
496 VNET_MTU_IP4) :
497 vnet_sw_interface_get_mtu (vnet_get_main (), rmt->peer.sw_if_index,
498 VNET_MTU_IP6);
Ole Troand7231612018-06-07 10:17:57 +0200499 sctp_conn->sub_conn[idx].PMTU = mtu;
Marco Varlese191a5942017-10-30 18:17:21 +0100500
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100501 transport_connection_t *trans_conn = &sctp_conn->sub_conn[idx].connection;
502 ip_copy (&trans_conn->rmt_ip, &rmt->ip, rmt->is_ip4);
503 ip_copy (&trans_conn->lcl_ip, &lcl_addr, rmt->is_ip4);
Marco Varlese04e5d642018-02-23 17:43:06 +0100504 sctp_conn->sub_conn[idx].subconn_idx = idx;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100505 trans_conn->rmt_port = rmt->port;
506 trans_conn->lcl_port = clib_host_to_net_u16 (lcl_port);
507 trans_conn->is_ip4 = rmt->is_ip4;
508 trans_conn->proto = TRANSPORT_PROTO_SCTP;
509 trans_conn->fib_index = rmt->fib_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100510
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100511 sctp_connection_timers_init (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100512 /* The other connection vars will be initialized after INIT_ACK chunk received */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100513 sctp_init_snd_vars (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100514
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100515 sctp_send_init (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100516
517 clib_spinlock_unlock_if_init (&tm->half_open_lock);
518
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100519 return sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100520}
521
522/**
523 * Cleans up connection state.
524 *
525 * No notifications.
526 */
527void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100528sctp_connection_cleanup (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100529{
530 sctp_main_t *tm = &sctp_main;
531 u8 i;
532
533 /* Cleanup local endpoint if this was an active connect */
534 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
535 transport_endpoint_cleanup (TRANSPORT_PROTO_SCTP,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100536 &sctp_conn->sub_conn[i].connection.lcl_ip,
537 sctp_conn->sub_conn[i].connection.lcl_port);
Marco Varlese191a5942017-10-30 18:17:21 +0100538
Marco Varlese200fa322018-02-26 16:33:54 +0100539 int thread_index =
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100540 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.thread_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100541
Marco Varlese200fa322018-02-26 16:33:54 +0100542 /* Make sure all timers are cleared */
543 sctp_connection_timers_reset (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100544
Marco Varlese200fa322018-02-26 16:33:54 +0100545 /* Poison the entry */
546 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400547 clib_memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
Marco Varlese200fa322018-02-26 16:33:54 +0100548 pool_put (tm->connections[thread_index], sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100549}
550
551int
Florin Coras5665ced2018-10-25 18:03:45 -0700552sctp_session_open (transport_endpoint_cfg_t * tep)
Marco Varlese191a5942017-10-30 18:17:21 +0100553{
554 return sctp_connection_open (tep);
555}
556
557u16
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100558sctp_check_outstanding_data_chunks (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100559{
Marco Varlese54432f82018-02-15 17:01:56 +0100560 u8 i;
561 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
562 {
563 if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
564 continue;
565
566 if (sctp_conn->sub_conn[i].is_retransmitting == 1 ||
567 sctp_conn->sub_conn[i].enqueue_state != SCTP_ERROR_ENQUEUED)
568 {
569 SCTP_DBG_OUTPUT
Marco Varlesef3ab4892018-02-19 15:23:13 +0100570 ("Connection %u has still DATA to be enqueued inboud / outboud",
571 sctp_conn->sub_conn[i].connection.c_index);
Marco Varlese54432f82018-02-15 17:01:56 +0100572 return 1;
573 }
574
575 }
Marco Varlese191a5942017-10-30 18:17:21 +0100576 return 0; /* Indicates no more data to be read/sent */
577}
578
579void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100580sctp_connection_close (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100581{
582 SCTP_DBG ("Closing connection %u...",
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100583 sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection.c_index);
Marco Varlese191a5942017-10-30 18:17:21 +0100584
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100585 sctp_conn->state = SCTP_STATE_SHUTDOWN_PENDING;
Marco Varlese191a5942017-10-30 18:17:21 +0100586
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100587 sctp_send_shutdown (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100588}
589
590void
591sctp_session_close (u32 conn_index, u32 thread_index)
592{
593 ASSERT (thread_index == 0);
Marco Varlesef3ab4892018-02-19 15:23:13 +0100594
Marco Varlese15cc6a82018-02-21 12:39:52 +0100595 sctp_connection_t *sctp_conn =
596 sctp_connection_get (conn_index, thread_index);
Marco Varlesea38783e2018-02-13 12:38:52 +0100597 if (sctp_conn != NULL)
598 sctp_connection_close (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100599}
600
601void
602sctp_session_cleanup (u32 conn_index, u32 thread_index)
603{
Marco Varlese15cc6a82018-02-21 12:39:52 +0100604 sctp_connection_t *sctp_conn =
605 sctp_connection_get (conn_index, thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +0100606
Marco Varlesea38783e2018-02-13 12:38:52 +0100607 if (sctp_conn != NULL)
608 {
609 sctp_connection_timers_reset (sctp_conn);
610 /* Wait for the session tx events to clear */
611 sctp_conn->state = SCTP_STATE_CLOSED;
612 }
Marco Varlese191a5942017-10-30 18:17:21 +0100613}
614
615/**
Marco Varlesef3ab4892018-02-19 15:23:13 +0100616 * Compute maximum segment size for session layer.
Marco Varlese191a5942017-10-30 18:17:21 +0100617 */
Marco Varlese191a5942017-10-30 18:17:21 +0100618u16
619sctp_session_send_mss (transport_connection_t * trans_conn)
620{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100621 sctp_connection_t *sctp_conn =
622 sctp_get_connection_from_transport (trans_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100623
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100624 if (sctp_conn == NULL)
Marco Varlese191a5942017-10-30 18:17:21 +0100625 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100626 SCTP_DBG ("sctp_conn == NULL");
Marco Varlese191a5942017-10-30 18:17:21 +0100627 return 0;
628 }
Marco Varlese191a5942017-10-30 18:17:21 +0100629
Marco Varlesef3ab4892018-02-19 15:23:13 +0100630 update_cwnd (sctp_conn);
631 update_smallest_pmtu_idx (sctp_conn);
632
Marco Varlese6e4d4a32018-03-12 12:36:59 +0100633 u8 idx = sctp_data_subconn_select (sctp_conn);
Marco Varlese6e4d4a32018-03-12 12:36:59 +0100634 return sctp_conn->sub_conn[idx].cwnd;
Marco Varlese191a5942017-10-30 18:17:21 +0100635}
636
637u16
638sctp_snd_space (sctp_connection_t * sctp_conn)
639{
Marco Varlese6e4d4a32018-03-12 12:36:59 +0100640 /* RFC 4096 Section 6.1; point (A) */
641 if (sctp_conn->peer_rwnd == 0)
642 return 0;
643
644 u8 idx = sctp_data_subconn_select (sctp_conn);
Marco Varlese3e9b4652018-03-13 15:44:56 +0100645
646 u32 available_wnd =
647 clib_min (sctp_conn->peer_rwnd, sctp_conn->sub_conn[idx].cwnd);
648 int flight_size = (int) (sctp_conn->next_tsn - sctp_conn->last_unacked_tsn);
649
650 if (available_wnd <= flight_size)
651 return 0;
652
Marco Varlesef3ab4892018-02-19 15:23:13 +0100653 /* Finally, let's subtract the DATA chunk headers overhead */
Marco Varlese3e9b4652018-03-13 15:44:56 +0100654 return available_wnd -
655 flight_size -
Marco Varlesef3ab4892018-02-19 15:23:13 +0100656 sizeof (sctp_payload_data_chunk_t) - sizeof (sctp_full_hdr_t);
Marco Varlese191a5942017-10-30 18:17:21 +0100657}
658
Marco Varlesef3ab4892018-02-19 15:23:13 +0100659/**
660 * Compute TX window session is allowed to fill.
661 */
Marco Varlese191a5942017-10-30 18:17:21 +0100662u32
663sctp_session_send_space (transport_connection_t * trans_conn)
664{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100665 sctp_connection_t *sctp_conn =
666 sctp_get_connection_from_transport (trans_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100667
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100668 return sctp_snd_space (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100669}
670
671transport_connection_t *
672sctp_session_get_transport (u32 conn_index, u32 thread_index)
673{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100674 sctp_connection_t *sctp_conn =
675 sctp_connection_get (conn_index, thread_index);
Marco Varlese2802bd72018-02-15 13:45:39 +0100676
677 if (PREDICT_TRUE (sctp_conn != NULL))
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100678 return &sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection;
Marco Varlese2802bd72018-02-15 13:45:39 +0100679
680 return NULL;
Marco Varlese191a5942017-10-30 18:17:21 +0100681}
682
683transport_connection_t *
684sctp_session_get_listener (u32 listener_index)
685{
686 sctp_main_t *tm = vnet_get_sctp_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100687 sctp_connection_t *sctp_conn;
688 sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100689 return &sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection;
Marco Varlese191a5942017-10-30 18:17:21 +0100690}
691
692u8 *
693format_sctp_session (u8 * s, va_list * args)
694{
Marco Varlesef3ab4892018-02-19 15:23:13 +0100695 u32 tci = va_arg (*args, u32);
696 u32 thread_index = va_arg (*args, u32);
697 u32 verbose = va_arg (*args, u32);
698 sctp_connection_t *tc;
699
700 tc = sctp_connection_get (tci, thread_index);
701 if (tc)
702 s = format (s, "%U", format_sctp_connection, tc, verbose);
703 else
704 s = format (s, "empty\n");
705 return s;
Marco Varlese191a5942017-10-30 18:17:21 +0100706}
707
708u8 *
709format_sctp_listener_session (u8 * s, va_list * args)
710{
Marco Varlesef3ab4892018-02-19 15:23:13 +0100711 u32 tci = va_arg (*args, u32);
712 sctp_connection_t *tc = sctp_listener_get (tci);
713 return format (s, "%U", format_sctp_connection_id, tc);
Marco Varlese191a5942017-10-30 18:17:21 +0100714}
715
716void
Marco Varlesef47276f2018-02-06 09:01:39 +0100717sctp_expired_timers_cb (u32 conn_index, u32 timer_id)
Marco Varlese191a5942017-10-30 18:17:21 +0100718{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100719 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +0100720
Marco Varlese93826732018-09-27 16:43:57 +0200721 SCTP_DBG ("%s expired", sctp_timer_to_string (timer_id));
722
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100723 sctp_conn = sctp_connection_get (conn_index, vlib_get_thread_index ());
Marco Varlese191a5942017-10-30 18:17:21 +0100724 /* note: the connection may have already disappeared */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100725 if (PREDICT_FALSE (sctp_conn == 0))
Marco Varlese191a5942017-10-30 18:17:21 +0100726 return;
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100727
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100728 if (sctp_conn->sub_conn[conn_index].unacknowledged_hb >
Marco Varlese54432f82018-02-15 17:01:56 +0100729 SCTP_PATH_MAX_RETRANS)
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100730 {
731 // The remote-peer is considered to be unreachable hence shutting down
Marco Varlese54432f82018-02-15 17:01:56 +0100732 u8 i, total_subs_down = 1;
733 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
734 {
735 if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
736 continue;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100737
Marco Varlese54432f82018-02-15 17:01:56 +0100738 u32 now = sctp_time_now ();
739 if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
740 {
741 total_subs_down += 1;
742 sctp_conn->sub_conn[i].state = SCTP_SUBCONN_STATE_DOWN;
743 }
744 }
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100745
Marco Varlese54432f82018-02-15 17:01:56 +0100746 if (total_subs_down == MAX_SCTP_CONNECTIONS)
747 {
748 /* Start cleanup. App wasn't notified yet so use delete notify as
749 * opposed to delete to cleanup session layer state. */
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800750 session_transport_delete_notify (&sctp_conn->sub_conn
751 [SCTP_PRIMARY_PATH_IDX].connection);
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100752
Marco Varlese54432f82018-02-15 17:01:56 +0100753 sctp_connection_timers_reset (sctp_conn);
754
755 sctp_connection_cleanup (sctp_conn);
756 }
Marco Varlese9e09ff32018-03-05 12:31:45 +0100757 return;
758 }
759
760 switch (timer_id)
761 {
762 case SCTP_TIMER_T1_INIT:
763 sctp_send_init (sctp_conn);
764 break;
765 case SCTP_TIMER_T1_COOKIE:
766 sctp_send_cookie_echo (sctp_conn);
767 break;
768 case SCTP_TIMER_T2_SHUTDOWN:
769 sctp_send_shutdown (sctp_conn);
770 break;
771 case SCTP_TIMER_T3_RXTX:
772 sctp_timer_reset (sctp_conn, conn_index, timer_id);
Marco Varlese3e9b4652018-03-13 15:44:56 +0100773 sctp_conn->flags |= SCTP_CONN_RECOVERY;
Marco Varlese9e09ff32018-03-05 12:31:45 +0100774 sctp_data_retransmit (sctp_conn);
775 break;
776 case SCTP_TIMER_T4_HEARTBEAT:
777 sctp_timer_reset (sctp_conn, conn_index, timer_id);
778 goto heartbeat;
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100779 }
780 return;
781
782heartbeat:
783 sctp_send_heartbeat (sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100784}
785
Marco Varlese191a5942017-10-30 18:17:21 +0100786static void
787sctp_expired_timers_dispatch (u32 * expired_timers)
788{
789 int i;
790 u32 connection_index, timer_id;
791
792 for (i = 0; i < vec_len (expired_timers); i++)
793 {
794 /* Get session index and timer id */
795 connection_index = expired_timers[i] & 0x0FFFFFFF;
796 timer_id = expired_timers[i] >> 28;
797
Marco Varlesef47276f2018-02-06 09:01:39 +0100798 SCTP_DBG ("Expired timer ID: %u", timer_id);
799
Marco Varlese191a5942017-10-30 18:17:21 +0100800 /* Handle expiration */
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100801 sctp_expired_timers_cb (connection_index, timer_id);
Marco Varlese191a5942017-10-30 18:17:21 +0100802 }
803}
804
805void
806sctp_initialize_timer_wheels (sctp_main_t * tm)
807{
808 tw_timer_wheel_16t_2w_512sl_t *tw;
809 /* *INDENT-OFF* */
810 foreach_vlib_main (({
811 tw = &tm->timer_wheels[ii];
812 tw_timer_wheel_init_16t_2w_512sl (tw, sctp_expired_timers_dispatch,
813 100e-3 /* timer period 100ms */ , ~0);
814 tw->last_run_time = vlib_time_now (this_vlib_main);
815 }));
816 /* *INDENT-ON* */
817}
818
819clib_error_t *
820sctp_main_enable (vlib_main_t * vm)
821{
822 sctp_main_t *tm = vnet_get_sctp_main ();
823 vlib_thread_main_t *vtm = vlib_get_thread_main ();
824 clib_error_t *error = 0;
825 u32 num_threads;
826 int thread;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100827 sctp_connection_t *sctp_conn __attribute__ ((unused));
Marco Varlese191a5942017-10-30 18:17:21 +0100828 u32 preallocated_connections_per_thread;
829
830 if ((error = vlib_call_init_function (vm, ip_main_init)))
831 return error;
832 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
833 return error;
834 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
835 return error;
836
837 /*
838 * Registrations
839 */
840
841 ip4_register_protocol (IP_PROTOCOL_SCTP, sctp4_input_node.index);
842 ip6_register_protocol (IP_PROTOCOL_SCTP, sctp6_input_node.index);
843
844 /*
845 * Initialize data structures
846 */
847
848 num_threads = 1 /* main thread */ + vtm->n_threads;
849 vec_validate (tm->connections, num_threads - 1);
850
851 /*
852 * Preallocate connections. Assume that thread 0 won't
853 * use preallocated threads when running multi-core
854 */
855 if (num_threads == 1)
856 {
857 thread = 0;
858 preallocated_connections_per_thread = tm->preallocated_connections;
859 }
860 else
861 {
862 thread = 1;
863 preallocated_connections_per_thread =
864 tm->preallocated_connections / (num_threads - 1);
865 }
866 for (; thread < num_threads; thread++)
867 {
868 if (preallocated_connections_per_thread)
869 pool_init_fixed (tm->connections[thread],
870 preallocated_connections_per_thread);
871 }
872
873 /* Initialize per worker thread tx buffers (used for control messages) */
874 vec_validate (tm->tx_buffers, num_threads - 1);
875
876 /* Initialize timer wheels */
877 vec_validate (tm->timer_wheels, num_threads - 1);
878 sctp_initialize_timer_wheels (tm);
879
880 /* Initialize clocks per tick for SCTP timestamp. Used to compute
881 * monotonically increasing timestamps. */
882 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
883 / SCTP_TSTAMP_RESOLUTION;
884
885 if (num_threads > 1)
886 {
Marco Varlesef47276f2018-02-06 09:01:39 +0100887 clib_spinlock_init (&tm->half_open_lock);
Marco Varlese191a5942017-10-30 18:17:21 +0100888 }
889
890 vec_validate (tm->tx_frames[0], num_threads - 1);
891 vec_validate (tm->tx_frames[1], num_threads - 1);
892 vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
893 vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
894
Damjan Marion671e60e2018-12-30 18:09:59 +0100895 tm->bytes_per_buffer = VLIB_BUFFER_DATA_SIZE;
Marco Varlese191a5942017-10-30 18:17:21 +0100896
897 vec_validate (tm->time_now, num_threads - 1);
898 return error;
899}
900
901clib_error_t *
902sctp_enable_disable (vlib_main_t * vm, u8 is_en)
903{
904 if (is_en)
905 {
906 if (sctp_main.is_enabled)
907 return 0;
908
909 return sctp_main_enable (vm);
910 }
911 else
912 {
913 sctp_main.is_enabled = 0;
914 }
915
916 return 0;
917}
918
919transport_connection_t *
920sctp_half_open_session_get_transport (u32 conn_index)
921{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100922 sctp_connection_t *sctp_conn = sctp_half_open_connection_get (conn_index);
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100923 return &sctp_conn->sub_conn[SCTP_PRIMARY_PATH_IDX].connection;
Marco Varlese191a5942017-10-30 18:17:21 +0100924}
925
926u8 *
927format_sctp_half_open (u8 * s, va_list * args)
928{
929 u32 tci = va_arg (*args, u32);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100930 sctp_connection_t *sctp_conn = sctp_half_open_connection_get (tci);
931 return format (s, "%U", format_sctp_connection_id, sctp_conn);
Marco Varlese191a5942017-10-30 18:17:21 +0100932}
933
Marco Varlese21c8baf2018-02-02 17:17:51 +0100934void
935sctp_update_time (f64 now, u8 thread_index)
936{
937 sctp_set_time_now (thread_index);
938 tw_timer_expire_timers_16t_2w_512sl (&sctp_main.timer_wheels[thread_index],
939 now);
940 sctp_flush_frames_to_output (thread_index);
941}
942
Marco Varlese191a5942017-10-30 18:17:21 +0100943/* *INDENT OFF* */
944const static transport_proto_vft_t sctp_proto = {
945 .enable = sctp_enable_disable,
Florin Coras1ee78302019-02-05 15:51:15 -0800946 .start_listen = sctp_session_bind,
947 .stop_listen = sctp_session_unbind,
948 .connect = sctp_session_open,
Marco Varlese191a5942017-10-30 18:17:21 +0100949 .close = sctp_session_close,
950 .cleanup = sctp_session_cleanup,
951 .push_header = sctp_push_header,
952 .send_mss = sctp_session_send_mss,
953 .send_space = sctp_session_send_space,
Marco Varlese21c8baf2018-02-02 17:17:51 +0100954 .update_time = sctp_update_time,
Marco Varlese191a5942017-10-30 18:17:21 +0100955 .get_connection = sctp_session_get_transport,
956 .get_listener = sctp_session_get_listener,
957 .get_half_open = sctp_half_open_session_get_transport,
958 .format_connection = format_sctp_session,
959 .format_listener = format_sctp_listener_session,
960 .format_half_open = format_sctp_half_open,
Florin Coras371ca502018-02-21 12:07:41 -0800961 .tx_type = TRANSPORT_TX_DEQUEUE,
962 .service_type = TRANSPORT_SERVICE_VC,
Marco Varlese191a5942017-10-30 18:17:21 +0100963};
964
965/* *INDENT ON* */
966
967clib_error_t *
968sctp_init (vlib_main_t * vm)
969{
970 sctp_main_t *tm = vnet_get_sctp_main ();
971 ip_main_t *im = &ip_main;
972 ip_protocol_info_t *pi;
973 /* Session layer, and by implication SCTP, are disabled by default */
974 tm->is_enabled = 0;
975
976 /* Register with IP for header parsing */
977 pi = ip_get_protocol_info (im, IP_PROTOCOL_SCTP);
978 if (pi == 0)
979 return clib_error_return (0, "SCTP protocol info AWOL");
980 pi->format_header = format_sctp_header;
981 pi->unformat_pg_edit = unformat_pg_sctp_header;
982
983 /* Register as transport with session layer */
984 transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
985 FIB_PROTOCOL_IP4, sctp4_output_node.index);
986 transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
987 FIB_PROTOCOL_IP6, sctp6_output_node.index);
988
Marco Varlese3c6a9762018-03-01 11:19:59 +0100989 sctp_api_reference ();
990
Marco Varlese191a5942017-10-30 18:17:21 +0100991 return 0;
992}
993
994VLIB_INIT_FUNCTION (sctp_init);
995
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100996static clib_error_t *
997show_sctp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
998 vlib_cli_command_t * cmd_arg)
999{
1000 sctp_main_t *tm = &sctp_main;
1001 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1002 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
1003 input);
1004 vlib_cli_output (vm, "IPv4 UDP punt: %s",
1005 tm->punt_unknown4 ? "enabled" : "disabled");
1006 vlib_cli_output (vm, "IPv6 UDP punt: %s",
1007 tm->punt_unknown6 ? "enabled" : "disabled");
1008 return 0;
1009}
1010/* *INDENT-OFF* */
1011VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
1012{
1013 .path = "show sctp punt",
1014 .short_help = "show sctp punt",
1015 .function = show_sctp_punt_fn,
1016};
1017/* *INDENT-ON* */
1018
Marco Varlese191a5942017-10-30 18:17:21 +01001019/*
1020 * fd.io coding-style-patch-verification: ON
1021 *
1022 * Local Variables:
1023 * eval: (c-set-style "gnu")
1024 * End:
1025 */