Fix session connect_* api message handling.
[vpp.git] / src / uri / uri_udp_test.c
index 598052b..f50ee68 100644 (file)
 #include <vpp/api/vpe_all_api_h.h>
 #undef vl_printfun
 
-/* Satisfy external references when not linking with -lvlib */
-vlib_main_t vlib_global_main;
-vlib_main_t **vlib_mains;
-
 typedef enum
 {
   STATE_START,
@@ -164,7 +160,7 @@ setup_signal_handlers (void)
 }
 
 void
-application_attach (uri_udp_test_main_t * utm)
+application_send_attach (uri_udp_test_main_t * utm)
 {
   vl_api_application_attach_t *bmp;
   u32 fifo_size = 3 << 20;
@@ -174,8 +170,9 @@ application_attach (uri_udp_test_main_t * utm)
   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
   bmp->client_index = utm->my_client_index;
   bmp->context = ntohl (0xfeedface);
-  bmp->options[SESSION_OPTIONS_FLAGS] =
-    SESSION_OPTIONS_FLAGS_USE_FIFO | SESSION_OPTIONS_FLAGS_ADD_SEGMENT;
+  bmp->options[APP_OPTIONS_FLAGS] =
+    APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
+  bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
   bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
   bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
   bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
@@ -232,7 +229,8 @@ vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
     }
 
   utm->our_event_queue =
-    (unix_shared_memory_queue_t *) mp->app_event_queue_address;
+    uword_to_pointer (mp->app_event_queue_address,
+                     unix_shared_memory_queue_t *);
 }
 
 static void
@@ -307,7 +305,7 @@ cut_through_thread_fn (void *arg)
       /* We read from the tx fifo and write to the rx fifo */
       do
        {
-         actual_transfer = svm_fifo_dequeue_nowait (tx_fifo, 0,
+         actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
                                                     vec_len (my_copy_buffer),
                                                     my_copy_buffer);
        }
@@ -318,7 +316,7 @@ cut_through_thread_fn (void *arg)
       buffer_offset = 0;
       while (actual_transfer > 0)
        {
-         rv = svm_fifo_enqueue_nowait (rx_fifo, 0, actual_transfer,
+         rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
                                        my_copy_buffer + buffer_offset);
          if (rv > 0)
            {
@@ -357,7 +355,6 @@ client_send (uri_udp_test_main_t * utm, session_t * session)
   u64 bytes_received = 0, bytes_sent = 0;
   i32 bytes_to_read;
   int rv;
-  int mypid = getpid ();
   f64 before, after, delta, bytes_per_second;
   svm_fifo_t *rx_fifo, *tx_fifo;
   int buffer_offset, bytes_to_send = 0;
@@ -382,8 +379,7 @@ client_send (uri_udp_test_main_t * utm, session_t * session)
       buffer_offset = 0;
       while (bytes_to_send > 0)
        {
-         rv = svm_fifo_enqueue_nowait (tx_fifo, mypid,
-                                       bytes_to_send,
+         rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
                                        test_data + buffer_offset);
 
          if (rv > 0)
@@ -402,7 +398,7 @@ client_send (uri_udp_test_main_t * utm, session_t * session)
       buffer_offset = 0;
       while (bytes_to_read > 0)
        {
-         rv = svm_fifo_dequeue_nowait (rx_fifo, mypid,
+         rv = svm_fifo_dequeue_nowait (rx_fifo,
                                        bytes_to_read,
                                        utm->rx_buf + buffer_offset);
          if (rv > 0)
@@ -415,8 +411,8 @@ client_send (uri_udp_test_main_t * utm, session_t * session)
     }
   while (bytes_received < bytes_sent)
     {
-      rv = svm_fifo_dequeue_nowait (rx_fifo, mypid,
-                                   vec_len (utm->rx_buf), utm->rx_buf);
+      rv =
+       svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
       if (rv > 0)
        {
 #if CLIB_DEBUG > 0
@@ -459,7 +455,7 @@ uri_udp_client_test (uri_udp_test_main_t * utm)
 {
   session_t *session;
 
-  application_attach (utm);
+  application_send_attach (utm);
   udp_client_connect (utm);
 
   if (wait_for_state_change (utm, STATE_READY))
@@ -522,8 +518,8 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
   svm_fifo_segment_create_args_t _a, *a = &_a;
   svm_fifo_segment_private_t *seg;
   unix_shared_memory_queue_t *client_q;
-  vl_api_connect_uri_reply_t *rmp;
-  session_t *session;
+  vl_api_connect_session_reply_t *rmp;
+  session_t *session = 0;
   int rv = 0;
 
   /* Create the segment */
@@ -546,21 +542,16 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
 
   pool_get (utm->sessions, session);
 
-  /*
-   * By construction the master's idea of the rx fifo ends up in
-   * fsh->fifos[0], and the master's idea of the tx fifo ends up in
-   * fsh->fifos[1].
-   */
-  session->server_rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
-                                                        128 * 1024);
+  session->server_rx_fifo = svm_fifo_segment_alloc_fifo
+    (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
   ASSERT (session->server_rx_fifo);
 
-  session->server_tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg,
-                                                        128 * 1024);
+  session->server_tx_fifo = svm_fifo_segment_alloc_fifo
+    (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
   ASSERT (session->server_tx_fifo);
 
-  session->server_rx_fifo->server_session_index = session - utm->sessions;
-  session->server_tx_fifo->server_session_index = session - utm->sessions;
+  session->server_rx_fifo->master_session_index = session - utm->sessions;
+  session->server_tx_fifo->master_session_index = session - utm->sessions;
   utm->cut_through_session_index = session - utm->sessions;
 
   rv = pthread_create (&utm->cut_through_thread_handle,
@@ -575,15 +566,22 @@ send_reply:
   rmp = vl_msg_api_alloc (sizeof (*rmp));
   memset (rmp, 0, sizeof (*rmp));
 
-  rmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI_REPLY);
+  rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
   rmp->context = mp->context;
   rmp->retval = ntohl (rv);
   rmp->segment_name_length = vec_len (a->segment_name);
+  if (session)
+    {
+      rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
+      rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
+    }
+
   memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
 
   vec_free (a->segment_name);
 
-  client_q = (unix_shared_memory_queue_t *) mp->client_queue_address;
+  client_q =
+    uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
   vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
 }
 
@@ -610,14 +608,15 @@ vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
   if (start_time == 0.0)
     start_time = clib_time_now (&utm->clib_time);
 
-  utm->vpp_event_queue = (unix_shared_memory_queue_t *)
-    mp->vpp_event_queue_address;
+  utm->vpp_event_queue =
+    uword_to_pointer (mp->vpp_event_queue_address,
+                     unix_shared_memory_queue_t *);
 
   pool_get (utm->sessions, session);
 
-  rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
+  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
   rx_fifo->client_session_index = session - utm->sessions;
-  tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
+  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
   tx_fifo->client_session_index = session - utm->sessions;
 
   session->server_rx_fifo = rx_fifo;
@@ -675,7 +674,7 @@ vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
 }
 
 static void
-vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
+vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
 {
   uri_udp_test_main_t *utm = &uri_udp_test_main;
 
@@ -688,9 +687,7 @@ vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
       svm_fifo_segment_create_args_t _a, *a = &_a;
       u32 segment_index;
       session_t *session;
-      ssvm_shared_header_t *sh;
       svm_fifo_segment_private_t *seg;
-      svm_fifo_segment_header_t *fsh;
       int rv;
 
       memset (a, 0, sizeof (*a));
@@ -706,22 +703,19 @@ vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
          return;
        }
 
-      segment_index = vec_len (sm->segments) - 1;
+      segment_index = a->new_segment_indices[0];
       vec_add2 (utm->seg, seg, 1);
-
       memcpy (seg, sm->segments + segment_index, sizeof (*seg));
-      sh = seg->ssvm.sh;
-      fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
-
-      while (vec_len (fsh->fifos) < 2)
-       sleep (1);
+      sleep (1);
 
       pool_get (utm->sessions, session);
       utm->cut_through_session_index = session - utm->sessions;
 
-      session->server_rx_fifo = (svm_fifo_t *) fsh->fifos[0];
+      session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
+                                                 svm_fifo_t *);
       ASSERT (session->server_rx_fifo);
-      session->server_tx_fifo = (svm_fifo_t *) fsh->fifos[1];
+      session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
+                                                 svm_fifo_t *);
       ASSERT (session->server_tx_fifo);
     }
 
@@ -733,7 +727,7 @@ vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
 #define foreach_uri_msg                                \
 _(BIND_URI_REPLY, bind_uri_reply)                      \
 _(CONNECT_URI, connect_uri)                            \
-_(CONNECT_URI_REPLY, connect_uri_reply)                \
+_(CONNECT_SESSION_REPLY, connect_session_reply)        \
 _(UNBIND_URI_REPLY, unbind_uri_reply)                  \
 _(ACCEPT_SESSION, accept_session)                      \
 _(DISCONNECT_SESSION, disconnect_session)              \
@@ -805,19 +799,19 @@ server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
 
   do
     {
-      nbytes = svm_fifo_dequeue_nowait (rx_fifo, 0,
-                                       vec_len (utm->rx_buf), utm->rx_buf);
+      nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
+                                       utm->rx_buf);
     }
   while (nbytes <= 0);
   do
     {
-      rv = svm_fifo_enqueue_nowait (tx_fifo, 0, nbytes, utm->rx_buf);
+      rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
     }
   while (rv == -2);
 
   /* Fabricate TX event, send to vpp */
   evt.fifo = tx_fifo;
-  evt.event_type = FIFO_EVENT_SERVER_TX;
+  evt.event_type = FIFO_EVENT_APP_TX;
   evt.event_id = e->event_id;
 
   if (svm_fifo_set_event (tx_fifo))
@@ -839,11 +833,11 @@ server_handle_event_queue (uri_udp_test_main_t * utm)
                                    0 /* nowait */ );
       switch (e->event_type)
        {
-       case FIFO_EVENT_SERVER_RX:
+       case FIFO_EVENT_APP_RX:
          server_handle_fifo_event_rx (utm, e);
          break;
 
-       case FIFO_EVENT_SERVER_EXIT:
+       case FIFO_EVENT_DISCONNECT:
          return;
 
        default:
@@ -893,7 +887,7 @@ void
 udp_server_test (uri_udp_test_main_t * utm)
 {
 
-  application_attach (utm);
+  application_send_attach (utm);
 
   /* Bind to uri */
   server_listen (utm);