Skip to content

Commit

Permalink
Linux: Support for IFF_NAPI and IFF_VNET_HDR (#114)
Browse files Browse the repository at this point in the history
* README: Correct name of required linux kernel module

I think a search and replace caught this when the crate name changed to tun2.

* linux: Support for IFF_NAPI

* linux: Support for IFF_VNET_HDR

* minor changes

---------

Co-authored-by: ssrlive <30760636+ssrlive@users.noreply.github.com>
  • Loading branch information
ijc and ssrlive authored Oct 22, 2024
1 parent 77fb9fb commit 4d10464
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// 0. You just DO WHAT THE FUCK YOU WANT TO.

use libc::{
self, c_char, c_short, ifreq, AF_INET, IFF_MULTI_QUEUE, IFF_NO_PI, IFF_RUNNING, IFF_TAP,
IFF_TUN, IFF_UP, IFNAMSIZ, O_RDWR, SOCK_DGRAM,
self, c_char, c_short, ifreq, AF_INET, IFF_MULTI_QUEUE, IFF_NAPI, IFF_NO_PI, IFF_RUNNING,
IFF_TAP, IFF_TUN, IFF_UP, IFF_VNET_HDR, IFNAMSIZ, O_RDWR, SOCK_DGRAM,
};
use std::{
ffi::{CStr, CString},
Expand Down Expand Up @@ -105,9 +105,15 @@ impl Device {

let iff_no_pi = IFF_NO_PI as c_short;
let iff_multi_queue = IFF_MULTI_QUEUE as c_short;
let iff_napi = IFF_NAPI as c_short;
let iff_vnet_hdr = IFF_VNET_HDR as c_short;
let packet_information = config.platform_config.packet_information;
let napi = config.platform_config.napi;
let vnet_hdr = config.platform_config.vnet_hdr;
req.ifr_ifru.ifru_flags = device_type
| if packet_information { 0 } else { iff_no_pi }
| if napi { iff_napi } else { 0 }
| if vnet_hdr { iff_vnet_hdr } else { 0 }
| if queues_num > 1 { iff_multi_queue } else { 0 };

let tun_fd = {
Expand Down
20 changes: 20 additions & 0 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub struct PlatformConfig {
pub(crate) packet_information: bool,
/// root privileges required or not
pub(crate) ensure_root_privileges: bool,

/// Enable IFF_NAPI
pub(crate) napi: bool,

/// Enable IFF_VNET_HDR
pub(crate) vnet_hdr: bool,
}

/// `packet_information` is default to be `false` and `ensure_root_privileges` is default to be `true`.
Expand All @@ -37,6 +43,8 @@ impl Default for PlatformConfig {
PlatformConfig {
packet_information: false,
ensure_root_privileges: true,
napi: false,
vnet_hdr: false,
}
}
}
Expand All @@ -61,6 +69,18 @@ impl PlatformConfig {
self.ensure_root_privileges = value;
self
}

/// Enable IFF_NAPI flag.
pub fn napi(&mut self) -> &mut Self {
self.napi = true;
self
}

/// Enable IFF_VNET_HDR flag.
pub fn vnet_hdr(&mut self) -> &mut Self {
self.vnet_hdr = true;
self
}
}

/// Create a TUN device with the given name.
Expand Down

0 comments on commit 4d10464

Please sign in to comment.