bryancall commented on code in PR #14034:
URL: https://github.com/apache/trafficserver/pull/14034#discussion_r4554992269


##########
src/iocore/net/OpenSSLQUICNetVConnection.cc:
##########
@@ -893,12 +988,39 @@ QUICNetVConnection::stream_write_capacity(QUICStreamId 
stream_id)
     return -1;
   }
 
-  uint64_t avail = 0;
-  if (SSL_get_stream_write_buf_avail(it->second, &avail) == 1) {
-    return std::min<uint64_t>(avail, 16 * 1024);
+  // OpenSSL starts a stream's send buffer small and grows it only when one 
write
+  // offers more than the buffer has free. The buffer keeps data until the peer
+  // acknowledges it, so offering no more than the free room would hold the 
stream to
+  // one initial buffer in flight. Offer room up to the configured limits for 
the
+  // stream and for the connection instead; OpenSSL takes less if the stream's 
flow
+  // control window allows less.
+  uint64_t size = 0;
+  uint64_t used = 0;
+  if (SSL_get_stream_write_buf_size(it->second, &size) != 1 || 
SSL_get_stream_write_buf_used(it->second, &used) != 1) {
+    unsigned long const error = ERR_get_error();
+    QUICConDebug("stream %" PRIu64 ": cannot read OpenSSL's send buffer state: 
%s", stream_id, ERR_error_string(error, nullptr));
+    return -1;
   }
 
-  return 0;
+  // OpenSSL frees the send buffer once the peer has acknowledged everything 
up to the
+  // FIN, and when the sending side is reset. Either way nothing more can be 
written. A
+  // write can process incoming packets, so the reset may arrive partway 
through a pass.
+  if (size == 0) {
+    return this->_openssl_stream_write_closed(stream_id, it->second) ? -1 : 0;
+  }
+
+  // Once the connection limit binds, room frees a little at a time as the 
peer acknowledges
+  // data, and every pass offers it to the streams in the same order. Holding 
each stream with
+  // data to an equal share of the limit leaves that room to the streams below 
their share, so
+  // no stream can take it all while another waits.
+  uint64_t const share = std::max(this->_max_connection_send_buffer / 
std::max<uint64_t>(this->_openssl_streams_with_data, 1),
+                                  OPENSSL_MIN_STREAM_SEND_SHARE);
+  uint64_t const stream_limit    = std::min(this->_max_stream_send_buffer, 
share);
+  uint64_t const stream_room     = used < stream_limit ? stream_limit - used : 
0;
+  uint64_t const connection_room = this->_openssl_send_buffered < 
this->_max_connection_send_buffer ?
+                                     this->_max_connection_send_buffer - 
this->_openssl_send_buffered :
+                                     0;
+  return std::min(stream_room, connection_room);

Review Comment:
   Thanks, both. Fixed in 74e53b686: a carried-over block is now capped by the 
write capacity read at the top of that pass as well as by the event budget, and 
the FIN only goes with the submission that carries the last byte. As Brian 
says, the limit is ATS policy, so OpenSSL can't enforce it for us. Two unit 
tests cover it, and both fail with the cap taken out: in the one added with the 
fix, the stream takes 40 KB of a 100 KB body and the capacity then drops to 10 
KB, where the retry used to submit the remaining 60 KB with the FIN and now 
submits 10 KB without it, then the last 50 KB with the FIN; 864a55902 adds 
Brian's exact case, a 32 KB final block with 16 KB accepted and then 1 byte of 
room under a 256 KB budget, where the retry used to submit 16384 bytes with the 
FIN and now submits 1 byte without it, with the last 16383 bytes and the FIN 
going once there is room. On the quiche-based backends the capacity is what 
quiche accepts anyway, so nothing changes there.



##########
doc/admin-guide/files/records.yaml.en.rst:
##########
@@ -6428,6 +6428,38 @@ removed in the future without prior notice.
 
    Specified the maximum outgoing UDP payload size.
 
+.. ts:cv:: CONFIG proxy.config.quic.max_stream_send_buffer INT 1048576
+   :units: bytes
+
+   The most stream data, in bytes, that |TS| hands the OpenSSL QUIC stack for 
one
+   stream at a time. The count covers data not yet sent and data sent but not 
yet
+   acknowledged by the client. It applies to the OpenSSL QUIC backend only.
+
+   This trades throughput against memory. A stream cannot have more than this 
much
+   data in flight, so it caps one stream's rate at about this value divided by 
the
+   round trip time: the default allows about 840 Mbps per stream at a 10 ms 
round
+   trip. OpenSSL grows a stream's send buffer to fit what it is given, up to 
its
+   own limit of 6 MB, and does not shrink it again until the stream ends, so 
each
+   busy stream can keep up to this much memory for its lifetime. See also
+   :ts:cv:`proxy.config.quic.max_connection_send_buffer`.
+
+.. ts:cv:: CONFIG proxy.config.quic.max_connection_send_buffer INT 4194304
+   :units: bytes
+
+   The most stream data, in bytes, that |TS| hands the OpenSSL QUIC stack for 
all
+   the streams of one connection together, counted the same way as
+   :ts:cv:`proxy.config.quic.max_stream_send_buffer`. It applies to the 
OpenSSL QUIC
+   backend only.
+
+   This bounds the memory one connection's send buffers can take, which would
+   otherwise grow with the number of streams the client opens. A lower value 
saves
+   memory; a value below the per-stream limit times the number of busy streams 
on
+   a connection lowers their combined rate. Each stream that has data to send 
may

Review Comment:
   Agreed, and thanks Brian for the pointer to the OpenSSL page. f955ff68e and 
ae9b3d722 rework the doc: the record bounds data not yet sent or acknowledged, 
not the memory allocated for the send buffers. ATS counts what OpenSSL reports 
as used in each stream's buffer, not the size it has allocated, and a buffer 
OpenSSL has grown keeps that size until its stream ends, so a connection whose 
busy streams stay open can keep about 
`proxy.config.quic.max_stream_send_buffer` of buffer for each of them, which 
can be more than this limit. The doc now says that and links the [OpenSSL 
buffer API 
documentation](https://docs.openssl.org/3.5/man3/SSL_get_value_uint/). 
c63cb77d6 also lowers the defaults to 256 KB per stream and 1 MB per 
connection, which takes the most a connection with 100 busy streams can keep 
from about 100 MB to about 25 MB. Nothing bounds that across the whole process 
yet; I'll do a process-wide cap as a follow-up, and the PR description now says 
so.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to