diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 9c2c9b00..e5daf24f 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -2813,6 +2813,29 @@ impl crate::Socket { ) } } + + /// Get the value for the `SO_BUSY_POLL` option on this socket. + /// + /// On Linux this function requires the `CAP_NET_ADMIN` capability. + #[cfg(all(feature = "all", target_os = "linux"))] + pub fn busy_poll(&self) -> io::Result { + unsafe { getsockopt(self.as_raw(), libc::SOL_SOCKET, libc::SO_BUSY_POLL) } + } + + /// Set the value for the `SO_BUSY_POLL` option on this socket. + /// + /// On Linux this function requires the `CAP_NET_ADMIN` capability. + #[cfg(all(feature = "all", target_os = "linux"))] + pub fn set_busy_poll(&self, busy_poll: u32) -> io::Result<()> { + unsafe { + setsockopt( + self.as_raw(), + libc::SOL_SOCKET, + libc::SO_BUSY_POLL, + busy_poll as c_int, + ) + } + } } /// Berkeley Packet Filter (BPF). diff --git a/tests/socket.rs b/tests/socket.rs index 7ea5a415..cc98b8a9 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -1827,3 +1827,16 @@ fn set_priority() { assert!(socket.priority().unwrap() == i); } } + +#[cfg(all(feature = "all", target_os = "linux"))] +#[test] +fn set_busy_poll() { + let socket = Socket::new(Domain::UNIX, Type::DGRAM, None).unwrap(); + assert!(socket.busy_poll().unwrap() == 0); + + // test busy poll values 0 .. 6; values above 6 require additional priviledges + for i in (0..=6).rev() { + socket.set_busy_poll(i).unwrap(); + assert!(socket.busy_poll().unwrap() == i); + } +}