host stack: update stale copyright
[vpp.git] / src / vnet / udp / udp_input.c
index 810278e..db634cb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2016-2019 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  * limitations under the License.
  */
 
+#include <vlibmemory/api.h>
 #include <vlib/vlib.h>
-#include <vnet/vnet.h>
-#include <vnet/pg/pg.h>
-#include <vnet/ip/ip.h>
 
-#include <vnet/udp/udp.h>
 #include <vppinfra/hash.h>
 #include <vppinfra/error.h>
 #include <vppinfra/elog.h>
 
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <vnet/ip/ip.h>
+#include <vnet/udp/udp.h>
 #include <vnet/udp/udp_packet.h>
+#include <vnet/session/session.h>
 
-#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 +96,20 @@ 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;
+         session_t *s0;
+         udp_connection_t *uc0, *child0, *new_uc0;
+         transport_connection_t *tc0;
+         int wrote0;
+         void *rmt_addr, *lcl_addr;
+         session_dgram_hdr_t hdr0;
 
          /* speculatively enqueue b0 to the current next frame */
          bi0 = from[0];
@@ -112,111 +122,165 @@ 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;
+         if (is_ip4)
+           {
+             /* TODO: must fix once udp_local does ip options correctly */
+             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);
+             lcl_addr = &ip40->dst_address;
+             rmt_addr = &ip40->src_address;
 
-         /* lookup session */
-         s0 = stream_session_lookup4 (&ip0->dst_address, &ip0->src_address,
-                                      udp0->dst_port, udp0->src_port,
-                                      SESSION_TYPE_IP4_UDP, my_thread_index);
+           }
+         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);
+             lcl_addr = &ip60->dst_address;
+             rmt_addr = &ip60->src_address;
+           }
 
-         /* no listener */
-         if (PREDICT_FALSE (s0 == 0))
+         if (PREDICT_FALSE (!s0))
            {
-             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))
+         if (s0->session_state == SESSION_STATE_OPENED)
            {
-             udp_len0 = clib_net_to_host_u16 (udp0->length);
-
-             if (PREDICT_FALSE (udp_len0 > svm_fifo_max_enqueue (f0)))
+             /* TODO optimization: move cl session to right thread
+              * However, since such a move would affect the session handle,
+              * which we pass 'raw' to the app, we'd also have notify the
+              * app of the change or change the way we pass handles to apps.
+              */
+             tc0 = session_get_transport (s0);
+             uc0 = udp_get_connection_from_transport (tc0);
+             if (uc0->is_connected)
                {
-                 error0 = SESSION_ERROR_FIFO_FULL;
-                 goto trace0;
+                 /*
+                  * Clone the transport. It will be cleaned up with the
+                  * session once we notify the session layer.
+                  */
+                 new_uc0 = udp_connection_clone_safe (s0->connection_index,
+                                                      s0->thread_index);
+                 ASSERT (s0->session_index == new_uc0->c_s_index);
+
+                 /*
+                  * Drop the 'lock' on pool resize
+                  */
+                 session_pool_remove_peeker (s0->thread_index);
+                 session_dgram_connect_notify (&new_uc0->connection,
+                                               s0->thread_index, &s0);
+                 tc0 = &new_uc0->connection;
                }
-
-             svm_fifo_enqueue_nowait (f0, 0 /* pid */ ,
-                                      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)
+           }
+         else if (s0->session_state == SESSION_STATE_READY)
+           {
+             tc0 = session_get_transport (s0);
+             uc0 = udp_get_connection_from_transport (tc0);
+           }
+         else if (s0->session_state == SESSION_STATE_LISTENING)
+           {
+             tc0 = listen_session_get_transport (s0);
+             uc0 = udp_get_connection_from_transport (tc0);
+             if (uc0->is_connected)
                {
-                 s0->enqueue_epoch = my_enqueue_epoch;
-
-                 vec_add1 (smm->session_indices_to_enqueue_by_thread
-                           [my_thread_index],
-                           s0 - smm->sessions[my_thread_index]);
+                 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;
+
+                 if (session_stream_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;
+                 uc0 = udp_get_connection_from_transport (tc0);
+                 error0 = UDP_ERROR_LISTENER;
                }
            }
-         /* listener hit */
-         else if (s0->session_state == SESSION_STATE_LISTENING)
+         else
            {
-             udp_connection_t *us;
-             int rv;
-
-             error0 = SESSION_ERROR_NOT_READY;
-
-             /*
-              * create udp transport session
-              */
-             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_proto = SESSION_TYPE_IP4_UDP;
-             us->c_c_index = us - um->udp_sessions[my_thread_index];
-
-             /*
-              * create stream session and attach the udp session to it
-              */
-             rv = stream_session_accept (&us->connection, s0->session_index,
-                                         SESSION_TYPE_IP4_UDP,
-                                         1 /*notify */ );
-             if (rv)
-               error0 = rv;
+             error0 = UDP_ERROR_NOT_READY;
+             goto trace0;
+           }
 
+         if (!uc0->is_connected)
+           {
+             if (svm_fifo_max_enqueue (s0->rx_fifo)
+                 < b0->current_length + sizeof (session_dgram_hdr_t))
+               {
+                 error0 = UDP_ERROR_FIFO_FULL;
+                 goto trace0;
+               }
+             hdr0.data_length = b0->current_length;
+             hdr0.data_offset = 0;
+             ip_set (&hdr0.lcl_ip, lcl_addr, is_ip4);
+             ip_set (&hdr0.rmt_ip, rmt_addr, is_ip4);
+             hdr0.lcl_port = udp0->dst_port;
+             hdr0.rmt_port = udp0->src_port;
+             hdr0.is_ip4 = is_ip4;
+
+             clib_spinlock_lock (&uc0->rx_lock);
+             wrote0 = session_enqueue_dgram_connection (s0, &hdr0, b0,
+                                                        TRANSPORT_PROTO_UDP,
+                                                        1 /* queue evt */ );
+             clib_spinlock_unlock (&uc0->rx_lock);
+             ASSERT (wrote0 > 0);
+
+             if (s0->session_state != SESSION_STATE_LISTENING)
+               session_pool_remove_peeker (s0->thread_index);
            }
          else
            {
-
-             error0 = SESSION_ERROR_NOT_READY;
-             goto trace0;
+             if (svm_fifo_max_enqueue (s0->rx_fifo) < b0->current_length)
+               {
+                 error0 = UDP_ERROR_FIFO_FULL;
+                 goto trace0;
+               }
+             wrote0 = session_enqueue_stream_connection (tc0, b0, 0,
+                                                         1 /* queue evt */ ,
+                                                         1 /* in order */ );
+             ASSERT (wrote0 > 0);
            }
 
        trace0:
+
          b0->error = node->errors[error0];
 
          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 = s0 ? s0->connection_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);
@@ -225,94 +289,63 @@ 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_SERVER_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_main_flush_all_enqueue_events (TRANSPORT_PROTO_UDP);
+  udp_input_inc_counter (vm, is_ip4, UDP_ERROR_EVENT_FIFO_FULL, errors);
+  return frame->n_vectors;
+}
 
-    }
 
-  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