session: cleanup part 1
[vpp.git] / src / plugins / tlsopenssl / tls_async.c
index aec1d7d..ade073c 100644 (file)
@@ -15,7 +15,6 @@
 #include <vnet/vnet.h>
 #include <vnet/ip/ip.h>
 #include <vnet/api_errno.h>
-#include <vnet/ipsec/ipsec.h>
 #include <vlib/node_funcs.h>
 #include <openssl/engine.h>
 #include <tlsopenssl/tls_openssl.h>
@@ -62,6 +61,7 @@ typedef struct openssl_async_
   openssl_evt_t ***evt_pool;
   openssl_async_status_t *status;
   void (*polling) (void);
+  void (*polling_conf) (void);
   u8 start_polling;
   ENGINE *engine;
 
@@ -69,6 +69,7 @@ typedef struct openssl_async_
 
 void qat_polling ();
 void qat_pre_init ();
+void qat_polling_config ();
 void dasync_polling ();
 
 struct engine_polling
@@ -76,11 +77,12 @@ struct engine_polling
   char *engine;
   void (*polling) (void);
   void (*pre_init) (void);
+  void (*polling_conf) (void);
 };
 
 struct engine_polling engine_list[] = {
-  {"qat", qat_polling, qat_pre_init},
-  {"dasync", dasync_polling, NULL}
+  {"qat", qat_polling, qat_pre_init, qat_polling_config},
+  {"dasync", dasync_polling, NULL, NULL}
 };
 
 openssl_async_t openssl_async_main;
@@ -99,7 +101,7 @@ evt_pool_init (vlib_main_t * vm)
 
   num_threads = 1 /* main thread */  + vtm->n_threads;
 
-  TLS_DBG ("Totally there is %d thread\n", num_threads);
+  TLS_DBG (2, "Totally there is %d thread\n", num_threads);
 
   vec_validate (om->evt_pool, num_threads - 1);
   vec_validate (om->status, num_threads - 1);
@@ -115,7 +117,6 @@ evt_pool_init (vlib_main_t * vm)
     }
   om->polling = NULL;
 
-  TLS_DBG ("Node disabled\n");
   openssl_async_node_enable_disable (0);
 
   return;
@@ -134,12 +135,14 @@ openssl_engine_register (char *engine_name, char *algorithm)
       if (!strcmp (engine_list[i].engine, engine_name))
        {
          om->polling = engine_list[i].polling;
+         om->polling_conf = engine_list[i].polling_conf;
 
          registered = i;
        }
     }
   if (registered < 0)
     {
+      clib_error ("engine %s is not regisered in VPP", engine_name);
       return 0;
     }
 
@@ -149,6 +152,7 @@ openssl_engine_register (char *engine_name, char *algorithm)
 
   if (engine == NULL)
     {
+      clib_warning ("Failed to find engine ENGINE_by_id %s", engine_name);
       return 0;
     }
 
@@ -237,13 +241,13 @@ openssl_evt_alloc (void)
   if (!(*evt))
     *evt = clib_mem_alloc (sizeof (openssl_evt_t));
 
-  memset (*evt, 0, sizeof (openssl_evt_t));
+  clib_memset (*evt, 0, sizeof (openssl_evt_t));
   (*evt)->event_index = evt - tm->evt_pool[thread_index];
   return ((*evt)->event_index);
 }
 
 int
-openssl_async_run (void *evt)
+tls_async_openssl_callback (SSL * s, void *evt)
 {
   openssl_evt_t *event, *event_tail;
   openssl_async_t *om = &openssl_async_main;
@@ -253,7 +257,7 @@ openssl_async_run (void *evt)
   int *evt_run_tail = &om->status[thread_index].evt_run_tail;
   int *evt_run_head = &om->status[thread_index].evt_run_head;
 
-  TLS_DBG ("Set event %d to run\n", event_index);
+  TLS_DBG (2, "Set event %d to run\n", event_index);
 
   event = openssl_evt_get_w_thread (event_index, thread_index);
 
@@ -271,7 +275,9 @@ openssl_async_run (void *evt)
     }
   *evt_run_tail = event_index;
   if (*evt_run_head < 0)
-    *evt_run_head = event_index;
+    {
+      *evt_run_head = event_index;
+    }
 
   return 1;
 }
@@ -295,7 +301,7 @@ vpp_add_async_pending_event (tls_ctx_t * ctx,
   event->handler = handler;
   event->cb_args.event_index = eidx;
   event->cb_args.thread_index = thread_id;
-  event->engine_callback.callback = openssl_async_run;
+  event->engine_callback.callback = tls_async_openssl_callback;
   event->engine_callback.arg = &event->cb_args;
 
   /* add to pending list */
@@ -303,17 +309,40 @@ vpp_add_async_pending_event (tls_ctx_t * ctx,
   event->next = *evt_pending_head;
   *evt_pending_head = eidx;
 
-
   return &event->engine_callback;
 }
 
+int
+vpp_add_async_run_event (tls_ctx_t * ctx, openssl_resume_handler * handler)
+{
+  u32 eidx;
+  openssl_evt_t *event;
+  openssl_ctx_t *oc = (openssl_ctx_t *) ctx;
+  u32 thread_id = ctx->c_thread_index;
+
+  eidx = openssl_evt_alloc ();
+  event = openssl_evt_get (eidx);
+
+  event->ctx_index = oc->openssl_ctx_index;
+  event->status = SSL_ASYNC_PENDING;
+  event->handler = handler;
+  event->cb_args.event_index = eidx;
+  event->cb_args.thread_index = thread_id;
+  event->engine_callback.callback = tls_async_openssl_callback;
+  event->engine_callback.arg = &event->cb_args;
+
+  /* This is a retry event, and need to put to ring to make it run again */
+  return tls_async_openssl_callback (NULL, &event->cb_args);
+
+}
+
 void
 event_handler (void *tls_async)
 {
 
   openssl_resume_handler *handler;
   openssl_evt_t *callback;
-  stream_session_t *tls_session;
+  session_t *tls_session;
   int thread_index;
   tls_ctx_t *ctx;
 
@@ -325,7 +354,6 @@ event_handler (void *tls_async)
 
   if (handler)
     {
-      TLS_DBG ("relaunch...\n");
       (*handler) (ctx, tls_session);
     }
 
@@ -339,26 +367,10 @@ event_handler (void *tls_async)
 void
 dasync_polling ()
 {
-  openssl_async_t *om = &openssl_async_main;
-  openssl_evt_t *event;
-  int *evt_pending;
-  openssl_tls_callback_t *engine_cb;
-  u8 thread_index = vlib_get_thread_index ();
-
-  /* POC code here to simulate the engine to call callback */
-  evt_pending = &om->status[thread_index].evt_pending_head;
-  while (*evt_pending >= 0)
-    {
-      TLS_DBG ("polling... current head = %d\n", *evt_pending);
-      event = openssl_evt_get_w_thread (*evt_pending, thread_index);
-      *evt_pending = event->next;
-      if (event->status == SSL_ASYNC_PENDING)
-       {
-         engine_cb = &event->engine_callback;
-         (*engine_cb->callback) (engine_cb->arg);
-       }
-    }
-
+/* dasync is a fake async device, and could not be polled.
+ * We have added code in the dasync engine to triggered the callback already,
+ * so nothing can be done here
+ */
 }
 
 void
@@ -378,14 +390,14 @@ qat_polling_config ()
   int *config;
 
   config = &om->status[thread_index].poll_config;
-  if (*config)
+  if (PREDICT_TRUE (*config))
     return;
 
   ENGINE_ctrl_cmd (om->engine, "SET_INSTANCE_FOR_THREAD", thread_index,
                   NULL, NULL, 0);
   *config = 1;
 
-  TLS_DBG ("set thread %d and instance %d mapping\n", thread_index,
+  TLS_DBG (2, "set thread %d and instance %d mapping\n", thread_index,
           thread_index);
 
 }
@@ -394,14 +406,11 @@ void
 qat_polling ()
 {
   openssl_async_t *om = &openssl_async_main;
-  int ret;
+  int poll_status = 0;
 
   if (om->start_polling)
     {
-      qat_polling_config ();
-#define QAT_CMD_POLL (ENGINE_CMD_BASE + 1)
-      ENGINE_ctrl (om->engine, QAT_CMD_POLL, 0, &ret, NULL);
-      ;
+      ENGINE_ctrl_cmd (om->engine, "POLL", 0, &poll_status, NULL, 0);
     }
 }
 
@@ -462,7 +471,7 @@ tls_resume_from_crypto (int thread_index)
       if (*evt_run_head >= 0)
        {
          event = openssl_evt_get_w_thread (*evt_run_head, thread_index);
-         TLS_DBG ("event run = %d\n", *evt_run_head);
+         TLS_DBG (2, "event run = %d\n", *evt_run_head);
          tls_async_do_job (*evt_run_head, thread_index);
 
          *evt_run_head = event->next;
@@ -481,8 +490,6 @@ tls_resume_from_crypto (int thread_index)
 static clib_error_t *
 tls_async_init (vlib_main_t * vm)
 {
-
-  TLS_DBG ("Start to call tls_async_init\n");
   evt_pool_init (vm);
   return 0;
 
@@ -493,11 +500,16 @@ tls_async_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
                   vlib_frame_t * f)
 {
   u8 thread_index;
+  openssl_async_t *om = &openssl_async_main;
 
+  if (om->polling_conf)
+    (*om->polling_conf) ();
   thread_index = vlib_get_thread_index ();
-  openssl_async_polling ();
-
-  tls_resume_from_crypto (thread_index);
+  if (pool_elts (om->evt_pool[thread_index]) > 0)
+    {
+      openssl_async_polling ();
+      tls_resume_from_crypto (thread_index);
+    }
 
   return 0;
 }