udp: store mss and sw_if_index to udp_connection_t
[vpp.git] / src / vnet / udp / udp.c
index b27f0d7..9b2ed88 100644 (file)
@@ -23,7 +23,7 @@
 udp_main_t udp_main;
 
 static void
-udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
+udp_connection_register_port (u16 lcl_port, u8 is_ip4)
 {
   udp_main_t *um = &udp_main;
   udp_dst_port_info_t *pi;
@@ -94,24 +94,10 @@ udp_connection_alloc (u32 thread_index)
 {
   udp_main_t *um = &udp_main;
   udp_connection_t *uc;
-  u32 will_expand = 0;
-  pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
-                               CLIB_CACHE_LINE_BYTES);
 
-  if (PREDICT_FALSE (will_expand))
-    {
-      clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
-                                 [thread_index]);
-      pool_get_aligned (udp_main.connections[thread_index], uc,
-                       CLIB_CACHE_LINE_BYTES);
-      clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
-                                   [thread_index]);
-    }
-  else
-    {
-      pool_get_aligned (um->connections[thread_index], uc,
-                       CLIB_CACHE_LINE_BYTES);
-    }
+  pool_get_aligned_safe (um->connections[thread_index], uc,
+                        CLIB_CACHE_LINE_BYTES);
+
   clib_memset (uc, 0, sizeof (*uc));
   uc->c_c_index = uc - um->connections[thread_index];
   uc->c_thread_index = thread_index;
@@ -165,10 +151,9 @@ udp_default_mtu (udp_main_t * um, u8 is_ip4)
 }
 
 static u32
-udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
+udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
 {
   udp_main_t *um = vnet_get_udp_main ();
-  vlib_main_t *vm = vlib_get_main ();
   transport_endpoint_cfg_t *lcl_ext;
   udp_connection_t *listener;
   u16 lcl_port_ho;
@@ -200,7 +185,8 @@ udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
   listener->c_proto = TRANSPORT_PROTO_UDP;
   listener->c_s_index = session_index;
   listener->c_fib_index = lcl->fib_index;
-  listener->mss = udp_default_mtu (um, listener->c_is_ip4);
+  listener->mss =
+    lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
   lcl_ext = (transport_endpoint_cfg_t *) lcl;
   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
@@ -209,7 +195,7 @@ udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
   clib_spinlock_init (&listener->rx_lock);
 
-  udp_connection_register_port (vm, lcl_port_ho, lcl->is_ip4);
+  udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
   return listener->c_c_index;
 }
 
@@ -236,26 +222,54 @@ udp_session_get_listener (u32 listener_index)
   return &us->connection;
 }
 
-static u32
-udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
+always_inline u32
+udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
 {
-  udp_connection_t *uc;
-  vlib_main_t *vm = vlib_get_main ();
-
-  uc = udp_connection_from_transport (tc);
-
   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
-  if (tc->is_ip4)
+  if (uc->c_is_ip4)
     vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
                                 IP_PROTOCOL_UDP, 1 /* csum offload */,
                                 0 /* is_df */, uc->c_dscp);
   else
     vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
                          IP_PROTOCOL_UDP);
-  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
+  vnet_buffer (b)->sw_if_index[VLIB_RX] = uc->sw_if_index;
   vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
 
+  return 0;
+}
+
+static u32
+udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  udp_connection_t *uc;
+
+  uc = udp_connection_from_transport (tc);
+
+  while (n_bufs >= 4)
+    {
+      vlib_prefetch_buffer_header (bs[2], STORE);
+      vlib_prefetch_buffer_header (bs[3], STORE);
+
+      udp_push_one_header (vm, uc, bs[0]);
+      udp_push_one_header (vm, uc, bs[1]);
+
+      n_bufs -= 2;
+      bs += 2;
+    }
+  while (n_bufs)
+    {
+      if (n_bufs > 1)
+       vlib_prefetch_buffer_header (bs[1], STORE);
+
+      udp_push_one_header (vm, uc, bs[0]);
+
+      n_bufs -= 1;
+      bs += 1;
+    }
+
   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
     {
       if (!transport_max_tx_dequeue (&uc->connection))
@@ -281,7 +295,7 @@ udp_session_close (u32 connection_index, u32 thread_index)
   udp_connection_t *uc;
 
   uc = udp_connection_get (connection_index, thread_index);
-  if (!uc)
+  if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
     return;
 
   if (!transport_max_tx_dequeue (&uc->connection))
@@ -323,11 +337,10 @@ udp_session_send_params (transport_connection_t * tconn,
 static int
 udp_open_connection (transport_endpoint_cfg_t * rmt)
 {
-  vlib_main_t *vm = vlib_get_main ();
-  u32 thread_index = vm->thread_index;
   udp_main_t *um = &udp_main;
   ip46_address_t lcl_addr;
   udp_connection_t *uc;
+  u32 thread_index;
   u16 lcl_port;
   int rv;
 
@@ -372,7 +385,7 @@ udp_open_connection (transport_endpoint_cfg_t * rmt)
 
 conn_alloc:
 
-  udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
+  udp_connection_register_port (lcl_port, rmt->is_ip4);
 
   /* We don't poll main thread if we have workers */
   thread_index = transport_cl_thread ();
@@ -387,6 +400,8 @@ conn_alloc:
   uc->c_fib_index = rmt->fib_index;
   uc->c_dscp = rmt->dscp;
   uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
+  if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
+    uc->sw_if_index = rmt->peer.sw_if_index;
   uc->flags |= UDP_CONN_F_OWNS_PORT;
   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
     {
@@ -478,7 +493,6 @@ udp_init (vlib_main_t * vm)
   vlib_thread_main_t *tm = vlib_get_thread_main ();
   u32 num_threads;
   ip_protocol_info_t *pi;
-  int i;
 
   /*
    * Registrations
@@ -503,16 +517,6 @@ udp_init (vlib_main_t * vm)
 
   num_threads = 1 /* main thread */  + tm->n_threads;
   vec_validate (um->connections, num_threads - 1);
-  vec_validate (um->connection_peekers, num_threads - 1);
-  vec_validate (um->peekers_readers_locks, num_threads - 1);
-  vec_validate (um->peekers_write_locks, num_threads - 1);
-
-  if (num_threads > 1)
-    for (i = 0; i < num_threads; i++)
-      {
-       clib_spinlock_init (&um->peekers_readers_locks[i]);
-       clib_spinlock_init (&um->peekers_write_locks[i]);
-      }
 
   um->local_to_input_edge[UDP_IP4] =
     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);