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


##########
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:
   Independently reproduced this against the `QUICStream::send_data()` method 
from f17974e1c4aed2c09f0d7bfc7cdafd9a9d2f146e, compiled in a standalone C++ 
harness with a fake adapter and transport:
   
   1. Offer a 32 KB final block and accept 16 KB, leaving 16 KB pending.
   2. Before the retry, reduce `stream_write_capacity()` to 1 byte, with 
sufficient underlying transport space to accept the pending block.
   3. Retry with a 256 KB event budget.
   
   The retry submitted and accepted **16384 bytes** and sent FIN despite the 
current capacity being **1 byte**. Adding the latest `len` to the `to_write` 
minimum in the harness reduced that submission to **1 byte** and correctly 
withheld FIN. This should have a unit regression alongside the 
carried-over-budget and partial-FIN tests; the configured limit is an ATS 
policy, so OpenSSL cannot enforce it for us.



-- 
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