Skip to main content

asterust_junos/interfaces/ether_options/
mod.rs

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