udp: refactor udp code
[vpp.git] / src / vnet / udp / udp_input.c
index 5d3a185..8170cfb 100644 (file)
 #include <vlibmemory/api.h>
 #include "../session/application_interface.h"
 
-vlib_node_registration_t udp4_uri_input_node;
+static char *udp_error_strings[] = {
+#define udp_error(n,s) s,
+#include "udp_error.def"
+#undef udp_error
+};
 
 typedef struct
 {
-  u32 session;
+  u32 connection;
   u32 disposition;
   u32 thread_index;
-} udp4_uri_input_trace_t;
+} udp_input_trace_t;
 
 /* packet trace format function */
 static u8 *
-format_udp4_uri_input_trace (u8 * s, va_list * args)
+format_udp_input_trace (u8 * s, va_list * args)
 {
   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
-  udp4_uri_input_trace_t *t = va_arg (*args, udp4_uri_input_trace_t *);
+  udp_input_trace_t *t = va_arg (*args, udp_input_trace_t *);
 
-  s = format (s, "UDP4_URI_INPUT: session %d, disposition %d, thread %d",
-             t->session, t->disposition, t->thread_index);
+  s = format (s, "UDP_INPUT: connection %d, disposition %d, thread %d",
+             t->connection, t->disposition, t->thread_index);
   return s;
 }
 
+#define foreach_udp_input_next                 \
+  _ (DROP, "error-drop")
+
 typedef enum
 {
-  UDP4_URI_INPUT_NEXT_DROP,
-  UDP4_URI_INPUT_N_NEXT,
-} udp4_uri_input_next_t;
-
-static char *udp4_uri_input_error_strings[] = {
-#define _(sym,string) string,
-  foreach_session_input_error
+#define _(s, n) UDP_INPUT_NEXT_##s,
+  foreach_udp_input_next
 #undef _
-};
+    UDP_INPUT_N_NEXT,
+} udp_input_next_t;
 
-static uword
-udp4_uri_input_node_fn (vlib_main_t * vm,
-                       vlib_node_runtime_t * node, vlib_frame_t * frame)
+always_inline void
+udp_input_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
+{
+  if (PREDICT_TRUE (!val))
+    return;
+
+  if (is_ip4)
+    vlib_node_increment_counter (vm, udp4_input_node.index, evt, val);
+  else
+    vlib_node_increment_counter (vm, udp6_input_node.index, evt, val);
+}
+
+always_inline uword
+udp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
+                   vlib_frame_t * frame, u8 is_ip4)
 {
   u32 n_left_from, *from, *to_next;
-  udp4_uri_input_next_t next_index;
-  udp_uri_main_t *um = vnet_get_udp_main ();
-  session_manager_main_t *smm = vnet_get_session_manager_main ();
+  u32 next_index, errors;
   u32 my_thread_index = vm->thread_index;
-  u8 my_enqueue_epoch;
-  u32 *session_indices_to_enqueue;
-  static u32 serial_number;
-  int i;
-
-  my_enqueue_epoch = ++smm->current_enqueue_epoch[my_thread_index];
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -90,16 +97,18 @@ udp4_uri_input_node_fn (vlib_main_t * vm,
 
       while (n_left_from > 0 && n_left_to_next > 0)
        {
-         u32 bi0;
+         u32 bi0, fib_index0;
          vlib_buffer_t *b0;
-         u32 next0 = UDP4_URI_INPUT_NEXT_DROP;
-         u32 error0 = SESSION_ERROR_ENQUEUED;
+         u32 next0 = UDP_INPUT_NEXT_DROP;
+         u32 error0 = UDP_ERROR_ENQUEUED;
          udp_header_t *udp0;
-         ip4_header_t *ip0;
-         stream_session_t *s0;
-         svm_fifo_t *f0;
-         u16 udp_len0;
+         ip4_header_t *ip40;
+         ip6_header_t *ip60;
          u8 *data0;
+         stream_session_t *s0;
+         transport_connection_t *tc0 = 0;
+         udp_connection_t *child0, *new_uc0;
+         int written0;
 
          /* speculatively enqueue b0 to the current next frame */
          bi0 = from[0];
@@ -112,89 +121,97 @@ udp4_uri_input_node_fn (vlib_main_t * vm,
          b0 = vlib_get_buffer (vm, bi0);
 
          /* udp_local hands us a pointer to the udp data */
-
          data0 = vlib_buffer_get_current (b0);
          udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
+         fib_index0 = vnet_buffer (b0)->ip.fib_index;
 
-         /* $$$$ fixme: udp_local doesn't do ip options correctly anyhow */
-         ip0 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip0));
-         s0 = 0;
-
-         /* lookup session */
-         s0 = session_lookup4 (0, &ip0->dst_address, &ip0->src_address,
-                               udp0->dst_port, udp0->src_port,
-                               TRANSPORT_PROTO_UDP);
+         if (is_ip4)
+           {
+             /* $$$$ fixme: udp_local doesn't do ip options correctly anyhow */
+             ip40 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip40));
+             s0 = session_lookup_safe4 (fib_index0, &ip40->dst_address,
+                                        &ip40->src_address, udp0->dst_port,
+                                        udp0->src_port, TRANSPORT_PROTO_UDP);
+           }
+         else
+           {
+             ip60 = (ip6_header_t *) (((u8 *) udp0) - sizeof (*ip60));
+             s0 = session_lookup_safe6 (fib_index0, &ip60->dst_address,
+                                        &ip60->src_address, udp0->dst_port,
+                                        udp0->src_port, TRANSPORT_PROTO_UDP);
+           }
 
-         /* no listener */
          if (PREDICT_FALSE (s0 == 0))
            {
-             error0 = SESSION_ERROR_NO_LISTENER;
+             error0 = UDP_ERROR_NO_LISTENER;
              goto trace0;
            }
 
-         f0 = s0->server_rx_fifo;
-
-         /* established hit */
          if (PREDICT_TRUE (s0->session_state == SESSION_STATE_READY))
            {
-             udp_len0 = clib_net_to_host_u16 (udp0->length);
-
-             if (PREDICT_FALSE (udp_len0 > svm_fifo_max_enqueue (f0)))
-               {
-                 error0 = SESSION_ERROR_FIFO_FULL;
-                 goto trace0;
-               }
-
-             svm_fifo_enqueue_nowait (f0, udp_len0 - sizeof (*udp0),
-                                      (u8 *) (udp0 + 1));
-
-             b0->error = node->errors[SESSION_ERROR_ENQUEUED];
-
-             /* We need to send an RX event on this fifo */
-             if (s0->enqueue_epoch != my_enqueue_epoch)
-               {
-                 s0->enqueue_epoch = my_enqueue_epoch;
-
-                 vec_add1 (smm->session_indices_to_enqueue_by_thread
-                           [my_thread_index],
-                           s0 - smm->sessions[my_thread_index]);
-               }
+             tc0 = session_get_transport (s0);
            }
-         /* listener hit */
-         else if (s0->session_state == SESSION_STATE_LISTENING)
+         else if (s0->session_state == SESSION_STATE_CONNECTING_READY)
            {
-             udp_connection_t *us;
-             int rv;
-
-             error0 = SESSION_ERROR_NOT_READY;
-
              /*
-              * create udp transport session
+              * Clone the transport. It will be cleaned up with the
+              * session once we notify the session layer.
               */
-             pool_get (um->udp_sessions[my_thread_index], us);
-
-             us->mtu = 1024;   /* $$$$ policy */
-
-             us->c_lcl_ip4.as_u32 = ip0->dst_address.as_u32;
-             us->c_rmt_ip4.as_u32 = ip0->src_address.as_u32;
-             us->c_lcl_port = udp0->dst_port;
-             us->c_rmt_port = udp0->src_port;
-             us->c_transport_proto = TRANSPORT_PROTO_UDP;
-             us->c_c_index = us - um->udp_sessions[my_thread_index];
+             new_uc0 = udp_conenction_clone_safe (s0->connection_index,
+                                                  s0->thread_index);
+             ASSERT (s0->session_index == new_uc0->c_s_index);
 
              /*
-              * create stream session and attach the udp session to it
+              * Drop the 'lock' on pool resize
               */
-             rv = stream_session_accept (&us->connection, s0->session_index,
-                                         1 /*notify */ );
-             if (rv)
-               error0 = rv;
+             session_pool_remove_peeker (s0->thread_index);
+             session_dgram_connect_notify (&new_uc0->connection,
+                                           s0->thread_index, &s0);
+             tc0 = &new_uc0->connection;
+           }
+         else if (s0->session_state == SESSION_STATE_LISTENING)
+           {
+             tc0 = listen_session_get_transport (s0);
+
+             child0 = udp_connection_alloc (my_thread_index);
+             if (is_ip4)
+               {
+                 ip_set (&child0->c_lcl_ip, &ip40->dst_address, 1);
+                 ip_set (&child0->c_rmt_ip, &ip40->src_address, 1);
+               }
+             else
+               {
+                 ip_set (&child0->c_lcl_ip, &ip60->dst_address, 0);
+                 ip_set (&child0->c_rmt_ip, &ip60->src_address, 0);
+               }
+             child0->c_lcl_port = udp0->dst_port;
+             child0->c_rmt_port = udp0->src_port;
+             child0->c_is_ip4 = is_ip4;
+             child0->mtu = 1460;       /* $$$$ policy */
+
+             if (stream_session_accept
+                 (&child0->connection, tc0->s_index, 1))
+               {
+                 error0 = UDP_ERROR_CREATE_SESSION;
+                 goto trace0;
+               }
+             s0 = session_get (child0->c_s_index, child0->c_thread_index);
+             s0->session_state = SESSION_STATE_READY;
+             tc0 = &child0->connection;
 
+             error0 = UDP_ERROR_LISTENER;
            }
          else
            {
+             error0 = UDP_ERROR_NOT_READY;
+             goto trace0;
+           }
 
-             error0 = SESSION_ERROR_NOT_READY;
+         written0 = session_enqueue_dgram_connection (s0, b0, tc0->proto,
+                                                      1 /* queue evt */ );
+         if (PREDICT_FALSE (written0 < 0))
+           {
+             error0 = UDP_ERROR_FIFO_FULL;
              goto trace0;
            }
 
@@ -204,17 +221,14 @@ udp4_uri_input_node_fn (vlib_main_t * vm,
          if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
            {
-             udp4_uri_input_trace_t *t =
-               vlib_add_trace (vm, node, b0, sizeof (*t));
+             udp_input_trace_t *t = vlib_add_trace (vm, node, b0,
+                                                    sizeof (*t));
 
-             t->session = ~0;
-             if (s0)
-               t->session = s0 - smm->sessions[my_thread_index];
+             t->connection = tc0 ? tc0->c_index : ~0;
              t->disposition = error0;
              t->thread_index = my_thread_index;
            }
 
-         /* verify speculative enqueue, maybe switch current next frame */
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
                                           to_next, n_left_to_next,
                                           bi0, next0);
@@ -223,94 +237,66 @@ udp4_uri_input_node_fn (vlib_main_t * vm,
       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
     }
 
-  /* Send enqueue events */
-
-  session_indices_to_enqueue =
-    smm->session_indices_to_enqueue_by_thread[my_thread_index];
-
-  for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
-    {
-      session_fifo_event_t evt;
-      unix_shared_memory_queue_t *q;
-      stream_session_t *s0;
-      application_t *server0;
-
-      /* Get session */
-      s0 = pool_elt_at_index (smm->sessions[my_thread_index],
-                             session_indices_to_enqueue[i]);
-
-      /* Get session's server */
-      server0 = application_get (s0->app_index);
-
-      /* Built-in server? Deliver the goods... */
-      if (server0->cb_fns.builtin_server_rx_callback)
-       {
-         server0->cb_fns.builtin_server_rx_callback (s0);
-         continue;
-       }
-
-      if (svm_fifo_set_event (s0->server_rx_fifo))
-       {
-         /* Fabricate event */
-         evt.fifo = s0->server_rx_fifo;
-         evt.event_type = FIFO_EVENT_APP_RX;
-         evt.event_id = serial_number++;
-
-         /* Add event to server's event queue */
-         q = server0->event_queue;
-
-         /* Don't block for lack of space */
-         if (PREDICT_TRUE (q->cursize < q->maxsize))
-           {
-             unix_shared_memory_queue_add (server0->event_queue,
-                                           (u8 *) & evt,
-                                           0 /* do wait for mutex */ );
-           }
-         else
-           {
-             vlib_node_increment_counter (vm, udp4_uri_input_node.index,
-                                          SESSION_ERROR_FIFO_FULL, 1);
-           }
-       }
-      /* *INDENT-OFF* */
-      if (1)
-       {
-         ELOG_TYPE_DECLARE (e) =
-         {
-             .format = "evt-enqueue: id %d length %d",
-             .format_args = "i4i4",};
-         struct
-         {
-           u32 data[2];
-         } *ed;
-         ed = ELOG_DATA (&vlib_global_main.elog_main, e);
-         ed->data[0] = evt.event_id;
-         ed->data[1] = svm_fifo_max_dequeue (s0->server_rx_fifo);
-       }
-      /* *INDENT-ON* */
+  errors = session_manager_flush_enqueue_events (TRANSPORT_PROTO_UDP,
+                                                my_thread_index);
+  udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
+  return frame->n_vectors;
+}
 
-    }
+vlib_node_registration_t udp4_input_node;
+vlib_node_registration_t udp6_input_node;
 
-  vec_reset_length (session_indices_to_enqueue);
+static uword
+udp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
+           vlib_frame_t * frame)
+{
+  return udp46_input_inline (vm, node, frame, 1);
+}
 
-  smm->session_indices_to_enqueue_by_thread[my_thread_index] =
-    session_indices_to_enqueue;
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (udp4_input_node) =
+{
+  .function = udp4_input,
+  .name = "udp4-input",
+  .vector_size = sizeof (u32),
+  .format_trace = format_udp_input_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  .n_errors = ARRAY_LEN (udp_error_strings),
+  .error_strings = udp_error_strings,
+  .n_next_nodes = UDP_INPUT_N_NEXT,
+  .next_nodes = {
+#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
+      foreach_udp_input_next
+#undef _
+  },
+};
+/* *INDENT-ON* */
 
-  return frame->n_vectors;
+static uword
+udp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
+           vlib_frame_t * frame)
+{
+  return udp46_input_inline (vm, node, frame, 0);
 }
 
-VLIB_REGISTER_NODE (udp4_uri_input_node) =
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (udp6_input_node) =
 {
-  .function = udp4_uri_input_node_fn,.name = "udp4-uri-input",.vector_size =
-    sizeof (u32),.format_trace = format_udp4_uri_input_trace,.type =
-    VLIB_NODE_TYPE_INTERNAL,.n_errors =
-    ARRAY_LEN (udp4_uri_input_error_strings),.error_strings =
-    udp4_uri_input_error_strings,.n_next_nodes = UDP4_URI_INPUT_N_NEXT,
-    /* edit / add dispositions here */
-    .next_nodes =
-  {
-  [UDP4_URI_INPUT_NEXT_DROP] = "error-drop",}
-,};
+  .function = udp6_input,
+  .name = "udp6-input",
+  .vector_size = sizeof (u32),
+  .format_trace = format_udp_input_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  .n_errors = ARRAY_LEN (udp_error_strings),
+  .error_strings = udp_error_strings,
+  .n_next_nodes = UDP_INPUT_N_NEXT,
+  .next_nodes = {
+#define _(s, n) [UDP_INPUT_NEXT_##s] = n,
+      foreach_udp_input_next
+#undef _
+  },
+};
+/* *INDENT-ON* */
 
 /*
  * fd.io coding-style-patch-verification: ON