Skip to content

Commit a0078c1

Browse files
authored
sys::socket: adding freebsd's ReusePortLb constant. (#2332)
Allows to use `SO_REUSEPORT_LB` so incoming connections can be distributed to bound (up to 256) sockets in a Load-Balanced fashion.
1 parent 36e0248 commit a0078c1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

changelog/2332.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add socket option ReusePortLb for FreeBSD.

src/sys/socket/sockopt.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ sockopt_impl!(
270270
libc::SO_REUSEPORT,
271271
bool
272272
);
273+
#[cfg(target_os = "freebsd")]
274+
sockopt_impl!(
275+
/// Enables incoming connections to be distributed among N sockets (up to 256)
276+
/// via a Load-Balancing hash based algorithm.
277+
ReusePortLb,
278+
Both,
279+
libc::SOL_SOCKET,
280+
libc::SO_REUSEPORT_LB,
281+
bool
282+
);
273283
#[cfg(feature = "net")]
274284
sockopt_impl!(
275285
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]

test/sys/test_sockopt.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,19 @@ fn test_utun_ifname() {
859859
let expected_name = format!("utun{}", unit - 1);
860860
assert_eq!(name.into_string(), Ok(expected_name));
861861
}
862+
863+
#[test]
864+
#[cfg(target_os = "freebsd")]
865+
fn test_reuseport_lb() {
866+
let fd = socket(
867+
AddressFamily::Inet6,
868+
SockType::Datagram,
869+
SockFlag::empty(),
870+
None,
871+
)
872+
.unwrap();
873+
setsockopt(&fd, sockopt::ReusePortLb, &false).unwrap();
874+
assert!(!getsockopt(&fd, sockopt::ReusePortLb).unwrap());
875+
setsockopt(&fd, sockopt::ReusePortLb, &true).unwrap();
876+
assert!(getsockopt(&fd, sockopt::ReusePortLb).unwrap());
877+
}

0 commit comments

Comments
 (0)