From d454932913f314c1be093efc19f440ec48961b32 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Wed, 6 May 2026 17:16:47 +0200 Subject: [PATCH] Use nix ioctl macro for TUNSETOFF in mycelium-tun Signed-off-by: Lee Smet --- mycelium-tun/src/linux.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mycelium-tun/src/linux.rs b/mycelium-tun/src/linux.rs index 1d24870..78a6b04 100644 --- a/mycelium-tun/src/linux.rs +++ b/mycelium-tun/src/linux.rs @@ -23,11 +23,11 @@ const TUNSETOFFLOAD: libc::c_ulong = 0x400454d0; const IFF_VNET_HDR: libc::c_short = 0x4000; // TUNSETOFFLOAD feature flags. -const TUN_F_CSUM: libc::c_ulong = 0x01; -const TUN_F_TSO4: libc::c_ulong = 0x02; -const TUN_F_TSO6: libc::c_ulong = 0x04; -const TUN_F_USO4: libc::c_ulong = 0x20; -const TUN_F_USO6: libc::c_ulong = 0x40; +const TUN_F_CSUM: libc::c_int = 0x01; +const TUN_F_TSO4: libc::c_int = 0x02; +const TUN_F_TSO6: libc::c_int = 0x04; +const TUN_F_USO4: libc::c_int = 0x20; +const TUN_F_USO6: libc::c_int = 0x40; /// Maximum read buffer: 65535 (max IP packet) + 12 (virtio header). const READ_BUF_SIZE: usize = 65535 + VIRTIO_NET_HDR_LEN; @@ -39,6 +39,12 @@ nix::ioctl_write_ptr_bad!( libc::ifreq ); +nix::ioctl_write_int_bad!( + /// Set TUN device offload features. + tunsetoffload, + TUNSETOFFLOAD +); + nix::ioctl_write_ptr_bad!( /// Set the MTU on a network interface. siocsifmtu, @@ -131,17 +137,14 @@ impl Tun { // Enable offloading: CSUM + TSO4 + TSO6 are mandatory. let offload_flags = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; // SAFETY: file is a valid TUN fd. - let ret = unsafe { libc::ioctl(file.as_raw_fd(), TUNSETOFFLOAD, offload_flags) }; - if ret < 0 { - return Err(io::Error::last_os_error()); - } + unsafe { tunsetoffload(file.as_raw_fd(), offload_flags) }.map_err(io::Error::from)?; debug!(name = %actual_name, "enabled TSO offload"); // Attempt USO4/USO6 — requires Linux 6.2+. Failure is non-fatal. let uso_flags = offload_flags | TUN_F_USO4 | TUN_F_USO6; // SAFETY: file is a valid TUN fd. - let ret = unsafe { libc::ioctl(file.as_raw_fd(), TUNSETOFFLOAD, uso_flags) }; - let uso_enabled = ret == 0; + let uso_enabled = + unsafe { tunsetoffload(file.as_raw_fd(), uso_flags) }.is_ok(); if uso_enabled { debug!(name = %actual_name, "enabled USO offload"); }