Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Ishu Brar via B4 Relay
Wed, 02 Dec 2026 07:09
[PATCH RFC net-next 0/2] af_unix: Per-socket datagram queue length
Author: Ishu Brar via B4 Relay
Date: Wed, 02 Dec 2026 07:09
Date: Wed, 02 Dec 2026 07:09
55 lines
12873 bytes
12873 bytes
Datagram queue length of AF_UNIX socket is copied from
net.unix.max_dgram_qlen at creation, can't be changed later. journald's
/dev/log is created by PID 1 before sysctl.d runs, and kept across
journald restarts, so admin's value never reaches it, all syslog(3)
callers share one queue, systemd works around it with global sysctl bump
early in boot [1]. Lennart asked for socket option instead [1], it's
also KF.71 on UAPI group wishlist [2].
Patch 1 adds SO_MAX_DGRAM_QLEN for SOCK_DGRAM sockets, patch 2 adds
selftest. Tested in virtme-ng VM (x86_64, clang, PROVE_LOCKING), 6/6
pass, no lockdep reports, both wakeup cases fail when wake_up call is
removed.
Questions for this RFC:
1. Permission, going above netns sysctl needs CAP_NET_ADMIN in user ns
owning netns, since sysctl can be set there after commit cec16052d5a7
("net: Enable max_dgram_qlen unix sysctl to be configurable by
non-init user namespaces"). Or init_user_ns, like SO_RCVBUFFORCE?
2. Read-back, no getsockopt(), af_unix has no unix_getsockopt() yet, and
sock_diag shows sk_max_ack_backlog only for listening sockets. Add
one of them?
SO_RCVBUF is no alternative, AF_UNIX datagram sockets don't check
receiver's sk_rcvbuf.
AI was used to write selftest and edit this message
[1] https://github.com/systemd/systemd/issues/45144
[2] https://github.com/uapi-group/kernel-features
Signed-off-by: Ishu Brar <patches@brar.si>
---
Ishu Brar (2):
af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN
arch/alpha/include/uapi/asm/socket.h | 2 +
arch/mips/include/uapi/asm/socket.h | 2 +
arch/parisc/include/uapi/asm/socket.h | 2 +
arch/sparc/include/uapi/asm/socket.h | 2 +
include/uapi/asm-generic/socket.h | 2 +
net/unix/af_unix.c | 27 ++-
tools/testing/selftests/net/af_unix/Makefile | 1 +
tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
8 files changed, 280 insertions(+), 1 deletion(-)
---
base-commit: 0a837a30e55bbea2afb01cf0b3ec0c1d789a73ad
change-id: 20261202-af-unix-dgram-qlen-1112c811d5cf
Best regards,
--
Ishu Brar <patches@brar.si>
[PATCH RFC net-next 2/2] selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN
Author: Ishu Brar via B4 Relay
Date: Wed, 02 Dec 2026 07:09
Date: Wed, 02 Dec 2026 07:09
285 lines
17728 bytes
17728 bytes
From: Ishu Brar <patches@brar.si>
Check SO_MAX_DGRAM_QLEN rejects bad values and non-datagram sockets,
limit applies, lowering keeps queued datagrams, raising wakes sender
blocked in send() and one waiting in poll() within 1s, and unprivileged
user can't go above sysctl.
Receiver is bound socket, not socketpair(), receiver whose peer is
sender is exempt from queue limit.
Wakeup cases checked to fail with wake_up call removed from kernel side.
Claude wrote these tests
Signed-off-by: Ishu Brar <patches@brar.si>
---
tools/testing/selftests/net/af_unix/Makefile | 1 +
tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
2 files changed, 244 insertions(+)
diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
index fb2e09f0f44b..110cc339598b 100644
--- a/tools/testing/selftests/net/af_unix/Makefile
+++ b/tools/testing/selftests/net/af_unix/Makefile
@@ -6,6 +6,7 @@ cc-option = $(call __cc-option, $(CC),,$(1),$(2))
CFLAGS += $(KHDR_INCLUDES) -Wall $(call cc-option,-Wflex-array-member-not-at-end)
TEST_GEN_PROGS := \
+ dgram_qlen \
diag_uid \
scm_inq \
scm_pidfd \
diff --git a/tools/testing/selftests/net/af_unix/dgram_qlen.c b/tools/testing/selftests/net/af_unix/dgram_qlen.c
new file mode 100644
index 000000000000..7a5f42339731
--- /dev/null
+++ b/tools/testing/selftests/net/af_unix/dgram_qlen.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/* SO_MAX_DGRAM_QLEN: per-socket datagram queue length for AF_UNIX. */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <poll.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#ifndef SO_MAX_DGRAM_QLEN
+#define SO_MAX_DGRAM_QLEN 89
+#endif
+
+FIXTURE(dgram_qlen)
+{
+ struct sockaddr_un addr;
+ socklen_t addrlen;
+ int rcv;
+};
+
+FIXTURE_SETUP(dgram_qlen)
+{
+ /* A bound receiver, not a socketpair: a receiver whose peer is the
+ * sender is exempt from the queue limit in unix_dgram_sendmsg().
+ */
+ self->addr.sun_family = AF_UNIX;
+ self->addrlen = offsetof(struct sockaddr_un, sun_path) + 1 +
+ snprintf(self->addr.sun_path + 1,
+ sizeof(self->addr.sun_path) - 1,
+ "dgram_qlen.%d", getpid());
+
+ self->rcv = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
+ ASSERT_LE(0, self->rcv);
+ ASSERT_EQ(0, bind(self->rcv, (struct sockaddr *)&self->addr,
+ self->addrlen));
+}
+
+FIXTURE_TEARDOWN(dgram_qlen)
+{
+ close(self->rcv);
+}
+
+static int sender(FIXTURE_DATA(dgram_qlen) *self, int flags)
+{
+ int fd = socket(AF_UNIX, SOCK_DGRAM | flags, 0);
+
+ if (fd < 0)
+ return -1;
+ if (connect(fd, (struct sockaddr *)&self->addr, self->addrlen)) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+static int set_qlen(int fd, int val)
+{
+ return setsockopt(fd, SOL_SOCKET, SO_MAX_DGRAM_QLEN, &val, sizeof(val));
+}
+
+/* Send 1-byte datagrams until the queue is full; return how many fit. */
+static int fill(int fd)
+{
+ int n = 0;
+
+ while (send(fd, "x", 1, 0) == 1)
+ n++;
+ return errno == EAGAIN ? n : -errno;
+}
+
+static int drain(int fd)
+{
+ char c;
+ int n = 0;
+
+ while (recv(fd, &c, 1, 0) == 1)
+ n++;
+ return n;
+}
+
+/* Milliseconds since @start. */
+static long since_ms(const struct timespec *start)
+{
+ struct timespec now;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ return (now.tv_sec - start->tv_sec) * 1000 +
+ (now.tv_nsec - start->tv_nsec) / 1000000;
+}
+
+static int sysctl_qlen(void)
+{
+ FILE *f = fopen("/proc/sys/net/unix/max_dgram_qlen", "r");
+ int val = -1;
+
+ if (f) {
+ if (fscanf(f, "%d", &val) != 1)
+ val = -1;
+ fclose(f);
+ }
+ return val;
+}
+
+TEST_F(dgram_qlen, bad_values)
+{
+ int stream = socket(AF_UNIX, SOCK_STREAM, 0);
+
+ ASSERT_EQ(-1, set_qlen(self->rcv, -1));
+ ASSERT_EQ(EINVAL, errno);
+
+ ASSERT_EQ(-1, set_qlen(stream, 4));
+ ASSERT_EQ(ENOPROTOOPT, errno);
+ close(stream);
+}
+
+/* Same off-by-one as the sysctl: a limit of N admits N + 1 datagrams. */
+TEST_F(dgram_qlen, limit_applies)
+{
+ int fd = sender(self, SOCK_NONBLOCK);
+
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, set_qlen(self->rcv, 4));
+ ASSERT_EQ(5, fill(fd));
+ close(fd);
+}
+
+TEST_F(dgram_qlen, lower_keeps_queue)
+{
+ int fd = sender(self, SOCK_NONBLOCK);
+
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, set_qlen(self->rcv, 8));
+ ASSERT_EQ(9, fill(fd));
+
+ ASSERT_EQ(0, set_qlen(self->rcv, 2));
+ ASSERT_EQ(0, fill(fd));
+ ASSERT_EQ(9, drain(self->rcv));
+ ASSERT_EQ(3, fill(fd));
+ close(fd);
+}
+
+/* Raising the limit must wake a sender blocked in send(), with no recv(). */
+TEST_F(dgram_qlen, raise_wakes_blocked_sender)
+{
+ struct timeval tv = { .tv_sec = 3 };
+ int fd = sender(self, 0), status;
+ pid_t pid;
+
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, set_qlen(self->rcv, 2));
+ ASSERT_EQ(0, setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)));
+ while (send(fd, "x", 1, MSG_DONTWAIT) == 1)
+ ;
+
+ pid = fork();
+ ASSERT_LE(0, pid);
+ if (pid == 0) {
+ struct timespec t;
+ int ret;
+
+ /* Without a wakeup, send() sleeps out the 3 s timeout. */
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ ret = send(fd, "x", 1, 0);
+ ksft_print_msg("send() returned %d after %ld ms\n",
+ ret, since_ms(&t));
+ _exit(ret == 1 && since_ms(&t) < 1000 ? 0 : 1);
+ }
+
+ usleep(200 * 1000);
+ ASSERT_EQ(0, set_qlen(self->rcv, 100));
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_EQ(0, WEXITSTATUS(status));
+ close(fd);
+}
+
+/* Same for a sender waiting in poll() for POLLOUT. */
+TEST_F(dgram_qlen, raise_wakes_poller)
+{
+ int fd = sender(self, SOCK_NONBLOCK), status;
+ struct pollfd pfd = { .events = POLLOUT };
+ pid_t pid;
+
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, set_qlen(self->rcv, 2));
+ ASSERT_EQ(3, fill(fd));
+
+ pid = fork();
+ ASSERT_LE(0, pid);
+ if (pid == 0) {
+ struct timespec t;
+ int ret;
+
+ pfd.fd = fd;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ ret = poll(&pfd, 1, 3000);
+ ksft_print_msg("poll() returned %d revents %#x after %ld ms\n",
+ ret, pfd.revents, since_ms(&t));
+ _exit(ret == 1 && (pfd.revents & POLLOUT) &&
+ since_ms(&t) < 1000 ? 0 : 1);
+ }
+
+ usleep(200 * 1000);
+ ASSERT_EQ(0, set_qlen(self->rcv, 100));
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_EQ(0, WEXITSTATUS(status));
+ close(fd);
+}
+
+/* Without CAP_NET_ADMIN, the sysctl value is the ceiling. */
+TEST_F(dgram_qlen, unprivileged_ceiling)
+{
+ int max = sysctl_qlen(), status;
+ pid_t pid;
+
+ if (geteuid())
+ SKIP(return, "needs root to drop to nobody");
+ ASSERT_LE(0, max);
+
+ pid = fork();
+ ASSERT_LE(0, pid);
+ if (pid == 0) {
+ if (setresuid(65534, 65534, 65534))
+ _exit(10);
+ if (set_qlen(self->rcv, max))
+ _exit(11);
+ if (set_qlen(self->rcv, max + 1) != -1 || errno != EPERM)
+ _exit(12);
+ _exit(0);
+ }
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_EQ(0, WEXITSTATUS(status));
+
+ ASSERT_EQ(0, set_qlen(self->rcv, max + 1)); /* root may */
+}
+
+TEST_HARNESS_MAIN
--
2.56.1
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads