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