diff --git a/src/sys/unix.rs b/src/sys/unix.rs index f4447213..935c6387 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -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 { + unsafe { + getsockopt::(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`]. diff --git a/tests/socket.rs b/tests/socket.rs index e12fab2d..af601442 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -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")]