runtime-rs: add open_named_tuntap to open a named tuntap device.

The open_named_tuntap function is designed as a public function to
open a tuntap device with the specified name. However, in order to
reference existing methods in dbs_utils, we still need to keep the
reference "path = "../../../dragonball/src/dbs_utils" in dependencies
and cannot hide it.

Fixes: #8865

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn 2024-03-12 22:26:32 +08:00
parent 005b333976
commit d3bca4597e
2 changed files with 20 additions and 1 deletions

View File

@ -29,6 +29,7 @@ path-clean = "1.0.1"
lazy_static = "1.4"
tracing = "0.1.36"
dbs-utils = { path = "../../../dragonball/src/dbs_utils" }
kata-sys-util = { path = "../../../libs/kata-sys-util" }
kata-types = { path = "../../../libs/kata-types" }
logging = { path = "../../../libs/logging" }
@ -43,7 +44,6 @@ crossbeam-channel = "0.5.6"
[target.'cfg(not(target_arch = "s390x"))'.dependencies]
dragonball = { path = "../../../dragonball", features = ["atomic-guest-memory", "virtio-vsock", "hotplug", "virtio-blk", "virtio-net", "virtio-fs", "vhost-net", "dbs-upcall", "virtio-mem", "virtio-balloon", "vhost-user-net", "host-device"] }
dbs-utils = { path = "../../../dragonball/src/dbs_utils" }
[features]
default = []

View File

@ -11,6 +11,7 @@ use std::{
};
use anyhow::{anyhow, Context, Result};
use dbs_utils::net::Tap;
use kata_types::config::KATA_PATH;
use nix::{
fcntl,
@ -81,3 +82,21 @@ pub fn enter_netns(netns_path: &str) -> Result<()> {
Ok(())
}
pub fn open_named_tuntap(if_name: &str, queues: u32) -> Result<Vec<File>> {
let (multi_vq, vq_pairs) = if queues > 1 {
(true, queues as usize)
} else {
(false, 1_usize)
};
let tap: Tap = Tap::open_named(if_name, multi_vq).context("open named tuntap device failed")?;
let taps: Vec<Tap> = tap.into_mq_taps(vq_pairs).context("into mq taps failed.")?;
let mut tap_files: Vec<std::fs::File> = Vec::new();
for tap in taps {
tap_files.push(tap.tap_file);
}
Ok(tap_files)
}