4928626
4928626
http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/ssl/ssl_engine_io.c?r1=1836237&r2=1836236&pathrev=1836237&view=patch
4928626
http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/ssl/ssl_engine_io.c?r1=1873985&r2=1876037&view=patch
4928626
4928626
--- httpd-2.4.43/modules/ssl/ssl_engine_io.c.sslcoalesce
4928626
+++ httpd-2.4.43/modules/ssl/ssl_engine_io.c
4928626
@@ -1585,18 +1585,32 @@
4928626
 }
4928626
 
4928626
 
4928626
-/* ssl_io_filter_output() produces one SSL/TLS message per bucket
4928626
+/* ssl_io_filter_output() produces one SSL/TLS record per bucket
4928626
  * passed down the output filter stack.  This results in a high
4928626
- * overhead (network packets) for any output comprising many small
4928626
- * buckets.  SSI page applied through the HTTP chunk filter, for
4928626
- * example, may produce many brigades containing small buckets -
4928626
- * [chunk-size CRLF] [chunk-data] [CRLF].
4928626
+ * overhead (more network packets & TLS processing) for any output
4928626
+ * comprising many small buckets.  SSI output passed through the HTTP
4928626
+ * chunk filter, for example, may produce many brigades containing
4928626
+ * small buckets - [chunk-size CRLF] [chunk-data] [CRLF].
4928626
  *
4928626
- * The coalescing filter merges many small buckets into larger buckets
4928626
- * where possible, allowing the SSL I/O output filter to handle them
4928626
- * more efficiently. */
4928626
+ * Sending HTTP response headers as a separate TLS record to the
4928626
+ * response body also reveals information to a network observer (the
4928626
+ * size of headers) which can be significant.
4928626
+ *
4928626
+ * The coalescing filter merges data buckets with the aim of producing
4928626
+ * fewer, larger TLS records - without copying/buffering all content
4928626
+ * and introducing unnecessary overhead.
4928626
+ *
4928626
+ * ### This buffering could be probably be done more comprehensively
4928626
+ * ### in ssl_io_filter_output itself. 
4928626
+ * 
4928626
+ * ### Another possible performance optimisation in particular for the
4928626
+ * ### [HEAP] [FILE] HTTP response case is using a brigade rather than
4928626
+ * ### a char array to buffer; using apr_brigade_write() to append
4928626
+ * ### will use already-allocated memory from the HEAP, reducing # of
4928626
+ * ### copies.
4928626
+ */
4928626
 
4928626
-#define COALESCE_BYTES (2048)
4928626
+#define COALESCE_BYTES (AP_IOBUFSIZE)
4928626
 
4928626
 struct coalesce_ctx {
4928626
     char buffer[COALESCE_BYTES];
4928626
@@ -1609,11 +1623,12 @@
4928626
     apr_bucket *e, *upto;
4928626
     apr_size_t bytes = 0;
4928626
     struct coalesce_ctx *ctx = f->ctx;
4928626
+    apr_size_t buffered = ctx ? ctx->bytes : 0; /* space used on entry */
4928626
     unsigned count = 0;
4928626
 
4928626
     /* The brigade consists of zero-or-more small data buckets which
4928626
-     * can be coalesced (the prefix), followed by the remainder of the
4928626
-     * brigade.
4928626
+     * can be coalesced (referred to as the "prefix"), followed by the
4928626
+     * remainder of the brigade.
4928626
      *
4928626
      * Find the last bucket - if any - of that prefix.  count gives
4928626
      * the number of buckets in the prefix.  The "prefix" must contain
4928626
@@ -1628,24 +1643,97 @@
4928626
          e != APR_BRIGADE_SENTINEL(bb)
4928626
              && !APR_BUCKET_IS_METADATA(e)
4928626
              && e->length != (apr_size_t)-1
4928626
-             && e->length < COALESCE_BYTES
4928626
-             && (bytes + e->length) < COALESCE_BYTES
4928626
-             && (ctx == NULL
4928626
-                 || bytes + ctx->bytes + e->length < COALESCE_BYTES);
4928626
+             && e->length <= COALESCE_BYTES
4928626
+             && (buffered + bytes + e->length) <= COALESCE_BYTES;
4928626
          e = APR_BUCKET_NEXT(e)) {
4928626
-        if (e->length) count++; /* don't count zero-length buckets */
4928626
-        bytes += e->length;
4928626
+        /* don't count zero-length buckets */
4928626
+        if (e->length) {
4928626
+            bytes += e->length;
4928626
+            count++;
4928626
+        }
4928626
     }
4928626
+
4928626
+    /* If there is room remaining and the next bucket is a data
4928626
+     * bucket, try to include it in the prefix to coalesce.  For a
4928626
+     * typical [HEAP] [FILE] HTTP response brigade, this handles
4928626
+     * merging the headers and the start of the body into a single TLS
4928626
+     * record. */
4928626
+    if (bytes + buffered > 0
4928626
+        && bytes + buffered < COALESCE_BYTES
4928626
+        && e != APR_BRIGADE_SENTINEL(bb)
4928626
+        && !APR_BUCKET_IS_METADATA(e)) {
4928626
+        apr_status_t rv = APR_SUCCESS;
4928626
+
4928626
+        /* For an indeterminate length bucket (PIPE/CGI/...), try a
4928626
+         * non-blocking read to have it morph into a HEAP.  If the
4928626
+         * read fails with EAGAIN, it is harmless to try a split
4928626
+         * anyway, split is ENOTIMPL for most PIPE-like buckets. */
4928626
+        if (e->length == (apr_size_t)-1) {
4928626
+            const char *discard;
4928626
+            apr_size_t ignore;
4928626
+
4928626
+            rv = apr_bucket_read(e, &discard, &ignore, APR_NONBLOCK_READ);
4928626
+            if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
4928626
+                ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, f->c, APLOGNO(10232)
4928626
+                              "coalesce failed to read from %s bucket",
4928626
+                              e->type->name);
4928626
+                return AP_FILTER_ERROR;
4928626
+            }
4928626
+        }
4928626
+
4928626
+        if (rv == APR_SUCCESS) {
4928626
+            /* If the read above made the bucket morph, it may now fit
4928626
+             * entirely within the buffer.  Otherwise, split it so it does
4928626
+             * fit. */
4928626
+            if (e->length > COALESCE_BYTES
4928626
+                || e->length + buffered + bytes > COALESCE_BYTES) {
4928626
+                rv = apr_bucket_split(e, COALESCE_BYTES - (buffered + bytes));
4928626
+            }
4928626
+
4928626
+            if (rv == APR_SUCCESS && e->length == 0) {
4928626
+                /* As above, don't count in the prefix if the bucket is
4928626
+                 * now zero-length. */
4928626
+            }
4928626
+            else if (rv == APR_SUCCESS) {
4928626
+                ap_log_cerror(APLOG_MARK, APLOG_TRACE4, 0, f->c,
4928626
+                              "coalesce: adding %" APR_SIZE_T_FMT " bytes "
4928626
+                              "from split %s bucket, total %" APR_SIZE_T_FMT,
4928626
+                              e->length, e->type->name, bytes + buffered);
4928626
+
4928626
+                count++;
4928626
+                bytes += e->length;
4928626
+                e = APR_BUCKET_NEXT(e);
4928626
+            }
4928626
+            else if (rv != APR_ENOTIMPL) {
4928626
+                ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, f->c, APLOGNO(10233)
4928626
+                              "coalesce: failed to split data bucket");
4928626
+                return AP_FILTER_ERROR;
4928626
+            }
4928626
+        }
4928626
+    }
4928626
+
4928626
     upto = e;
4928626
 
4928626
-    /* Coalesce the prefix, if:
4928626
-     * a) more than one bucket is found to coalesce, or
4928626
-     * b) the brigade contains only a single data bucket, or
4928626
-     * c) the data bucket is not last but we have buffered data already.
4928626
+    /* Coalesce the prefix, if any of the following are true:
4928626
+     * 
4928626
+     * a) the prefix is more than one bucket
4928626
+     * OR
4928626
+     * b) the prefix is the entire brigade, which is a single bucket
4928626
+     *    AND the prefix length is smaller than the buffer size,
4928626
+     * OR
4928626
+     * c) the prefix is a single bucket
4928626
+     *    AND there is buffered data from a previous pass.
4928626
+     * 
4928626
+     * The aim with (b) is to buffer a small bucket so it can be
4928626
+     * coalesced with future invocations of this filter.  e.g.  three
4928626
+     * calls each with a single 100 byte HEAP bucket should get
4928626
+     * coalesced together.  But an invocation with a 8192 byte HEAP
4928626
+     * should pass through untouched.
4928626
      */
4928626
     if (bytes > 0
4928626
         && (count > 1
4928626
-            || (upto == APR_BRIGADE_SENTINEL(bb))
4928626
+            || (upto == APR_BRIGADE_SENTINEL(bb)
4928626
+                && bytes < COALESCE_BYTES)
4928626
             || (ctx && ctx->bytes > 0))) {
4928626
         /* If coalescing some bytes, ensure a context has been
4928626
          * created. */
4928626
@@ -1656,7 +1744,8 @@
4928626
 
4928626
         ap_log_cerror(APLOG_MARK, APLOG_TRACE4, 0, f->c,
4928626
                       "coalesce: have %" APR_SIZE_T_FMT " bytes, "
4928626
-                      "adding %" APR_SIZE_T_FMT " more", ctx->bytes, bytes);
4928626
+                      "adding %" APR_SIZE_T_FMT " more (buckets=%u)",
4928626
+                      ctx->bytes, bytes, count);
4928626
 
4928626
         /* Iterate through the prefix segment.  For non-fatal errors
4928626
          * in this loop it is safe to break out and fall back to the
4928626
@@ -1671,7 +1760,8 @@
4928626
             if (APR_BUCKET_IS_METADATA(e)
4928626
                 || e->length == (apr_size_t)-1) {
4928626
                 ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, f->c, APLOGNO(02012)
4928626
-                              "unexpected bucket type during coalesce");
4928626
+                              "unexpected %s bucket during coalesce",
4928626
+                              e->type->name);
4928626
                 break; /* non-fatal error; break out */
4928626
             }
4928626