Front End for QUIC Thread Assisted Mode
authorHugo Landau <hlandau@openssl.org>
Tue, 21 Feb 2023 10:18:59 +0000 (10:18 +0000)
committerHugo Landau <hlandau@openssl.org>
Thu, 30 Mar 2023 10:14:08 +0000 (11:14 +0100)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20348)

ssl/quic/quic_impl.c
ssl/quic/quic_local.h

index 1ab6b8ba8062f70d5205ea8849d58282bf648a6e..73071d4a73014b993d0e192e9ba644f57a083b48 100644 (file)
@@ -161,6 +161,9 @@ SSL *ossl_quic_new(SSL_CTX *ctx)
     if ((qc->mutex = CRYPTO_THREAD_lock_new()) == NULL)
         goto err;
 
+    qc->is_thread_assisted
+        = (ssl_base->method == OSSL_QUIC_client_thread_method());
+
     /* Channel is not created yet. */
     qc->ssl_mode   = qc->ssl.ctx->mode;
     qc->last_error = SSL_ERROR_NONE;
@@ -185,6 +188,10 @@ void ossl_quic_free(SSL *s)
         return;
 
     quic_lock(qc); /* best effort */
+
+    if (qc->is_thread_assisted && qc->started)
+        ossl_quic_thread_assist_wait_stopped(&qc->thread_assist);
+
     ossl_quic_channel_free(qc->ch);
 
     BIO_free(qc->net_rbio);
@@ -694,21 +701,24 @@ static int ensure_channel_and_start(QUIC_CONNECTION *qc)
         return 0;
 
     if (!configure_channel(qc)
-        || !ossl_quic_channel_start(qc->ch)) {
-        ossl_quic_channel_free(qc->ch);
-        qc->ch = NULL;
-        return 0;
-    }
+        || !ossl_quic_channel_start(qc->ch))
+        goto err;
 
     qc->stream0 = ossl_quic_channel_get_stream_by_id(qc->ch, 0);
-    if (qc->stream0 == NULL) {
-        ossl_quic_channel_free(qc->ch);
-        qc->ch = NULL;
-        return 0;
-    }
+    if (qc->stream0 == NULL)
+        goto err;
+
+    if (qc->is_thread_assisted)
+        if (!ossl_quic_thread_assist_init_start(&qc->thread_assist, qc->ch))
+            goto err;
 
     qc->started = 1;
     return 1;
+
+err:
+    ossl_quic_channel_free(qc->ch);
+    qc->ch = NULL;
+    return 0;
 }
 
 QUIC_NEEDS_LOCK
index 300332307c129e021300b7cb986365499c5199ab..d40505267459ebb2e8ff8203560c0b9c8ee18578 100644 (file)
@@ -21,6 +21,7 @@
 # include "internal/quic_stream.h"
 # include "internal/quic_channel.h"
 # include "internal/quic_reactor.h"
+# include "internal/quic_thread_assist.h"
 # include "../ssl_local.h"
 
 # ifndef OPENSSL_NO_QUIC
@@ -65,6 +66,11 @@ struct quic_conn_st {
     /* Initial peer L4 address. */
     BIO_ADDR                        init_peer_addr;
 
+#ifndef OPENSSL_NO_QUIC_THREAD_ASSIST
+    /* Manages thread for QUIC thread assisted mode. */
+    QUIC_THREAD_ASSIST              thread_assist;
+#endif
+
     /* Have we started? */
     unsigned int                    started                 : 1;
 
@@ -81,6 +87,9 @@ struct quic_conn_st {
      */
     unsigned int                    as_server               : 1;
 
+    /* Are we using thread assisted mode? Never changes after init. */
+    unsigned int                    is_thread_assisted      : 1;
+
     /*
      * This state tracks SSL_write all-or-nothing (AON) write semantics
      * emulation.