SCTP: minor refactor

This patch adds some missing information in various debugging messages
which can help with debugging state-machine and connection-tracking.
It also renames some internal variable names for better code
readibility.

Change-Id: I68503fc0214300032f7514426c8b5b2b12edf11a
Signed-off-by: Marco Varlese <marco.varlese@suse.com>
diff --git a/src/vnet/buffer.h b/src/vnet/buffer.h
index 4e01a06..5aedb43 100644
--- a/src/vnet/buffer.h
+++ b/src/vnet/buffer.h
@@ -333,7 +333,7 @@
       u16 hdr_offset;		/**< offset relative to ip hdr */
       u16 data_offset;		/**< offset relative to ip hdr */
       u16 data_len;		/**< data len */
-      u8 conn_idx;
+      u8 subconn_idx; /**< index of the sub_connection being used */
       u8 flags;
     } sctp;
 
diff --git a/src/vnet/sctp/sctp.c b/src/vnet/sctp/sctp.c
index b81d4d0..4643e8e 100644
--- a/src/vnet/sctp/sctp.c
+++ b/src/vnet/sctp/sctp.c
@@ -305,14 +305,14 @@
 sctp_connection_t *
 sctp_connection_new (u8 thread_index)
 {
-  sctp_main_t *tm = vnet_get_sctp_main ();
+  sctp_main_t *sctp_main = vnet_get_sctp_main ();
   sctp_connection_t *sctp_conn;
 
-  pool_get (tm->connections[thread_index], sctp_conn);
+  pool_get (sctp_main->connections[thread_index], sctp_conn);
   memset (sctp_conn, 0, sizeof (*sctp_conn));
   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].parent = sctp_conn;
   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index =
-    sctp_conn - tm->connections[thread_index];
+    sctp_conn - sctp_main->connections[thread_index];
   sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_thread_index = thread_index;
   sctp_conn->local_tag = 0;
   sctp_conn->next_avail_sub_conn = 1;
@@ -473,8 +473,8 @@
 {
   ASSERT (thread_index == 0);
 
-  sctp_connection_t *sctp_conn;
-  sctp_conn = sctp_connection_get (conn_index, thread_index);
+  sctp_connection_t *sctp_conn =
+    sctp_connection_get (conn_index, thread_index);
   if (sctp_conn != NULL)
     sctp_connection_close (sctp_conn);
 }
@@ -482,8 +482,8 @@
 void
 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_t *sctp_conn =
+    sctp_connection_get (conn_index, thread_index);
 
   if (sctp_conn != NULL)
     {
diff --git a/src/vnet/sctp/sctp.h b/src/vnet/sctp/sctp.h
index 0d2e4b3..fd9d8da 100644
--- a/src/vnet/sctp/sctp.h
+++ b/src/vnet/sctp/sctp.h
@@ -524,13 +524,13 @@
 always_inline void
 sctp_half_open_connection_del (sctp_connection_t * tc)
 {
-  sctp_main_t *tm = vnet_get_sctp_main ();
-  clib_spinlock_lock_if_init (&tm->half_open_lock);
-  pool_put_index (tm->half_open_connections,
+  sctp_main_t *sctp_main = vnet_get_sctp_main ();
+  clib_spinlock_lock_if_init (&sctp_main->half_open_lock);
+  pool_put_index (sctp_main->half_open_connections,
 		  tc->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_c_index);
   if (CLIB_DEBUG)
     memset (tc, 0xFA, sizeof (*tc));
-  clib_spinlock_unlock_if_init (&tm->half_open_lock);
+  clib_spinlock_unlock_if_init (&sctp_main->half_open_lock);
 }
 
 always_inline u32
diff --git a/src/vnet/sctp/sctp_input.c b/src/vnet/sctp/sctp_input.c
index 35218d5..d3e69c6 100644
--- a/src/vnet/sctp/sctp_input.c
+++ b/src/vnet/sctp/sctp_input.c
@@ -537,8 +537,6 @@
   sctp_timer_set (sctp_conn, idx,
 		  SCTP_TIMER_T1_COOKIE, sctp_conn->sub_conn[idx].RTO);
 
-  stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
-
   return SCTP_ERROR_NONE;
 }
 
@@ -806,6 +804,8 @@
   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
 		  sctp_conn->sub_conn[idx].RTO);
 
+  stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
+
   return SCTP_ERROR_NONE;
 
 }
@@ -834,6 +834,8 @@
   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
 		  sctp_conn->sub_conn[idx].RTO);
 
+  stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
+
   return SCTP_ERROR_NONE;
 
 }
@@ -2084,10 +2086,13 @@
 	      error0 = tm->dispatch_table[sctp_conn->state][chunk_type].error;
 
 	      SCTP_DBG_STATE_MACHINE
-		("SESSION_INDEX = %u, CURRENT_CONNECTION_STATE = %s,"
+		("S_INDEX = %u, C_INDEX = %u, TRANS_CONN = %p, SCTP_CONN = %p, CURRENT_CONNECTION_STATE = %s,"
 		 "CHUNK_TYPE_RECEIVED = %s " "NEXT_PHASE = %s",
 		 sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
-		 connection.s_index, sctp_state_to_string (sctp_conn->state),
+		 connection.s_index,
+		 sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
+		 connection.c_index, trans_conn, sctp_conn,
+		 sctp_state_to_string (sctp_conn->state),
 		 sctp_chunk_to_string (chunk_type), phase_to_string (next0));
 
 	      if (chunk_type == DATA)
diff --git a/src/vnet/sctp/sctp_output.c b/src/vnet/sctp/sctp_output.c
index fba06d9..39e5e75 100644
--- a/src/vnet/sctp/sctp_output.c
+++ b/src/vnet/sctp/sctp_output.c
@@ -261,7 +261,7 @@
   b->current_length = 0;
   b->total_length_not_including_first_buffer = 0;
   vnet_buffer (b)->sctp.flags = 0;
-  vnet_buffer (b)->sctp.conn_idx = MAX_SCTP_CONNECTIONS;
+  vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
 
   /* Leave enough space for headers */
   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
@@ -275,7 +275,7 @@
   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
   b->total_length_not_including_first_buffer = 0;
   vnet_buffer (b)->sctp.flags = 0;
-  vnet_buffer (b)->sctp.conn_idx = MAX_SCTP_CONNECTIONS;
+  vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
   /* Leave enough space for headers */
   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
@@ -478,7 +478,7 @@
   sctp_conn->local_tag = init_chunk->initiate_tag;
 
   vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 
   SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
 			  "CHUNK_TYPE = %s, "
@@ -551,7 +551,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 void
@@ -585,7 +585,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /**
@@ -728,7 +728,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /**
@@ -764,7 +764,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /*
@@ -822,7 +822,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /*
@@ -877,7 +877,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /**
@@ -916,7 +916,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 /**
@@ -951,7 +951,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 void
@@ -1016,7 +1016,7 @@
 
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 void
@@ -1093,7 +1093,12 @@
     vlib_buffer_push_uninit (b, bytes_to_add);
 
   u8 idx = sctp_data_subconn_select (sctp_conn);
-
+  SCTP_DBG_OUTPUT
+    ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, LCL_PORT = %u, RMT_PORT = %u",
+     sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
+     sctp_conn->sub_conn[idx].connection.c_index,
+     sctp_conn->sub_conn[idx].connection.lcl_port,
+     sctp_conn->sub_conn[idx].connection.rmt_port);
   data_chunk->sctp_hdr.checksum = 0;
   data_chunk->sctp_hdr.src_port =
     sctp_conn->sub_conn[idx].connection.lcl_port;
@@ -1133,7 +1138,7 @@
   vnet_buffer (b)->sctp.connection_index =
     sctp_conn->sub_conn[idx].connection.c_index;
 
-  vnet_buffer (b)->sctp.conn_idx = idx;
+  vnet_buffer (b)->sctp.subconn_idx = idx;
 }
 
 u32
@@ -1142,6 +1147,15 @@
   sctp_connection_t *sctp_conn =
     sctp_get_connection_from_transport (trans_conn);
 
+  SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
+		   "S_INDEX = %u, C_INDEX = %u,"
+		   "LCL_PORT = %u, RMT_PORT = %u",
+		   trans_conn,
+		   sctp_conn,
+		   trans_conn->s_index,
+		   trans_conn->c_index,
+		   trans_conn->lcl_port, trans_conn->rmt_port);
+
   sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
 
   sctp_trajectory_add_start (b0, 3);
@@ -1244,7 +1258,7 @@
 	      goto done;
 	    }
 
-	  u8 idx = vnet_buffer (b0)->sctp.conn_idx;
+	  u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
 
 	  th0 = vlib_buffer_get_current (b0);