git: b656161b84dc - stable/13 - netlink: make the maximum allowed netlink socket buffer runtime tunable.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Wed, 01 Mar 2023 14:29:43 UTC
The branch stable/13 has been updated by melifaro:

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

commit b656161b84dce0322ab00d7958267fa47cda6318
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-02-27 10:44:54 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-03-01 14:28:47 +0000

    netlink: make the maximum allowed netlink socket buffer runtime tunable.
    
    Dumping large routng tables (>1M paths with multipath) require the socket
     buffer which is larger than the currently defined limit.
    Allow the limit to be set in runtime, similar to kern.ipc.maxsockbuf.
    
    Reported by:    Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
    MFC after:      1 day
    
    (cherry picked from commit 28a5d88f7091d1fc72f4f1bd8562d3c8b15883f5)
---
 share/man/man4/netlink.4     |  6 ++++++
 sys/kern/uipc_socket.c       |  3 ++-
 sys/netlink/netlink_domain.c | 25 ++++++++++++++++++++++---
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/share/man/man4/netlink.4 b/share/man/man4/netlink.4
index a5cde2852158..e1a0c4b12933 100644
--- a/share/man/man4/netlink.4
+++ b/share/man/man4/netlink.4
@@ -280,6 +280,12 @@ Default receive buffer for the netlink socket.
 Note that the socket recvspace has to be least as long as the longest
 message that can be received from this socket.
 .El
+.Bl -tag -width indent
+.It Va net.netlink.nl_maxsockbuf
+Maximum receive buffer for the netlink socket that can be set via
+.Dv SO_RCVBUF
+socket option.
+.El
 .Sh DEBUGGING
 Netlink implements per-functional-unit debugging, with different severities
 controllable via the
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index fdf718de2483..5b1e572d786f 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -2976,12 +2976,13 @@ sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
 	return (0);
 }
 
+u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
+
 u_long
 sogetmaxbuf(struct socket *so)
 {
 	if (so->so_proto->pr_domain->dom_family != PF_NETLINK)
 		return (sb_max);
-	u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
 	return ((priv_check(curthread, PRIV_NET_ROUTE) == 0) ? nl_maxsockbuf : sb_max);
 }
 
diff --git a/sys/netlink/netlink_domain.c b/sys/netlink/netlink_domain.c
index c451274b1e63..348788434175 100644
--- a/sys/netlink/netlink_domain.c
+++ b/sys/netlink/netlink_domain.c
@@ -76,9 +76,12 @@ SYSCTL_ULONG(_net_netlink, OID_AUTO, recvspace, CTLFLAG_RW, &nl_recvspace, 0,
     "Default netlink socket receive space");
 
 extern u_long sb_max_adj;
-#if 0
-static u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
-#endif
+extern u_long nl_maxsockbuf;
+static int sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS);
+SYSCTL_OID(_net_netlink, OID_AUTO, nl_maxsockbuf,
+    CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &nl_maxsockbuf, 0,
+    sysctl_handle_nl_maxsockbuf, "LU",
+    "Maximum Netlink socket buffer size");
 
 uint32_t
 nlp_get_pid(const struct nlpcb *nlp)
@@ -673,6 +676,22 @@ nl_ctloutput(struct socket *so, struct sockopt *sopt)
 	return (error);
 }
 
+static int
+sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS)
+{
+	int error = 0;
+	u_long tmp_maxsockbuf = nl_maxsockbuf;
+
+	error = sysctl_handle_long(oidp, &tmp_maxsockbuf, arg2, req);
+	if (error || !req->newptr)
+		return (error);
+	if (tmp_maxsockbuf < MSIZE + MCLBYTES)
+		return (EINVAL);
+	nl_maxsockbuf = tmp_maxsockbuf;
+
+	return (0);
+}
+
 #if 0
 static int
 nl_setsbopt(struct socket *so, struct sockopt *sopt)