gre: set proper fib index for unnumbered interfaces, unset fib index before forwardin...
[vpp.git] / src / vnet / tcp / tcp.c
index a6c1e21..4b1dd8e 100644 (file)
@@ -181,6 +181,13 @@ tcp_session_get_listener (u32 listener_index)
   return &tc->connection;
 }
 
+static tcp_connection_t *
+tcp_half_open_connection_alloc (void)
+{
+  ASSERT (vlib_get_thread_index () == 0);
+  return tcp_connection_alloc (0);
+}
+
 /**
  * Cleanup half-open connection
  *
@@ -188,12 +195,8 @@ tcp_session_get_listener (u32 listener_index)
 static void
 tcp_half_open_connection_free (tcp_connection_t * tc)
 {
-  tcp_main_t *tm = vnet_get_tcp_main ();
-  clib_spinlock_lock_if_init (&tm->half_open_lock);
-  if (CLIB_DEBUG)
-    clib_memset (tc, 0xFA, sizeof (*tc));
-  pool_put (tm->half_open_connections, tc);
-  clib_spinlock_unlock_if_init (&tm->half_open_lock);
+  ASSERT (vlib_get_thread_index () == 0);
+  return tcp_connection_free (tc);
 }
 
 /**
@@ -214,25 +217,13 @@ tcp_half_open_connection_cleanup (tcp_connection_t * tc)
   if (tc->c_thread_index != vlib_get_thread_index ())
     return 1;
 
-  session_half_open_delete_notify (TRANSPORT_PROTO_TCP, tc->c_s_ho_handle);
+  session_half_open_delete_notify (&tc->connection);
   wrk = tcp_get_worker (tc->c_thread_index);
   tcp_timer_reset (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT_SYN);
   tcp_half_open_connection_free (tc);
   return 0;
 }
 
-static tcp_connection_t *
-tcp_half_open_connection_new (void)
-{
-  tcp_main_t *tm = vnet_get_tcp_main ();
-  tcp_connection_t *tc = 0;
-  ASSERT (vlib_get_thread_index () == 0);
-  pool_get (tm->half_open_connections, tc);
-  clib_memset (tc, 0, sizeof (*tc));
-  tc->c_c_index = tc - tm->half_open_connections;
-  return tc;
-}
-
 /**
  * Cleans up connection state.
  *
@@ -306,13 +297,23 @@ tcp_connection_alloc (u8 thread_index)
 }
 
 tcp_connection_t *
-tcp_connection_alloc_w_base (u8 thread_index, tcp_connection_t * base)
+tcp_connection_alloc_w_base (u8 thread_index, tcp_connection_t **base)
 {
   tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
   tcp_connection_t *tc;
 
-  pool_get (wrk->connections, tc);
-  clib_memcpy_fast (tc, base, sizeof (*tc));
+  /* Make sure connection is still valid if pool moves */
+  if ((*base)->c_thread_index == thread_index)
+    {
+      u32 base_index = (*base)->c_c_index;
+      pool_get (wrk->connections, tc);
+      *base = tcp_connection_get (base_index, thread_index);
+    }
+  else
+    {
+      pool_get (wrk->connections, tc);
+    }
+  clib_memcpy_fast (tc, *base, sizeof (*tc));
   tc->c_c_index = tc - wrk->connections;
   tc->c_thread_index = thread_index;
   return tc;
@@ -354,7 +355,6 @@ tcp_program_cleanup (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
  * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
  * and cleanup is called.
  *
- * N.B. Half-close connections are not supported
  */
 void
 tcp_connection_close (tcp_connection_t * tc)
@@ -425,6 +425,30 @@ tcp_connection_close (tcp_connection_t * tc)
     }
 }
 
+static void
+tcp_session_half_close (u32 conn_index, u32 thread_index)
+{
+  tcp_worker_ctx_t *wrk;
+  tcp_connection_t *tc;
+
+  tc = tcp_connection_get (conn_index, thread_index);
+  wrk = tcp_get_worker (tc->c_thread_index);
+
+  /* If the connection is not in ESTABLISHED state, ignore it */
+  if (tc->state != TCP_STATE_ESTABLISHED)
+    return;
+  if (!transport_max_tx_dequeue (&tc->connection))
+    tcp_send_fin (tc);
+  else
+    tc->flags |= TCP_CONN_FINPNDG;
+  tcp_connection_set_state (tc, TCP_STATE_FIN_WAIT_1);
+  /* Set a timer in case the peer stops responding. Otherwise the
+   * connection will be stuck here forever. */
+  ASSERT (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID);
+  tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
+                tcp_cfg.finwait1_time);
+}
+
 static void
 tcp_session_close (u32 conn_index, u32 thread_index)
 {
@@ -782,21 +806,20 @@ tcp_session_open (transport_endpoint_cfg_t * rmt)
        return rv;
 
       if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
-                                    lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
+                                    lcl_port, rmt->port, TRANSPORT_PROTO_TCP,
                                     rmt->is_ip4))
        return SESSION_E_PORTINUSE;
 
       /* 5-tuple is available so increase lcl endpoint refcount and proceed
        * with connection allocation */
-      transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
+      transport_share_local_endpoint (TRANSPORT_PROTO_TCP, &lcl_addr,
                                      lcl_port);
     }
 
   /*
    * Create connection and send SYN
    */
-  clib_spinlock_lock_if_init (&tm->half_open_lock);
-  tc = tcp_half_open_connection_new ();
+  tc = tcp_half_open_connection_alloc ();
   ip_copy (&tc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
   ip_copy (&tc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
   tc->c_rmt_port = rmt->port;
@@ -808,12 +831,13 @@ tcp_session_open (transport_endpoint_cfg_t * rmt)
   /* The other connection vars will be initialized after SYN ACK */
   tcp_connection_timers_init (tc);
   tc->mss = rmt->mss;
+  tc->next_node_index = rmt->next_node_index;
+  tc->next_node_opaque = rmt->next_node_opaque;
 
   TCP_EVT (TCP_EVT_OPEN, tc);
   tc->state = TCP_STATE_SYN_SENT;
   tcp_init_snd_vars (tc);
   tcp_send_syn (tc);
-  clib_spinlock_unlock_if_init (&tm->half_open_lock);
 
   return tc->c_c_index;
 }
@@ -853,8 +877,20 @@ format_tcp_half_open_session (u8 * s, va_list * args)
 {
   u32 tci = va_arg (*args, u32);
   u32 __clib_unused thread_index = va_arg (*args, u32);
-  tcp_connection_t *tc = tcp_half_open_connection_get (tci);
-  return format (s, "%U", format_tcp_connection_id, tc);
+  u32 verbose = va_arg (*args, u32);
+  tcp_connection_t *tc;
+  u8 *state = 0;
+
+  tc = tcp_half_open_connection_get (tci);
+  if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
+    state = format (state, "%s", "CLOSED");
+  else
+    state = format (state, "%U", format_tcp_state, tc->state);
+  s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tcp_connection_id, tc);
+  if (verbose)
+    s = format (s, "%-" SESSION_CLI_STATE_LEN "v", state);
+  vec_free (state);
+  return s;
 }
 
 static transport_connection_t *
@@ -1304,6 +1340,7 @@ const static transport_proto_vft_t tcp_proto = {
   .get_half_open = tcp_half_open_session_get_transport,
   .attribute = tcp_session_attribute,
   .connect = tcp_session_open,
+  .half_close = tcp_session_half_close,
   .close = tcp_session_close,
   .cleanup = tcp_session_cleanup,
   .cleanup_ho = tcp_session_cleanup_ho,
@@ -1483,18 +1520,6 @@ tcp_main_enable (vlib_main_t * vm)
                                  vlib_time_now (vm));
     }
 
-  /*
-   * Use a preallocated half-open connection pool?
-   */
-  if (tcp_cfg.preallocated_half_open_connections)
-    pool_init_fixed (tm->half_open_connections,
-                    tcp_cfg.preallocated_half_open_connections);
-
-  if (num_threads > 1)
-    {
-      clib_spinlock_init (&tm->half_open_lock);
-    }
-
   tcp_initialize_iss_seed (tm);
 
   tm->bytes_per_buffer = vlib_buffer_get_default_data_size (vm);
@@ -1563,6 +1588,7 @@ tcp_configuration_init (void)
   tcp_cfg.lastack_time = 300000;       /* 30s */
   tcp_cfg.finwait2_time = 300000;      /* 30s */
   tcp_cfg.closing_time = 300000;       /* 30s */
+  tcp_cfg.alloc_err_timeout = 1000;    /* 100ms */
 
   /* This value is seconds */
   tcp_cfg.cleanup_time = 0.1;  /* 100ms */