udp: postpone cleanup of udp connections 12/37512/3
authorFlorin Coras <fcoras@cisco.com>
Tue, 25 Oct 2022 01:46:20 +0000 (18:46 -0700)
committerDave Barach <openvpp@barachs.net>
Tue, 25 Oct 2022 17:19:03 +0000 (17:19 +0000)
Avoid deleting connections in session layer io event handler.

Type: improvement

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I87b3e53f9039161688467d9716875583ad797c07

src/vnet/udp/udp.c
src/vnet/udp/udp.h

index 7851da8..5e4b29f 100644 (file)
@@ -132,6 +132,38 @@ udp_connection_delete (udp_connection_t * uc)
   udp_connection_cleanup (uc);
 }
 
+static void
+udp_handle_cleanups (void *args)
+{
+  u32 thread_index = (u32) pointer_to_uword (args);
+  udp_connection_t *uc;
+  udp_worker_t *wrk;
+  u32 *uc_index;
+
+  wrk = udp_worker_get (thread_index);
+  vec_foreach (uc_index, wrk->pending_cleanups)
+    {
+      uc = udp_connection_get (*uc_index, thread_index);
+      udp_connection_delete (uc);
+    }
+  vec_reset_length (wrk->pending_cleanups);
+}
+
+static void
+udp_connection_program_cleanup (udp_connection_t *uc)
+{
+  uword thread_index = uc->c_thread_index;
+  udp_worker_t *wrk;
+
+  wrk = udp_worker_get (uc->c_thread_index);
+  vec_add1 (wrk->pending_cleanups, uc->c_c_index);
+
+  if (vec_len (wrk->pending_cleanups) == 1)
+    session_send_rpc_evt_to_thread_force (
+      thread_index, udp_handle_cleanups,
+      uword_to_pointer (thread_index, void *));
+}
+
 static u8
 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
 {
@@ -269,7 +301,7 @@ udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
     {
       if (!transport_max_tx_dequeue (&uc->connection))
-       udp_connection_delete (uc);
+       udp_connection_program_cleanup (uc);
     }
 
   return 0;
@@ -295,7 +327,7 @@ udp_session_close (u32 connection_index, u32 thread_index)
     return;
 
   if (!transport_max_tx_dequeue (&uc->connection))
-    udp_connection_delete (uc);
+    udp_connection_program_cleanup (uc);
   else
     uc->flags |= UDP_CONN_F_CLOSING;
 }
@@ -517,6 +549,7 @@ udp_init (vlib_main_t * vm)
 
   num_threads = 1 /* main thread */  + tm->n_threads;
   vec_validate (um->connections, num_threads - 1);
+  vec_validate (um->wrk, num_threads - 1);
 
   um->local_to_input_edge[UDP_IP4] =
     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
index 94362ad..ee71e36 100644 (file)
@@ -117,6 +117,11 @@ typedef enum
   N_UDP_AF,
 } udp_af_t;
 
+typedef struct udp_worker_
+{
+  u32 *pending_cleanups;
+} udp_worker_t;
+
 typedef struct
 {
   udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
@@ -138,7 +143,9 @@ typedef struct
   /*
    * Per-worker thread udp connection pools used with session layer
    */
+
   udp_connection_t **connections;
+  udp_worker_t *wrk;
   udp_connection_t *listener_pool;
 
   u16 default_mtu;
@@ -185,6 +192,12 @@ udp_connection_from_transport (transport_connection_t * tc)
   return ((udp_connection_t *) tc);
 }
 
+always_inline udp_worker_t *
+udp_worker_get (u32 thread_index)
+{
+  return vec_elt_at_index (udp_main.wrk, thread_index);
+}
+
 void udp_connection_free (udp_connection_t * uc);
 udp_connection_t *udp_connection_alloc (u32 thread_index);