Skip to content

Commit

Permalink
Add support for SO_REUSEPORT_LB
Browse files Browse the repository at this point in the history
  • Loading branch information
jpds authored May 21, 2023
1 parent 3047737 commit fea2cb4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,35 @@ impl crate::Socket {
}
}

/// Get the value of the `SO_REUSEPORT_LB` option on this socket.
///
/// For more information about this option, see [`set_reuse_port_lb`].
///
/// [`set_reuse_port_lb`]: crate::Socket::set_reuse_port_lb
#[cfg(all(feature = "all", target_os = "freebsd"))]
pub fn reuse_port_lb(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT_LB)
.map(|reuse| reuse != 0)
}
}

/// Set value for the `SO_REUSEPORT_LB` option on this socket.
///
/// This allows multiple programs or threads to bind to the same port and
/// incoming connections will be load balanced using a hash function.
#[cfg(all(feature = "all", target_os = "freebsd"))]
pub fn set_reuse_port_lb(&self, reuse: bool) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
libc::SOL_SOCKET,
libc::SO_REUSEPORT_LB,
reuse as c_int,
)
}
}

/// Get the value of the `IP_FREEBIND` option on this socket.
///
/// For more information about this option, see [`set_freebind`].
Expand Down
2 changes: 2 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,8 @@ test!(reuse_address, set_reuse_address(true));
not(any(windows, target_os = "solaris", target_os = "illumos"))
))]
test!(reuse_port, set_reuse_port(true));
#[cfg(all(feature = "all", target_os = "freebsd"))]
test!(reuse_port_lb, set_reuse_port_lb(true));
#[cfg(all(feature = "all", unix, not(target_os = "redox")))]
test!(
#[cfg_attr(target_os = "linux", ignore = "Different value returned")]
Expand Down

0 comments on commit fea2cb4

Please sign in to comment.