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//! - [ieee-802-3az-eee](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ieee-802-3az-eee-interfaces.html)
19//! - [autostate-exclude](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autostate-exclude-edit-interfaces-ether-options.html)
20//! - [configured-flow-control](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/configured-flow-control-edit-interfaces-qfx-series.html)
21//! - [redundant-parent (Interfaces Gigabit Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-redundant-parent-gigabit-ethernet.html)
22//! - [redundant-parent (Interfaces Fast Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-redundant-parent-fast-ethernet.html)
23//! - [mru (Gigabit Ethernet Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-gigether-options-mru.html)
24//! - [loopback-remote](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-gigether-options-loopback-remote.html)
25//! - [asynchronous-notification](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/asynchronous-notification-edit-interfaces.html)
26//! - [ignore-l3-incompletes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ignore-l3-incompletes-edit-interfaces.html)
27//! - [ingress-rate-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ingress-rate-limit-edit-interfaces.html)
28//! - [source-address-filter](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-address-filter-edit-interfaces.html)
29//! - [fec (ether)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-ether-options-fec.html)
30
31mod ethernet_switch_profile;
32mod ieee_802_3ad;
33
34pub(in crate::interfaces) use ethernet_switch_profile::parse_ethernet_switch_profile;
35pub use ethernet_switch_profile::{EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile};
36pub use ieee_802_3ad::Ieee8023ad;
37
38use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, toggle, value_after};
39use asterust::config::ConfigLine;
40use ieee_802_3ad::parse_ieee_802_3ad;
41
42/// The Ethernet options of a physical interface.
43#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct EtherOptions {
46    /// `Some(true)` for `auto-negotiation`, `Some(false)` for `no-auto-negotiation`.
47    pub auto_negotiation: Option<bool>,
48    /// `local-interface-online` or `local-interface-offline`.
49    pub remote_fault: Option<String>,
50    /// `Some(true)` for `flow-control`, `Some(false)` for `no-flow-control`.
51    pub flow_control: Option<bool>,
52    /// `Some(true)` for `loopback`, `Some(false)` for `no-loopback`.
53    pub loopback: Option<bool>,
54    /// `Some(true)` for `source-filtering`, `Some(false)` for `no-source-filtering`.
55    pub source_filtering: Option<bool>,
56    pub speed: Option<String>,
57    /// `full-duplex`, `half-duplex` or `automatic`.
58    pub link_mode: Option<String>,
59    /// e.g. `"fec91"`, `"fec74"`, `"fec108"` or `"none"`.
60    pub fec: Option<String>,
61    /// The bundle this interface is a member of; the statement is `802.3ad`.
62    pub ieee_802_3ad: Option<Ieee8023ad>,
63    pub mdi_mode: Option<String>,
64    pub no_auto_mdix: bool,
65    pub autostate_exclude: bool,
66    pub ieee_802_3az_eee: bool,
67    /// `Some(true)` for `on`, from `configured-flow-control { rx-buffers }`.
68    pub configured_flow_control_rx_buffers: Option<bool>,
69    /// `Some(true)` for `on`, from `configured-flow-control { tx-buffers }`.
70    pub configured_flow_control_tx_buffers: Option<bool>,
71    pub ethernet_switch_profile: Option<EthernetSwitchProfile>,
72    /// The redundant Ethernet interface the interface is a child of, from `redundant-parent`.
73    pub redundant_parent: Option<String>,
74    pub mru: Option<String>,
75    pub loopback_remote: bool,
76    pub ignore_l3_incompletes: bool,
77    /// `Some(true)` for `asynchronous-notification`, `Some(false)` for `no-asynchronous-notification`.
78    pub asynchronous_notification: Option<bool>,
79    /// From `fastether-options { ingress-rate-limit }`.
80    pub ingress_rate_limit: Option<String>,
81    /// The MAC addresses of `source-address-filter`.
82    pub source_address_filter: Vec<String>,
83}
84
85/// The MAC addresses of `source-address-filter { mac-address; }` or `source-address-filter <mac>;`.
86pub(super) fn source_address_filter(options: &ConfigLine) -> Vec<String> {
87    let mut found: Vec<String> = args_of(options, "source-address-filter")
88        .into_iter()
89        .flat_map(list_values)
90        .collect();
91    if let Some(filter) = child_block(options, "source-address-filter") {
92        found.extend(crate::active_children(filter).map(|c| c.text.clone()));
93    }
94    found
95}
96
97pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
98    let options = ["ether-options", "gigether-options", "fastether-options"]
99        .iter()
100        .find_map(|name| child_block(interface, name))?;
101    let negotiation =
102        statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
103    let flow_control = child_block(options, "configured-flow-control");
104    Some(EtherOptions {
105        auto_negotiation: toggle(options, "auto-negotiation"),
106        remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
107        flow_control: toggle(options, "flow-control"),
108        loopback: toggle(options, "loopback"),
109        source_filtering: toggle(options, "source-filtering"),
110        speed: arg_of(options, "speed").map(str::to_string),
111        link_mode: arg_of(options, "link-mode").map(str::to_string),
112        fec: arg_of(options, "fec").map(str::to_string),
113        ieee_802_3ad: parse_ieee_802_3ad(options),
114        mdi_mode: arg_of(options, "mdi-mode").map(str::to_string),
115        no_auto_mdix: has_leaf(options, "no-auto-mdix"),
116        autostate_exclude: has_leaf(options, "autostate-exclude"),
117        ieee_802_3az_eee: has_leaf(options, "ieee-802-3az-eee"),
118        configured_flow_control_rx_buffers: flow_control.and_then(|f| arg_of(f, "rx-buffers")).map(|v| v == "on"),
119        configured_flow_control_tx_buffers: flow_control.and_then(|f| arg_of(f, "tx-buffers")).map(|v| v == "on"),
120        ethernet_switch_profile: child_block(options, "ethernet-switch-profile").map(parse_ethernet_switch_profile),
121        redundant_parent: arg_of(options, "redundant-parent").map(str::to_string),
122        mru: arg_of(options, "mru").map(str::to_string),
123        loopback_remote: has_leaf(options, "loopback-remote"),
124        ignore_l3_incompletes: has_leaf(options, "ignore-l3-incompletes"),
125        asynchronous_notification: toggle(options, "asynchronous-notification"),
126        ingress_rate_limit: arg_of(options, "ingress-rate-limit").map(str::to_string),
127        source_address_filter: source_address_filter(options),
128    })
129}
130
131#[cfg(test)]
132mod tests {
133    use crate::interfaces::interfaces;
134    use asterust::config::juniper;
135
136    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
137    const CONFIG: &str = r#"
138interfaces {
139    ge-0/0/0 {
140        gigether-options {
141            802.3ad ae0;
142            no-auto-negotiation;
143            no-flow-control;
144            loopback;
145            source-filtering;
146            speed 1g;
147        }
148    }
149    ge-0/0/1 {
150        gigether-options {
151            802.3ad ae1 {
152                primary;
153                link-index 2;
154                distribution-list DL1;
155                lacp {
156                    port-priority 64;
157                }
158            }
159            auto-negotiation remote-fault local-interface-offline;
160        }
161    }
162    xe-0/0/0 {
163        ether-options {
164            802.3ad {
165                ae2;
166                backup;
167            }
168            link-mode full-duplex;
169            fec fec91;
170            no-loopback;
171            no-source-filtering;
172            flow-control;
173        }
174    }
175}
176"#;
177
178    #[test]
179    fn extracts_ether_options_and_bundle_membership() {
180        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
181        let options = ifaces[0].ether_options.as_ref().unwrap();
182        assert_eq!(options.auto_negotiation, Some(false));
183        assert_eq!(options.remote_fault, None);
184        assert_eq!(options.flow_control, Some(false));
185        assert_eq!(options.loopback, Some(true));
186        assert_eq!(options.source_filtering, Some(true));
187        assert_eq!(options.speed.as_deref(), Some("1g"));
188        assert_eq!(options.link_mode, None);
189
190        let bundle = options.ieee_802_3ad.as_ref().unwrap();
191        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
192        assert_eq!(bundle.role, None);
193    }
194
195    #[test]
196    fn extracts_802_3ad_block_and_remote_fault() {
197        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
198        let options = ifaces[1].ether_options.as_ref().unwrap();
199        assert_eq!(options.auto_negotiation, Some(true));
200        assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));
201
202        let bundle = options.ieee_802_3ad.as_ref().unwrap();
203        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
204        assert_eq!(bundle.role.as_deref(), Some("primary"));
205        assert_eq!(bundle.link_index.as_deref(), Some("2"));
206        assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
207        assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
208    }
209
210    #[test]
211    fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
212        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
213        let options = ifaces[2].ether_options.as_ref().unwrap();
214        assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
215        assert_eq!(options.fec.as_deref(), Some("fec91"));
216        assert_eq!(options.loopback, Some(false));
217        assert_eq!(options.source_filtering, Some(false));
218        assert_eq!(options.flow_control, Some(true));
219        assert_eq!(options.auto_negotiation, None);
220
221        let bundle = options.ieee_802_3ad.as_ref().unwrap();
222        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
223        assert_eq!(bundle.role.as_deref(), Some("backup"));
224    }
225
226    #[test]
227    fn extracts_the_other_ethernet_options() {
228        let ifaces = interfaces(&juniper::parse_braces(
229            r#"
230interfaces {
231    ge-0/0/5 {
232        gigether-options {
233            redundant-parent reth0;
234            mru 9216;
235            loopback-remote;
236            no-asynchronous-notification;
237            ignore-l3-incompletes;
238            source-address-filter {
239                00:11:22:33:44:55;
240                00:11:22:33:44:66;
241            }
242        }
243    }
244    fe-0/0/6 {
245        fastether-options {
246            ingress-rate-limit 100;
247            redundant-parent reth1;
248        }
249    }
250    xe-0/0/7 {
251        ether-options {
252            ieee-802-3az-eee;
253            autostate-exclude;
254            configured-flow-control {
255                rx-buffers on;
256                tx-buffers off;
257            }
258            802.3ad {
259                ae3;
260                link-protection-sub-group grp1;
261            }
262        }
263    }
264}
265"#,
266        ));
267        let gig = ifaces[0].ether_options.as_ref().unwrap();
268        assert_eq!(gig.redundant_parent.as_deref(), Some("reth0"));
269        assert_eq!(gig.mru.as_deref(), Some("9216"));
270        assert!(gig.loopback_remote);
271        assert_eq!(gig.asynchronous_notification, Some(false));
272        assert!(gig.ignore_l3_incompletes);
273        assert_eq!(gig.source_address_filter, ["00:11:22:33:44:55", "00:11:22:33:44:66"]);
274        let fast = ifaces[1].ether_options.as_ref().unwrap();
275        assert_eq!(fast.ingress_rate_limit.as_deref(), Some("100"));
276        assert_eq!(fast.redundant_parent.as_deref(), Some("reth1"));
277        let xe = ifaces[2].ether_options.as_ref().unwrap();
278        assert!(xe.ieee_802_3az_eee);
279        assert!(xe.autostate_exclude);
280        assert_eq!(xe.configured_flow_control_rx_buffers, Some(true));
281        assert_eq!(xe.configured_flow_control_tx_buffers, Some(false));
282        assert_eq!(
283            xe.ieee_802_3ad.as_ref().unwrap().link_protection_sub_group.as_deref(),
284            Some("grp1")
285        );
286        assert!(!gig.autostate_exclude);
287        assert_eq!(gig.configured_flow_control_rx_buffers, None);
288        assert!(gig.ethernet_switch_profile.is_none());
289    }
290
291    #[test]
292    fn ether_options_are_optional() {
293        let ifaces = interfaces(&juniper::parse_braces(
294            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
295        ));
296        assert!(ifaces[0].ether_options.is_none());
297    }
298}