Skip to content

Commit 60d597a

Browse files
committed
feat(pkt-io): add initial tap creation routines
Signed-off-by: Fredi Raspall <[email protected]>
1 parent 81061a1 commit 60d597a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkt-io/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "Apache-2.0"
77

88
[dependencies]
99
# internal
10+
args = { workspace = true }
1011
interface-manager = { workspace = true }
1112
net = { workspace = true }
1213
pipeline = { workspace = true }

pkt-io/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
mod ctl;
1010
mod io;
11-
1211
mod nf;
12+
mod tapinit;
1313
mod tests;
1414

1515
// re-exports
1616
pub use ctl::IoManagerCtl;
1717
pub use io::{IoManagerError, start_io};
1818
pub use nf::PktIo;
1919
pub use nf::PktQueue;
20+
pub use tapinit::{tap_init, tap_init_async};

pkt-io/src/tapinit.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Open Network Fabric Authors
3+
4+
//! Tap initialization
5+
6+
use args::InterfaceArg;
7+
use interface_manager::interface::TapDevice;
8+
use tokio::runtime::Runtime;
9+
use tracing::{error, info};
10+
11+
/// Creates a tap device for each of the [`InterfaceArg`]s provided.
12+
///
13+
/// # Errors
14+
///
15+
/// This function fails if any of the taps cannot be created.
16+
pub async fn tap_init_async(ifargs: &[InterfaceArg]) -> std::io::Result<()> {
17+
info!("Creating tap devices");
18+
for ifarg in ifargs.iter() {
19+
if let Err(e) = TapDevice::open(&ifarg.interface).await {
20+
error!("Failed to create tap '{}':{e}", ifarg.interface);
21+
return Err(e);
22+
} else {
23+
info!("Created tap device '{}'", ifarg.interface);
24+
}
25+
}
26+
Ok(())
27+
}
28+
29+
/// Creates a tap device for each of the [`InterfaceArg`]s provided.
30+
/// This is a sync wrapper to `tap_init_async`.
31+
///
32+
/// # Errors
33+
///
34+
/// This function fails if any of the taps cannot be created.
35+
pub fn tap_init(port_specs: &[InterfaceArg]) -> std::io::Result<()> {
36+
Runtime::new()
37+
.expect("Tokio runtime creation failed!")
38+
.block_on(tap_init_async(port_specs))
39+
}

0 commit comments

Comments
 (0)