Skip to content

Commit

Permalink
sys::socket: adding freebsd's ReusePortLb constant. (#2332)
Browse files Browse the repository at this point in the history
Allows to use `SO_REUSEPORT_LB` so incoming connections can be distributed
to bound (up to 256) sockets in a Load-Balanced fashion.
  • Loading branch information
devnexen authored Mar 16, 2024
1 parent 36e0248 commit a0078c1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2332.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add socket option ReusePortLb for FreeBSD.
10 changes: 10 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ sockopt_impl!(
libc::SO_REUSEPORT,
bool
);
#[cfg(target_os = "freebsd")]
sockopt_impl!(
/// Enables incoming connections to be distributed among N sockets (up to 256)
/// via a Load-Balancing hash based algorithm.
ReusePortLb,
Both,
libc::SOL_SOCKET,
libc::SO_REUSEPORT_LB,
bool
);
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand Down
16 changes: 16 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,19 @@ fn test_utun_ifname() {
let expected_name = format!("utun{}", unit - 1);
assert_eq!(name.into_string(), Ok(expected_name));
}

#[test]
#[cfg(target_os = "freebsd")]
fn test_reuseport_lb() {
let fd = socket(
AddressFamily::Inet6,
SockType::Datagram,
SockFlag::empty(),
None,
)
.unwrap();
setsockopt(&fd, sockopt::ReusePortLb, &false).unwrap();
assert!(!getsockopt(&fd, sockopt::ReusePortLb).unwrap());
setsockopt(&fd, sockopt::ReusePortLb, &true).unwrap();
assert!(getsockopt(&fd, sockopt::ReusePortLb).unwrap());
}

0 comments on commit a0078c1

Please sign in to comment.