session: segment manager improvements
[vpp.git] / src / vnet / tcp / tcp.c
index 4652618..0a826a5 100644 (file)
  * limitations under the License.
  */
 
+/**
+ * @file
+ * @brief TCP host stack utilities
+ */
+
 #include <vnet/tcp/tcp.h>
 #include <vnet/session/session.h>
 #include <vnet/fib/fib.h>
 #include <vnet/dpo/load_balance.h>
+#include <vnet/dpo/receive_dpo.h>
+#include <vnet/ip/ip6_neighbor.h>
 #include <math.h>
 
 tcp_main_t tcp_main;
@@ -309,8 +316,10 @@ tcp_connection_close (tcp_connection_t * tc)
       tcp_send_fin (tc);
       tc->state = TCP_STATE_LAST_ACK;
       break;
+    case TCP_STATE_FIN_WAIT_1:
+      break;
     default:
-      clib_warning ("shouldn't be here");
+      clib_warning ("state: %u", tc->state);
     }
 
   TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
@@ -362,7 +371,11 @@ ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
       /* *INDENT-OFF* */
       foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
       ({
-        return ip_interface_address_get_address (lm6, ia);
+        ip6_address_t *rv;
+        rv = ip_interface_address_get_address (lm6, ia);
+        /* Trying to use a link-local ip6 src address is a fool's errand */
+        if (!ip6_address_is_link_local_unicast (rv))
+          return rv;
       }));
       /* *INDENT-ON* */
     }
@@ -584,6 +597,12 @@ tcp_connection_open (transport_endpoint_t * rmt)
   prefix.fp_len = rmt->is_ip4 ? 32 : 128;
 
   fib_index = fib_table_find (prefix.fp_proto, rmt->vrf);
+  if (fib_index == (u32) ~ 0)
+    {
+      clib_warning ("no fib table");
+      return -1;
+    }
+
   fei = fib_table_lookup (fib_index, &prefix);
 
   /* Couldn't find route to destination. Bail out. */
@@ -635,6 +654,14 @@ tcp_connection_open (transport_endpoint_t * rmt)
       else
        {
          ip6 = ip_interface_get_first_ip (sw_if_index, 0);
+         if (ip6 == 0)
+           {
+             clib_warning ("no routable ip6 addresses on %U",
+                           format_vnet_sw_if_index_name, vnet_get_main (),
+                           sw_if_index);
+             return -1;
+           }
+
          clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
        }
     }
@@ -773,7 +800,8 @@ format_tcp_vars (u8 * s, va_list * args)
   s = format (s, "rtt_seq %u\n", tc->rtt_seq);
   s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
              tcp_time_now () - tc->tsval_recent_age);
-  s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb);
+  s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
+             tc);
   if (vec_len (tc->snd_sacks))
     s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
 
@@ -810,6 +838,8 @@ format_tcp_connection (u8 * s, va_list * args)
   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
   u32 verbose = va_arg (*args, u32);
 
+  if (!tc)
+    return s;
   s = format (s, "%-50U", format_tcp_connection_id, tc);
   if (verbose)
     {
@@ -833,7 +863,7 @@ format_tcp_session (u8 * s, va_list * args)
   if (tc)
     s = format (s, "%U", format_tcp_connection, tc, verbose);
   else
-    s = format (s, "empty");
+    s = format (s, "empty\n");
   return s;
 }
 
@@ -905,7 +935,11 @@ u8 *
 format_tcp_sack_hole (u8 * s, va_list * args)
 {
   sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
-  s = format (s, "[%u, %u]", hole->start, hole->end);
+  tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
+  if (tc)
+    s = format (s, "  [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
+  else
+    s = format (s, "  [%u, %u]", hole->start, hole->end);
   return s;
 }
 
@@ -913,6 +947,7 @@ u8 *
 format_tcp_scoreboard (u8 * s, va_list * args)
 {
   sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
+  tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
   sack_scoreboard_hole_t *hole;
   s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
              sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
@@ -927,7 +962,7 @@ format_tcp_scoreboard (u8 * s, va_list * args)
 
   while (hole)
     {
-      s = format (s, "%U", format_tcp_sack_hole, hole);
+      s = format (s, "%U", format_tcp_sack_hole, hole, tc);
       hole = scoreboard_next_hole (sb, hole);
     }
 
@@ -976,13 +1011,10 @@ tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
       return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
     }
 
-  /* If we can't write at least a segment, don't try at all */
+  /* If not snd_wnd constrained and we can't write at least a segment,
+   * don't try at all */
   if (PREDICT_FALSE (snd_space < tc->snd_mss))
-    {
-      if (snd_space > clib_min (tc->mss, tc->rcv_opts.mss) - TCP_HDR_LEN_MAX)
-       return snd_space;
-      return 0;
-    }
+    return 0;
 
   /* round down to mss multiple */
   return snd_space - (snd_space % tc->snd_mss);
@@ -1005,7 +1037,7 @@ tcp_snd_space (tcp_connection_t * tc)
 
   if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
     {
-      snd_space = tcp_available_snd_space (tc);
+      snd_space = tcp_available_output_snd_space (tc);
 
       /* If we haven't gotten dupacks or if we did and have gotten sacked
        * bytes then we can still send as per Limited Transmit (RFC3042) */
@@ -1026,17 +1058,20 @@ tcp_snd_space (tcp_connection_t * tc)
   if (tcp_in_recovery (tc))
     {
       tc->snd_nxt = tc->snd_una_max;
-      snd_space = tcp_available_wnd (tc) - tc->snd_rxt_bytes
+      snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
        - (tc->snd_una_max - tc->snd_congestion);
       if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
        return 0;
       return tcp_round_snd_space (tc, snd_space);
     }
 
-  /* If in fast recovery, send 1 SMSS if wnd allows */
-  if (tcp_in_fastrecovery (tc)
-      && tcp_available_snd_space (tc) && !tcp_fastrecovery_sent_1_smss (tc))
+  /* RFC 5681: When previously unsent data is available and the new value of
+   * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
+   * bytes of previously unsent data. */
+  if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
     {
+      if (tcp_available_output_snd_space (tc) < tc->snd_mss)
+       return 0;
       tcp_fastrecovery_1_smss_on (tc);
       return tc->snd_mss;
     }
@@ -1048,7 +1083,8 @@ u32
 tcp_session_send_space (transport_connection_t * trans_conn)
 {
   tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
-  return tcp_snd_space (tc);
+  return clib_min (tcp_snd_space (tc),
+                  tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
 }
 
 i32
@@ -1106,10 +1142,17 @@ tcp_timer_establish_handler (u32 conn_index)
   tcp_connection_t *tc;
 
   tc = tcp_half_open_connection_get (conn_index);
+  if (tc)
+    {
+      ASSERT (tc->state == TCP_STATE_SYN_SENT);
+      stream_session_connect_notify (&tc->connection, 1 /* fail */ );
+    }
+  else
+    {
+      tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
+      ASSERT (tc->state == TCP_STATE_SYN_RCVD);
+    }
   tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
-
-  ASSERT (tc->state == TCP_STATE_SYN_SENT);
-  stream_session_connect_notify (&tc->connection, 1 /* fail */ );
   tcp_connection_cleanup (tc);
 }
 
@@ -1224,6 +1267,7 @@ tcp_main_enable (vlib_main_t * vm)
   pi->unformat_pg_edit = unformat_pg_tcp_header;
 
   ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
+  ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
 
   /* Register as transport with session layer */
   session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
@@ -1281,9 +1325,14 @@ tcp_main_enable (vlib_main_t * vm)
   tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
     / TCP_TSTAMP_RESOLUTION;
 
+  if (tm->local_endpoints_table_buckets == 0)
+    tm->local_endpoints_table_buckets = 250000;
+  if (tm->local_endpoints_table_memory == 0)
+    tm->local_endpoints_table_memory = 512 << 20;
+
   clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
-                        1000000 /* $$$$ config parameter nbuckets */ ,
-                        (512 << 20) /*$$$ config parameter table size */ );
+                        tm->local_endpoints_table_buckets,
+                        tm->local_endpoints_table_memory);
 
   /* Initialize [port-allocator] random number seed */
   tm->port_allocator_seed = (u32) clib_cpu_time_now ();
@@ -1299,6 +1348,8 @@ tcp_main_enable (vlib_main_t * vm)
 
   tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
     (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+
+  vec_validate (tm->time_now, num_threads - 1);
   return error;
 }
 
@@ -1325,6 +1376,7 @@ tcp_init (vlib_main_t * vm)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
   tm->is_enabled = 0;
+  tcp_api_reference ();
   return 0;
 }
 
@@ -1334,6 +1386,7 @@ static clib_error_t *
 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
+  u64 tmp;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1344,6 +1397,19 @@ tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
       else if (unformat (input, "preallocated-half-open-connections %d",
                         &tm->preallocated_half_open_connections))
        ;
+      else if (unformat (input, "local-endpoints-table-memory %U",
+                        unformat_memory_size, &tmp))
+       {
+         if (tmp >= 0x100000000)
+           return clib_error_return (0, "memory size %llx (%lld) too large",
+                                     tmp, tmp);
+         tm->local_endpoints_table_memory = tmp;
+       }
+      else if (unformat (input, "local-endpoints-table-buckets %d",
+                        &tm->local_endpoints_table_buckets))
+       ;
+
+
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);
@@ -1353,15 +1419,191 @@ tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
 
 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
 
+
+/**
+ * \brief Configure an ipv4 source address range
+ * @param vm vlib_main_t pointer
+ * @param start first ipv4 address in the source address range
+ * @param end last ipv4 address in the source address range
+ * @param table_id VRF / table ID, 0 for the default FIB
+ * @return 0 if all OK, else an error indication from api_errno.h
+ */
+
+int
+tcp_configure_v4_source_address_range (vlib_main_t * vm,
+                                      ip4_address_t * start,
+                                      ip4_address_t * end, u32 table_id)
+{
+  tcp_main_t *tm = vnet_get_tcp_main ();
+  vnet_main_t *vnm = vnet_get_main ();
+  u32 start_host_byte_order, end_host_byte_order;
+  fib_prefix_t prefix;
+  vnet_sw_interface_t *si;
+  fib_node_index_t fei;
+  u32 fib_index = 0;
+  u32 sw_if_index;
+  int rv;
+  int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
+                             ip4_address_t * hi_addr, u32 fib_index,
+                             int is_del);
+
+  memset (&prefix, 0, sizeof (prefix));
+
+  fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
+
+  if (fib_index == ~0)
+    return VNET_API_ERROR_NO_SUCH_FIB;
+
+  start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
+  end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
+
+  /* sanity check for reversed args or some such */
+  if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
+    return VNET_API_ERROR_INVALID_ARGUMENT;
+
+  /* Lookup the last address, to identify the interface involved */
+  prefix.fp_len = 32;
+  prefix.fp_proto = FIB_PROTOCOL_IP4;
+  memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
+
+  fei = fib_table_lookup (fib_index, &prefix);
+
+  /* Couldn't find route to destination. Bail out. */
+  if (fei == FIB_NODE_INDEX_INVALID)
+    return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
+
+  sw_if_index = fib_entry_get_resolving_interface (fei);
+
+  /* Enable proxy arp on the interface */
+  si = vnet_get_sw_interface (vnm, sw_if_index);
+  si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
+
+  /* Configure proxy arp across the range */
+  rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
+
+  if (rv)
+    return rv;
+
+  do
+    {
+      dpo_id_t dpo = DPO_INVALID;
+
+      vec_add1 (tm->ip4_src_addresses, start[0]);
+
+      /* Add local adjacencies for the range */
+
+      receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
+                              NULL, &dpo);
+      prefix.fp_len = 32;
+      prefix.fp_proto = FIB_PROTOCOL_IP4;
+      prefix.fp_addr.ip4.as_u32 = start->as_u32;
+
+      fib_table_entry_special_dpo_update (fib_index,
+                                         &prefix,
+                                         FIB_SOURCE_API,
+                                         FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
+      dpo_reset (&dpo);
+
+      start_host_byte_order++;
+      start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
+    }
+  while (start_host_byte_order <= end_host_byte_order);
+
+  return 0;
+}
+
+/**
+ * \brief Configure an ipv6 source address range
+ * @param vm vlib_main_t pointer
+ * @param start first ipv6 address in the source address range
+ * @param end last ipv6 address in the source address range
+ * @param table_id VRF / table ID, 0 for the default FIB
+ * @return 0 if all OK, else an error indication from api_errno.h
+ */
+
+int
+tcp_configure_v6_source_address_range (vlib_main_t * vm,
+                                      ip6_address_t * start,
+                                      ip6_address_t * end, u32 table_id)
+{
+  tcp_main_t *tm = vnet_get_tcp_main ();
+  fib_prefix_t prefix;
+  u32 fib_index = 0;
+  fib_node_index_t fei;
+  u32 sw_if_index;
+
+  memset (&prefix, 0, sizeof (prefix));
+
+  fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
+
+  if (fib_index == ~0)
+    return VNET_API_ERROR_NO_SUCH_FIB;
+
+  while (1)
+    {
+      int i;
+      ip6_address_t tmp;
+      dpo_id_t dpo = DPO_INVALID;
+
+      /* Remember this address */
+      vec_add1 (tm->ip6_src_addresses, start[0]);
+
+      /* Lookup the prefix, to identify the interface involved */
+      prefix.fp_len = 128;
+      prefix.fp_proto = FIB_PROTOCOL_IP6;
+      memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
+
+      fei = fib_table_lookup (fib_index, &prefix);
+
+      /* Couldn't find route to destination. Bail out. */
+      if (fei == FIB_NODE_INDEX_INVALID)
+       return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
+
+      sw_if_index = fib_entry_get_resolving_interface (fei);
+
+      if (sw_if_index == (u32) ~ 0)
+       return VNET_API_ERROR_NO_MATCHING_INTERFACE;
+
+      /* Add a proxy neighbor discovery entry for this address */
+      ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
+
+      /* Add a receive adjacency for this address */
+      receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
+                              NULL, &dpo);
+
+      fib_table_entry_special_dpo_update (fib_index,
+                                         &prefix,
+                                         FIB_SOURCE_API,
+                                         FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
+      dpo_reset (&dpo);
+
+      /* Done with the entire range? */
+      if (!memcmp (start, end, sizeof (start[0])))
+       break;
+
+      /* Increment the address. DGMS. */
+      tmp = start[0];
+      for (i = 15; i >= 0; i--)
+       {
+         tmp.as_u8[i] += 1;
+         if (tmp.as_u8[i] != 0)
+           break;
+       }
+      start[0] = tmp;
+    }
+  return 0;
+}
+
 static clib_error_t *
 tcp_src_address (vlib_main_t * vm,
                 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
 {
-  tcp_main_t *tm = vnet_get_tcp_main ();
   ip4_address_t v4start, v4end;
   ip6_address_t v6start, v6end;
+  u32 table_id = 0;
   int v4set = 0;
   int v6set = 0;
+  int rv;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1374,13 +1616,15 @@ tcp_src_address (vlib_main_t * vm,
          v4set = 1;
        }
       else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
-                        unformat_ip4_address, &v6end))
+                        unformat_ip6_address, &v6end))
        v6set = 1;
       else if (unformat (input, "%U", unformat_ip6_address, &v6start))
        {
          memcpy (&v6end, &v6start, sizeof (v4start));
          v6set = 1;
        }
+      else if (unformat (input, "fib-table %d", &table_id))
+       ;
       else
        break;
     }
@@ -1390,21 +1634,41 @@ tcp_src_address (vlib_main_t * vm,
 
   if (v4set)
     {
-      u32 tmp;
-
-      do
+      rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
+                                                 table_id);
+      switch (rv)
        {
-         vec_add1 (tm->ip4_src_addresses, v4start);
-         tmp = clib_net_to_host_u32 (v4start.as_u32);
-         tmp++;
-         v4start.as_u32 = clib_host_to_net_u32 (tmp);
+       case 0:
+         break;
+
+       case VNET_API_ERROR_NO_SUCH_FIB:
+         return clib_error_return (0, "Invalid table-id %d", table_id);
+
+       case VNET_API_ERROR_INVALID_ARGUMENT:
+         return clib_error_return (0, "Invalid address range %U - %U",
+                                   format_ip4_address, &v4start,
+                                   format_ip4_address, &v4end);
+       default:
+         return clib_error_return (0, "error %d", rv);
+         break;
        }
-      while (clib_host_to_net_u32 (v4start.as_u32) <=
-            clib_host_to_net_u32 (v4end.as_u32));
     }
   if (v6set)
     {
-      clib_warning ("v6 src address list unimplemented...");
+      rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
+                                                 table_id);
+      switch (rv)
+       {
+       case 0:
+         break;
+
+       case VNET_API_ERROR_NO_SUCH_FIB:
+         return clib_error_return (0, "Invalid table-id %d", table_id);
+
+       default:
+         return clib_error_return (0, "error %d", rv);
+         break;
+       }
     }
   return 0;
 }