session: separate local session logic
[vpp.git] / src / vnet / session / session_node.c
index 64c873c..7855bf8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Copyright (c) 2017-2019 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -21,6 +21,7 @@
 #include <vnet/session/session.h>
 #include <vnet/session/application.h>
 #include <vnet/session/application_interface.h>
+#include <vnet/session/application_local.h>
 #include <vnet/session/session_debug.h>
 #include <svm/queue.h>
 
@@ -29,9 +30,10 @@ 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;
+  session_state_t old_state;
   app_worker_t *app_wrk;
   local_session_t *ls;
-  stream_session_t *s;
+  session_t *s;
 
   /* Server isn't interested, kill the session */
   if (mp->retval)
@@ -44,7 +46,7 @@ session_mq_accepted_reply_handler (void *data)
 
   if (session_handle_is_local (mp->handle))
     {
-      ls = application_get_local_session_from_handle (mp->handle);
+      ls = app_worker_get_local_session_from_handle (mp->handle);
       if (!ls)
        {
          clib_warning ("unknown local handle 0x%lx", mp->handle);
@@ -57,7 +59,7 @@ session_mq_accepted_reply_handler (void *data)
                        mp->context, mp->handle);
          return;
        }
-      if (application_local_session_connect_notify (ls))
+      if (app_worker_local_session_connect_notify (ls))
        return;
       ls->session_state = SESSION_STATE_READY;
     }
@@ -65,43 +67,53 @@ 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))
+      if (!svm_fifo_is_empty (s->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;
+       }
     }
 }
 
 static void
 session_mq_reset_reply_handler (void *data)
 {
+  vnet_disconnect_args_t _a = { 0 }, *a = &_a;
   session_reset_reply_msg_t *mp;
   app_worker_t *app_wrk;
-  stream_session_t *s;
+  session_t *s;
   application_t *app;
   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)
-    {
-      clib_warning ("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)
     {
@@ -119,7 +131,9 @@ session_mq_reset_reply_handler (void *data)
 
   /* This comes as a response to a reset, transport only waiting for
    * confirmation to remove connection state, no need to disconnect */
-  stream_session_cleanup (s);
+  a->handle = mp->handle;
+  a->app_index = app->app_index;
+  vnet_disconnect_session (a);
 }
 
 static void
@@ -131,7 +145,7 @@ session_mq_disconnected_handler (void *data)
   session_disconnected_msg_t *mp;
   app_worker_t *app_wrk;
   session_event_t *evt;
-  stream_session_t *s;
+  session_t *s;
   application_t *app;
   int rv = 0;
 
@@ -160,7 +174,7 @@ session_mq_disconnected_handler (void *data)
   svm_msg_q_unlock (app_wrk->event_queue);
   evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
   clib_memset (evt, 0, sizeof (*evt));
-  evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
+  evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
   rmp = (session_disconnected_reply_msg_t *) evt->data;
   rmp->handle = mp->handle;
   rmp->context = mp->context;
@@ -194,6 +208,86 @@ session_mq_disconnected_reply_handler (void *data)
     }
 }
 
+static void
+session_mq_worker_update_handler (void *data)
+{
+  session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
+  session_worker_update_reply_msg_t *rmp;
+  svm_msg_q_msg_t _msg, *msg = &_msg;
+  app_worker_t *app_wrk;
+  u32 owner_app_wrk_map;
+  session_event_t *evt;
+  session_t *s;
+  application_t *app;
+
+  app = application_lookup (mp->client_index);
+  if (!app)
+    return;
+  if (!(s = session_get_from_handle_if_valid (mp->handle)))
+    {
+      clib_warning ("invalid handle %llu", mp->handle);
+      return;
+    }
+  app_wrk = app_worker_get (s->app_wrk_index);
+  if (app_wrk->app_index != app->app_index)
+    {
+      clib_warning ("app %u does not own session %llu", app->app_index,
+                   mp->handle);
+      return;
+    }
+  owner_app_wrk_map = app_wrk->wrk_map_index;
+  app_wrk = application_get_worker (app, mp->wrk_index);
+
+  /* This needs to come from the new owner */
+  if (mp->req_wrk_index == owner_app_wrk_map)
+    {
+      session_req_worker_update_msg_t *wump;
+
+      svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
+                                          SESSION_MQ_CTRL_EVT_RING,
+                                          SVM_Q_WAIT, msg);
+      svm_msg_q_unlock (app_wrk->event_queue);
+      evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
+      clib_memset (evt, 0, sizeof (*evt));
+      evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
+      wump = (session_req_worker_update_msg_t *) evt->data;
+      wump->session_handle = mp->handle;
+      svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
+      return;
+    }
+
+  app_worker_own_session (app_wrk, s);
+
+  /*
+   * Send reply
+   */
+  svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
+                                      SESSION_MQ_CTRL_EVT_RING,
+                                      SVM_Q_WAIT, msg);
+  svm_msg_q_unlock (app_wrk->event_queue);
+  evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
+  clib_memset (evt, 0, sizeof (*evt));
+  evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
+  rmp = (session_worker_update_reply_msg_t *) evt->data;
+  rmp->handle = mp->handle;
+  rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
+  rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
+  rmp->segment_handle = session_segment_handle (s);
+  svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
+
+  /*
+   * Retransmit messages that may have been lost
+   */
+  if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
+    session_send_io_evt_to_thread (s->tx_fifo, FIFO_EVENT_APP_TX);
+
+  if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
+    app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
+
+  if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
+    app->cb_fns.session_disconnect_callback (s);
+}
+
 vlib_node_registration_t session_queue_node;
 
 typedef struct
@@ -244,7 +338,7 @@ enum
 static void
 session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
                        u32 next_index, u32 * to_next, u16 n_segs,
-                       stream_session_t * s, u32 n_trace)
+                       session_t * s, u32 n_trace)
 {
   session_queue_trace_t *t;
   vlib_buffer_t *b;
@@ -286,14 +380,12 @@ session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
 
       *n_bufs -= 1;
       chain_bi0 = wrk->tx_buffers[*n_bufs];
-      _vec_len (wrk->tx_buffers) = *n_bufs;
-
       chain_b = vlib_get_buffer (vm, chain_bi0);
       chain_b->current_data = 0;
       data = vlib_buffer_get_current (chain_b);
       if (peek_data)
        {
-         n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
+         n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo,
                                        ctx->tx_offset, len_to_deq, data);
          ctx->tx_offset += n_bytes_read;
        }
@@ -301,7 +393,7 @@ session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
        {
          if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
            {
-             svm_fifo_t *f = ctx->s->server_tx_fifo;
+             svm_fifo_t *f = ctx->s->tx_fifo;
              session_dgram_hdr_t *hdr = &ctx->hdr;
              u16 deq_now;
              deq_now = clib_min (hdr->data_length - hdr->data_offset,
@@ -318,7 +410,7 @@ session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
                }
            }
          else
-           n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
+           n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->tx_fifo,
                                                    len_to_deq, data);
        }
       ASSERT (n_bytes_read == len_to_deq);
@@ -341,20 +433,6 @@ session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
   ctx->left_to_snd -= left_from_seg;
 }
 
-always_inline int
-session_output_try_get_buffers (vlib_main_t * vm,
-                               session_manager_worker_t * wrk,
-                               u32 thread_index, u16 * n_bufs, u32 wanted)
-{
-  u32 n_alloc;
-  vec_validate_aligned (wrk->tx_buffers, wanted - 1, CLIB_CACHE_LINE_BYTES);
-  n_alloc = vlib_buffer_alloc (vm, &wrk->tx_buffers[*n_bufs],
-                              wanted - *n_bufs);
-  *n_bufs += n_alloc;
-  _vec_len (wrk->tx_buffers) = *n_bufs;
-  return n_alloc;
-}
-
 always_inline void
 session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
                        vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
@@ -370,12 +448,12 @@ session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
   b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
   b->current_data = 0;
 
-  data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
+  data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
   len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
 
   if (peek_data)
     {
-      n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
+      n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo, ctx->tx_offset,
                                    len_to_deq, data0);
       ASSERT (n_bytes_read > 0);
       /* Keep track of progress locally, transport is also supposed to
@@ -387,7 +465,7 @@ session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
       if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
        {
          session_dgram_hdr_t *hdr = &ctx->hdr;
-         svm_fifo_t *f = ctx->s->server_tx_fifo;
+         svm_fifo_t *f = ctx->s->tx_fifo;
          u16 deq_now;
          u32 offset;
 
@@ -412,7 +490,7 @@ session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
        }
       else
        {
-         n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
+         n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->tx_fifo,
                                                  len_to_deq, data0);
          ASSERT (n_bytes_read > 0);
        }
@@ -437,7 +515,7 @@ session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
 }
 
 always_inline u8
-session_tx_not_ready (stream_session_t * s, u8 peek_data)
+session_tx_not_ready (session_t * s, u8 peek_data)
 {
   if (peek_data)
     {
@@ -445,7 +523,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;
@@ -476,7 +554,7 @@ session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
                               u32 max_segs, u8 peek_data)
 {
   u32 n_bytes_per_buf, n_bytes_per_seg;
-  ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
+  ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->tx_fifo);
   if (peek_data)
     {
       /* Offset in rx fifo from where to peek data */
@@ -497,7 +575,7 @@ session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
              ctx->max_len_to_snd = 0;
              return;
            }
-         svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
+         svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
                         (u8 *) & ctx->hdr);
          ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
          ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
@@ -528,50 +606,55 @@ session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
       ctx->max_len_to_snd = max_segs * ctx->snd_mss;
     }
 
-  n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
-                                                      VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-  ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
-  n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
+  n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
+  ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
+  n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->snd_mss;
   ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
   ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
   ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
-                                    n_bytes_per_buf - MAX_HDRS_LEN);
+                                    n_bytes_per_buf -
+                                    TRANSPORT_MAX_HDRS_LEN);
 }
 
 always_inline int
 session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
-                               session_event_t * e,
-                               stream_session_t * s, int *n_tx_packets,
+                               session_manager_worker_t * wrk,
+                               session_event_t * e, int *n_tx_packets,
                                u8 peek_data)
 {
-  u32 next_index, next0, next1, *to_next, n_left_to_next;
+  u32 next_index, next0, next1, *to_next, n_left_to_next, n_left, pbi;
   u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
-  u32 thread_index = s->thread_index, n_left, pbi;
   session_manager_main_t *smm = &session_manager_main;
-  session_manager_worker_t *wrk = &smm->wrk[thread_index];
   session_tx_context_t *ctx = &wrk->ctx;
   transport_proto_t tp;
   vlib_buffer_t *pb;
   u16 n_bufs, rv;
 
-  if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
+  if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
     {
       if (rv < 2)
        vec_add1 (wrk->pending_event_vector, *e);
       return SESSION_TX_NO_DATA;
     }
 
-  next_index = smm->session_type_to_next[s->session_type];
+  next_index = smm->session_type_to_next[ctx->s->session_type];
   next0 = next1 = next_index;
 
-  tp = session_get_transport_proto (s);
-  ctx->s = s;
+  tp = session_get_transport_proto (ctx->s);
   ctx->transport_vft = transport_protocol_get_vft (tp);
   ctx->tc = session_tx_get_transport (ctx, peek_data);
   ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
-  ctx->snd_space =
-    transport_connection_snd_space (ctx->tc, vm->clib_time.last_cpu_time,
-                                   ctx->snd_mss);
+
+  if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
+    {
+      if (ctx->transport_vft->flush_data)
+       ctx->transport_vft->flush_data (ctx->tc);
+    }
+
+  ctx->snd_space = transport_connection_snd_space (ctx->tc,
+                                                  vm->clib_time.
+                                                  last_cpu_time,
+                                                  ctx->snd_mss);
   if (ctx->snd_space == 0 || ctx->snd_mss == 0)
     {
       vec_add1 (wrk->pending_event_vector, *e);
@@ -579,7 +662,7 @@ session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
     }
 
   /* Allow enqueuing of a new event */
-  svm_fifo_unset_event (s->server_tx_fifo);
+  svm_fifo_unset_event (ctx->s->tx_fifo);
 
   /* Check how much we can pull. */
   session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
@@ -588,21 +671,16 @@ session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
   if (PREDICT_FALSE (!ctx->max_len_to_snd))
     return SESSION_TX_NO_DATA;
 
-  n_bufs = vec_len (wrk->tx_buffers);
   n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
-
-  /*
-   * Make sure we have at least one full frame of buffers ready
-   */
-  if (n_bufs < n_bufs_needed)
+  vec_validate_aligned (wrk->tx_buffers, n_bufs_needed - 1,
+                       CLIB_CACHE_LINE_BYTES);
+  n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_needed);
+  if (PREDICT_FALSE (n_bufs < n_bufs_needed))
     {
-      session_output_try_get_buffers (vm, wrk, thread_index, &n_bufs,
-                                     ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
-      if (PREDICT_FALSE (n_bufs < n_bufs_needed))
-       {
-         vec_add1 (wrk->pending_event_vector, *e);
-         return SESSION_TX_NO_BUFFERS;
-       }
+      if (n_bufs)
+       vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
+      vec_add1 (wrk->pending_event_vector, *e);
+      return SESSION_TX_NO_BUFFERS;
     }
 
   /*
@@ -684,9 +762,12 @@ session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
 
   if (PREDICT_FALSE (n_trace > 0))
     session_tx_trace_frame (vm, node, next_index, to_next,
-                           ctx->n_segs_per_evt, s, n_trace);
-
-  _vec_len (wrk->tx_buffers) = n_bufs;
+                           ctx->n_segs_per_evt, ctx->s, n_trace);
+  if (PREDICT_FALSE (n_bufs))
+    {
+      clib_warning ("not all buffers consumed");
+      vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
+    }
   *n_tx_packets += ctx->n_segs_per_evt;
   transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
@@ -694,18 +775,18 @@ session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
   /* If we couldn't dequeue all bytes mark as partially read */
   ASSERT (ctx->left_to_snd == 0);
   if (ctx->max_len_to_snd < ctx->max_dequeue)
-    if (svm_fifo_set_event (s->server_tx_fifo))
+    if (svm_fifo_set_event (ctx->s->tx_fifo))
       vec_add1 (wrk->pending_event_vector, *e);
 
   if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
     {
       /* Fix dgram pre header */
       if (ctx->max_len_to_snd < ctx->max_dequeue)
-       svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
+       svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
                                 sizeof (session_dgram_pre_hdr_t));
       /* More data needs to be read */
-      else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
-       if (svm_fifo_set_event (s->server_tx_fifo))
+      else if (svm_fifo_max_dequeue (ctx->s->tx_fifo) > 0)
+       if (svm_fifo_set_event (ctx->s->tx_fifo))
          vec_add1 (wrk->pending_event_vector, *e);
     }
   return SESSION_TX_OK;
@@ -713,35 +794,37 @@ session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
 
 int
 session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
-                             session_event_t * e,
-                             stream_session_t * s, int *n_tx_pkts)
+                             session_manager_worker_t * wrk,
+                             session_event_t * e, int *n_tx_pkts)
 {
-  return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
+  return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 1);
 }
 
 int
 session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
-                                session_event_t * e,
-                                stream_session_t * s, int *n_tx_pkts)
+                                session_manager_worker_t * wrk,
+                                session_event_t * e, int *n_tx_pkts)
 {
-  return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
+  return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 0);
 }
 
 int
 session_tx_fifo_dequeue_internal (vlib_main_t * vm,
                                  vlib_node_runtime_t * node,
-                                 session_event_t * e,
-                                 stream_session_t * s, int *n_tx_pkts)
+                                 session_manager_worker_t * wrk,
+                                 session_event_t * e, int *n_tx_pkts)
 {
+  session_t *s = wrk->ctx.s;
   application_t *app;
+
   if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
     return 0;
   app = application_get (s->t_app_index);
-  svm_fifo_unset_event (s->server_tx_fifo);
+  svm_fifo_unset_event (s->tx_fifo);
   return app->cb_fns.builtin_app_tx_callback (s);
 }
 
-always_inline stream_session_t *
+always_inline session_t *
 session_event_get_session (session_event_t * e, u8 thread_index)
 {
   return session_get_if_valid (e->fifo->master_session_index, thread_index);
@@ -751,7 +834,7 @@ static void
 session_update_dispatch_period (session_manager_worker_t * wrk, f64 now,
                                u32 thread_index)
 {
-  if (wrk->last_tx_packets > 1)
+  if (wrk->last_tx_packets)
     {
       f64 sample = now - wrk->last_vlib_time;
       wrk->dispatch_period = (wrk->dispatch_period + sample) * 0.5;
@@ -800,7 +883,10 @@ 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);
-         clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
+         /* 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);
        }
       svm_msg_q_unlock (mq);
@@ -818,13 +904,14 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
 
   for (i = 0; i < n_events; i++)
     {
-      stream_session_t *s;     /* $$$ prefetch 1 ahead maybe */
+      session_t *s;            /* $$$ prefetch 1 ahead maybe */
       session_event_t *e;
-      u8 want_tx_evt;
+      u8 need_tx_ntf;
 
       e = &fifo_events[i];
       switch (e->event_type)
        {
+       case SESSION_IO_EVT_TX_FLUSH:
        case FIFO_EVENT_APP_TX:
          /* Don't try to send more that one frame per dispatch cycle */
          if (n_tx_packets == VLIB_FRAME_SIZE)
@@ -839,19 +926,17 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
              clib_warning ("session was freed!");
              continue;
            }
-
-         want_tx_evt = svm_fifo_want_tx_evt (s->server_tx_fifo);
+         wrk->ctx.s = s;
          /* Spray packets in per session type frames, since they go to
           * different nodes */
-         rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
+         rv = (smm->session_tx_fns[s->session_type]) (vm, node, wrk, e,
                                                       &n_tx_packets);
          if (PREDICT_TRUE (rv == SESSION_TX_OK))
            {
-             if (PREDICT_FALSE (want_tx_evt))
-               {
-                 svm_fifo_set_want_tx_evt (s->server_tx_fifo, 0);
-                 session_dequeue_notify (s);
-               }
+             need_tx_ntf = svm_fifo_needs_tx_ntf (s->tx_fifo,
+                                                  wrk->ctx.max_len_to_snd);
+             if (PREDICT_FALSE (need_tx_ntf))
+               session_dequeue_notify (s);
            }
          else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
            {
@@ -861,37 +946,39 @@ 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->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);
          if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
            continue;
-         svm_fifo_unset_event (s->server_rx_fifo);
+         svm_fifo_unset_event (s->rx_fifo);
          app_wrk = app_worker_get (s->app_wrk_index);
          app = application_get (app_wrk->app_index);
          app->cb_fns.builtin_app_rx_callback (s);
          break;
        case FIFO_EVENT_BUILTIN_TX:
          s = session_get_from_handle_if_valid (e->session_handle);
+         wrk->ctx.s = s;
          if (PREDICT_TRUE (s != 0))
-           session_tx_fifo_dequeue_internal (vm, node, e, s, &n_tx_packets);
+           session_tx_fifo_dequeue_internal (vm, node, wrk, e,
+                                             &n_tx_packets);
          break;
        case FIFO_EVENT_RPC:
          fp = e->rpc_args.fp;
@@ -911,6 +998,9 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
        case SESSION_CTRL_EVT_RESET_REPLY:
          session_mq_reset_reply_handler (e->data);
          break;
+       case SESSION_CTRL_EVT_WORKER_UPDATE:
+         session_mq_worker_update_handler (e->data);
+         break;
        default:
          clib_warning ("unhandled event type %d", e->event_type);
        }
@@ -949,7 +1039,7 @@ dump_thread_0_event_queue (void)
   u32 my_thread_index = vm->thread_index;
   session_event_t _e, *e = &_e;
   svm_msg_q_ring_t *ring;
-  stream_session_t *s0;
+  session_t *s0;
   svm_msg_q_msg_t *msg;
   svm_msg_q_t *mq;
   int i, index;
@@ -961,7 +1051,7 @@ dump_thread_0_event_queue (void)
     {
       msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
       ring = svm_msg_q_ring (mq, msg->ring_index);
-      clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
+      clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
 
       switch (e->event_type)
        {
@@ -983,7 +1073,8 @@ dump_thread_0_event_queue (void)
 
        case FIFO_EVENT_RPC:
          fformat (stdout, "[%04d] RPC call %llx with %llx\n",
-                  i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
+                  i, (u64) (uword) (e->rpc_args.fp),
+                  (u64) (uword) (e->rpc_args.arg));
          break;
 
        default:
@@ -1002,7 +1093,7 @@ dump_thread_0_event_queue (void)
 static u8
 session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
 {
-  stream_session_t *s;
+  session_t *s;
   switch (e->event_type)
     {
     case FIFO_EVENT_APP_RX:
@@ -1020,7 +1111,7 @@ session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
          clib_warning ("session has event but doesn't exist!");
          break;
        }
-      if (s->server_rx_fifo == f || s->server_tx_fifo == f)
+      if (s->rx_fifo == f || s->tx_fifo == f)
        return 1;
       break;
     default:
@@ -1053,7 +1144,7 @@ session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
     {
       msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
       ring = svm_msg_q_ring (mq, msg->ring_index);
-      clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
+      clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
       found = session_node_cmp_event (e, f);
       if (found)
        return 1;
@@ -1069,7 +1160,7 @@ session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
     found = session_node_cmp_event (evt, f);
     if (found)
       {
-       clib_memcpy (e, evt, sizeof (*evt));
+       clib_memcpy_fast (e, evt, sizeof (*evt));
        break;
       }
   }
@@ -1139,7 +1230,6 @@ VLIB_REGISTER_NODE (session_queue_process_node) =
 };
 /* *INDENT-ON* */
 
-
 /*
  * fd.io coding-style-patch-verification: ON
  *