vcl/ldp: add write msg function and fine tuning
[vpp.git] / src / vnet / session / session_node.c
index f4e0eaa..45018da 100644 (file)
@@ -29,6 +29,7 @@ session_mq_accepted_reply_handler (void *data)
 {
   session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
+  stream_session_state_t old_state;
   app_worker_t *app_wrk;
   local_session_t *ls;
   stream_session_t *s;
@@ -65,19 +66,28 @@ session_mq_accepted_reply_handler (void *data)
     {
       s = session_get_from_handle_if_valid (mp->handle);
       if (!s)
-       {
-         clib_warning ("session doesn't exist");
-         return;
-       }
+       return;
+
       app_wrk = app_worker_get (s->app_wrk_index);
       if (app_wrk->app_index != mp->context)
        {
          clib_warning ("app doesn't own session");
          return;
        }
+
+      old_state = s->session_state;
       s->session_state = SESSION_STATE_READY;
       if (!svm_fifo_is_empty (s->server_rx_fifo))
        app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
+
+      /* Closed while waiting for app to reply. Resend disconnect */
+      if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
+       {
+         application_t *app = application_get (app_wrk->app_index);
+         app->cb_fns.session_disconnect_callback (s);
+         s->session_state = old_state;
+         return;
+       }
     }
 }
 
@@ -92,17 +102,17 @@ session_mq_reset_reply_handler (void *data)
   u32 index, thread_index;
 
   mp = (session_reset_reply_msg_t *) data;
-  app = application_lookup (mp->client_index);
+  app = application_lookup (mp->context);
   if (!app)
     return;
 
   session_parse_handle (mp->handle, &index, &thread_index);
   s = session_get_if_valid (index, thread_index);
-  if (!s)
-    {
-      SESSION_DBG ("Invalid session!");
-      return;
-    }
+
+  /* Session was already closed or already cleaned up */
+  if (!s || s->session_state != SESSION_STATE_TRANSPORT_CLOSING)
+    return;
+
   app_wrk = app_worker_get (s->app_wrk_index);
   if (!app_wrk || app_wrk->app_index != app->app_index)
     {
@@ -448,7 +458,7 @@ session_tx_not_ready (stream_session_t * s, u8 peek_data)
        * session is not ready or closed */
       if (s->session_state < SESSION_STATE_READY)
        return 1;
-      if (s->session_state == SESSION_STATE_CLOSED)
+      if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
        return 2;
     }
   return 0;
@@ -811,6 +821,9 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
        {
          vec_add2 (fifo_events, e, 1);
          svm_msg_q_sub_w_lock (mq, msg);
+         /* Works because reply messages are smaller than a session evt.
+          * If we ever need to support bigger messages this needs to be
+          * fixed */
          clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
          svm_msg_q_free_msg (mq, msg);
        }
@@ -873,23 +886,24 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
            }
          break;
        case FIFO_EVENT_DISCONNECT:
-         /* Make sure stream disconnects run after the pending list is
-          * drained */
-         s = session_get_from_handle (e->session_handle);
-         if (!e->postponed)
-           {
-             e->postponed = 1;
-             vec_add1 (wrk->pending_disconnects, *e);
-             continue;
-           }
-         /* If tx queue is still not empty, wait */
-         if (svm_fifo_max_dequeue (s->server_tx_fifo))
+         s = session_get_from_handle_if_valid (e->session_handle);
+         if (PREDICT_FALSE (!s))
+           break;
+
+         /* Make sure session disconnects run after the pending list is
+          * drained, i.e., postpone if the first time. If not the first
+          * and the tx queue is still not empty, try to wait for some
+          * dispatch cycles */
+         if (!e->postponed
+             || (e->postponed < 200
+                 && svm_fifo_max_dequeue (s->server_tx_fifo)))
            {
+             e->postponed += 1;
              vec_add1 (wrk->pending_disconnects, *e);
              continue;
            }
 
-         stream_session_disconnect_transport (s);
+         session_transport_close (s);
          break;
        case FIFO_EVENT_BUILTIN_RX:
          s = session_event_get_session (e, thread_index);