Skip to content

Commit 5309e13

Browse files
committed
refactor(env): seperate env-related code
1 parent cbcace6 commit 5309e13

22 files changed

Lines changed: 229 additions & 140 deletions

File tree

Cargo.lock

Lines changed: 21 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ default-run = "rattan"
99

1010
[workspace]
1111
resolver = "2"
12-
members = [".", "rattan-core", "rattan-log"]
12+
members = [".", "rattan-core", "rattan-env", "rattan-log"]
1313

1414
[workspace.package]
1515
version = "0.1.0"
@@ -27,7 +27,9 @@ ctrlc = "3.5.2"
2727
figment = { version = "0.10", features = ["env", "json", "toml"] }
2828
futures = "0.3.32"
2929
human-bandwidth = { version = "0.1.4", features = ["binary-system", "serde"] }
30+
ipnet = { version = "2.11.0", features = ["serde"] }
3031
jiff = "0.2.15"
32+
libc = "0.2.141"
3133
netem-trace = { version = "0.4.4", features = [
3234
"human",
3335
"mahimahi",
@@ -45,10 +47,14 @@ nix = { version = "0.31.1", features = [
4547
] }
4648
num_enum = "0.7.5"
4749
once_cell = "1.19"
50+
parking_lot = "0.12.5"
4851
pcap-file = { version = "2.0.0" }
4952
rand = "0.10.0"
53+
rtnetlink = "0.20.0"
5054
serde = { version = "1.0.160", features = ["derive"] }
55+
tempfile = "3"
5156
test-log = { version = "0.2.18", features = ["trace"] }
57+
thiserror = "2.0.0"
5258
tokio = { version = "1.26.0", features = [
5359
"macros",
5460
"net",

rattan-core/Cargo.toml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ etherparse = "0.19.0"
3636
figment = { workspace = true }
3737
futures = { workspace = true }
3838
human-bandwidth = { workspace = true }
39-
ipnet = { version = "2.8.0", features = ["serde"] }
39+
ipnet = { workspace = true }
4040
jiff = { workspace = true }
41-
lazy_static = "1.4.0"
42-
libc = "0.2.141"
41+
libc = { workspace = true }
4342
netem-trace = { workspace = true }
4443
nix = { workspace = true }
4544
num_enum = { workspace = true }
4645
once_cell = { workspace = true }
47-
parking_lot = "0.12.1"
46+
parking_lot = { workspace = true }
4847
pcap-file = { workspace = true }
4948
rand = { workspace = true }
49+
rattan-env = { path = "../rattan-env" }
5050
rattan-log = { path = "../rattan-log" }
5151
regex = "1.7.3"
52-
rtnetlink = "0.20.0"
5352
serde = { workspace = true, optional = true }
5453
serde_json = { version = "1.0.96", optional = true }
5554
serde_with = "3"
56-
tempfile = "3.10.1"
57-
thiserror = "2.0.0"
55+
thiserror = { workspace = true }
5856
tokio = { workspace = true }
5957
tokio-util = "0.7.7"
6058
tracing = { workspace = true }
@@ -76,7 +74,9 @@ reqwest = { version = "0.13.2", default-features = false, features = [
7674
"json",
7775
] }
7876
rstest = "0.26.1"
77+
rtnetlink = { workspace = true }
7978
serial_test = "3.2.0"
79+
tempfile = { workspace = true }
8080
test-log = { workspace = true }
8181
tracing-subscriber = { workspace = true }
8282

@@ -86,4 +86,10 @@ camellia = ["dep:camellia"]
8686
first-packet = []
8787
http = ["dep:axum", "serde"]
8888
packet-dump = []
89-
serde = ["bandwidth/serde", "bytesize/serde", "dep:serde", "dep:serde_json"]
89+
serde = [
90+
"bandwidth/serde",
91+
"bytesize/serde",
92+
"dep:serde",
93+
"dep:serde_json",
94+
"rattan-env/serde",
95+
]

rattan-core/src/cells/external.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::{
1212
use async_trait::async_trait;
1313
use bitfield::{BitRange, BitRangeMut};
1414
use parking_lot::RwLock;
15+
use rattan_env::veth::VethCell;
1516
use rattan_log::{
1617
log_entry::general_packet::GeneralPacketType, log_entry::PktAction, FlowDesc, PlainBytes,
1718
RattanLogOp, RawLogEntry, LOGGING_TX,
@@ -24,10 +25,7 @@ use tracing::{debug, error, instrument, warn};
2425
use crate::{
2526
cells::{Cell, ControlInterface, Egress, Ingress, Packet},
2627
error::{Error, TokioRuntimeError},
27-
metal::{
28-
io::common::{InterfaceDriver, InterfaceReceiver, InterfaceSender},
29-
veth::VethCell,
30-
},
28+
metal::io::common::{InterfaceDriver, InterfaceReceiver, InterfaceSender},
3129
radix::{PacketLogMode, BASE_TS, PKT_LOG_MODE},
3230
};
3331

rattan-core/src/error.rs

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ pub type Result<T> = std::result::Result<T, Error>;
66

77
#[derive(Debug, thiserror::Error)]
88
pub enum Error {
9-
#[error("MacParseError: {0}")]
10-
MacParseError(#[from] MacParseError),
11-
#[error("NsError: {0}")]
12-
NsError(#[from] NsError),
13-
#[error("VethError: {0}")]
14-
VethError(#[from] VethError),
9+
#[error("RattanEnvError: {0}")]
10+
RattanEnvError(#[from] rattan_env::Error),
1511
#[error("RoutingTableError: {0}")]
1612
RoutingTableError(#[from] RoutingTableError),
1713
#[error("Encounter IO error, {0}")]
@@ -44,65 +40,6 @@ pub enum Error {
4440
Custom(String),
4541
}
4642

47-
#[derive(Debug, thiserror::Error)]
48-
pub enum NsError {
49-
#[error("Can not create netns directory, {0}")]
50-
CreateNsDirError(std::io::Error),
51-
52-
#[error("Can not create netns, {0}")]
53-
CreateNsError(std::io::Error),
54-
55-
#[error("Can not open netns {0}, {1}")]
56-
OpenNsError(std::path::PathBuf, std::io::Error),
57-
58-
#[error("Failed to close netns, {0}")]
59-
CloseNsError(nix::Error),
60-
61-
#[error("Failed to mount {0}, {1}")]
62-
MountError(String, nix::Error),
63-
64-
#[error("Failed to unmount {0}, {1}")]
65-
UnmountError(std::path::PathBuf, nix::Error),
66-
67-
#[error("Failed to unshare, {0}")]
68-
UnshareError(nix::Error),
69-
70-
#[error("Failed to join thread, {0}")]
71-
JoinThreadError(String),
72-
73-
#[error("Can not setns, {0}")]
74-
SetNsError(nix::Error),
75-
}
76-
77-
/// An error that may occur when parsing a MAC address string.
78-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, thiserror::Error)]
79-
pub enum MacParseError {
80-
/// Parsing of the MAC address contained an invalid digit.
81-
#[error("Invalid digit")]
82-
InvalidDigit,
83-
/// The MAC address did not have the correct length.
84-
#[error("Invalid length")]
85-
InvalidLength,
86-
}
87-
88-
#[derive(Debug, thiserror::Error)]
89-
pub enum VethError {
90-
#[error("Can not create veth pair, {0}")]
91-
CreateVethPairError(String),
92-
#[error("Encounter namespace error, {0}")]
93-
NsError(#[from] NsError),
94-
#[error("Encounter IO error, {0}")]
95-
IoError(#[from] std::io::Error),
96-
#[error("Encounter system error, {0}")]
97-
SystemError(#[from] nix::errno::Errno),
98-
#[error("Already in namespace {0}")]
99-
AlreadyInNamespace(String),
100-
#[error("Set Veth error, {0}")]
101-
SetError(String),
102-
#[error("Failed to build veth, {0}")]
103-
TokioRuntimeError(#[from] TokioRuntimeError),
104-
}
105-
10643
#[derive(Debug, thiserror::Error)]
10744
pub enum RoutingTableError {
10845
#[error("IpNet parse error: {0}")]
@@ -167,6 +104,12 @@ impl From<csv::Error> for VisualizeTraceError {
167104
}
168105
}
169106

107+
impl From<rattan_env::NsError> for Error {
108+
fn from(e: rattan_env::NsError) -> Self {
109+
Error::RattanEnvError(e.into())
110+
}
111+
}
112+
170113
#[cfg(feature = "http")]
171114
impl axum::response::IntoResponse for Error {
172115
fn into_response(self) -> axum::response::Response {
@@ -175,9 +118,7 @@ impl axum::response::IntoResponse for Error {
175118
use serde_json::json;
176119

177120
let status = match self {
178-
Error::MacParseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
179-
Error::NsError(_) => StatusCode::INTERNAL_SERVER_ERROR,
180-
Error::VethError(_) => StatusCode::INTERNAL_SERVER_ERROR,
121+
Error::RattanEnvError(_) => StatusCode::INTERNAL_SERVER_ERROR,
181122
Error::RoutingTableError(_) => StatusCode::INTERNAL_SERVER_ERROR,
182123
Error::IoError(_) => StatusCode::INTERNAL_SERVER_ERROR,
183124
Error::MetalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
@@ -204,9 +145,7 @@ impl axum::response::IntoResponse for Error {
204145
impl Termination for Error {
205146
fn report(self) -> ExitCode {
206147
match self {
207-
Error::MacParseError(_) => ExitCode::from(78),
208-
Error::NsError(_) => ExitCode::from(71),
209-
Error::VethError(_) => ExitCode::from(71),
148+
Error::RattanEnvError(e) => e.report(),
210149
Error::RoutingTableError(_) => ExitCode::from(78),
211150
Error::IoError(_) => ExitCode::from(74),
212151
Error::MetalError(_) => ExitCode::from(74),

rattan-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ pub mod cells;
22
pub mod config;
33
pub mod control;
44
pub mod core;
5-
pub mod env;
65
pub mod error;
76
pub mod metal;
87
pub mod radix;
98
pub mod utils;
109
pub use netem_trace;
10+
pub use rattan_env::env;

rattan-core/src/metal/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ pub enum MetalError {
1010
#[cfg_attr(feature = "camellia", error("Encounter XDP error, {0}"))]
1111
#[cfg(feature = "camellia")]
1212
XDPError(#[from] CamelliaError),
13-
#[error("Encounter Rtnetlink error, {0}")]
14-
RtnetlinkError(#[from] rtnetlink::Error),
1513
#[error("not interested packet")]
1614
NotInterestedPacket,
1715
}

rattan-core/src/metal/io/af_packet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use nix::{
88
errno::Errno,
99
sys::socket::{AddressFamily, SockFlag, SockType},
1010
};
11+
use rattan_env::veth::VethCell;
1112
use tracing::{debug, error, trace, warn};
1213

1314
use super::common::PacketType;
1415
use crate::cells::{Packet, StdPacket};
16+
use crate::metal::error::MetalError;
1517
use crate::metal::io::common::{InterfaceDriver, InterfaceReceiver, InterfaceSender};
16-
use crate::metal::{error::MetalError, veth::VethCell};
1718

1819
pub struct AfPacketSender {
1920
raw_fd: Mutex<i32>,

rattan-core/src/metal/io/af_xdp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use camellia::{
1515
};
1616
use etherparse::{Ethernet2Header, Ipv4Header};
1717
use once_cell::sync::Lazy;
18+
use rattan_env::veth::VethCell;
1819
use tokio::sync::Mutex;
1920
use tokio::time::{sleep, Instant};
2021
use tracing::debug;
@@ -25,9 +26,8 @@ static UMEM: Lazy<Arc<std::sync::Mutex<UMem>>> = Lazy::new(|| {
2526
))
2627
});
2728

28-
use crate::{cells::Packet, metal::veth::VethCell};
29-
30-
use super::common::{InterfaceDriver, InterfaceReceiver, InterfaceSender};
29+
use crate::cells::Packet;
30+
use crate::metal::io::common::{InterfaceDriver, InterfaceReceiver, InterfaceSender};
3131

3232
type XDPSocketRef = Arc<Mutex<XskSocket<SharedAccessorRef>>>;
3333

rattan-core/src/metal/io/common.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::sync::Arc;
22

3-
use crate::{
4-
cells::Packet,
5-
metal::{error::MetalError, veth::VethCell},
6-
};
3+
use rattan_env::veth::VethCell;
4+
5+
use crate::{cells::Packet, metal::error::MetalError};
76

87
pub enum PacketType {
98
PacketHost = 0,

0 commit comments

Comments
 (0)