Skip to main content

asterust_junos/interfaces/
mod.rs

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