Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils/interface): use vectored write #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 33 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ etherparse = "^0.13.0"
ipnet = "^2.7"
libc = "^0.2.147"

# Tokio
tokio = { version = "^1.25", features = ["rt-multi-thread", "macros", "sync", "io-util"] }
# Tokio and futures
tokio = { version = "^1.29", features = ["rt-multi-thread", "macros", "sync", "io-util"] }
dashmap = "^5.4"

# Configuration
Expand Down Expand Up @@ -70,3 +70,6 @@ once_cell = "^1.17"

[dev-dependencies]
tokio-test = "^0.4.2"

[replace]
"tokio:1.29.1" = { git = "https://github.com/M0dEx/tokio.git", branch = "write-half-write-vectored" }
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(io_slice_advance)]

Check failure on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, stable)

`#![feature]` may not be used on the stable release channel

Check failure on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

`#![feature]` may not be used on the stable release channel

pub mod auth;
pub mod client;
pub mod config;
Expand Down
28 changes: 18 additions & 10 deletions src/utils/interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use bytes::{Bytes, BytesMut};
use ipnet::IpNet;
use std::io::IoSlice;

Check failure on line 4 in src/utils/interface.rs

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, stable)

unused import: `std::io::IoSlice`

Check warning on line 4 in src/utils/interface.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, stable)

unused import: `std::io::IoSlice`
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
use tun::{AsyncDevice, Configuration};

Expand Down Expand Up @@ -65,12 +66,10 @@
#[inline]
pub async fn write_to_interface(interface: &mut WriteHalf<AsyncDevice>, data: Bytes) -> Result<()> {
#[cfg(target_os = "macos")]
let packet_data = prepend_packet_info_header(data)?;
write_with_packet_info_header(interface, data).await?;

#[cfg(not(target_os = "macos"))]
let packet_data = data;

interface.write_all(&packet_data).await?;
interface.write_all(&data).await?;

Ok(())
}
Expand All @@ -84,7 +83,10 @@
/// - `Bytes` - the prepended packet
#[cfg(target_os = "macos")]
#[inline]
fn prepend_packet_info_header(data: Bytes) -> Result<Bytes> {
async fn write_with_packet_info_header(
interface: &mut WriteHalf<AsyncDevice>,
data: Bytes,
) -> Result<()> {
use crate::constants::DARWIN_PI_HEADER_IPV4;
use crate::constants::DARWIN_PI_HEADER_IPV6;
use anyhow::anyhow;
Expand All @@ -96,13 +98,19 @@
.ip
.ok_or_else(|| anyhow!("Received packet with invalid IP header"))?;

let pi_header = match ip_header {
IpHeader::Version4(_, _) => Bytes::from_static(DARWIN_PI_HEADER_IPV4.as_ref()),
IpHeader::Version6(_, _) => Bytes::from_static(DARWIN_PI_HEADER_IPV6.as_ref()),
let packet_info_header = match ip_header {
IpHeader::Version4(_, _) => DARWIN_PI_HEADER_IPV4,
IpHeader::Version6(_, _) => DARWIN_PI_HEADER_IPV6,
};

// TODO: Do not copy
Ok([pi_header.as_ref(), data.as_ref()].concat().into())
let mut packet_data = [IoSlice::new(&packet_info_header), IoSlice::new(&data)];

while !packet_data.is_empty() {
let written = interface.write_vectored(&packet_data).await?;
IoSlice::advance_slices(&mut packet_data.as_mut_slice(), written);
}

Ok(())
}

/// Truncates the packet info header from the packet.
Expand Down
Loading