Skip to content

Commit

Permalink
builder: merge try_build and try_build_mq
Browse files Browse the repository at this point in the history
  • Loading branch information
yaa110 committed Dec 29, 2024
1 parent 7ac3069 commit 27dba47
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ license = "MIT OR Apache-2.0"
name = "tokio-tun"
readme = "README.md"
repository = "https://github.com/yaa110/tokio-tun"
version = "0.12.1"
version = "0.13.0"

[dependencies]
libc = "0.2"
nix = {version = "0.29", default-features = false, features = ["ioctl"]}
thiserror = "1"
thiserror = "2"
tokio = {version = "1", features = ["net"]}

[dev-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ async fn main() {
.tap() // uses TAP instead of TUN (default).
.packet_info() // avoids setting IFF_NO_PI.
.up() // or set it up manually using `sudo ip link set <tun-name> up`.
.try_build() // or `.try_build_mq(queues)` for multi-queue support.
.build()
.unwrap()
.pop()
.unwrap(),
);

Expand Down
3 changes: 2 additions & 1 deletion examples/read-mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ async fn main() {
.destination(Ipv4Addr::new(10, 1, 0, 1))
.broadcast(Ipv4Addr::BROADCAST)
.netmask(Ipv4Addr::new(255, 255, 255, 0))
.try_build_mq(queues)
.queues(queues)
.build()
.unwrap();

println!("--------------");
Expand Down
4 changes: 3 additions & 1 deletion examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ async fn main() {
.destination(Ipv4Addr::new(10, 1, 0, 1))
.broadcast(Ipv4Addr::BROADCAST)
.netmask(Ipv4Addr::new(255, 255, 255, 0))
.try_build()
.build()
.unwrap()
.pop()
.unwrap(),
);

Expand Down
26 changes: 16 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct TunBuilder {
destination: Option<Ipv4Addr>,
broadcast: Option<Ipv4Addr>,
netmask: Option<Ipv4Addr>,
queues: Option<usize>,
}

impl Default for TunBuilder {
Expand All @@ -38,6 +39,7 @@ impl Default for TunBuilder {
destination: None,
broadcast: None,
netmask: None,
queues: None,
}
}
}
Expand Down Expand Up @@ -66,6 +68,15 @@ impl TunBuilder {
self
}

/// Builds multiple instances of [`Tun`](struct.Tun.html) with `IFF_MULTI_QUEUE` flag.
///
/// Internally this creates multiple file descriptors to parallelize packet sending and receiving.
/// Default value is `1`.
pub fn queues(mut self, queues: usize) -> Self {
self.queues = Some(queues);
self
}

/// Set `packet_info` to `true` (default is `false`), thereby unsetting the `IFF_NO_PI` flag on
/// allocation.
///
Expand Down Expand Up @@ -164,16 +175,11 @@ impl TunBuilder {
}

/// Builds a new instance of [`Tun`](struct.Tun.html).
pub fn try_build(self) -> Result<Tun> {
Tun::new(self.into())
}

/// Builds multiple instances of [`Tun`](struct.Tun.html) with `IFF_MULTI_QUEUE` flag.
///
/// Internally this creates multiple file descriptors to parallelize packet sending and receiving.
#[cfg(target_os = "linux")]
pub fn try_build_mq(self, queues: usize) -> Result<Vec<Tun>> {
Tun::new_mq(self.into(), queues)
pub fn build(self) -> Result<Vec<Tun>> {
match self.queues {
Some(queues) if queues > 1 => Tun::new_mq(self.into(), queues),
_ => Tun::new(self.into()).map(|tun| vec![tun]),
}
}
}

Expand Down

0 comments on commit 27dba47

Please sign in to comment.