From 68bc6beb233c9c69cd8060f9642030dfd5645e18 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sat, 12 Oct 2024 01:20:04 +0100 Subject: [PATCH] Expose socket option SO_RCVBUFFORCE (#1174) --- src/backend/libc/net/sockopt.rs | 6 ++++++ src/backend/linux_raw/c.rs | 8 ++++---- src/backend/linux_raw/net/sockopt.rs | 6 ++++++ src/net/sockopt.rs | 11 +++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/backend/libc/net/sockopt.rs b/src/backend/libc/net/sockopt.rs index 7824e69a1..d72b3fd6a 100644 --- a/src/backend/libc/net/sockopt.rs +++ b/src/backend/libc/net/sockopt.rs @@ -344,6 +344,12 @@ pub(crate) fn set_socket_recv_buffer_size(fd: BorrowedFd<'_>, size: usize) -> io setsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUF, size) } +#[inline] +pub(crate) fn set_socket_recv_buffer_size_force(fd: BorrowedFd<'_>, size: usize) -> io::Result<()> { + let size: c::c_int = size.try_into().map_err(|_| io::Errno::INVAL)?; + setsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUFFORCE, size) +} + #[inline] pub(crate) fn get_socket_recv_buffer_size(fd: BorrowedFd<'_>) -> io::Result { getsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUF).map(|size: u32| size as usize) diff --git a/src/backend/linux_raw/c.rs b/src/backend/linux_raw/c.rs index 95f701b16..c4e1001b7 100644 --- a/src/backend/linux_raw/c.rs +++ b/src/backend/linux_raw/c.rs @@ -74,10 +74,10 @@ pub(crate) use linux_raw_sys::{ SCM_RIGHTS, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SOL_XDP, SO_ACCEPTCONN, SO_BROADCAST, SO_COOKIE, SO_DOMAIN, SO_ERROR, SO_INCOMING_CPU, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_ORIGINAL_DST, - SO_PASSCRED, SO_PROTOCOL, SO_RCVBUF, SO_RCVTIMEO_NEW, SO_RCVTIMEO_NEW as SO_RCVTIMEO, - SO_RCVTIMEO_OLD, SO_REUSEADDR, SO_REUSEPORT, SO_SNDBUF, SO_SNDTIMEO_NEW, - SO_SNDTIMEO_NEW as SO_SNDTIMEO, SO_SNDTIMEO_OLD, SO_TYPE, TCP_CONGESTION, TCP_CORK, - TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, TCP_QUICKACK, + SO_PASSCRED, SO_PROTOCOL, SO_RCVBUF, SO_RCVBUFFORCE, SO_RCVTIMEO_NEW, + SO_RCVTIMEO_NEW as SO_RCVTIMEO, SO_RCVTIMEO_OLD, SO_REUSEADDR, SO_REUSEPORT, SO_SNDBUF, + SO_SNDTIMEO_NEW, SO_SNDTIMEO_NEW as SO_SNDTIMEO, SO_SNDTIMEO_OLD, SO_TYPE, TCP_CONGESTION, + TCP_CORK, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, TCP_QUICKACK, TCP_THIN_LINEAR_TIMEOUTS, TCP_USER_TIMEOUT, }, netlink::*, diff --git a/src/backend/linux_raw/net/sockopt.rs b/src/backend/linux_raw/net/sockopt.rs index 642f9f3eb..1715868d4 100644 --- a/src/backend/linux_raw/net/sockopt.rs +++ b/src/backend/linux_raw/net/sockopt.rs @@ -349,6 +349,12 @@ pub(crate) fn set_socket_recv_buffer_size(fd: BorrowedFd<'_>, size: usize) -> io setsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUF, size) } +#[inline] +pub(crate) fn set_socket_recv_buffer_size_force(fd: BorrowedFd<'_>, size: usize) -> io::Result<()> { + let size: c::c_int = size.try_into().map_err(|_| io::Errno::INVAL)?; + setsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUFFORCE, size) +} + #[inline] pub(crate) fn get_socket_recv_buffer_size(fd: BorrowedFd<'_>) -> io::Result { getsockopt(fd, c::SOL_SOCKET, c::SO_RCVBUF).map(|size: u32| size as usize) diff --git a/src/net/sockopt.rs b/src/net/sockopt.rs index 9f41f1ba4..d3465166d 100644 --- a/src/net/sockopt.rs +++ b/src/net/sockopt.rs @@ -396,6 +396,17 @@ pub fn set_socket_recv_buffer_size(fd: Fd, value: usize) -> io::Result backend::net::sockopt::set_socket_recv_buffer_size(fd.as_fd(), value) } +/// `setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, value)` +/// +/// See the [module-level documentation] for more. +/// +/// [module-level documentation]: self#references-for-get_socket_-and-set_socket_-functions +#[inline] +#[doc(alias = "SO_RCVBUFFORCE")] +pub fn set_socket_recv_buffer_size_force(fd: Fd, value: usize) -> io::Result<()> { + backend::net::sockopt::set_socket_recv_buffer_size_force(fd.as_fd(), value) +} + /// `getsockopt(fd, SOL_SOCKET, SO_RCVBUF)` /// /// See the [module-level documentation] for more.