Skip to main content

asterust_junos/interfaces/
mod.rs

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