SCTP: coverity warning fix
[vpp.git] / src / vnet / sctp / sctp.c
index d0f37f4..529e408 100644 (file)
@@ -135,6 +135,7 @@ sctp_connection_timers_init (sctp_connection_t * sctp_conn)
   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
     {
       sctp_conn->sub_conn[i].RTO = SCTP_RTO_INIT;
+
       for (j = 0; j < SCTP_N_TIMERS; j++)
        {
          sctp_conn->sub_conn[i].timers[j] = SCTP_TIMER_HANDLE_INVALID;
@@ -469,10 +470,10 @@ void
 sctp_session_close (u32 conn_index, u32 thread_index)
 {
   ASSERT (thread_index == 0);
-
   sctp_connection_t *sctp_conn;
   sctp_conn = sctp_connection_get (conn_index, thread_index);
-  sctp_connection_close (sctp_conn);
+  if (sctp_conn != NULL)
+    sctp_connection_close (sctp_conn);
 }
 
 void
@@ -480,10 +481,13 @@ sctp_session_cleanup (u32 conn_index, u32 thread_index)
 {
   sctp_connection_t *sctp_conn;
   sctp_conn = sctp_connection_get (conn_index, thread_index);
-  sctp_connection_timers_reset (sctp_conn);
 
-  /* Wait for the session tx events to clear */
-  sctp_conn->state = SCTP_STATE_CLOSED;
+  if (sctp_conn != NULL)
+    {
+      sctp_connection_timers_reset (sctp_conn);
+      /* Wait for the session tx events to clear */
+      sctp_conn->state = SCTP_STATE_CLOSED;
+    }
 }
 
 /**
@@ -572,7 +576,11 @@ sctp_session_get_transport (u32 conn_index, u32 thread_index)
 {
   sctp_connection_t *sctp_conn =
     sctp_connection_get (conn_index, thread_index);
-  return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
+
+  if (PREDICT_TRUE (sctp_conn != NULL))
+    return &sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection;
+
+  return NULL;
 }
 
 transport_connection_t *
@@ -597,42 +605,51 @@ format_sctp_listener_session (u8 * s, va_list * args)
 }
 
 void
-sctp_timer_init_handler (u32 conn_index, u32 timer_id)
+sctp_expired_timers_cb (u32 conn_index, u32 timer_id)
 {
   sctp_connection_t *sctp_conn;
 
-  clib_warning ("");
   sctp_conn = sctp_connection_get (conn_index, vlib_get_thread_index ());
   /* note: the connection may have already disappeared */
   if (PREDICT_FALSE (sctp_conn == 0))
     return;
-  ASSERT (sctp_conn->state == SCTP_STATE_COOKIE_ECHOED);
+
+  SCTP_DBG ("%s expired", sctp_timer_to_string (timer_id));
 
   switch (timer_id)
     {
+    case SCTP_TIMER_T1_INIT:
+    case SCTP_TIMER_T1_COOKIE:
+    case SCTP_TIMER_T2_SHUTDOWN:
+    case SCTP_TIMER_T3_RXTX:
+      clib_smp_atomic_add (&sctp_conn->sub_conn[conn_index].unacknowledged_hb,
+                          1);
+      sctp_timer_reset (sctp_conn, conn_index, timer_id);
+      break;
     case SCTP_TIMER_T4_HEARTBEAT:
-      {
-       clib_warning ("Heartbeat timeout");
-       break;
-      }
+      sctp_timer_reset (sctp_conn, conn_index, timer_id);
+      goto heartbeat;
     }
-  /* Start cleanup. App wasn't notified yet so use delete notify as
-   * opposed to delete to cleanup session layer state. */
-  stream_session_delete_notify (&sctp_conn->
-                               sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection);
 
-  sctp_connection_timers_reset (sctp_conn);
+  if (sctp_conn->sub_conn[conn_index].unacknowledged_hb >
+      SCTP_ASSOCIATION_MAX_RETRANS)
+    {
+      // The remote-peer is considered to be unreachable hence shutting down
 
-  sctp_connection_cleanup (sctp_conn);
-}
+      /* Start cleanup. App wasn't notified yet so use delete notify as
+       * opposed to delete to cleanup session layer state. */
+      stream_session_delete_notify (&sctp_conn->sub_conn
+                                   [MAIN_SCTP_SUB_CONN_IDX].connection);
 
-/* *INDENT OFF* */
-static sctp_timer_expiration_handler
-  * sctp_timer_expiration_handlers[SCTP_N_TIMERS] = {
-  sctp_timer_init_handler
-};
+      sctp_connection_timers_reset (sctp_conn);
 
-/* *INDENT ON* */
+      sctp_connection_cleanup (sctp_conn);
+    }
+  return;
+
+heartbeat:
+  sctp_send_heartbeat (sctp_conn);
+}
 
 static void
 sctp_expired_timers_dispatch (u32 * expired_timers)
@@ -646,12 +663,11 @@ sctp_expired_timers_dispatch (u32 * expired_timers)
       connection_index = expired_timers[i] & 0x0FFFFFFF;
       timer_id = expired_timers[i] >> 28;
 
+      SCTP_DBG ("Expired timer ID: %u", timer_id);
+
       /* Handle expiration */
-      (*sctp_timer_expiration_handlers[timer_id]) (connection_index,
-                                                  timer_id);
+      sctp_expired_timers_cb (connection_index, timer_id);
     }
-
-  clib_warning ("");
 }
 
 void
@@ -736,6 +752,7 @@ sctp_main_enable (vlib_main_t * vm)
 
   if (num_threads > 1)
     {
+      clib_spinlock_init (&tm->half_open_lock);
     }
 
   vec_validate (tm->tx_frames[0], num_threads - 1);
@@ -783,6 +800,15 @@ format_sctp_half_open (u8 * s, va_list * args)
   return format (s, "%U", format_sctp_connection_id, sctp_conn);
 }
 
+void
+sctp_update_time (f64 now, u8 thread_index)
+{
+  sctp_set_time_now (thread_index);
+  tw_timer_expire_timers_16t_2w_512sl (&sctp_main.timer_wheels[thread_index],
+                                      now);
+  sctp_flush_frames_to_output (thread_index);
+}
+
 /* *INDENT OFF* */
 const static transport_proto_vft_t sctp_proto = {
   .enable = sctp_enable_disable,
@@ -795,6 +821,7 @@ const static transport_proto_vft_t sctp_proto = {
   .send_mss = sctp_session_send_mss,
   .send_space = sctp_session_send_space,
   .tx_fifo_offset = NULL,      //sctp_session_tx_fifo_offset,
+  .update_time = sctp_update_time,
   .get_connection = sctp_session_get_transport,
   .get_listener = sctp_session_get_listener,
   .get_half_open = sctp_half_open_session_get_transport,