Skip to content

Commit 5e5f929

Browse files
committed
Add simple interface for busy_poll on Linux
1 parent 50fc51e commit 5e5f929

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/socket.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ impl Socket {
381381
sys::set_nonblocking(self.as_raw(), nonblocking)
382382
}
383383

384+
/// Sets the busy polling timeout in microseconds for blocking socket operations.
385+
///
386+
/// When there is no data available, the socket will busy poll for the specified
387+
/// duration (in microseconds) before falling back to blocking behavior.
388+
///
389+
/// # Notes
390+
///
391+
/// - Requires `CAP_NET_ADMIN` capability to increase the value
392+
/// - Default value is controlled by `/proc/sys/net/core/busy_read`
393+
/// - Available since Linux 3.11
394+
#[cfg(target_os = "linux")]
395+
pub fn set_busy_poll(&self, busy_poll: u16) -> io::Result<()> {
396+
sys::set_busy_poll(self.as_raw(), busy_poll)
397+
}
398+
384399
/// Shuts down the read, write, or both halves of this connection.
385400
///
386401
/// This function will cause all pending and future I/O on the specified

src/sys/unix.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub(crate) use libc::{
201201
IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP,
202202
IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, MSG_OOB, MSG_PEEK, SOL_SOCKET,
203203
SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF,
204-
SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
204+
SO_SNDTIMEO, SO_TYPE, TCP_NODELAY
205205
};
206206
#[cfg(not(any(
207207
target_os = "dragonfly",
@@ -993,6 +993,18 @@ pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()
993993
}
994994
}
995995

996+
#[cfg(target_os = "linux")]
997+
pub(crate) fn set_busy_poll(fd: RawSocket, busy_poll: u16) -> io::Result<()> {
998+
unsafe {
999+
setsockopt(
1000+
fd,
1001+
libc::SOL_SOCKET,
1002+
libc::SO_BUSY_POLL,
1003+
busy_poll as c_int,
1004+
)
1005+
}
1006+
}
1007+
9961008
pub(crate) fn shutdown(fd: RawSocket, how: Shutdown) -> io::Result<()> {
9971009
let how = match how {
9981010
Shutdown::Write => libc::SHUT_WR,

0 commit comments

Comments
 (0)