git: 08384f613cd8 - stable/13 - sockets: virtualize kern.ipc.numopensockets

From: Eugene Grosbein <eugen_at_FreeBSD.org>
Date: Fri, 17 Jan 2025 19:42:10 UTC
The branch stable/13 has been updated by eugen:

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

commit 08384f613cd808e31fa3ae3dd86fa078b49d3f4b
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-01-13 18:08:58 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2025-01-17 19:41:51 +0000

    sockets: virtualize kern.ipc.numopensockets
    
    To avoid breaking POLA on the host machine it reports the same value as
    before.  In a VNET jail it now reports number of sockets in this jail.
    
    PR:                     219655
    Differential Revision:  https://reviews.freebsd.org/D48315
    
    (cherry picked from commit 59498e099cc055da7afca8266087b7668be6d7cb)
---
 sys/kern/uipc_socket.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 81145ff1e1ac..24cb885504c9 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -246,9 +246,23 @@ SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
     sizeof(int), sysctl_somaxconn, "I",
     "Maximum listen socket pending connection accept queue size (compat)");
 
-static int numopensockets;
-SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
-    &numopensockets, 0, "Number of open sockets");
+static u_int numopensockets;
+static int
+sysctl_numopensockets(SYSCTL_HANDLER_ARGS)
+{
+	u_int val;
+
+#ifdef VIMAGE
+	if(!IS_DEFAULT_VNET(curvnet))
+		val = curvnet->vnet_sockcnt;
+	else
+#endif
+		val = numopensockets;
+	return (sysctl_handle_int(oidp, &val, 0, req));
+}
+SYSCTL_PROC(_kern_ipc, OID_AUTO, numopensockets,
+    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_VNET, 0, sizeof(u_int),
+    sysctl_numopensockets, "IU", "Number of open sockets");
 
 /*
  * accept_mtx locks down per-socket fields relating to accept queues.  See