vcl: avoid coalescing dgrams on read
[vpp.git] / src / vcl / vppcom.c
index 95d8b41..6c85cee 100644 (file)
@@ -293,32 +293,33 @@ vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
 }
 
 static void
-vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
-                                    session_handle_t handle, int retval)
+vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
+                                    int retval)
 {
   app_session_evt_t _app_evt, *app_evt = &_app_evt;
   session_disconnected_reply_msg_t *rmp;
-  app_alloc_ctrl_evt_to_vpp (mq, app_evt,
+  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
                             SESSION_CTRL_EVT_DISCONNECTED_REPLY);
   rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
-  rmp->handle = handle;
-  rmp->context = context;
+  rmp->handle = s->vpp_handle;
+  rmp->context = wrk->api_client_handle;
   rmp->retval = retval;
-  app_send_ctrl_evt_to_vpp (mq, app_evt);
+  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
 }
 
 static void
-vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
-                             session_handle_t handle, int retval)
+vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
+                             int retval)
 {
   app_session_evt_t _app_evt, *app_evt = &_app_evt;
   session_reset_reply_msg_t *rmp;
-  app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
+  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
+                            SESSION_CTRL_EVT_RESET_REPLY);
   rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
-  rmp->handle = handle;
-  rmp->context = context;
+  rmp->handle = s->vpp_handle;
+  rmp->context = wrk->api_client_handle;
   rmp->retval = retval;
-  app_send_ctrl_evt_to_vpp (mq, app_evt);
+  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
 }
 
 void
@@ -327,16 +328,15 @@ vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
 {
   app_session_evt_t _app_evt, *app_evt = &_app_evt;
   session_worker_update_msg_t *mp;
-  svm_msg_q_t *mq;
 
-  mq = vcl_session_vpp_evt_q (wrk, s);
-  app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
+  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
+                            SESSION_CTRL_EVT_WORKER_UPDATE);
   mp = (session_worker_update_msg_t *) app_evt->evt->data;
   mp->client_index = wrk->api_client_handle;
   mp->handle = s->vpp_handle;
   mp->req_wrk_index = wrk->vpp_wrk_index;
   mp->wrk_index = wrk_index;
-  app_send_ctrl_evt_to_vpp (mq, app_evt);
+  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
 }
 
 int
@@ -378,7 +378,6 @@ vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
 {
   vcl_session_t *session, *listen_session;
   svm_fifo_t *rx_fifo, *tx_fifo;
-  u32 vpp_wrk_index;
   svm_msg_q_t *evt_q;
 
   session = vcl_session_alloc (wrk);
@@ -406,12 +405,8 @@ vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
   tx_fifo->client_session_index = session->session_index;
   rx_fifo->client_thread_index = vcl_get_worker_index ();
   tx_fifo->client_thread_index = vcl_get_worker_index ();
-  vpp_wrk_index = tx_fifo->master_thread_index;
-  vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
-  wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
 
   session->vpp_handle = mp->handle;
-  session->vpp_thread_index = rx_fifo->master_thread_index;
   session->rx_fifo = rx_fifo;
   session->tx_fifo = tx_fifo;
 
@@ -453,9 +448,9 @@ static u32
 vcl_session_connected_handler (vcl_worker_t * wrk,
                               session_connected_msg_t * mp)
 {
-  u32 session_index, vpp_wrk_index;
   svm_fifo_t *rx_fifo, *tx_fifo;
   vcl_session_t *session = 0;
+  u32 session_index;
 
   session_index = mp->context;
   session = vcl_session_get (wrk, session_index);
@@ -493,10 +488,6 @@ vcl_session_connected_handler (vcl_worker_t * wrk,
   rx_fifo->client_thread_index = vcl_get_worker_index ();
   tx_fifo->client_thread_index = vcl_get_worker_index ();
 
-  vpp_wrk_index = tx_fifo->master_thread_index;
-  vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
-  wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
-
   if (mp->ct_rx_fifo)
     {
       session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
@@ -513,7 +504,6 @@ vcl_session_connected_handler (vcl_worker_t * wrk,
 
   session->rx_fifo = rx_fifo;
   session->tx_fifo = tx_fifo;
-  session->vpp_thread_index = rx_fifo->master_thread_index;
   session->transport.is_ip4 = mp->lcl.is_ip4;
   clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
                    sizeof (session->transport.lcl_ip));
@@ -617,10 +607,7 @@ vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
   session->transport.lcl_port = mp->lcl_port;
   vcl_session_table_add_listener (wrk, mp->handle, sid);
   session->session_state = VCL_STATE_LISTEN;
-
   session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
-  vec_validate (wrk->vpp_event_queues, 0);
-  wrk->vpp_event_queues[0] = session->vpp_evt_q;
 
   if (vcl_session_is_cl (session))
     {
@@ -685,13 +672,9 @@ vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
       return;
     }
 
-  s->vpp_thread_index = mp->vpp_thread_index;
   s->vpp_handle = mp->new_handle;
   s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
 
-  vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
-  wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
-
   vcl_session_table_del_vpp_handle (wrk, mp->handle);
   vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
 
@@ -701,7 +684,7 @@ vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
                            SESSION_IO_EVT_TX, SVM_Q_WAIT);
 
   VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
-       s->vpp_thread_index, mp->new_handle);
+       mp->vpp_thread_index, mp->new_handle);
 }
 
 static vcl_session_t *
@@ -769,7 +752,6 @@ static int
 vppcom_session_disconnect (u32 session_handle)
 {
   vcl_worker_t *wrk = vcl_worker_get_current ();
-  svm_msg_q_t *vpp_evt_q;
   vcl_session_t *session, *listen_session;
   vcl_session_state_t state;
   u64 vpp_handle;
@@ -792,9 +774,7 @@ vppcom_session_disconnect (u32 session_handle)
 
   if (state == VCL_STATE_VPP_CLOSING)
     {
-      vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
-      vcl_send_session_disconnected_reply (vpp_evt_q, wrk->api_client_handle,
-                                          vpp_handle, 0);
+      vcl_send_session_disconnected_reply (wrk, session, 0);
       VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
            session->session_index, vpp_handle);
     }
@@ -854,9 +834,7 @@ vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
        }
       else if (session->session_state == VCL_STATE_DISCONNECT)
        {
-         vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
-                                       wrk->api_client_handle,
-                                       session->vpp_handle, 0);
+         vcl_send_session_reset_reply (wrk, session, 0);
          session->session_state = VCL_STATE_UPDATED;
        }
       return;
@@ -1410,9 +1388,7 @@ vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
     }
   else if (s->session_state == VCL_STATE_DISCONNECT)
     {
-      svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, s);
-      vcl_send_session_reset_reply (mq, wrk->api_client_handle,
-                                   s->vpp_handle, 0);
+      vcl_send_session_reset_reply (wrk, s, 0);
     }
   else if (s->session_state == VCL_STATE_DETACHED)
     {
@@ -1933,10 +1909,10 @@ read_again:
          e->session_index = s->session_index;
        }
     }
-  else if (PREDICT_FALSE (rv < n))
+  else if (PREDICT_FALSE (rv < n && !s->is_dgram))
     {
       /* More data enqueued while reading. Try to drain it
-       * or fill the buffer */
+       * or fill the buffer. Avoid doing that for dgrams */
       buf += rv;
       n -= rv;
       goto read_again;
@@ -1991,16 +1967,15 @@ vppcom_session_read_segments (uint32_t session_handle,
   is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
   is_ct = vcl_session_is_ct (s);
   mq = wrk->app_event_queue;
-  rx_fifo = s->rx_fifo;
+  rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
   s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
 
-  if (is_ct)
-    svm_fifo_unset_event (s->rx_fifo);
-
   if (svm_fifo_is_empty_cons (rx_fifo))
     {
       if (is_nonblocking)
        {
+         if (is_ct)
+           svm_fifo_unset_event (s->rx_fifo);
          svm_fifo_unset_event (rx_fifo);
          return VPPCOM_EWOULDBLOCK;
        }
@@ -2009,6 +1984,8 @@ vppcom_session_read_segments (uint32_t session_handle,
          if (vcl_session_is_closing (s))
            return vcl_session_closing_error (s);
 
+         if (is_ct)
+           svm_fifo_unset_event (s->rx_fifo);
          svm_fifo_unset_event (rx_fifo);
          svm_msg_q_lock (mq);
          if (svm_msg_q_is_empty (mq))
@@ -2030,9 +2007,11 @@ vppcom_session_read_segments (uint32_t session_handle,
 
   if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
     {
-      svm_fifo_unset_event (s->rx_fifo);
+      if (is_ct)
+       svm_fifo_unset_event (s->rx_fifo);
+      svm_fifo_unset_event (rx_fifo);
       if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
-         && svm_fifo_set_event (s->rx_fifo)
+         && svm_fifo_set_event (rx_fifo)
          && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
        {
          session_event_t *e;
@@ -2051,12 +2030,14 @@ vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
 {
   vcl_worker_t *wrk = vcl_worker_get_current ();
   vcl_session_t *s;
+  u8 is_ct;
 
   s = vcl_session_get_w_handle (wrk, session_handle);
   if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
     return;
 
-  svm_fifo_dequeue_drop (s->rx_fifo, n_bytes);
+  is_ct = vcl_session_is_ct (s);
+  svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
 
   ASSERT (s->rx_bytes_pending < n_bytes);
   s->rx_bytes_pending -= n_bytes;