diff --git a/rust/relay/ebpf-shared/src/lib.rs b/rust/relay/ebpf-shared/src/lib.rs index 16615b86c..2f3da6486 100644 --- a/rust/relay/ebpf-shared/src/lib.rs +++ b/rust/relay/ebpf-shared/src/lib.rs @@ -204,17 +204,52 @@ impl PortAndPeerV6 { #[derive(Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Debug))] pub struct Config { - pub udp_checksum_enabled: bool, - pub lowest_allocation_port: u16, - pub highest_allocation_port: u16, + udp_checksum_enabled: bool, + lowest_allocation_port: [u8; 2], + highest_allocation_port: [u8; 2], +} + +impl Config { + pub fn udp_checksum_enabled(&self) -> bool { + self.udp_checksum_enabled + } + + pub fn with_udp_checksum(self, enabled: bool) -> Self { + Self { + udp_checksum_enabled: enabled, + ..self + } + } + + pub fn lowest_allocation_port(&self) -> u16 { + u16::from_be_bytes(self.lowest_allocation_port) + } + + pub fn with_lowest_allocation_port(self, port: u16) -> Self { + Self { + lowest_allocation_port: port.to_be_bytes(), + ..self + } + } + + pub fn highest_allocation_port(&self) -> u16 { + u16::from_be_bytes(self.highest_allocation_port) + } + + pub fn with_highest_allocation_port(self, port: u16) -> Self { + Self { + highest_allocation_port: port.to_be_bytes(), + ..self + } + } } impl Default for Config { fn default() -> Self { Self { udp_checksum_enabled: true, - lowest_allocation_port: 49152, - highest_allocation_port: 65535, + lowest_allocation_port: 49152_u16.to_be_bytes(), + highest_allocation_port: 65535_u16.to_be_bytes(), } } } diff --git a/rust/relay/ebpf-turn-router/src/config.rs b/rust/relay/ebpf-turn-router/src/config.rs index 4e89a35c6..2a474a523 100644 --- a/rust/relay/ebpf-turn-router/src/config.rs +++ b/rust/relay/ebpf-turn-router/src/config.rs @@ -8,13 +8,13 @@ use ebpf_shared::Config; static CONFIG: Array = Array::with_max_entries(1, 0); pub fn udp_checksum_enabled() -> bool { - config().udp_checksum_enabled + config().udp_checksum_enabled() } pub fn allocation_range() -> RangeInclusive { let config = config(); - config.lowest_allocation_port..=(config.highest_allocation_port) + config.lowest_allocation_port()..=(config.highest_allocation_port()) } fn config() -> Config { diff --git a/rust/relay/server/src/main.rs b/rust/relay/server/src/main.rs index 360af0d5b..7613aae7f 100644 --- a/rust/relay/server/src/main.rs +++ b/rust/relay/server/src/main.rs @@ -150,11 +150,12 @@ async fn try_main(args: Args) -> Result<()> { .context("Failed to load eBPF TURN router")?; if let Some(ebpf) = ebpf.as_mut() { - ebpf.set_config(Config { - udp_checksum_enabled: true, - lowest_allocation_port: args.lowest_port, - highest_allocation_port: args.highest_port, - }) + ebpf.set_config( + Config::default() + .with_udp_checksum(true) + .with_lowest_allocation_port(args.lowest_port) + .with_highest_allocation_port(args.highest_port), + ) .context("Failed to set config of eBPF program")?; } diff --git a/rust/relay/server/tests/ebpf_ipv4.rs b/rust/relay/server/tests/ebpf_ipv4.rs index 1f662fedb..ee2415b54 100644 --- a/rust/relay/server/tests/ebpf_ipv4.rs +++ b/rust/relay/server/tests/ebpf_ipv4.rs @@ -24,10 +24,7 @@ async fn ping_pong() { // Linux does not set the correct UDP checksum when sending the packet, so our updated checksum in the eBPF code will be wrong and later dropped. // To make the test work, we therefore need to tell the eBPF program to disable UDP checksumming by just setting it to 0. program - .set_config(Config { - udp_checksum_enabled: false, - ..Config::default() - }) + .set_config(Config::default().with_udp_checksum(false)) .unwrap(); let client = UdpSocket::bind("127.0.0.1:0").await.unwrap();