git: ba045424b33d - main - tests/ssl_sendfile: size socket buffers before connecting

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Thu, 19 Nov 2026 21:55:06 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=ba045424b33d2a48f127adc8126d79d9eb82954a

commit ba045424b33d2a48f127adc8126d79d9eb82954a
Author:     Nick Banks <nickbanks@netflix.com>
AuthorDate: 2026-11-19 21:45:21 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-11-19 21:54:19 +0000

    tests/ssl_sendfile: size socket buffers before connecting
    
    The :grow, :truncate and :eagain_vs_eof cases rely on the socket buffers
    filling up before the whole file has been sent. :grow and :truncate shrank
    SO_SNDBUF and SO_RCVBUF only after the connection was established, when
    the receiver had already advertised a window based on the default buffer
    size; TCP does not shrink an advertised window. :eagain_vs_eof used the
    default, autosized buffers. With a large net.inet.tcp.recvspace, or once
    autosizing has grown the buffers, the whole 2 MB file fits in the socket
    buffers:
    
      :grow and :truncate: sendfile(2) returns before the file is resized, and
    the poll loop that waits for the sender to block spins forever on a
    writable socket.
      :eagain_vs_eof: the loop that keeps writing until
    EAGAIN never sees it, since sendfile(2) returns 0 at EOF.
    
    Either way kyua reports a timeout, which is why doubling the timeouts did
    not help. Apply a 128 KB buffer size to the listening socket, which the
    accepted socket inherits, and to the client socket before connect(2) in
    all three cases.
    
    Under CPU load in a test VM, :eagain_vs_eof timed out in 3 of 11 runs
    without this change and passed 40 of 40 runs with it. With 1 MB recvspace
    and sendspace, :grow hung on its first run in 3 of 4 boots without it and
    passed 20 of 20 runs with it.
    
    Differential Revision:  https://reviews.freebsd.org/D61752
    Reviewed by:            glebius, ngie
    PR:                     299447
    Co-Authored-By:         Claude Opus 5.5 <noreply@anthropic.com>
---
 tests/sys/kern/ssl_sendfile.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/tests/sys/kern/ssl_sendfile.c b/tests/sys/kern/ssl_sendfile.c
index adb3ee818437..4b36e5243a77 100644
--- a/tests/sys/kern/ssl_sendfile.c
+++ b/tests/sys/kern/ssl_sendfile.c
@@ -60,6 +60,7 @@ struct ctx {
 	off_t offset;		/* SSL_sendfile offset */
 	size_t size;		/* SSL_sendfile size */
 	bool nb;		/* SSL_sendfile mode */
+	int sbsize;		/* socket buffer size, 0 for default */
 	ossl_ssize_t sbytes;	/* SSL_sendfile returned sbytes */
 	enum {
 		INIT,
@@ -74,7 +75,7 @@ struct ctx {
 static void *server_thread(void *arg);
 
 static void
-common_init(struct ctx *c)
+common_init(struct ctx *c, int sbsize)
 {
 	char hostname[sizeof("localhost:65536")];
 	char tempname[] = "/tmp/ssl_sendfile_test.XXXXXXXXXX";
@@ -93,6 +94,7 @@ common_init(struct ctx *c)
 		atf_tc_skip("kern.ipc.tls.enable is off");
 
 	c->state = INIT;
+	c->sbsize = sbsize;
 
 	/*
 	 * Generate self signed key & certificate.
@@ -152,6 +154,9 @@ common_init(struct ctx *c)
 	ssl = c->cln = SSL_new(c->ctx);
 	ATF_REQUIRE(c->cln != NULL);
 	ATF_REQUIRE((c->cs = socket(AF_INET, SOCK_STREAM, 0)) > 0);
+	if (c->sbsize != 0)
+		ATF_REQUIRE(setsockopt(c->cs, SOL_SOCKET, SO_RCVBUF,
+		    &c->sbsize, sizeof(int)) == 0);
 	ATF_REQUIRE(connect(c->cs, (struct sockaddr *)&(struct sockaddr_in)
 	    { .sin_family = AF_INET, .sin_len = sizeof(struct sockaddr_in),
 	      .sin_addr.s_addr = htonl(INADDR_LOOPBACK), .sin_port = c->port },
@@ -207,6 +212,9 @@ server_thread(void *arg)
 	ATF_REQUIRE((s = socket(AF_INET, SOCK_STREAM, 0)) > 0);
 	ATF_REQUIRE(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1},
 	     sizeof(int)) == 0);
+	if (c->sbsize != 0)
+		ATF_REQUIRE(setsockopt(s, SOL_SOCKET, SO_SNDBUF, &c->sbsize,
+		    sizeof(int)) == 0);
 	ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
 	ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin,
 	    &(socklen_t){ sizeof(sin) }) == 0);
@@ -308,7 +316,7 @@ ATF_TC_BODY(basic, tc)
 	size_t nread;
 	int n;
 
-	common_init(&c);
+	common_init(&c, 0);
 
 	sendme(&c, 0, 0, false);
 	nread = 0;
@@ -328,7 +336,7 @@ ATF_TC_BODY(random, tc)
 	struct ctx c;
 #define	RSIZE	(256*1024)
 
-	common_init(&c);
+	common_init(&c, 0);
 
 	for (u_int i = 0; i < 10; i++) {
 		char buf[RSIZE];
@@ -363,12 +371,7 @@ ATF_TC_BODY(truncate, tc)
 	int n;
 #define	TRUNC	(FSIZE - 1024)
 
-	common_init(&c);
-
-	ATF_REQUIRE(setsockopt(c.ss, SOL_SOCKET, SO_SNDBUF, &(int){FSIZE / 16},
-	    sizeof(int)) == 0);
-	ATF_REQUIRE(setsockopt(c.cs, SOL_SOCKET, SO_RCVBUF, &(int){FSIZE / 16},
-	    sizeof(int)) == 0);
+	common_init(&c, FSIZE / 16);
 
 	sendme(&c, 0, 0, false);
 	/* Make sure sender is waiting on the socket buffer. */
@@ -398,12 +401,7 @@ ATF_TC_BODY(grow, tc)
 	int n;
 #define	GROW	(FSIZE/2)
 
-	common_init(&c);
-
-	ATF_REQUIRE(setsockopt(c.ss, SOL_SOCKET, SO_SNDBUF, &(int){FSIZE / 16},
-	    sizeof(int)) == 0);
-	ATF_REQUIRE(setsockopt(c.cs, SOL_SOCKET, SO_RCVBUF, &(int){FSIZE / 16},
-	    sizeof(int)) == 0);
+	common_init(&c, FSIZE / 16);
 
 	sendme(&c, 0, 0, false);
 	/* Make sure sender is waiting on the socket buffer. */
@@ -441,7 +439,7 @@ ATF_TC_BODY(offset_beyond_eof, tc)
 {
 	struct ctx c;
 
-	common_init(&c);
+	common_init(&c, 0);
 
 	c.sbytes = -1;
 	sendme(&c, FSIZE + 1, 0, false);
@@ -466,7 +464,7 @@ ATF_TC_BODY(eagain_vs_eof, tc)
 	ssize_t nread, offset = 0;
 	int n;
 
-	common_init(&c);
+	common_init(&c, FSIZE / 16);
 
 	/*
 	 * Exercise short write due to no buffer space on non-blocking