Skip to content
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
19 changes: 4 additions & 15 deletions patchbay-runner/src/sim/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,16 @@ gateway = "r1"

#[test]
fn rejects_inline_when_topology_ref_set() {
let sim = SimFile {
let mut sim = SimFile {
sim: SimMeta {
name: "x".into(),
topology: Some("a".into()),
},
topology: patchbay::config::LabConfig {
router: vec![patchbay::config::RouterConfig {
name: "r1".into(),
region: None,
upstream: None,
nat: patchbay::Nat::None,
ip_support: patchbay::IpSupport::V4Only,
nat_v6: patchbay::NatV6Mode::None,
ra_enabled: None,
ra_interval_secs: None,
ra_lifetime_secs: None,
}],
..Default::default()
},
..Default::default()
};
sim.topology
.router
.push(patchbay::config::RouterConfig::new("r1"));
let err = match load_topology(&sim, Path::new("sims/sim.toml")) {
Ok(_) => panic!("expected inline-topology rejection"),
Err(err) => err,
Expand Down
22 changes: 21 additions & 1 deletion patchbay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{IpSupport, Nat, NatV6Mode};

/// Parsed lab configuration from TOML.
#[derive(Deserialize, Clone, Default)]
#[non_exhaustive]
pub struct LabConfig {
/// Optional region-latency map.
pub region: Option<HashMap<String, RegionConfig>>,
Expand All @@ -20,7 +21,8 @@ pub struct LabConfig {
}

/// Per-region latency configuration.
#[derive(Deserialize, Clone)]
#[derive(Deserialize, Clone, Default)]
#[non_exhaustive]
pub struct RegionConfig {
/// Map of target-region name to one-way latency in ms.
#[serde(default)]
Expand All @@ -29,6 +31,7 @@ pub struct RegionConfig {

/// Router configuration entry.
#[derive(Deserialize, Clone)]
#[non_exhaustive]
pub struct RouterConfig {
/// Router name.
pub name: String,
Expand All @@ -52,3 +55,20 @@ pub struct RouterConfig {
/// Optional RA lifetime in seconds.
pub ra_lifetime_secs: Option<u64>,
}

impl RouterConfig {
/// Creates a router entry named `name` with the defaults of the TOML format.
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
region: None,
upstream: None,
nat: Nat::default(),
ip_support: IpSupport::default(),
nat_v6: NatV6Mode::default(),
ra_enabled: None,
ra_interval_secs: None,
ra_lifetime_secs: None,
}
}
}
2 changes: 1 addition & 1 deletion patchbay/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) struct CoreConfig {
/// Cheaply copyable and usable as a hash-map key.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, derive_more::Display)]
#[display("{_0}")]
pub struct NodeId(pub u64);
pub struct NodeId(pub(crate) u64);

/// Selects the address pool used for router downstream links.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
36 changes: 36 additions & 0 deletions patchbay/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{

/// A single lab event with global ordering.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LabEvent {
/// Monotonically increasing operation id.
pub opid: u64,
Expand All @@ -40,19 +41,22 @@ pub struct LabEvent {
pub enum LabEventKind {
// ── Lifecycle ──
/// Emitted once during `Lab::new()`.
#[non_exhaustive]
LabCreated {
/// The lab's process-unique prefix (e.g. `"lab-p12340"`).
lab_prefix: String,
/// Optional human-readable label.
label: Option<String>,
},
/// Full state snapshot, emitted by `Lab::from_config()`.
#[non_exhaustive]
InitialState {
/// Serialized [`LabState`].
state: serde_json::Value,
},
// ── IX ──
/// Emitted once during `Lab::new()` after IX bridge creation.
#[non_exhaustive]
IxCreated {
/// IX bridge name (e.g. `"br-p1230-1"`).
bridge: String,
Expand All @@ -68,6 +72,7 @@ pub enum LabEventKind {

// ── Topology ──
/// A router was added to the lab.
#[non_exhaustive]
RouterAdded {
/// Router name.
name: String,
Expand All @@ -76,11 +81,13 @@ pub enum LabEventKind {
state: Box<RouterState>,
},
/// A router was removed from the lab.
#[non_exhaustive]
RouterRemoved {
/// Router name.
name: String,
},
/// A device was added to the lab.
#[non_exhaustive]
DeviceAdded {
/// Device name.
name: String,
Expand All @@ -89,27 +96,31 @@ pub enum LabEventKind {
state: DeviceState,
},
/// A device was removed from the lab.
#[non_exhaustive]
DeviceRemoved {
/// Device name.
name: String,
},

// ── Regions ──
/// A region was added.
#[non_exhaustive]
RegionAdded {
/// Region name.
name: String,
/// Router assigned to this region.
router: String,
},
/// An inter-region link was created.
#[non_exhaustive]
RegionLinkAdded {
/// Router A name.
router_a: String,
/// Router B name.
router_b: String,
},
/// An inter-region link was broken/impaired.
#[non_exhaustive]
RegionLinkBroken {
/// Router A name.
router_a: String,
Expand All @@ -119,6 +130,7 @@ pub enum LabEventKind {
condition: Option<LinkCondition>,
},
/// An inter-region link was restored.
#[non_exhaustive]
RegionLinkRestored {
/// Router A name.
router_a: String,
Expand All @@ -128,32 +140,37 @@ pub enum LabEventKind {

// ── Mutations ──
/// Router NAT mode changed.
#[non_exhaustive]
NatChanged {
/// Router name.
router: String,
/// New NAT configuration. `None` means NAT is disabled.
nat: Option<NatConfig>,
},
/// Router IPv6 NAT mode changed.
#[non_exhaustive]
NatV6Changed {
/// Router name.
router: String,
/// New IPv6 NAT mode.
nat_v6: NatV6Mode,
},
/// Router NAT conntrack state flushed.
#[non_exhaustive]
NatStateFlushed {
/// Router name.
router: String,
},
/// Router firewall changed.
#[non_exhaustive]
FirewallChanged {
/// Router name.
router: String,
/// New firewall configuration.
firewall: Firewall,
},
/// Device interface link condition changed.
#[non_exhaustive]
LinkConditionChanged {
/// Device name.
device: String,
Expand All @@ -165,20 +182,23 @@ pub enum LabEventKind {
ingress: Option<LinkCondition>,
},
/// Router downlink condition changed (affects all downstream traffic).
#[non_exhaustive]
DownlinkConditionChanged {
/// Router name.
router: String,
/// New condition (`None` = removed).
condition: Option<LinkCondition>,
},
/// Device interface brought up.
#[non_exhaustive]
LinkUp {
/// Device name.
device: String,
/// Interface name.
iface: String,
},
/// Device interface brought down.
#[non_exhaustive]
LinkDown {
/// Device name.
device: String,
Expand All @@ -200,20 +220,23 @@ pub enum LabEventKind {
iface: String,
},
/// A new interface was added to a device.
#[non_exhaustive]
InterfaceAdded {
/// Device name.
device: String,
/// Interface snapshot.
iface: IfaceSnapshot,
},
/// An interface was removed from a device.
#[non_exhaustive]
InterfaceRemoved {
/// Device name.
device: String,
/// Interface name.
iface_name: String,
},
/// A device interface was replugged to a different router.
#[non_exhaustive]
InterfaceReplugged {
/// Device name.
device: String,
Expand All @@ -229,6 +252,7 @@ pub enum LabEventKind {
new_ip_v6: Option<Ipv6Addr>,
},
/// Device IP address changed (e.g. DHCP renewal).
#[non_exhaustive]
DeviceIpChanged {
/// Device name.
device: String,
Expand All @@ -242,6 +266,7 @@ pub enum LabEventKind {

// ── Processes ──
/// A command was spawned in a node namespace.
#[non_exhaustive]
CommandSpawned {
/// Node name (device or router).
node: String,
Expand All @@ -251,6 +276,7 @@ pub enum LabEventKind {
cmd: String,
},
/// A spawned command exited.
#[non_exhaustive]
CommandExited {
/// Node name.
node: String,
Expand All @@ -262,6 +288,7 @@ pub enum LabEventKind {

// ── Counters ──
/// Packet counters snapshot for a node.
#[non_exhaustive]
PacketCounters {
/// Node name.
node: String,
Expand All @@ -271,6 +298,7 @@ pub enum LabEventKind {

// ── Test lifecycle ──
/// Test completed with a result.
#[non_exhaustive]
TestCompleted {
/// Whether the test passed.
passed: bool,
Expand All @@ -279,6 +307,7 @@ pub enum LabEventKind {

/// Snapshot of a device interface at the time of an event.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct IfaceSnapshot {
/// Interface name.
pub name: String,
Expand All @@ -301,6 +330,7 @@ pub struct IfaceSnapshot {

/// Traffic counters for one interface (from `/proc/net/dev`).
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub struct IfaceCounters {
/// Interface name.
pub iface: String,
Expand All @@ -320,6 +350,7 @@ pub struct IfaceCounters {

/// Lab state derived from the event stream. Mirrors the `state.json` schema.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LabState {
/// Latest applied opid.
pub opid: u64,
Expand All @@ -346,6 +377,7 @@ pub struct LabState {

/// IX state in state.json.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IxState {
/// Bridge name.
pub bridge: String,
Expand All @@ -361,6 +393,7 @@ pub struct IxState {

/// Router state in state.json.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RouterState {
/// Namespace name.
pub ns: String,
Expand Down Expand Up @@ -406,6 +439,7 @@ pub struct RouterState {

/// Device state in state.json.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DeviceState {
/// Namespace name.
pub ns: String,
Expand All @@ -421,13 +455,15 @@ pub struct DeviceState {

/// Region state in state.json.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RegionState {
/// Router assigned to this region.
pub router: String,
}

/// Inter-region link state.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RegionLinkState {
/// Router A name.
pub a: String,
Expand Down
3 changes: 3 additions & 0 deletions patchbay/src/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
/// Use [`Firewall::Custom`] for full control.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Firewall {
/// No filtering beyond NAT (default).
#[default]
Expand Down Expand Up @@ -58,6 +59,7 @@ pub enum Firewall {
/// traversing the router's forward chain.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PortPolicy {
/// All destination ports are allowed (no filtering).
#[default]
Expand All @@ -80,6 +82,7 @@ pub enum PortPolicy {
/// .build();
/// ```
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FirewallConfig {
/// Block unsolicited inbound traffic on the WAN interface (RFC 6092).
/// When true, only established/related return traffic is allowed inbound.
Expand Down
Loading
Loading