Skip to main content

asterust_junos/interfaces/
mod.rs

1//! `interfaces { ... }`: physical interfaces and their logical units.
2
3mod aggregated_ether_options;
4mod auto_configure;
5mod ether_options;
6mod hold_time;
7mod unit;
8
9pub use aggregated_ether_options::{AggregatedEtherOptions, BfdLivenessDetection, Lacp};
10pub use auto_configure::{AutoConfigure, AutoConfigureProfile, AutoConfigureRanges};
11pub use ether_options::{EtherOptions, Ieee8023ad};
12pub use hold_time::HoldTime;
13pub use unit::{
14    AddressArp, BandwidthThreshold, FamilyFilter, FamilyPolicer, FamilySampling, InterfaceUnit, RpfCheck,
15    TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, VlanMap, VlanTags, VrrpGroup, VrrpInheritFrom, VrrpTrack,
16};
17
18use crate::{active_children, arg_of, child_block, has_leaf, named_children, top_level};
19use aggregated_ether_options::parse_aggregated_ether_options;
20use asterust::config::{Config, ConfigLine};
21use auto_configure::parse_auto_configure;
22use ether_options::parse_ether_options;
23use hold_time::parse_hold_time;
24use unit::parse_unit;
25
26/// A physical or loopback interface such as `ge-0/0/0` or `lo0`.
27#[derive(Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub struct Interface {
30    pub name: String,
31    pub description: Option<String>,
32    /// Set by the `disable` statement.
33    pub disabled: bool,
34    pub mtu: Option<String>,
35    /// e.g. `"1g"` or `"auto"`.
36    pub speed: Option<String>,
37    /// Physical encapsulation, e.g. `"flexible-ethernet-services"`.
38    pub encapsulation: Option<String>,
39    pub vlan_tagging: bool,
40    pub flexible_vlan_tagging: bool,
41    pub native_vlan_id: Option<String>,
42    pub per_unit_scheduler: bool,
43    pub no_gratuitous_arp_request: bool,
44    pub hold_time: Option<HoldTime>,
45    pub ether_options: Option<EtherOptions>,
46    pub aggregated_ether_options: Option<AggregatedEtherOptions>,
47    pub auto_configure: Option<AutoConfigure>,
48    pub units: Vec<InterfaceUnit>,
49}
50
51/// Every interface under the top-level `interfaces` block.
52pub fn interfaces(config: &Config) -> Vec<Interface> {
53    let Some(root) = top_level(config, "interfaces") else {
54        return Vec::new();
55    };
56    // Interface names have no spaces; this skips `apply-groups`, `interface-range <x>` and similar.
57    active_children(root)
58        .filter(|c| !c.text.contains(' '))
59        .map(parse_interface)
60        .collect()
61}
62
63fn parse_interface(line: &ConfigLine) -> Interface {
64    Interface {
65        name: line.text.clone(),
66        description: arg_of(line, "description").map(str::to_string),
67        disabled: has_leaf(line, "disable"),
68        mtu: arg_of(line, "mtu").map(str::to_string),
69        speed: arg_of(line, "speed").map(str::to_string),
70        encapsulation: arg_of(line, "encapsulation").map(str::to_string),
71        vlan_tagging: has_leaf(line, "vlan-tagging"),
72        flexible_vlan_tagging: has_leaf(line, "flexible-vlan-tagging"),
73        native_vlan_id: arg_of(line, "native-vlan-id").map(str::to_string),
74        per_unit_scheduler: has_leaf(line, "per-unit-scheduler"),
75        no_gratuitous_arp_request: has_leaf(line, "no-gratuitous-arp-request"),
76        hold_time: parse_hold_time(line),
77        ether_options: parse_ether_options(line),
78        aggregated_ether_options: child_block(line, "aggregated-ether-options").map(parse_aggregated_ether_options),
79        auto_configure: child_block(line, "auto-configure").map(parse_auto_configure),
80        units: named_children(line, "unit")
81            .map(|(number, unit)| parse_unit(number, unit))
82            .collect(),
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use asterust::config::juniper;
90
91    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
92    const CONFIG: &str = r#"
93interfaces {
94    ge-0/0/0 {
95        description "to p1";
96        unit 0 {
97            description "core link";
98            family inet {
99                address 10.0.1.1/30;
100                address 10.0.2.1/30 {
101                    primary;
102                }
103            }
104            family inet6 {
105                address 2001:db8:1::1/64;
106            }
107            family iso;
108            family mpls;
109        }
110    }
111    ge-0/0/1 {
112        disable;
113        vlan-tagging;
114        unit 100 {
115            vlan-id 100;
116            family inet {
117                address 192.0.2.1/24;
118            }
119        }
120    }
121    inactive: ge-0/0/2 {
122        unit 0;
123    }
124    lo0 {
125        unit 0 {
126            family inet {
127                address 10.255.0.1/32;
128            }
129            family iso {
130                address 49.0001.0100.2550.0001.00;
131            }
132        }
133    }
134    apply-groups GLOBAL;
135}
136"#;
137
138    #[test]
139    fn extracts_units_families_and_addresses() {
140        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
141        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
142        assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);
143
144        let core = &ifaces[0];
145        assert_eq!(core.description.as_deref(), Some("to p1"));
146        assert!(!core.disabled);
147        let unit = &core.units[0];
148        assert_eq!(unit.number, "0");
149        assert_eq!(unit.description.as_deref(), Some("core link"));
150        assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
151        assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
152        assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
153        assert!(unit.iso_addresses.is_empty());
154    }
155
156    #[test]
157    fn disabled_vlan_units_and_loopback_net() {
158        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
159        assert!(ifaces[1].disabled);
160        assert_eq!(ifaces[1].units[0].number, "100");
161        assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
162        assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
163    }
164
165    #[test]
166    fn missing_interfaces_block_is_empty() {
167        assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
168    }
169
170    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
171    #[test]
172    fn extracts_physical_interface_settings() {
173        let ifaces = interfaces(&juniper::parse_braces(
174            r#"
175interfaces {
176    xe-0/0/0 {
177        mtu 9192;
178        speed 10g;
179        encapsulation flexible-ethernet-services;
180        vlan-tagging;
181        flexible-vlan-tagging;
182        native-vlan-id 10;
183        per-unit-scheduler;
184        no-gratuitous-arp-request;
185    }
186    inactive: xe-0/0/1 {
187        mtu 1500;
188    }
189    xe-0/0/2 {
190        inactive: mtu 1500;
191    }
192}
193"#,
194        ));
195        assert_eq!(ifaces.len(), 2);
196
197        let xe0 = &ifaces[0];
198        assert_eq!(xe0.mtu.as_deref(), Some("9192"));
199        assert_eq!(xe0.speed.as_deref(), Some("10g"));
200        assert_eq!(xe0.encapsulation.as_deref(), Some("flexible-ethernet-services"));
201        assert!(xe0.vlan_tagging);
202        assert!(xe0.flexible_vlan_tagging);
203        assert_eq!(xe0.native_vlan_id.as_deref(), Some("10"));
204        assert!(xe0.per_unit_scheduler);
205        assert!(xe0.no_gratuitous_arp_request);
206
207        assert_eq!(ifaces[1].mtu, None);
208    }
209
210    #[test]
211    fn physical_interface_settings_default_to_off() {
212        let ifaces = interfaces(&juniper::parse_braces(
213            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
214        ));
215        let ge = &ifaces[0];
216        assert_eq!(ge.mtu, None);
217        assert_eq!(ge.speed, None);
218        assert_eq!(ge.encapsulation, None);
219        assert!(!ge.vlan_tagging);
220        assert!(!ge.flexible_vlan_tagging);
221        assert_eq!(ge.native_vlan_id, None);
222        assert!(!ge.per_unit_scheduler);
223        assert!(!ge.no_gratuitous_arp_request);
224    }
225
226    // The DOC_* fixtures are adapted from Juniper's BGP User Guide, "BGP Peering Sessions"; see the README "Sources".
227
228    // "Example: Configuring External BGP Point-to-Point Peer Sessions", Device E `show interfaces` (two of four interfaces).
229    const DOC_POINT_TO_POINT: &str = r#"
230interfaces {
231    ge-1/2/0 {
232        unit 0 {
233            description to-A;
234            family inet {
235                address 10.10.10.1/30;
236            }
237        }
238    }
239    ge-0/0/1 {
240        unit 5 {
241            description to-B;
242            family inet {
243                address 10.10.10.5/30;
244            }
245        }
246    }
247}
248"#;
249
250    // "Example: Configure Multiple Single-Hop EBGP Sessions on Different Links Using the Same IPv6 Link-Local
251    // Address", R1 `show interfaces`.
252    const DOC_LINK_LOCAL: &str = r#"
253interfaces {
254    ge-0/0/1 {
255        description R1-to-R2-Link;
256        vlan-tagging;
257        unit 1 {
258            vlan-id 1;
259            family inet6 {
260                address fe80::10/64;
261            }
262        }
263        unit 2 {
264            vlan-id 2;
265            family inet6 {
266                address fe80::10/64;
267            }
268        }
269    }
270    lo0 {
271        unit 0 {
272            family inet {
273                address 198.51.100.1/24 {
274                    primary;
275                }
276            }
277        }
278    }
279}
280"#;
281
282    #[test]
283    fn doc_point_to_point_interfaces() {
284        let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
285        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
286        assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
287        assert_eq!(ifaces[0].units[0].number, "0");
288        assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
289        assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
290        assert_eq!(ifaces[1].units[0].number, "5");
291        assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
292    }
293
294    #[test]
295    fn doc_link_local_units_vlans_and_primary_address() {
296        let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
297        let ge = &ifaces[0];
298        assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
299        assert_eq!(ge.units.len(), 2);
300        assert_eq!(ge.units[0].vlan_id, Some(1));
301        assert_eq!(ge.units[1].vlan_id, Some(2));
302        assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);
303
304        let lo0 = &ifaces[1];
305        assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
306    }
307}