Skip to main content

asterust_junos/interfaces/ether_options/
mod.rs

1//! `interfaces { <name> { ether-options { ... } } }`, also written `gigether-options` or `fastether-options`.
2
3mod ieee_802_3ad;
4
5pub use ieee_802_3ad::Ieee8023ad;
6
7use crate::{arg_of, child_block, statement_words, toggle, value_after};
8use asterust::config::ConfigLine;
9use ieee_802_3ad::parse_ieee_802_3ad;
10
11/// The Ethernet options of a physical interface.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct EtherOptions {
15    /// `Some(true)` for `auto-negotiation`, `Some(false)` for `no-auto-negotiation`.
16    pub auto_negotiation: Option<bool>,
17    /// `local-interface-online` or `local-interface-offline`.
18    pub remote_fault: Option<String>,
19    /// `Some(true)` for `flow-control`, `Some(false)` for `no-flow-control`.
20    pub flow_control: Option<bool>,
21    /// `Some(true)` for `loopback`, `Some(false)` for `no-loopback`.
22    pub loopback: Option<bool>,
23    /// `Some(true)` for `source-filtering`, `Some(false)` for `no-source-filtering`.
24    pub source_filtering: Option<bool>,
25    pub speed: Option<String>,
26    /// `full-duplex`, `half-duplex` or `automatic`.
27    pub link_mode: Option<String>,
28    /// The bundle this interface is a member of; the statement is `802.3ad`.
29    pub ieee_802_3ad: Option<Ieee8023ad>,
30}
31
32pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
33    let options = ["ether-options", "gigether-options", "fastether-options"]
34        .iter()
35        .find_map(|name| child_block(interface, name))?;
36    let negotiation =
37        statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
38    Some(EtherOptions {
39        auto_negotiation: toggle(options, "auto-negotiation"),
40        remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
41        flow_control: toggle(options, "flow-control"),
42        loopback: toggle(options, "loopback"),
43        source_filtering: toggle(options, "source-filtering"),
44        speed: arg_of(options, "speed").map(str::to_string),
45        link_mode: arg_of(options, "link-mode").map(str::to_string),
46        ieee_802_3ad: parse_ieee_802_3ad(options),
47    })
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::interfaces::interfaces;
53    use asterust::config::juniper;
54
55    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
56    const CONFIG: &str = r#"
57interfaces {
58    ge-0/0/0 {
59        gigether-options {
60            802.3ad ae0;
61            no-auto-negotiation;
62            no-flow-control;
63            loopback;
64            source-filtering;
65            speed 1g;
66        }
67    }
68    ge-0/0/1 {
69        gigether-options {
70            802.3ad ae1 {
71                primary;
72                link-index 2;
73                distribution-list DL1;
74                lacp {
75                    port-priority 64;
76                }
77            }
78            auto-negotiation remote-fault local-interface-offline;
79        }
80    }
81    xe-0/0/0 {
82        ether-options {
83            802.3ad {
84                ae2;
85                backup;
86            }
87            link-mode full-duplex;
88            no-loopback;
89            no-source-filtering;
90            flow-control;
91        }
92    }
93}
94"#;
95
96    #[test]
97    fn extracts_ether_options_and_bundle_membership() {
98        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
99        let options = ifaces[0].ether_options.as_ref().unwrap();
100        assert_eq!(options.auto_negotiation, Some(false));
101        assert_eq!(options.remote_fault, None);
102        assert_eq!(options.flow_control, Some(false));
103        assert_eq!(options.loopback, Some(true));
104        assert_eq!(options.source_filtering, Some(true));
105        assert_eq!(options.speed.as_deref(), Some("1g"));
106        assert_eq!(options.link_mode, None);
107
108        let bundle = options.ieee_802_3ad.as_ref().unwrap();
109        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
110        assert_eq!(bundle.role, None);
111    }
112
113    #[test]
114    fn extracts_802_3ad_block_and_remote_fault() {
115        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
116        let options = ifaces[1].ether_options.as_ref().unwrap();
117        assert_eq!(options.auto_negotiation, Some(true));
118        assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));
119
120        let bundle = options.ieee_802_3ad.as_ref().unwrap();
121        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
122        assert_eq!(bundle.role.as_deref(), Some("primary"));
123        assert_eq!(bundle.link_index.as_deref(), Some("2"));
124        assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
125        assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
126    }
127
128    #[test]
129    fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
130        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
131        let options = ifaces[2].ether_options.as_ref().unwrap();
132        assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
133        assert_eq!(options.loopback, Some(false));
134        assert_eq!(options.source_filtering, Some(false));
135        assert_eq!(options.flow_control, Some(true));
136        assert_eq!(options.auto_negotiation, None);
137
138        let bundle = options.ieee_802_3ad.as_ref().unwrap();
139        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
140        assert_eq!(bundle.role.as_deref(), Some("backup"));
141    }
142
143    #[test]
144    fn ether_options_are_optional() {
145        let ifaces = interfaces(&juniper::parse_braces(
146            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
147        ));
148        assert!(ifaces[0].ether_options.is_none());
149    }
150}