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