Skip to main content

testground/
network_conf.rs

1#![allow(dead_code)]
2
3use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
4use serde::Serialize;
5
6use serde_repr::{Deserialize_repr, Serialize_repr};
7
8#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Debug)]
9#[repr(u8)]
10pub enum FilterAction {
11    Accept = 0,
12    Reject = 1,
13    Drop = 2,
14}
15
16#[derive(Serialize, Debug)]
17/// LinkShape defines how traffic should be shaped.
18pub struct LinkShape {
19    /// Latency is the egress latency.
20    pub latency: u64,
21
22    /// Jitter is the egress jitter.
23    pub jitter: u64,
24
25    /// Bandwidth is egress bits per second.
26    pub bandwidth: u64,
27
28    /// Drop all inbound traffic.
29    /// TODO: Not implemented
30    pub filter: FilterAction,
31
32    /// Loss is the egress packet loss (%)
33    pub loss: f32,
34
35    /// Corrupt is the egress packet corruption probability (%)
36    pub corrupt: f32,
37
38    /// Corrupt is the egress packet corruption correlation (%)
39    pub corrupt_corr: f32,
40
41    /// Reorder is the probability that an egress packet will be reordered (%)
42    ///
43    /// Reordered packets will skip the latency delay and be sent
44    /// immediately. You must specify a non-zero Latency for this option to
45    /// make sense.
46    pub reorder: f32,
47
48    /// ReorderCorr is the egress packet reordering correlation (%)
49    pub reorder_corr: f32,
50
51    /// Duplicate is the percentage of packets that are duplicated (%)
52    pub duplicate: f32,
53
54    /// DuplicateCorr is the correlation between egress packet duplication (%)
55    pub duplicate_corr: f32,
56}
57
58#[derive(Serialize, Debug)]
59/// LinkRule applies a LinkShape to a subnet.
60pub struct LinkRule {
61    #[serde(flatten)]
62    pub link_shape: LinkShape,
63
64    pub subnet: IpNetwork,
65}
66
67pub const DEFAULT_DATA_NETWORK: &str = "default";
68
69#[derive(Serialize, Debug)]
70pub enum RoutingPolicyType {
71    #[serde(rename = "allow_all")]
72    AllowAll,
73    #[serde(rename = "deny_all")]
74    DenyAll,
75}
76
77/// NetworkConfiguration specifies how a node's network should be configured.
78#[derive(Serialize, Debug)]
79pub struct NetworkConfiguration {
80    /// Network is the name of the network to configure.
81    pub network: String,
82
83    /// IPv4 and IPv6 set the IP addresses of this network device. If
84    /// unspecified, the sidecar will leave them alone.
85    ///
86    /// Your test-case will be assigned a B block in the range
87    /// 16.0.0.1-32.0.0.0. X.Y.0.1 will always be reserved for the gateway
88    /// and shouldn't be used by the test.
89    #[serde(rename = "IPv4")]
90    pub ipv4: Option<Ipv4Network>,
91
92    /// TODO: IPv6 is currently not supported.
93    #[serde(rename = "IPv6")]
94    pub ipv6: Option<Ipv6Network>,
95
96    /// Enable enables this network device.
97    pub enable: bool,
98
99    /// Default is the default link shaping rule.
100    pub default: LinkShape,
101
102    /// Rules defines how traffic should be shaped to different subnets.
103    ///
104    /// TODO: This is not implemented.
105    pub rules: Option<Vec<LinkRule>>,
106
107    /// CallbackState will be signalled when the link changes are applied.
108    ///
109    /// Nodes can use the same state to wait for _all_ or a subset of nodes to
110    /// enter the desired network state. See CallbackTarget.
111    pub callback_state: String,
112
113    /// CallbackTarget is the amount of instances that will have needed to signal
114    /// on the Callback state to consider the configuration operation a success.
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub callback_target: Option<u64>,
117
118    /// RoutingPolicy defines the data routing policy of a certain node. This affects
119    /// external networks other than the network 'Default', e.g., external Internet
120    /// access.
121    pub routing_policy: RoutingPolicyType,
122}
123
124#[cfg(test)]
125mod tests {
126    use std::net::Ipv4Addr;
127
128    use super::*;
129
130    #[test]
131    fn serde_test() {
132        let output = r#"{"network":"default","IPv4":"16.0.1.1/24","IPv6":null,"enable":true,"default":{"latency":10000000,"jitter":0,"bandwidth":1048576,"filter":0,"loss":0.0,"corrupt":0.0,"corrupt_corr":0.0,"reorder":0.0,"reorder_corr":0.0,"duplicate":0.0,"duplicate_corr":0.0},"rules":null,"callback_state":"latency-reduced","routing_policy":"deny_all"}"#;
133
134        let network_conf = NetworkConfiguration {
135            network: DEFAULT_DATA_NETWORK.to_owned(),
136            ipv4: Some(Ipv4Network::new(Ipv4Addr::new(16, 0, 1, 1), 24).unwrap()),
137            ipv6: None,
138            enable: true,
139            default: LinkShape {
140                latency: 10000000,
141                jitter: 0,
142                bandwidth: 1048576,
143                filter: FilterAction::Accept,
144                loss: 0.0,
145                corrupt: 0.0,
146                corrupt_corr: 0.0,
147                reorder: 0.0,
148                reorder_corr: 0.0,
149                duplicate: 0.0,
150                duplicate_corr: 0.0,
151            },
152            rules: None,
153            callback_state: "latency-reduced".to_owned(),
154            callback_target: None,
155            routing_policy: RoutingPolicyType::DenyAll,
156        };
157
158        let input = serde_json::to_string(&network_conf).unwrap();
159
160        println!("{}", input);
161
162        assert_eq!(input, output)
163    }
164}