tls: use fifo segments instead of chunks
[vpp.git] / src / plugins / tlsopenssl / tls_openssl.c
index 8e5e73a..789c64d 100644 (file)
@@ -26,6 +26,7 @@
 #include <vnet/tls/tls.h>
 #include <ctype.h>
 #include <tlsopenssl/tls_openssl.h>
+#include <tlsopenssl/tls_bio.h>
 
 #define MAX_CRYPTO_LEN 64
 
@@ -110,71 +111,61 @@ openssl_lctx_get (u32 lctx_index)
 }
 
 static int
-openssl_try_handshake_read (openssl_ctx_t * oc, session_t * tls_session)
+openssl_read_from_ssl_into_fifo (svm_fifo_t * f, SSL * ssl)
 {
-  u32 deq_max, deq_now;
-  svm_fifo_t *f;
-  int wrote, rv;
-
-  f = tls_session->rx_fifo;
-  deq_max = svm_fifo_max_dequeue_cons (f);
-  if (!deq_max)
+  int read, rv, n_fs, i;
+  const int n_segs = 2;
+  svm_fifo_seg_t fs[n_segs];
+  u32 max_enq;
+
+  max_enq = svm_fifo_max_enqueue_prod (f);
+  n_fs = svm_fifo_provision_chunks (f, fs, n_segs, max_enq);
+  if (n_fs < 0)
     return 0;
 
-  deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max);
-  wrote = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
-  if (wrote <= 0)
+  /* Return early if we can't read anything */
+  read = SSL_read (ssl, fs[0].data, fs[0].len);
+  if (read <= 0)
     return 0;
 
-  svm_fifo_dequeue_drop (f, wrote);
-  if (wrote < deq_max)
+  for (i = 1; i < n_fs; i++)
     {
-      deq_now = clib_min (svm_fifo_max_read_chunk (f), deq_max - wrote);
-      rv = BIO_write (oc->wbio, svm_fifo_head (f), deq_now);
-      if (rv > 0)
-       {
-         svm_fifo_dequeue_drop (f, rv);
-         wrote += rv;
-       }
+      rv = SSL_read (ssl, fs[i].data, fs[i].len);
+      read += rv > 0 ? rv : 0;
+
+      if (rv < fs[i].len)
+       break;
     }
-  return wrote;
+
+  svm_fifo_enqueue_nocopy (f, read);
+
+  return read;
 }
 
 static int
-openssl_try_handshake_write (openssl_ctx_t * oc, session_t * tls_session)
+openssl_write_from_fifo_into_ssl (svm_fifo_t *f, SSL *ssl, u32 max_len)
 {
-  u32 enq_max, deq_now;
-  svm_fifo_t *f;
-  int read, rv;
-
-  if (BIO_ctrl_pending (oc->rbio) <= 0)
-    return 0;
+  int wrote = 0, rv, i = 0, len;
+  const int n_segs = 2;
+  svm_fifo_seg_t fs[n_segs];
 
-  f = tls_session->tx_fifo;
-  enq_max = svm_fifo_max_enqueue_prod (f);
-  if (!enq_max)
+  len = svm_fifo_segments (f, 0, fs, n_segs, max_len);
+  if (len <= 0)
     return 0;
 
-  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
-  read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
-  if (read <= 0)
-    return 0;
-
-  svm_fifo_enqueue_nocopy (f, read);
-  tls_add_vpp_q_tx_evt (tls_session);
-
-  if (read < enq_max)
+  while (wrote < len && i < n_segs)
     {
-      deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
-      rv = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
-      if (rv > 0)
-       {
-         svm_fifo_enqueue_nocopy (f, rv);
-         read += rv;
-       }
+      rv = SSL_write (ssl, fs[i].data, fs[i].len);
+      wrote += (rv > 0) ? rv : 0;
+      if (rv < fs[i].len)
+       break;
+      i++;
     }
 
-  return read;
+  if (wrote)
+    svm_fifo_dequeue_drop (f, wrote);
+
+  return wrote;
 }
 
 #ifdef HAVE_OPENSSL_ASYNC
@@ -204,22 +195,29 @@ openssl_check_async_status (tls_ctx_t * ctx, openssl_resume_handler * handler,
 static void
 openssl_handle_handshake_failure (tls_ctx_t * ctx)
 {
+  session_t *app_session;
+
   if (SSL_is_server (((openssl_ctx_t *) ctx)->ssl))
     {
       /*
        * Cleanup pre-allocated app session and close transport
        */
-      session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
-      ctx->no_app_session = 1;
-      ctx->c_s_index = SESSION_INVALID_INDEX;
-      tls_disconnect_transport (ctx);
+      app_session =
+       session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
+      if (app_session)
+       {
+         session_free (app_session);
+         ctx->no_app_session = 1;
+         ctx->c_s_index = SESSION_INVALID_INDEX;
+         tls_disconnect_transport (ctx);
+       }
     }
   else
     {
       /*
        * Also handles cleanup of the pre-allocated session
        */
-      tls_notify_app_connected (ctx, /* is failed */ 1);
+      tls_notify_app_connected (ctx, SESSION_E_TLS_HANDSHAKE);
     }
 }
 
@@ -235,7 +233,7 @@ openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
        {
          ctx->resume = 0;
        }
-      else if (!openssl_try_handshake_read (oc, tls_session))
+      else if (!svm_fifo_max_dequeue_cons (tls_session->rx_fifo))
        break;
 
       rv = SSL_do_handshake (oc->ssl);
@@ -258,9 +256,7 @@ openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
          return -1;
        }
 
-      openssl_try_handshake_write (oc, tls_session);
-
-      if (err != SSL_ERROR_WANT_WRITE)
+      if (err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_READ)
        break;
     }
   TLS_DBG (2, "tls state for %u is %s", oc->openssl_ctx_index,
@@ -287,15 +283,19 @@ openssl_ctx_handshake_rx (tls_ctx_t * ctx, session_t * tls_session)
           */
          if (ctx->srv_hostname)
            {
-             tls_notify_app_connected (ctx, /* is failed */ 0);
+             tls_notify_app_connected (ctx, SESSION_E_TLS_HANDSHAKE);
              return -1;
            }
        }
-      tls_notify_app_connected (ctx, /* is failed */ 0);
+      tls_notify_app_connected (ctx, SESSION_E_NONE);
     }
   else
     {
-      tls_notify_app_accept (ctx);
+      /* Need to check transport status */
+      if (ctx->is_passive_close)
+       openssl_handle_handshake_failure (ctx);
+      else
+       tls_notify_app_accept (ctx);
     }
 
   TLS_DBG (1, "Handshake for %u complete. TLS cipher is %s",
@@ -311,84 +311,49 @@ openssl_confirm_app_close (tls_ctx_t * ctx)
 }
 
 static inline int
-openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session)
+openssl_ctx_write (tls_ctx_t * ctx, session_t * app_session,
+                  transport_send_params_t * sp)
 {
   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
-  int wrote = 0, rv, read, max_buf = 100 * TLS_CHUNK_SIZE, max_space;
-  u32 enq_max, deq_max, deq_now, to_write;
-  session_t *tls_session;
+  u32 deq_max, space, enq_buf;
+  session_t *ts;
+  int wrote = 0;
   svm_fifo_t *f;
 
+  ts = session_get_from_handle (ctx->tls_session_handle);
+  space = svm_fifo_max_enqueue_prod (ts->tx_fifo);
+  /* Leave a bit of extra space for tls ctrl data, if any needed */
+  space = clib_max ((int) space - TLSO_CTRL_BYTES, 0);
+
   f = app_session->tx_fifo;
+
   deq_max = svm_fifo_max_dequeue_cons (f);
+  deq_max = clib_min (deq_max, space);
   if (!deq_max)
     goto check_tls_fifo;
 
-  max_space = max_buf - BIO_ctrl_pending (oc->rbio);
-  max_space = (max_space < 0) ? 0 : max_space;
-  deq_now = clib_min (deq_max, (u32) max_space);
-  to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now);
-  wrote = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
-  if (wrote <= 0)
-    {
-      tls_add_vpp_q_builtin_tx_evt (app_session);
-      goto check_tls_fifo;
-    }
-  svm_fifo_dequeue_drop (app_session->tx_fifo, wrote);
-  if (wrote < deq_now)
-    {
-      to_write = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
-      rv = SSL_write (oc->ssl, svm_fifo_head (f), to_write);
-      if (rv > 0)
-       {
-         svm_fifo_dequeue_drop (app_session->tx_fifo, rv);
-         wrote += rv;
-       }
-    }
+  deq_max = clib_min (deq_max, sp->max_burst_size);
 
-  if (svm_fifo_needs_deq_ntf (app_session->tx_fifo, wrote))
-    session_dequeue_notify (app_session);
+  wrote = openssl_write_from_fifo_into_ssl (f, oc->ssl, deq_max);
+  if (!wrote)
+    goto check_tls_fifo;
 
-  if (wrote < deq_max)
-    tls_add_vpp_q_builtin_tx_evt (app_session);
+  if (svm_fifo_needs_deq_ntf (f, wrote))
+    session_dequeue_notify (app_session);
 
 check_tls_fifo:
 
-  if (BIO_ctrl_pending (oc->rbio) <= 0)
-    return wrote;
-
-  tls_session = session_get_from_handle (ctx->tls_session_handle);
-  f = tls_session->tx_fifo;
-  enq_max = svm_fifo_max_enqueue_prod (f);
-  if (!enq_max)
-    {
-      tls_add_vpp_q_builtin_tx_evt (app_session);
-      return wrote;
-    }
-
-  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
-  read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
-  if (read <= 0)
-    {
-      tls_add_vpp_q_builtin_tx_evt (app_session);
-      return wrote;
-    }
-
-  svm_fifo_enqueue_nocopy (f, read);
-  tls_add_vpp_q_tx_evt (tls_session);
-
-  if (read < enq_max && BIO_ctrl_pending (oc->rbio) > 0)
+  /* Deschedule and wait for deq notification if fifo is almost full */
+  enq_buf = clib_min (svm_fifo_size (ts->tx_fifo) / 2, TLSO_MIN_ENQ_SPACE);
+  if (space < wrote + enq_buf)
     {
-      deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
-      read = BIO_read (oc->rbio, svm_fifo_tail (f), deq_now);
-      if (read > 0)
-       svm_fifo_enqueue_nocopy (f, read);
+      svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
+      transport_connection_deschedule (&ctx->connection);
+      sp->flags |= TRANSPORT_SND_F_DESCHED;
     }
-
-  if (BIO_ctrl_pending (oc->rbio) > 0)
-    tls_add_vpp_q_builtin_tx_evt (app_session);
-  else if (ctx->app_closed)
-    openssl_confirm_app_close (ctx);
+  else
+    /* Request tx reschedule of the app session */
+    app_session->flags |= SESSION_F_CUSTOM_TX;
 
   return wrote;
 }
@@ -396,82 +361,26 @@ check_tls_fifo:
 static inline int
 openssl_ctx_read (tls_ctx_t * ctx, session_t * tls_session)
 {
-  int read, wrote = 0, max_space, max_buf = 100 * TLS_CHUNK_SIZE, rv;
   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
-  u32 deq_max, enq_max, deq_now, to_read;
   session_t *app_session;
+  int read, wrote = 0;
   svm_fifo_t *f;
 
   if (PREDICT_FALSE (SSL_in_init (oc->ssl)))
     {
       if (openssl_ctx_handshake_rx (ctx, tls_session) < 0)
        return 0;
-      else
-       goto check_app_fifo;
-    }
-
-  f = tls_session->rx_fifo;
-  deq_max = svm_fifo_max_dequeue_cons (f);
-  max_space = max_buf - BIO_ctrl_pending (oc->wbio);
-  max_space = max_space < 0 ? 0 : max_space;
-  deq_now = clib_min (deq_max, max_space);
-  if (!deq_now)
-    goto check_app_fifo;
-
-  to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now);
-  wrote = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
-  if (wrote <= 0)
-    {
-      tls_add_vpp_q_builtin_rx_evt (tls_session);
-      goto check_app_fifo;
-    }
-  svm_fifo_dequeue_drop (f, wrote);
-  if (wrote < deq_now)
-    {
-      to_read = clib_min (svm_fifo_max_read_chunk (f), deq_now - wrote);
-      rv = BIO_write (oc->wbio, svm_fifo_head (f), to_read);
-      if (rv > 0)
-       {
-         svm_fifo_dequeue_drop (f, rv);
-         wrote += rv;
-       }
     }
-  if (svm_fifo_max_dequeue_cons (f))
-    tls_add_vpp_q_builtin_rx_evt (tls_session);
-
-check_app_fifo:
-
-  if (BIO_ctrl_pending (oc->wbio) <= 0)
-    return wrote;
 
   app_session = session_get_from_handle (ctx->app_session_handle);
   f = app_session->rx_fifo;
-  enq_max = svm_fifo_max_enqueue_prod (f);
-  if (!enq_max)
-    {
-      tls_add_vpp_q_builtin_rx_evt (tls_session);
-      return wrote;
-    }
 
-  deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
-  read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
-  if (read <= 0)
-    {
-      tls_add_vpp_q_builtin_rx_evt (tls_session);
-      return wrote;
-    }
-  svm_fifo_enqueue_nocopy (f, read);
-  if (read < enq_max && SSL_pending (oc->ssl) > 0)
-    {
-      deq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max - read);
-      read = SSL_read (oc->ssl, svm_fifo_tail (f), deq_now);
-      if (read > 0)
-       svm_fifo_enqueue_nocopy (f, read);
-    }
+  read = openssl_read_from_ssl_into_fifo (f, oc->ssl);
 
   /* If handshake just completed, session may still be in accepting state */
-  if (app_session->session_state >= SESSION_STATE_READY)
+  if (read && app_session->session_state >= SESSION_STATE_READY)
     tls_notify_app_enqueue (ctx, app_session);
+
   if (SSL_pending (oc->ssl) > 0)
     tls_add_vpp_q_builtin_rx_evt (tls_session);
 
@@ -484,7 +393,6 @@ openssl_ctx_init_client (tls_ctx_t * ctx)
   long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
   openssl_main_t *om = &openssl_main;
-  session_t *tls_session;
   const SSL_METHOD *method;
   int rv, err;
 
@@ -525,11 +433,8 @@ openssl_ctx_init_client (tls_ctx_t * ctx)
       return -1;
     }
 
-  oc->rbio = BIO_new (BIO_s_mem ());
-  oc->wbio = BIO_new (BIO_s_mem ());
-
-  BIO_set_mem_eof_return (oc->rbio, -1);
-  BIO_set_mem_eof_return (oc->wbio, -1);
+  oc->rbio = BIO_new_tls (ctx->tls_session_handle);
+  oc->wbio = BIO_new_tls (ctx->tls_session_handle);
 
   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
   SSL_set_connect_state (oc->ssl);
@@ -547,16 +452,14 @@ openssl_ctx_init_client (tls_ctx_t * ctx)
   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
           oc->openssl_ctx_index);
 
-  tls_session = session_get_from_handle (ctx->tls_session_handle);
-
 #ifdef HAVE_OPENSSL_ASYNC
+  session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
   vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
 #endif
   while (1)
     {
       rv = SSL_do_handshake (oc->ssl);
       err = SSL_get_error (oc->ssl, rv);
-      openssl_try_handshake_write (oc, tls_session);
 #ifdef HAVE_OPENSSL_ASYNC
       if (err == SSL_ERROR_WANT_ASYNC)
        {
@@ -689,7 +592,6 @@ openssl_ctx_init_server (tls_ctx_t * ctx)
   openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
   u32 olc_index = ctx->tls_ssl_ctx;
   openssl_listen_ctx_t *olc;
-  session_t *tls_session;
   int rv, err;
 
   /* Start a new connection */
@@ -702,11 +604,8 @@ openssl_ctx_init_server (tls_ctx_t * ctx)
       return -1;
     }
 
-  oc->rbio = BIO_new (BIO_s_mem ());
-  oc->wbio = BIO_new (BIO_s_mem ());
-
-  BIO_set_mem_eof_return (oc->rbio, -1);
-  BIO_set_mem_eof_return (oc->wbio, -1);
+  oc->rbio = BIO_new_tls (ctx->tls_session_handle);
+  oc->wbio = BIO_new_tls (ctx->tls_session_handle);
 
   SSL_set_bio (oc->ssl, oc->wbio, oc->rbio);
   SSL_set_accept_state (oc->ssl);
@@ -714,15 +613,14 @@ openssl_ctx_init_server (tls_ctx_t * ctx)
   TLS_DBG (1, "Initiating handshake for [%u]%u", ctx->c_thread_index,
           oc->openssl_ctx_index);
 
-  tls_session = session_get_from_handle (ctx->tls_session_handle);
 #ifdef HAVE_OPENSSL_ASYNC
+  session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
   vpp_tls_async_init_event (ctx, openssl_ctx_handshake_rx, tls_session);
 #endif
   while (1)
     {
       rv = SSL_do_handshake (oc->ssl);
       err = SSL_get_error (oc->ssl, rv);
-      openssl_try_handshake_write (oc, tls_session);
 #ifdef HAVE_OPENSSL_ASYNC
       if (err == SSL_ERROR_WANT_ASYNC)
        {
@@ -752,6 +650,11 @@ openssl_handshake_is_over (tls_ctx_t * ctx)
 static int
 openssl_transport_close (tls_ctx_t * ctx)
 {
+#ifdef HAVE_OPENSSL_ASYNC
+  if (vpp_openssl_is_inflight (ctx))
+    return 0;
+#endif
+
   if (!openssl_handshake_is_over (ctx))
     {
       openssl_handle_handshake_failure (ctx);