Skip to main content

asterust_junos/interfaces/unit/
tunnel.rs

1//! `tunnel`
2//! `interfaces <name> { unit <n> { tunnel { encapsulation <type> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [tunnel (FTI)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tunnel-edit-interfaces-fti.html)
7//! - [udp (tunnel encapsulation)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/udp-edit-interfaces-unit-tunnel-encapsulation.html)
8
9use crate::{active_children, arg_of, child_block, has_leaf};
10use asterust::config::ConfigLine;
11
12/// The `tunnel` of a logical unit, as written under `encapsulation`.
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct UnitTunnel {
16    /// `"gre"`, `"ipip"` or `"udp"`.
17    pub encapsulation: Option<String>,
18    pub source_address: Option<String>,
19    pub source_interface: Option<String>,
20    pub destination_address: Option<String>,
21    pub key: Option<String>,
22    pub routing_instance: Option<String>,
23    pub tunnel_termination: bool,
24    pub virtual_loop: bool,
25}
26
27pub(super) fn parse_tunnel(unit: &ConfigLine) -> Option<UnitTunnel> {
28    let tunnel = child_block(unit, "tunnel")?;
29    let encapsulation = active_children(tunnel).find(|c| c.arg("encapsulation").is_some());
30    let mut parsed = UnitTunnel {
31        encapsulation: encapsulation
32            .and_then(|c| c.arg("encapsulation"))
33            .map(|kind| kind.split_whitespace().next().unwrap_or(kind).to_string()),
34        ..UnitTunnel::default()
35    };
36    if let Some(block) = encapsulation {
37        let source = child_block(block, "source");
38        parsed.source_address = source.and_then(|s| arg_of(s, "address")).map(str::to_string);
39        parsed.source_interface = source.and_then(|s| arg_of(s, "interface")).map(str::to_string);
40        parsed.destination_address = child_block(block, "destination")
41            .and_then(|d| arg_of(d, "address"))
42            .map(str::to_string);
43        parsed.key = arg_of(block, "key").map(str::to_string);
44        parsed.routing_instance = arg_of(block, "routing-instance").map(str::to_string);
45        parsed.tunnel_termination = has_leaf(block, "tunnel-termination");
46        parsed.virtual_loop = has_leaf(block, "virtual-loop");
47    }
48    Some(parsed)
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::interfaces::interfaces;
54    use asterust::config::juniper;
55
56    // Adapted from the IS-IS User Guide, How to Configure IS-IS Flood Reflectors: the `fti0` interface of Device R0
57    // (the `mpls` port is from the guide's `set` commands, written in brace form); see the README "Sources".
58    #[test]
59    fn reads_the_guide_fti_unit() {
60        let ifaces = interfaces(&juniper::parse_braces(
61            r#"
62interfaces {
63    fti0 {
64        unit 0 {
65            tunnel {
66                encapsulation udp {
67                    source {
68                        address 192.168.255.10;
69                    }
70                    destination {
71                        address 192.168.255.6;
72                    }
73                }
74            }
75            family inet {
76                address 10.10.6.1/30;
77                destination-udp-port 10000;
78            }
79            family iso {
80                destination-udp-port 10030;
81            }
82            family inet6 {
83                destination-udp-port 10010;
84            }
85            family mpls {
86                destination-udp-port 10020;
87            }
88        }
89    }
90}
91"#,
92        ));
93        let unit = &ifaces[0].units[0];
94        let tunnel = unit.tunnel.as_ref().unwrap();
95        assert_eq!(tunnel.encapsulation.as_deref(), Some("udp"));
96        assert_eq!(tunnel.source_address.as_deref(), Some("192.168.255.10"));
97        assert_eq!(tunnel.destination_address.as_deref(), Some("192.168.255.6"));
98        assert_eq!(tunnel.source_interface, None);
99
100        let ports: Vec<_> = unit
101            .family_settings
102            .iter()
103            .map(|f| (f.name.as_str(), f.destination_udp_port.as_deref()))
104            .collect();
105        assert_eq!(
106            ports,
107            [
108                ("inet", Some("10000")),
109                ("iso", Some("10030")),
110                ("inet6", Some("10010")),
111                ("mpls", Some("10020"))
112            ]
113        );
114        assert_eq!(unit.inet_addresses, ["10.10.6.1/30"]);
115    }
116
117    // Not from documentation: the other tunnel statements of the fti CLI Reference page.
118    #[test]
119    fn reads_the_other_tunnel_statements() {
120        let ifaces = interfaces(&juniper::parse_braces(
121            r#"
122interfaces {
123    fti1 {
124        unit 0 {
125            tunnel {
126                encapsulation gre {
127                    source {
128                        address 192.0.2.1;
129                        interface lo0.0;
130                    }
131                    destination {
132                        address 192.0.2.2;
133                    }
134                    key 5;
135                    routing-instance VRF-A;
136                    tunnel-termination;
137                    virtual-loop;
138                }
139            }
140        }
141        unit 1 {
142            tunnel {
143                encapsulation ipip;
144            }
145        }
146        unit 2;
147    }
148}
149"#,
150        ));
151        let units = &ifaces[0].units;
152        let gre = units[0].tunnel.as_ref().unwrap();
153        assert_eq!(gre.encapsulation.as_deref(), Some("gre"));
154        assert_eq!(gre.source_interface.as_deref(), Some("lo0.0"));
155        assert_eq!(gre.key.as_deref(), Some("5"));
156        assert_eq!(gre.routing_instance.as_deref(), Some("VRF-A"));
157        assert!(gre.tunnel_termination);
158        assert!(gre.virtual_loop);
159
160        let ipip = units[1].tunnel.as_ref().unwrap();
161        assert_eq!(ipip.encapsulation.as_deref(), Some("ipip"));
162        assert_eq!(ipip.source_address, None);
163        assert!(units[2].tunnel.is_none());
164    }
165}