vcl: fix event triggered after closing connections.
[vpp.git] / src / vcl / vppcom.c
index 3847bf2..9f8edce 100644 (file)
@@ -455,9 +455,9 @@ vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
       goto error;
     }
 
-  if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
-                                 mp->server_tx_fifo,
-                                 mp->vpp_event_queue_address, 0, session))
+  if (vcl_segment_attach_session (
+       mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
+       mp->vpp_event_queue_address, mp->mq_index, 0, session))
     {
       VDBG (0, "failed to attach fifos for %u", session->session_index);
       goto error;
@@ -525,9 +525,9 @@ vcl_session_connected_handler (vcl_worker_t * wrk,
 
   session->vpp_handle = mp->handle;
 
-  if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
-                                 mp->server_tx_fifo,
-                                 mp->vpp_event_queue_address, 0, session))
+  if (vcl_segment_attach_session (
+       mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
+       mp->vpp_event_queue_address, mp->mq_index, 0, session))
     {
       VDBG (0, "failed to attach fifos for %u", session->session_index);
       session->session_state = VCL_STATE_DETACHED;
@@ -538,7 +538,8 @@ vcl_session_connected_handler (vcl_worker_t * wrk,
   if (mp->ct_rx_fifo)
     {
       if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
-                                     mp->ct_tx_fifo, (uword) ~0, 1, session))
+                                     mp->ct_tx_fifo, (uword) ~0, ~0, 1,
+                                     session))
        {
          VDBG (0, "failed to attach ct fifos for %u", session->session_index);
          session->session_state = VCL_STATE_DETACHED;
@@ -654,7 +655,8 @@ vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
   if (vcl_session_is_cl (session))
     {
       if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
-                                     mp->tx_fifo, mp->vpp_evt_q, 0, session))
+                                     mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
+                                     0, session))
        {
          VDBG (0, "failed to attach fifos for %u", session->session_index);
          session->session_state = VCL_STATE_DETACHED;
@@ -984,7 +986,7 @@ vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
   if (s->rx_fifo)
     {
       if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
-                                     msg->tx_fifo, (uword) ~0, 0, s))
+                                     msg->tx_fifo, (uword) ~0, ~0, 0, s))
        {
          VDBG (0, "failed to attach fifos for %u", s->session_index);
          return;
@@ -1366,8 +1368,8 @@ vppcom_app_create (const char *app_name)
   vcm->main_cpu = pthread_self ();
   vcm->main_pid = getpid ();
   vcm->app_name = format (0, "%s", app_name);
-  fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
-                         20 /* timeout in secs */ );
+  fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
+                         20 /* timeout in secs */);
   pool_alloc (vcm->workers, vcl_cfg->max_workers);
   clib_spinlock_init (&vcm->workers_lock);
   clib_rwlock_init (&vcm->segment_table_lock);
@@ -1643,9 +1645,6 @@ vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
   if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
     return VPPCOM_EBADFD;
 
-  if (q_len == 0 || q_len == ~0)
-    q_len = vcm->cfg.listen_queue_size;
-
   listen_vpp_handle = listen_session->vpp_handle;
   if (listen_session->session_state == VCL_STATE_LISTEN)
     {
@@ -2194,7 +2193,7 @@ vppcom_session_free_segments (uint32_t session_handle, uint32_t 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);
+  ASSERT (s->rx_bytes_pending >= n_bytes);
   s->rx_bytes_pending -= n_bytes;
 }
 
@@ -3014,8 +3013,9 @@ vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
        break;
       vcl_fifo_rx_evt_valid_or_break (s);
       session_events = s->vep.ev.events;
-      if (!(EPOLLIN & s->vep.ev.events)
-         || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
+      if (!(EPOLLIN & s->vep.ev.events) ||
+         (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
+         (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
        break;
       add_event = 1;
       events[*num_ev].events = EPOLLIN;
@@ -3045,7 +3045,8 @@ vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
        break;
       session_events = s->vep.ev.events;
       sid = s->session_index;
-      if (!(EPOLLIN & session_events))
+      if (!(EPOLLIN & session_events) ||
+         (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
        break;
       add_event = 1;
       events[*num_ev].events = EPOLLIN;
@@ -3088,8 +3089,22 @@ vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
       sid = s->session_index;
       session_events = s->vep.ev.events;
       add_event = 1;
-      events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
+      if (EPOLLRDHUP & session_events)
+       {
+         /* If app can distinguish between RDHUP and HUP,
+          * we make finer control */
+         events[*num_ev].events = EPOLLRDHUP;
+         if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
+           {
+             events[*num_ev].events |= EPOLLHUP;
+           }
+       }
+      else
+       {
+         events[*num_ev].events = EPOLLHUP;
+       }
       session_evt_data = s->vep.ev.data.u64;
+
       break;
     case SESSION_CTRL_EVT_RESET:
       if (!e->postponed)
@@ -3361,6 +3376,10 @@ vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
        }
       vec_reset_length (wrk->unhandled_evts_vector);
     }
+
+  if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
+    vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
+
   /* Request to only drain unhandled */
   if ((int) wait_for_time == -2)
     return n_evts;
@@ -3373,9 +3392,6 @@ vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
     n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
                                        wait_for_time);
 
-  if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
-    vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
-
   return n_evts;
 }
 
@@ -4058,6 +4074,10 @@ vppcom_session_sendto (uint32_t session_handle, void *buffer,
       if (!vcl_session_is_cl (s))
        return VPPCOM_EINVAL;
 
+      s->transport.is_ip4 = ep->is_ip4;
+      s->transport.rmt_port = ep->port;
+      vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
+
       /* Session not connected/bound in vpp. Create it by 'connecting' it */
       if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
        {
@@ -4073,10 +4093,6 @@ vppcom_session_sendto (uint32_t session_handle, void *buffer,
            return rv;
          s = vcl_session_get (wrk, session_index);
        }
-
-      s->transport.is_ip4 = ep->is_ip4;
-      s->transport.rmt_port = ep->port;
-      vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
     }
 
   if (flags)
@@ -4386,22 +4402,18 @@ int
 vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
 {
   if (vcm->cfg.vpp_app_socket_api)
-    {
-      clib_warning ("not supported");
-      return VPPCOM_EINVAL;
-    }
-  return vcl_bapi_add_cert_key_pair (ckpair);
+    return vcl_sapi_add_cert_key_pair (ckpair);
+  else
+    return vcl_bapi_add_cert_key_pair (ckpair);
 }
 
 int
 vppcom_del_cert_key_pair (uint32_t ckpair_index)
 {
   if (vcm->cfg.vpp_app_socket_api)
-    {
-      clib_warning ("not supported");
-      return VPPCOM_EINVAL;
-    }
-  return vcl_bapi_del_cert_key_pair (ckpair_index);
+    return vcl_sapi_del_cert_key_pair (ckpair_index);
+  else
+    return vcl_bapi_del_cert_key_pair (ckpair_index);
 }
 
 /*