svm: minimal initial fifo
[vpp.git] / src / vnet / session / application_local.c
index b5cac56..cc02026 100644 (file)
 #include <vnet/session/application_local.h>
 #include <vnet/session/session.h>
 
-ct_connection_t *connections;
+static ct_connection_t *connections;
 
-ct_connection_t *
+static void
+ct_enable_disable_main_pre_input_node (u8 is_add)
+{
+  u32 n_conns;
+
+  if (!vlib_num_workers ())
+    return;
+
+  n_conns = pool_elts (connections);
+  if (n_conns > 2)
+    return;
+
+  if (n_conns > 0 && is_add)
+    vlib_node_set_state (vlib_get_main (),
+                        session_queue_pre_input_node.index,
+                        VLIB_NODE_STATE_POLLING);
+  else if (n_conns == 0)
+    vlib_node_set_state (vlib_get_main (),
+                        session_queue_pre_input_node.index,
+                        VLIB_NODE_STATE_DISABLED);
+}
+
+static ct_connection_t *
 ct_connection_alloc (void)
 {
   ct_connection_t *ct;
@@ -31,7 +53,7 @@ ct_connection_alloc (void)
   return ct;
 }
 
-ct_connection_t *
+static ct_connection_t *
 ct_connection_get (u32 ct_index)
 {
   if (pool_is_free_index (connections, ct_index))
@@ -39,7 +61,7 @@ ct_connection_get (u32 ct_index)
   return pool_elt_at_index (connections, ct_index);
 }
 
-void
+static void
 ct_connection_free (ct_connection_t * ct)
 {
   if (CLIB_DEBUG)
@@ -69,10 +91,10 @@ ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
 int
 ct_session_connect_notify (session_t * ss)
 {
-  svm_fifo_segment_private_t *seg;
   ct_connection_t *sct, *cct;
   app_worker_t *client_wrk;
   segment_manager_t *sm;
+  fifo_segment_t *seg;
   u64 segment_handle;
   int is_fail = 0;
   session_t *cs;
@@ -106,12 +128,17 @@ ct_session_connect_notify (session_t * ss)
   ss = session_get (ss_index, 0);
   cs->session_type = ss->session_type;
   cs->connection_index = sct->c_c_index;
-  cs->listener_index = SESSION_INVALID_INDEX;
+  cs->listener_handle = SESSION_INVALID_HANDLE;
   cs->session_state = SESSION_STATE_CONNECTING;
   cs->app_wrk_index = client_wrk->wrk_index;
   cs->connection_index = cct->c_c_index;
 
   cct->c_s_index = cs->session_index;
+  cct->client_rx_fifo = ss->tx_fifo;
+  cct->client_tx_fifo = ss->rx_fifo;
+
+  cct->client_rx_fifo->refcnt++;
+  cct->client_tx_fifo->refcnt++;
 
   /* This will allocate fifos for the session. They won't be used for
    * exchanging data but they will be used to close the connection if
@@ -135,47 +162,29 @@ ct_session_connect_notify (session_t * ss)
   return 0;
 }
 
-static void
-ct_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
-{
-  int fd;
-
-  /*
-   * segment manager initializes only the producer eventds, since vpp is
-   * typically the producer. But for local sessions, we also pass to the
-   * apps the mqs they listen on for events from peer apps, so they are also
-   * consumer fds.
-   */
-  fd = svm_msg_q_get_producer_eventfd (sq);
-  svm_msg_q_set_consumer_eventfd (sq, fd);
-  fd = svm_msg_q_get_producer_eventfd (cq);
-  svm_msg_q_set_consumer_eventfd (cq, fd);
-}
-
-int
+static int
 ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
                       ct_connection_t * ct, session_t * ls, session_t * ll)
 {
-  u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
-  u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index;
-  segment_manager_properties_t *props, *cprops;
-  svm_fifo_segment_private_t *seg;
-  application_t *server, *client;
+  u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
+  segment_manager_props_t *props;
+  application_t *server;
   segment_manager_t *sm;
-  svm_msg_q_t *sq, *cq;
+  u32 margin = 16 << 10;
+  fifo_segment_t *seg;
   u64 segment_handle;
   int seg_index, rv;
 
   server = application_get (server_wrk->app_index);
-  client = application_get (client_wrk->app_index);
 
   props = application_segment_manager_properties (server);
-  cprops = application_segment_manager_properties (client);
-  evt_q_elts = props->evt_q_size + cprops->evt_q_size;
-  evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
   round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
-  seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
+  /* Increase size because of inefficient chunk allocations. Depending on
+   * how data is consumed, it may happen that more chunks than needed are
+   * allocated.
+   * TODO should remove once allocations are done more efficiently */
+  seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
 
   sm = app_worker_get_listen_segment_manager (server_wrk, ll);
   seg_index = segment_manager_add_segment (sm, seg_size);
@@ -185,15 +194,9 @@ ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
       return seg_index;
     }
   seg = segment_manager_get_segment_w_lock (sm, seg_index);
-  sq = segment_manager_alloc_queue (seg, props);
-  cq = segment_manager_alloc_queue (seg, cprops);
 
-  if (props->use_mq_eventfd)
-    ct_session_fix_eventds (sq, cq);
-
-  ct->server_evt_q = pointer_to_uword (sq);
-  ct->client_evt_q = pointer_to_uword (cq);
-  rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
+  rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
+                                       props->rx_fifo_size,
                                        props->tx_fifo_size, &ls->rx_fifo,
                                        &ls->tx_fifo);
   if (rv)
@@ -204,13 +207,12 @@ ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
     }
 
   sm_index = segment_manager_index (sm);
-  ls->rx_fifo->ct_session_index = ls->session_index;
-  ls->tx_fifo->ct_session_index = ls->session_index;
+  ls->rx_fifo->master_session_index = ls->session_index;
+  ls->tx_fifo->master_session_index = ls->session_index;
   ls->rx_fifo->segment_manager = sm_index;
   ls->tx_fifo->segment_manager = sm_index;
   ls->rx_fifo->segment_index = seg_index;
   ls->tx_fifo->segment_index = seg_index;
-  ls->svm_segment_index = seg_index;
 
   segment_handle = segment_manager_segment_handle (sm, seg);
   if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
@@ -229,7 +231,7 @@ failed:
   return rv;
 }
 
-int
+static int
 ct_connect (app_worker_t * client_wrk, session_t * ll,
            session_endpoint_cfg_t * sep)
 {
@@ -256,6 +258,7 @@ ct_connect (app_worker_t * client_wrk, session_t * ll,
   cct->c_is_ip4 = sep->is_ip4;
   clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
   cct->actual_tp = ll_ct->actual_tp;
+  cct->is_client = 1;
 
   /*
    * Init server transport
@@ -279,9 +282,10 @@ ct_connect (app_worker_t * client_wrk, session_t * ll,
    */
   ss = session_alloc (0);
   ll = listen_session_get (ll_index);
-  ss->session_type = ll->session_type;
+  ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
+                                                    sct->c_is_ip4);
   ss->connection_index = sct->c_c_index;
-  ss->listener_index = ll->session_index;
+  ss->listener_handle = listen_session_get_handle (ll);
   ss->session_state = SESSION_STATE_CREATED;
 
   server_wrk = application_listener_select_worker (ll);
@@ -307,14 +311,12 @@ ct_connect (app_worker_t * client_wrk, session_t * ll,
       return -1;
     }
 
-  cct->client_evt_q = sct->client_evt_q;
-  cct->server_evt_q = sct->server_evt_q;
   cct->segment_handle = sct->segment_handle;
-
+  ct_enable_disable_main_pre_input_node (1 /* is_add */ );
   return 0;
 }
 
-u32
+static u32
 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
 {
   session_endpoint_cfg_t *sep;
@@ -327,25 +329,27 @@ ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
   clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
   ct->c_lcl_port = sep->port;
   ct->actual_tp = sep->transport_proto;
+  ct_enable_disable_main_pre_input_node (1 /* is_add */ );
   return ct->c_c_index;
 }
 
-u32
+static u32
 ct_stop_listen (u32 ct_index)
 {
   ct_connection_t *ct;
   ct = ct_connection_get (ct_index);
   ct_connection_free (ct);
+  ct_enable_disable_main_pre_input_node (0 /* is_add */ );
   return 0;
 }
 
-transport_connection_t *
+static transport_connection_t *
 ct_listener_get (u32 ct_index)
 {
   return (transport_connection_t *) ct_connection_get (ct_index);
 }
 
-int
+static int
 ct_session_connect (transport_endpoint_cfg_t * tep)
 {
   session_endpoint_cfg_t *sep_ext;
@@ -398,8 +402,8 @@ global_scope:
     return VNET_API_ERROR_APP_CONNECT_SCOPE;
 
   fib_proto = session_endpoint_fib_proto (sep);
-  table_index = application_session_table (app, fib_proto);
-  ll = session_lookup_listener (table_index, sep);
+  table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
+  ll = session_lookup_listener_wildcard (table_index, sep);
 
   if (ll)
     return ct_connect (app_wrk, ll, sep_ext);
@@ -408,10 +412,11 @@ global_scope:
   return 1;
 }
 
-void
+static void
 ct_session_close (u32 ct_index, u32 thread_index)
 {
   ct_connection_t *ct, *peer_ct;
+  app_worker_t *app_wrk;
   session_t *s;
 
   ct = ct_connection_get (ct_index);
@@ -423,13 +428,18 @@ ct_session_close (u32 ct_index, u32 thread_index)
     }
 
   s = session_get (ct->c_s_index, 0);
-  app_worker_del_segment_notify (app_worker_get (s->app_wrk_index),
-                                ct->segment_handle);
+  app_wrk = app_worker_get_if_valid (s->app_wrk_index);
+  if (app_wrk)
+    app_worker_del_segment_notify (app_wrk, ct->segment_handle);
   session_free_w_fifos (s);
+  if (ct->is_client)
+    segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
+
   ct_connection_free (ct);
+  ct_enable_disable_main_pre_input_node (0 /* is_add */ );
 }
 
-transport_connection_t *
+static transport_connection_t *
 ct_session_get (u32 ct_index, u32 thread_index)
 {
   return (transport_connection_t *) ct_connection_get (ct_index);
@@ -461,10 +471,33 @@ format_ct_connection_id (u8 * s, va_list * args)
   return s;
 }
 
-u8 *
+static int
+ct_custom_tx (void *session, u32 max_burst_size)
+{
+  session_t *s = (session_t *) session;
+  if (session_has_transport (s))
+    return 0;
+  return ct_session_tx (s);
+}
+
+static int
+ct_app_rx_evt (transport_connection_t * tc)
+{
+  ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
+  session_t *ps;
+
+  peer_ct = ct_connection_get (ct->peer_index);
+  if (!peer_ct)
+    return -1;
+  ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
+  return session_dequeue_notify (ps);
+}
+
+static u8 *
 format_ct_listener (u8 * s, va_list * args)
 {
   u32 tc_index = va_arg (*args, u32);
+  u32 __clib_unused thread_index = va_arg (*args, u32);
   u32 __clib_unused verbose = va_arg (*args, u32);
   ct_connection_t *ct = ct_connection_get (tc_index);
   s = format (s, "%-50U", format_ct_connection_id, ct);
@@ -473,7 +506,7 @@ format_ct_listener (u8 * s, va_list * args)
   return s;
 }
 
-u8 *
+static u8 *
 format_ct_connection (u8 * s, va_list * args)
 {
   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
@@ -493,7 +526,7 @@ format_ct_connection (u8 * s, va_list * args)
   return s;
 }
 
-u8 *
+static u8 *
 format_ct_session (u8 * s, va_list * args)
 {
   u32 ct_index = va_arg (*args, u32);
@@ -513,25 +546,47 @@ format_ct_session (u8 * s, va_list * args)
 }
 
 /* *INDENT-OFF* */
-const static transport_proto_vft_t cut_thru_proto = {
+static const transport_proto_vft_t cut_thru_proto = {
   .start_listen = ct_start_listen,
   .stop_listen = ct_stop_listen,
   .get_listener = ct_listener_get,
   .connect = ct_session_connect,
   .close = ct_session_close,
   .get_connection = ct_session_get,
-  .tx_type = TRANSPORT_TX_INTERNAL,
-  .service_type = TRANSPORT_SERVICE_APP,
+  .custom_tx = ct_custom_tx,
+  .app_rx_evt = ct_app_rx_evt,
   .format_listener = format_ct_listener,
   .format_connection = format_ct_session,
+  .transport_options = {
+    .tx_type = TRANSPORT_TX_INTERNAL,
+    .service_type = TRANSPORT_SERVICE_APP,
+  },
 };
 /* *INDENT-ON* */
 
+int
+ct_session_tx (session_t * s)
+{
+  ct_connection_t *ct, *peer_ct;
+  session_t *peer_s;
+
+  ct = (ct_connection_t *) session_get_transport (s);
+  peer_ct = ct_connection_get (ct->peer_index);
+  if (!peer_ct)
+    return -1;
+  peer_s = session_get (peer_ct->c_s_index, 0);
+  if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
+    return 0;
+  return session_enqueue_notify (peer_s);
+}
+
 static clib_error_t *
 ct_transport_init (vlib_main_t * vm)
 {
   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
                               FIB_PROTOCOL_IP4, ~0);
+  transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
+                              FIB_PROTOCOL_IP6, ~0);
   return 0;
 }