SCTP: API to delete a sub-connection
[vpp.git] / src / vnet / sctp / sctp.c
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].subconn_idx =
31     MAIN_SCTP_SUB_CONN_IDX;
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
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);
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 ();
71   sctp_connection_t *sctp_conn;
72
73   sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
74
75   /* Poison the entry */
76   if (CLIB_DEBUG > 0)
77     memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
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
133 sctp_connection_timers_init (sctp_connection_t * sctp_conn)
134 {
135   int i, j;
136
137   /* Set all to invalid */
138   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
139     {
140       sctp_conn->sub_conn[i].RTO = SCTP_RTO_INIT;
141
142       for (j = 0; j < SCTP_N_TIMERS; j++)
143         {
144           sctp_conn->sub_conn[i].timers[j] = SCTP_TIMER_HANDLE_INVALID;
145         }
146     }
147 }
148
149 /**
150  * Stop all connection timers
151  */
152 void
153 sctp_connection_timers_reset (sctp_connection_t * sctp_conn)
154 {
155   int i, j;
156   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
157     {
158       for (j = 0; j < SCTP_N_TIMERS; j++)
159         sctp_timer_reset (sctp_conn, i, j);
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 {
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     }
222   return s;
223 }
224
225 u8 *
226 format_sctp_connection (u8 * s, va_list * args)
227 {
228   sctp_connection_t *sctp_conn = va_arg (*args, sctp_connection_t *);
229   u32 verbose = va_arg (*args, u32);
230
231   if (!sctp_conn)
232     return s;
233   s = format (s, "%-50U", format_sctp_connection_id, sctp_conn);
234   if (verbose)
235     {
236       s = format (s, "%-15U", format_sctp_state, sctp_conn->state);
237     }
238
239   return s;
240 }
241
242 /**
243  * Initialize connection send variables.
244  */
245 void
246 sctp_init_snd_vars (sctp_connection_t * sctp_conn)
247 {
248   u32 time_now;
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    */
255
256   sctp_set_time_now (vlib_get_thread_index ());
257   time_now = sctp_time_now ();
258
259   sctp_conn->local_initial_tsn = random_u32 (&time_now);
260   sctp_conn->last_unacked_tsn = sctp_conn->local_initial_tsn;
261   sctp_conn->next_tsn = sctp_conn->local_initial_tsn + 1;
262
263   sctp_conn->remote_initial_tsn = 0x0;
264   sctp_conn->last_rcvd_tsn = sctp_conn->remote_initial_tsn;
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 ();
271   sctp_connection_t *sctp_conn = tm->connections[thread_index];
272
273   u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
274
275   ASSERT (subconn_idx < MAX_SCTP_CONNECTIONS);
276
277   sctp_conn->sub_conn[subconn_idx].connection.c_index =
278     sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index;
279   sctp_conn->sub_conn[subconn_idx].connection.thread_index = thread_index;
280   sctp_conn->sub_conn[subconn_idx].subconn_idx = subconn_idx;
281
282   return sctp_conn;
283 }
284
285 u8
286 sctp_sub_connection_add_ip4 (vlib_main_t * vm,
287                              ip4_address_t * lcl_addr,
288                              ip4_address_t * rmt_addr)
289 {
290   sctp_connection_t *sctp_conn = sctp_sub_connection_add (vm->thread_index);
291
292   u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
293
294   if (subconn_idx == MAX_SCTP_CONNECTIONS)
295     return SCTP_ERROR_MAX_CONNECTIONS;
296
297   clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.lcl_ip,
298                &lcl_addr, sizeof (lcl_addr));
299
300   clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.rmt_ip,
301                &rmt_addr, sizeof (rmt_addr));
302
303   sctp_conn->forming_association_changed = 1;
304
305   return SCTP_ERROR_NONE;
306 }
307
308 u8
309 sctp_sub_connection_del_ip4 (ip4_address_t * lcl_addr,
310                              ip4_address_t * rmt_addr)
311 {
312   sctp_main_t *sctp_main = vnet_get_sctp_main ();
313
314   u32 thread_idx = vlib_get_thread_index ();
315   u8 i;
316
317   ASSERT (thread_idx == 0);
318
319   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
320     {
321       sctp_connection_t *sctp_conn = sctp_main->connections[thread_idx];
322       sctp_sub_connection_t *sub_conn =
323         &sctp_main->connections[thread_idx]->sub_conn[i];
324       ip46_address_t *lcl_ip =
325         &sctp_main->connections[thread_idx]->sub_conn[i].connection.lcl_ip;
326       ip46_address_t *rmt_ip =
327         &sctp_main->connections[thread_idx]->sub_conn[i].connection.rmt_ip;
328
329       if (!sub_conn->connection.is_ip4)
330         continue;
331       if (lcl_ip->ip4.as_u32 == lcl_addr->as_u32 &&
332           rmt_ip->ip4.as_u32 == rmt_addr->as_u32)
333         {
334           sub_conn->state = SCTP_SUBCONN_STATE_DOWN;
335           sctp_conn->forming_association_changed = 1;
336           break;
337         }
338     }
339   return SCTP_ERROR_NONE;
340 }
341
342 u8
343 sctp_sub_connection_add_ip6 (vlib_main_t * vm,
344                              ip6_address_t * lcl_addr,
345                              ip6_address_t * rmt_addr)
346 {
347   sctp_connection_t *sctp_conn = sctp_sub_connection_add (vm->thread_index);
348
349   u8 subconn_idx = sctp_next_avail_subconn (sctp_conn);
350
351   if (subconn_idx == MAX_SCTP_CONNECTIONS)
352     return SCTP_ERROR_MAX_CONNECTIONS;
353
354   clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.lcl_ip,
355                &lcl_addr, sizeof (lcl_addr));
356
357   clib_memcpy (&sctp_conn->sub_conn[subconn_idx].connection.rmt_ip,
358                &rmt_addr, sizeof (rmt_addr));
359
360   sctp_conn->forming_association_changed = 1;
361
362   return SCTP_ERROR_NONE;
363 }
364
365 u8
366 sctp_sub_connection_del_ip6 (ip6_address_t * lcl_addr,
367                              ip6_address_t * rmt_addr)
368 {
369   sctp_main_t *sctp_main = vnet_get_sctp_main ();
370
371   u32 thread_idx = vlib_get_thread_index ();
372   u8 i;
373
374   ASSERT (thread_idx == 0);
375
376   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
377     {
378       sctp_connection_t *sctp_conn = sctp_main->connections[thread_idx];
379       sctp_sub_connection_t *sub_conn =
380         &sctp_main->connections[thread_idx]->sub_conn[i];
381       ip46_address_t *lcl_ip =
382         &sctp_main->connections[thread_idx]->sub_conn[i].connection.lcl_ip;
383       ip46_address_t *rmt_ip =
384         &sctp_main->connections[thread_idx]->sub_conn[i].connection.rmt_ip;
385
386       if (!sub_conn->connection.is_ip4)
387         continue;
388       if ((lcl_ip->ip6.as_u64[0] == lcl_addr->as_u64[0]
389            && lcl_ip->ip6.as_u64[1] == lcl_addr->as_u64[1])
390           && (rmt_ip->ip6.as_u64[0] == rmt_addr->as_u64[0]
391               && rmt_ip->ip6.as_u64[1] == rmt_addr->as_u64[1]))
392         {
393           sub_conn->state = SCTP_SUBCONN_STATE_DOWN;
394           sctp_conn->forming_association_changed = 1;
395           break;
396         }
397     }
398   return SCTP_ERROR_NONE;
399 }
400
401 sctp_connection_t *
402 sctp_connection_new (u8 thread_index)
403 {
404   sctp_main_t *sctp_main = vnet_get_sctp_main ();
405   sctp_connection_t *sctp_conn;
406
407   pool_get (sctp_main->connections[thread_index], sctp_conn);
408   memset (sctp_conn, 0, sizeof (*sctp_conn));
409   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
410     MAIN_SCTP_SUB_CONN_IDX;
411   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
412     sctp_conn - sctp_main->connections[thread_index];
413   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_thread_index = thread_index;
414   sctp_conn->local_tag = 0;
415
416   return sctp_conn;
417 }
418
419 sctp_connection_t *
420 sctp_half_open_connection_new (u8 thread_index)
421 {
422   sctp_main_t *tm = vnet_get_sctp_main ();
423   sctp_connection_t *sctp_conn = 0;
424   ASSERT (vlib_get_thread_index () == 0);
425   pool_get (tm->half_open_connections, sctp_conn);
426   memset (sctp_conn, 0, sizeof (*sctp_conn));
427   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
428     sctp_conn - tm->half_open_connections;
429   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
430     MAIN_SCTP_SUB_CONN_IDX;
431   return sctp_conn;
432 }
433
434 static inline int
435 sctp_connection_open (transport_endpoint_t * rmt)
436 {
437   sctp_main_t *tm = vnet_get_sctp_main ();
438   sctp_connection_t *sctp_conn;
439   ip46_address_t lcl_addr;
440   u16 lcl_port;
441   uword thread_id;
442   int rv;
443
444   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
445
446   /*
447    * Allocate local endpoint
448    */
449   if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
450       || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
451     rv = sctp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
452                                            rmt->is_ip4);
453   else
454     rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_SCTP,
455                                          rmt, &lcl_addr, &lcl_port);
456
457   if (rv)
458     return -1;
459
460   /*
461    * Create connection and send INIT CHUNK
462    */
463   thread_id = vlib_get_thread_index ();
464   ASSERT (thread_id == 0);
465
466   clib_spinlock_lock_if_init (&tm->half_open_lock);
467   sctp_conn = sctp_half_open_connection_new (thread_id);
468   sctp_conn->sub_conn[idx].PMTU =
469     vnet_sw_interface_get_mtu (vnet_get_main (), rmt->sw_if_index, VLIB_TX);
470
471   transport_connection_t *trans_conn = &sctp_conn->sub_conn[idx].connection;
472   ip_copy (&trans_conn->rmt_ip, &rmt->ip, rmt->is_ip4);
473   ip_copy (&trans_conn->lcl_ip, &lcl_addr, rmt->is_ip4);
474   sctp_conn->sub_conn[idx].subconn_idx = idx;
475   trans_conn->rmt_port = rmt->port;
476   trans_conn->lcl_port = clib_host_to_net_u16 (lcl_port);
477   trans_conn->is_ip4 = rmt->is_ip4;
478   trans_conn->proto = TRANSPORT_PROTO_SCTP;
479   trans_conn->fib_index = rmt->fib_index;
480
481   sctp_connection_timers_init (sctp_conn);
482   /* The other connection vars will be initialized after INIT_ACK chunk received */
483   sctp_init_snd_vars (sctp_conn);
484
485   sctp_send_init (sctp_conn);
486
487   clib_spinlock_unlock_if_init (&tm->half_open_lock);
488
489   return sctp_conn->sub_conn[idx].connection.c_index;
490 }
491
492 /**
493  * Cleans up connection state.
494  *
495  * No notifications.
496  */
497 void
498 sctp_connection_cleanup (sctp_connection_t * sctp_conn)
499 {
500   sctp_main_t *tm = &sctp_main;
501   u8 i;
502
503   /* Cleanup local endpoint if this was an active connect */
504   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
505     transport_endpoint_cleanup (TRANSPORT_PROTO_SCTP,
506                                 &sctp_conn->sub_conn[i].connection.lcl_ip,
507                                 sctp_conn->sub_conn[i].connection.lcl_port);
508
509   int thread_index =
510     sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.thread_index;
511
512   /* Make sure all timers are cleared */
513   sctp_connection_timers_reset (sctp_conn);
514
515   /* Poison the entry */
516   if (CLIB_DEBUG > 0)
517     memset (sctp_conn, 0xFA, sizeof (*sctp_conn));
518   pool_put (tm->connections[thread_index], sctp_conn);
519 }
520
521 int
522 sctp_session_open (transport_endpoint_t * tep)
523 {
524   return sctp_connection_open (tep);
525 }
526
527 u16
528 sctp_check_outstanding_data_chunks (sctp_connection_t * sctp_conn)
529 {
530   u8 i;
531   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
532     {
533       if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
534         continue;
535
536       if (sctp_conn->sub_conn[i].is_retransmitting == 1 ||
537           sctp_conn->sub_conn[i].enqueue_state != SCTP_ERROR_ENQUEUED)
538         {
539           SCTP_DBG_OUTPUT
540             ("Connection %u has still DATA to be enqueued inboud / outboud",
541              sctp_conn->sub_conn[i].connection.c_index);
542           return 1;
543         }
544
545     }
546   return 0;                     /* Indicates no more data to be read/sent */
547 }
548
549 void
550 sctp_connection_close (sctp_connection_t * sctp_conn)
551 {
552   SCTP_DBG ("Closing connection %u...",
553             sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index);
554
555   sctp_conn->state = SCTP_STATE_SHUTDOWN_PENDING;
556
557   sctp_send_shutdown (sctp_conn);
558 }
559
560 void
561 sctp_session_close (u32 conn_index, u32 thread_index)
562 {
563   ASSERT (thread_index == 0);
564
565   sctp_connection_t *sctp_conn =
566     sctp_connection_get (conn_index, thread_index);
567   if (sctp_conn != NULL)
568     sctp_connection_close (sctp_conn);
569 }
570
571 void
572 sctp_session_cleanup (u32 conn_index, u32 thread_index)
573 {
574   sctp_connection_t *sctp_conn =
575     sctp_connection_get (conn_index, thread_index);
576
577   if (sctp_conn != NULL)
578     {
579       sctp_connection_timers_reset (sctp_conn);
580       /* Wait for the session tx events to clear */
581       sctp_conn->state = SCTP_STATE_CLOSED;
582     }
583 }
584
585 /**
586  * Compute maximum segment size for session layer.
587  */
588 u16
589 sctp_session_send_mss (transport_connection_t * trans_conn)
590 {
591   sctp_connection_t *sctp_conn =
592     sctp_get_connection_from_transport (trans_conn);
593
594   if (sctp_conn == NULL)
595     {
596       SCTP_DBG ("sctp_conn == NULL");
597       return 0;
598     }
599
600   update_cwnd (sctp_conn);
601   update_smallest_pmtu_idx (sctp_conn);
602
603   return sctp_conn->sub_conn[sctp_conn->smallest_PMTU_idx].cwnd;
604 }
605
606 u16
607 sctp_snd_space (sctp_connection_t * sctp_conn)
608 {
609   /* Finally, let's subtract the DATA chunk headers overhead */
610   return sctp_conn->sub_conn[sctp_conn->smallest_PMTU_idx].cwnd -
611     sizeof (sctp_payload_data_chunk_t) - sizeof (sctp_full_hdr_t);
612 }
613
614 /**
615  * Compute TX window session is allowed to fill.
616  */
617 u32
618 sctp_session_send_space (transport_connection_t * trans_conn)
619 {
620   sctp_connection_t *sctp_conn =
621     sctp_get_connection_from_transport (trans_conn);
622
623   return sctp_snd_space (sctp_conn);
624 }
625
626 transport_connection_t *
627 sctp_session_get_transport (u32 conn_index, u32 thread_index)
628 {
629   sctp_connection_t *sctp_conn =
630     sctp_connection_get (conn_index, thread_index);
631
632   if (PREDICT_TRUE (sctp_conn != NULL))
633     return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
634
635   return NULL;
636 }
637
638 transport_connection_t *
639 sctp_session_get_listener (u32 listener_index)
640 {
641   sctp_main_t *tm = vnet_get_sctp_main ();
642   sctp_connection_t *sctp_conn;
643   sctp_conn = pool_elt_at_index (tm->listener_pool, listener_index);
644   return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
645 }
646
647 u8 *
648 format_sctp_session (u8 * s, va_list * args)
649 {
650   u32 tci = va_arg (*args, u32);
651   u32 thread_index = va_arg (*args, u32);
652   u32 verbose = va_arg (*args, u32);
653   sctp_connection_t *tc;
654
655   tc = sctp_connection_get (tci, thread_index);
656   if (tc)
657     s = format (s, "%U", format_sctp_connection, tc, verbose);
658   else
659     s = format (s, "empty\n");
660   return s;
661 }
662
663 u8 *
664 format_sctp_listener_session (u8 * s, va_list * args)
665 {
666   u32 tci = va_arg (*args, u32);
667   sctp_connection_t *tc = sctp_listener_get (tci);
668   return format (s, "%U", format_sctp_connection_id, tc);
669 }
670
671 void
672 sctp_expired_timers_cb (u32 conn_index, u32 timer_id)
673 {
674   sctp_connection_t *sctp_conn;
675
676   sctp_conn = sctp_connection_get (conn_index, vlib_get_thread_index ());
677   /* note: the connection may have already disappeared */
678   if (PREDICT_FALSE (sctp_conn == 0))
679     return;
680
681   SCTP_DBG ("%s expired", sctp_timer_to_string (timer_id));
682
683   switch (timer_id)
684     {
685     case SCTP_TIMER_T1_INIT:
686     case SCTP_TIMER_T1_COOKIE:
687     case SCTP_TIMER_T2_SHUTDOWN:
688     case SCTP_TIMER_T3_RXTX:
689       sctp_timer_reset (sctp_conn, conn_index, timer_id);
690       break;
691     case SCTP_TIMER_T4_HEARTBEAT:
692       sctp_timer_reset (sctp_conn, conn_index, timer_id);
693       goto heartbeat;
694     }
695
696   if (sctp_conn->sub_conn[conn_index].unacknowledged_hb >
697       SCTP_PATH_MAX_RETRANS)
698     {
699       // The remote-peer is considered to be unreachable hence shutting down
700       u8 i, total_subs_down = 1;
701       for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
702         {
703           if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
704             continue;
705
706           u32 now = sctp_time_now ();
707           if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
708             {
709               total_subs_down += 1;
710               sctp_conn->sub_conn[i].state = SCTP_SUBCONN_STATE_DOWN;
711             }
712         }
713
714       if (total_subs_down == MAX_SCTP_CONNECTIONS)
715         {
716           /* Start cleanup. App wasn't notified yet so use delete notify as
717            * opposed to delete to cleanup session layer state. */
718           stream_session_delete_notify (&sctp_conn->sub_conn
719                                         [MAIN_SCTP_SUB_CONN_IDX].connection);
720
721           sctp_connection_timers_reset (sctp_conn);
722
723           sctp_connection_cleanup (sctp_conn);
724         }
725     }
726   return;
727
728 heartbeat:
729   sctp_send_heartbeat (sctp_conn);
730 }
731
732 static void
733 sctp_expired_timers_dispatch (u32 * expired_timers)
734 {
735   int i;
736   u32 connection_index, timer_id;
737
738   for (i = 0; i < vec_len (expired_timers); i++)
739     {
740       /* Get session index and timer id */
741       connection_index = expired_timers[i] & 0x0FFFFFFF;
742       timer_id = expired_timers[i] >> 28;
743
744       SCTP_DBG ("Expired timer ID: %u", timer_id);
745
746       /* Handle expiration */
747       sctp_expired_timers_cb (connection_index, timer_id);
748     }
749 }
750
751 void
752 sctp_initialize_timer_wheels (sctp_main_t * tm)
753 {
754   tw_timer_wheel_16t_2w_512sl_t *tw;
755   /* *INDENT-OFF* */
756   foreach_vlib_main (({
757     tw = &tm->timer_wheels[ii];
758     tw_timer_wheel_init_16t_2w_512sl (tw, sctp_expired_timers_dispatch,
759                                       100e-3 /* timer period 100ms */ , ~0);
760     tw->last_run_time = vlib_time_now (this_vlib_main);
761   }));
762   /* *INDENT-ON* */
763 }
764
765 clib_error_t *
766 sctp_main_enable (vlib_main_t * vm)
767 {
768   sctp_main_t *tm = vnet_get_sctp_main ();
769   vlib_thread_main_t *vtm = vlib_get_thread_main ();
770   clib_error_t *error = 0;
771   u32 num_threads;
772   int thread;
773   sctp_connection_t *sctp_conn __attribute__ ((unused));
774   u32 preallocated_connections_per_thread;
775
776   if ((error = vlib_call_init_function (vm, ip_main_init)))
777     return error;
778   if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
779     return error;
780   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
781     return error;
782
783   /*
784    * Registrations
785    */
786
787   ip4_register_protocol (IP_PROTOCOL_SCTP, sctp4_input_node.index);
788   ip6_register_protocol (IP_PROTOCOL_SCTP, sctp6_input_node.index);
789
790   /*
791    * Initialize data structures
792    */
793
794   num_threads = 1 /* main thread */  + vtm->n_threads;
795   vec_validate (tm->connections, num_threads - 1);
796
797   /*
798    * Preallocate connections. Assume that thread 0 won't
799    * use preallocated threads when running multi-core
800    */
801   if (num_threads == 1)
802     {
803       thread = 0;
804       preallocated_connections_per_thread = tm->preallocated_connections;
805     }
806   else
807     {
808       thread = 1;
809       preallocated_connections_per_thread =
810         tm->preallocated_connections / (num_threads - 1);
811     }
812   for (; thread < num_threads; thread++)
813     {
814       if (preallocated_connections_per_thread)
815         pool_init_fixed (tm->connections[thread],
816                          preallocated_connections_per_thread);
817     }
818
819   /* Initialize per worker thread tx buffers (used for control messages) */
820   vec_validate (tm->tx_buffers, num_threads - 1);
821
822   /* Initialize timer wheels */
823   vec_validate (tm->timer_wheels, num_threads - 1);
824   sctp_initialize_timer_wheels (tm);
825
826   /* Initialize clocks per tick for SCTP timestamp. Used to compute
827    * monotonically increasing timestamps. */
828   tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
829     / SCTP_TSTAMP_RESOLUTION;
830
831   if (num_threads > 1)
832     {
833       clib_spinlock_init (&tm->half_open_lock);
834     }
835
836   vec_validate (tm->tx_frames[0], num_threads - 1);
837   vec_validate (tm->tx_frames[1], num_threads - 1);
838   vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
839   vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
840
841   tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
842     (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
843
844   vec_validate (tm->time_now, num_threads - 1);
845   return error;
846 }
847
848 clib_error_t *
849 sctp_enable_disable (vlib_main_t * vm, u8 is_en)
850 {
851   if (is_en)
852     {
853       if (sctp_main.is_enabled)
854         return 0;
855
856       return sctp_main_enable (vm);
857     }
858   else
859     {
860       sctp_main.is_enabled = 0;
861     }
862
863   return 0;
864 }
865
866 transport_connection_t *
867 sctp_half_open_session_get_transport (u32 conn_index)
868 {
869   sctp_connection_t *sctp_conn = sctp_half_open_connection_get (conn_index);
870   return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
871 }
872
873 u8 *
874 format_sctp_half_open (u8 * s, va_list * args)
875 {
876   u32 tci = va_arg (*args, u32);
877   sctp_connection_t *sctp_conn = sctp_half_open_connection_get (tci);
878   return format (s, "%U", format_sctp_connection_id, sctp_conn);
879 }
880
881 void
882 sctp_update_time (f64 now, u8 thread_index)
883 {
884   sctp_set_time_now (thread_index);
885   tw_timer_expire_timers_16t_2w_512sl (&sctp_main.timer_wheels[thread_index],
886                                        now);
887   sctp_flush_frames_to_output (thread_index);
888 }
889
890 /* *INDENT OFF* */
891 const static transport_proto_vft_t sctp_proto = {
892   .enable = sctp_enable_disable,
893   .bind = sctp_session_bind,
894   .unbind = sctp_session_unbind,
895   .open = sctp_session_open,
896   .close = sctp_session_close,
897   .cleanup = sctp_session_cleanup,
898   .push_header = sctp_push_header,
899   .send_mss = sctp_session_send_mss,
900   .send_space = sctp_session_send_space,
901   .update_time = sctp_update_time,
902   .get_connection = sctp_session_get_transport,
903   .get_listener = sctp_session_get_listener,
904   .get_half_open = sctp_half_open_session_get_transport,
905   .format_connection = format_sctp_session,
906   .format_listener = format_sctp_listener_session,
907   .format_half_open = format_sctp_half_open,
908 };
909
910 /* *INDENT ON* */
911
912 clib_error_t *
913 sctp_init (vlib_main_t * vm)
914 {
915   sctp_main_t *tm = vnet_get_sctp_main ();
916   ip_main_t *im = &ip_main;
917   ip_protocol_info_t *pi;
918   /* Session layer, and by implication SCTP, are disabled by default */
919   tm->is_enabled = 0;
920
921   /* Register with IP for header parsing */
922   pi = ip_get_protocol_info (im, IP_PROTOCOL_SCTP);
923   if (pi == 0)
924     return clib_error_return (0, "SCTP protocol info AWOL");
925   pi->format_header = format_sctp_header;
926   pi->unformat_pg_edit = unformat_pg_sctp_header;
927
928   /* Register as transport with session layer */
929   transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
930                                FIB_PROTOCOL_IP4, sctp4_output_node.index);
931   transport_register_protocol (TRANSPORT_PROTO_SCTP, &sctp_proto,
932                                FIB_PROTOCOL_IP6, sctp6_output_node.index);
933
934   sctp_api_reference ();
935
936   return 0;
937 }
938
939 VLIB_INIT_FUNCTION (sctp_init);
940
941 /*
942  * fd.io coding-style-patch-verification: ON
943  *
944  * Local Variables:
945  * eval: (c-set-style "gnu")
946  * End:
947  */