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