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(net): use a tun/tap interface as a backend #14

Merged
merged 1 commit into from
May 10, 2024
Merged
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
7 changes: 5 additions & 2 deletions alioth-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ fn parse_mem(s: &str) -> Result<usize> {
fn parse_net<'a>(s: &'a str) -> Result<NetParam> {
let mut parts = s.trim().splitn(3, ',');
let splitter = |s: &'a str| s.split_once::<'a, _>('=');
let Some(("tap", tap_path)) = parts.next().and_then(splitter) else {
bail!("invalid net opt: {s}");
let (tap_path, if_name) = match parts.next().and_then(splitter) {
Some(("tap", p)) => (p, None),
Some(("if", name)) => ("/dev/net/tun", Some(name.to_owned())),
_ => bail!("invalid net opt: {s}"),
};
let Some(("mac", mac_str)) = parts.next().and_then(splitter) else {
bail!("invalid net opt: {s}");
Expand Down Expand Up @@ -133,6 +135,7 @@ fn parse_net<'a>(s: &'a str) -> Result<NetParam> {
mtu: mtu_str.parse()?,
queue_pairs: 1,
tap: tap_path.into(),
if_name,
})
}

Expand Down
19 changes: 16 additions & 3 deletions alioth/src/virtio/dev/net/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use std::fmt::Debug;
use std::fs::{self, File};
use std::io;
use std::iter::zip;
use std::mem::MaybeUninit;
use std::os::fd::AsRawFd;
use std::os::unix::prelude::OpenOptionsExt;
use std::path::PathBuf;
Expand Down Expand Up @@ -107,6 +109,7 @@ pub struct NetParam {
pub mtu: u16,
pub queue_pairs: u16,
pub tap: PathBuf,
pub if_name: Option<String>,
}

impl DevParam for NetParam {
Expand All @@ -125,7 +128,7 @@ impl Net {
.write(true)
.open(param.tap)?;

setup_tap(&mut file)?;
setup_tap(&mut file, param.if_name.as_deref())?;
let net = Net {
name,
config: Arc::new(NetConfig {
Expand Down Expand Up @@ -244,8 +247,18 @@ pub const TOKEN_TAP: Token = Token(0);

const VNET_HEADER_SIZE: i32 = 12;

fn setup_tap(file: &mut File) -> Result<()> {
let mut tap_ifconfig = unsafe { tun_get_iff(file) }?;
fn setup_tap(file: &mut File, if_name: Option<&str>) -> Result<()> {
let mut tap_ifconfig = match if_name {
None => unsafe { tun_get_iff(file) }?,
Some(name) => {
let mut tap_ifconfig = unsafe { MaybeUninit::<libc::ifreq>::zeroed().assume_init() };
for (s, d) in zip(name.as_bytes(), tap_ifconfig.ifr_name.as_mut()) {
*d = *s as i8;
}
tap_ifconfig
}
};

tap_ifconfig.ifr_ifru.ifru_flags = (IFF_TAP | IFF_NO_PI | IFF_VNET_HDR) as i16;
unsafe { tun_set_iff(file, &tap_ifconfig) }?;
unsafe { tun_set_vnet_hdr_sz(file, &VNET_HEADER_SIZE) }?;
Expand Down