asterust_junos/interfaces/unit/
tunnel.rs1use crate::{active_children, arg_of, child_block, has_leaf, statement_words, toggle, value_after};
20use asterust::config::ConfigLine;
21
22#[derive(Clone, Debug, Default, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct UnitTunnel {
26 pub encapsulation: Option<String>,
28 pub source_address: Option<String>,
29 pub source_interface: Option<String>,
30 pub destination_address: Option<String>,
31 pub key: Option<String>,
32 pub routing_instance: Option<String>,
33 pub tunnel_termination: bool,
34 pub virtual_loop: bool,
35 pub allow_fragmentation: bool,
37 pub do_not_fragment: bool,
38 pub path_mtu_discovery: Option<bool>,
40 pub ttl: Option<String>,
41 pub traffic_class: Option<String>,
42 pub backup_destination: Option<String>,
43 pub tunnel_routing_instance: Option<String>,
45 pub destination_udp_port: Option<String>,
47 pub source_udp_port_min: Option<String>,
49 pub source_udp_port_max: Option<String>,
51 pub tunnel_endpoint: Option<String>,
53 pub vni: Option<String>,
54}
55
56pub(super) fn parse_tunnel(unit: &ConfigLine) -> Option<UnitTunnel> {
57 let tunnel = child_block(unit, "tunnel")?;
58 let encapsulation = active_children(tunnel).find(|c| c.arg("encapsulation").is_some());
59 let mut parsed = UnitTunnel {
60 encapsulation: encapsulation
61 .and_then(|c| c.arg("encapsulation"))
62 .map(|kind| kind.split_whitespace().next().unwrap_or(kind).to_string()),
63 allow_fragmentation: has_leaf(tunnel, "allow-fragmentation"),
64 do_not_fragment: has_leaf(tunnel, "do-not-fragment"),
65 path_mtu_discovery: toggle(tunnel, "path-mtu-discovery"),
66 ttl: arg_of(tunnel, "ttl").map(str::to_string),
67 traffic_class: arg_of(tunnel, "traffic-class").map(str::to_string),
68 backup_destination: arg_of(tunnel, "backup-destination").map(str::to_string),
69 ..UnitTunnel::default()
70 };
71 if let Some(block) = encapsulation {
72 let source = child_block(block, "source");
73 parsed.source_address = source.and_then(|s| arg_of(s, "address")).map(str::to_string);
74 parsed.source_interface = source.and_then(|s| arg_of(s, "interface")).map(str::to_string);
75 parsed.destination_address = child_block(block, "destination")
76 .and_then(|d| arg_of(d, "address"))
77 .map(str::to_string);
78 parsed.key = arg_of(block, "key").map(str::to_string);
79 parsed.routing_instance = arg_of(block, "routing-instance").map(str::to_string);
80 parsed.tunnel_termination = has_leaf(block, "tunnel-termination");
81 parsed.virtual_loop = has_leaf(block, "virtual-loop");
82 parsed.allow_fragmentation |= has_leaf(block, "allow-fragmentation");
83 parsed.tunnel_routing_instance = child_block(block, "tunnel-routing-instance")
84 .and_then(|t| arg_of(t, "routing-instance"))
85 .map(str::to_string);
86 parsed.destination_udp_port = arg_of(block, "destination-udp-port").map(str::to_string);
87 let ports = statement_words(block, "source-udp-port-range");
88 parsed.source_udp_port_min = ports.as_ref().and_then(|w| value_after(w, "min"));
89 parsed.source_udp_port_max = ports.as_ref().and_then(|w| value_after(w, "max"));
90 parsed.tunnel_endpoint = arg_of(block, "tunnel-endpoint").map(str::to_string);
91 parsed.vni = arg_of(block, "vni").map(str::to_string);
92 }
93 Some(parsed)
94}
95
96#[cfg(test)]
97mod tests {
98 use crate::interfaces::interfaces;
99 use asterust::config::juniper;
100
101 #[test]
104 fn reads_the_guide_fti_unit() {
105 let ifaces = interfaces(&juniper::parse_braces(
106 r#"
107interfaces {
108 fti0 {
109 unit 0 {
110 tunnel {
111 encapsulation udp {
112 source {
113 address 192.168.255.10;
114 }
115 destination {
116 address 192.168.255.6;
117 }
118 }
119 }
120 family inet {
121 address 10.10.6.1/30;
122 destination-udp-port 10000;
123 }
124 family iso {
125 destination-udp-port 10030;
126 }
127 family inet6 {
128 destination-udp-port 10010;
129 }
130 family mpls {
131 destination-udp-port 10020;
132 }
133 }
134 }
135}
136"#,
137 ));
138 let unit = &ifaces[0].units[0];
139 let tunnel = unit.tunnel.as_ref().unwrap();
140 assert_eq!(tunnel.encapsulation.as_deref(), Some("udp"));
141 assert_eq!(tunnel.source_address.as_deref(), Some("192.168.255.10"));
142 assert_eq!(tunnel.destination_address.as_deref(), Some("192.168.255.6"));
143 assert_eq!(tunnel.source_interface, None);
144
145 let ports: Vec<_> = unit
146 .family_settings
147 .iter()
148 .map(|f| (f.name.as_str(), f.destination_udp_port.as_deref()))
149 .collect();
150 assert_eq!(
151 ports,
152 [
153 ("inet", Some("10000")),
154 ("iso", Some("10030")),
155 ("inet6", Some("10010")),
156 ("mpls", Some("10020"))
157 ]
158 );
159 assert_eq!(unit.inet_addresses, ["10.10.6.1/30"]);
160 }
161
162 #[test]
164 fn reads_the_other_tunnel_statements() {
165 let ifaces = interfaces(&juniper::parse_braces(
166 r#"
167interfaces {
168 fti1 {
169 unit 0 {
170 tunnel {
171 encapsulation gre {
172 source {
173 address 192.0.2.1;
174 interface lo0.0;
175 }
176 destination {
177 address 192.0.2.2;
178 }
179 key 5;
180 routing-instance VRF-A;
181 tunnel-termination;
182 virtual-loop;
183 }
184 }
185 }
186 unit 1 {
187 tunnel {
188 encapsulation ipip;
189 }
190 }
191 unit 2;
192 }
193}
194"#,
195 ));
196 let units = &ifaces[0].units;
197 let gre = units[0].tunnel.as_ref().unwrap();
198 assert_eq!(gre.encapsulation.as_deref(), Some("gre"));
199 assert_eq!(gre.source_interface.as_deref(), Some("lo0.0"));
200 assert_eq!(gre.key.as_deref(), Some("5"));
201 assert_eq!(gre.routing_instance.as_deref(), Some("VRF-A"));
202 assert!(gre.tunnel_termination);
203 assert!(gre.virtual_loop);
204
205 let ipip = units[1].tunnel.as_ref().unwrap();
206 assert_eq!(ipip.encapsulation.as_deref(), Some("ipip"));
207 assert_eq!(ipip.source_address, None);
208 assert!(units[2].tunnel.is_none());
209 }
210}