git: 28a5d88f7091 - main - netlink: make the maximum allowed netlink socket buffer runtime tunable.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Mon, 27 Feb 2023 10:57:44 UTC
The branch main has been updated by melifaro:

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

commit 28a5d88f7091d1fc72f4f1bd8562d3c8b15883f5
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-02-27 10:44:54 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-02-27 10:48:31 +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
---
 share/man/man4/netlink.4     |  6 ++++++
 sys/netlink/netlink_domain.c | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/share/man/man4/netlink.4 b/share/man/man4/netlink.4
index fdcc823b01a6..ece4892f481e 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/netlink/netlink_domain.c b/sys/netlink/netlink_domain.c
index 5f9120f14308..01023f7244b6 100644
--- a/sys/netlink/netlink_domain.c
+++ b/sys/netlink/netlink_domain.c
@@ -77,6 +77,11 @@ SYSCTL_ULONG(_net_netlink, OID_AUTO, recvspace, CTLFLAG_RW, &nl_recvspace, 0,
 
 extern u_long sb_max_adj;
 static u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
+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)
@@ -675,6 +680,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);
+}
+
 static int
 nl_setsbopt(struct socket *so, struct sockopt *sopt)
 {