Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
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).
Expand Down
13 changes: 13 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading