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