diff --git a/Cargo.lock b/Cargo.lock index 1be5a581..5c0a83d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,6 +245,44 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "4.5.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_derive" +version = "4.5.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4ced95c6f4a675af3da73304b9ac4ed991640c36374e4b46795c49e17cf1ed" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.98", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + [[package]] name = "ctrlc" version = "3.4.5" @@ -259,6 +297,7 @@ dependencies = [ name = "dataplane" version = "0.1.0" dependencies = [ + "clap", "ctrlc", "dpdk", "net", @@ -372,6 +411,12 @@ version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "indexmap" version = "2.7.1" diff --git a/Cargo.toml b/Cargo.toml index 0f7c16c2..575bcf21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ arbitrary = { version = "1.4.1", default-features = false, features = [] } arrayvec = { version = "0.7.6", default-features = false, features = [] } bindgen = { version = "0.71.1", default-features = false, features = [] } bolero = { version = "0.12.0", default-features = false, features = [] } +clap = { version = "4.5.27", default-features = false, features = ["std"] } ctrlc = { version = "3.4.5", default-features = false, features = [] } doxygen-rs = { version = "0.4.0", default-features = false, features = [] } etherparse = { version = "0.17.0", default-features = false, features = [] } diff --git a/dataplane/Cargo.toml b/dataplane/Cargo.toml index ee07312f..81f9b892 100644 --- a/dataplane/Cargo.toml +++ b/dataplane/Cargo.toml @@ -7,6 +7,7 @@ license = "Apache-2.0" [dependencies] +clap = { workspace = true, features = ["derive"] } ctrlc = { workspace = true, features = ["termination"] } net = { workspace = true, features = ["serde"] } dpdk = { workspace = true } diff --git a/dataplane/src/args.rs b/dataplane/src/args.rs new file mode 100644 index 00000000..5e30358c --- /dev/null +++ b/dataplane/src/args.rs @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Open Network Fabric Authors + +pub(crate) use clap::Parser; +#[derive(Parser)] +#[command(name = "Hedgehog Fabric Gateway dataplane")] +#[command(version = "1.0")] // FIXME +#[command(about = "A next-gen dataplane for next-gen fabric gateway", long_about = None)] +pub(crate) struct CmdArgs { + #[arg(long, value_name = "core-id used as main", default_value_t = 2)] + main_lcore: u8, + #[arg(long, value_name = "map lcore set to cpu set")] + lcores: Option, + #[arg(long, value_name = "PCI devices to probe")] + allow: Vec, + #[arg(long, value_name = "huge pages", default_value_t = 8192)] + huge_worker_stack: u32, + #[arg(long, value_name = "socket memory")] + socket_mem: Option, + #[arg(long, value_name = "iova mode(va|pa)")] + iova_mode: Option, + #[arg(long, value_name = "loglevel for a specific component")] + log_level: Vec, + // other non-EAL params (NAT, routing, etc.) +} +impl CmdArgs { + pub fn eal_params(&self) -> Vec { + let mut out = Vec::new(); + /* hardcoded (always) */ + out.push("--in-memory".to_string()); + + out.push("--main-lcore".to_owned()); + out.push(self.main_lcore.to_string()); + + out.push("--lcores".to_string()); + out.push( + self.lcores + .clone() + .map_or_else(|| "2-4".to_owned(), |lcores| lcores.to_owned()), + ); + + /* IOVA mode */ + out.push(format!( + "--iova-mode={}", + &self + .iova_mode + .clone() + .map_or_else(|| { "va".to_owned() }, |mode| mode.to_owned()) + )); + + /* worker huge page stack size */ + out.push(format!("--huge-worker-stack={}", self.huge_worker_stack)); + + /* --allow */ + for a in self.allow.iter() { + out.push("--allow".to_string()); + out.push(a.to_owned()); + } + + // To be removed + if self.allow.len() == 0 { + out.push("--allow".to_string()); + out.push("0000:01:00.0,dv_flow_en=1".to_string()); + } + + /* --log-level */ + for level in self.log_level.iter() { + out.push("--log-level".to_string()); + out.push(level.to_owned()); + } + + // To replace by log + println!("DPDK EAL init params: {:#?}", out); + + out + } +} diff --git a/dataplane/src/main.rs b/dataplane/src/main.rs index 40238c19..2884549d 100644 --- a/dataplane/src/main.rs +++ b/dataplane/src/main.rs @@ -11,9 +11,11 @@ use dpdk::{dev, eal, socket}; use net::packet::Packet; use net::parse::Parse; use tracing::{info, warn}; - +mod args; mod nat; +use args::{CmdArgs, Parser}; + #[global_allocator] static GLOBAL_ALLOCATOR: RteAllocator = RteAllocator::new_uninitialized(); @@ -30,19 +32,8 @@ fn init(args: impl IntoIterator>) -> Eal { } fn main() { - let eal: Eal = init([ - "--main-lcore", - "2", - "--lcores", - "2-4", - "--in-memory", - "--allow", - "0000:01:00.0,dv_flow_en=1", - "--huge-worker-stack=8192", - "--socket-mem=8192,0,0,0", - "--no-telemetry", - "--iova-mode=va", - ]); + let args = CmdArgs::parse(); + let eal: Eal = init(args.eal_params()); let (stop_tx, stop_rx) = std::sync::mpsc::channel(); ctrlc::set_handler(move || stop_tx.send(()).expect("Error sending SIGINT signal"))