mod ethernet_switch_profile;
mod ieee_802_3ad;
pub(in crate::interfaces) use ethernet_switch_profile::parse_ethernet_switch_profile;
pub use ethernet_switch_profile::{EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile};
pub use ieee_802_3ad::Ieee8023ad;
use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, toggle, value_after};
use asterust::config::ConfigLine;
use ieee_802_3ad::parse_ieee_802_3ad;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EtherOptions {
pub auto_negotiation: Option<bool>,
pub remote_fault: Option<String>,
pub flow_control: Option<bool>,
pub loopback: Option<bool>,
pub source_filtering: Option<bool>,
pub speed: Option<String>,
pub link_mode: Option<String>,
pub fec: Option<String>,
pub ieee_802_3ad: Option<Ieee8023ad>,
pub mdi_mode: Option<String>,
pub no_auto_mdix: bool,
pub autostate_exclude: bool,
pub ieee_802_3az_eee: bool,
pub configured_flow_control_rx_buffers: Option<bool>,
pub configured_flow_control_tx_buffers: Option<bool>,
pub ethernet_switch_profile: Option<EthernetSwitchProfile>,
pub redundant_parent: Option<String>,
pub mru: Option<String>,
pub loopback_remote: bool,
pub ignore_l3_incompletes: bool,
pub asynchronous_notification: Option<bool>,
pub ingress_rate_limit: Option<String>,
pub source_address_filter: Vec<String>,
}
pub(super) fn source_address_filter(options: &ConfigLine) -> Vec<String> {
let mut found: Vec<String> = args_of(options, "source-address-filter")
.into_iter()
.flat_map(list_values)
.collect();
if let Some(filter) = child_block(options, "source-address-filter") {
found.extend(crate::active_children(filter).map(|c| c.text.clone()));
}
found
}
pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
let options = ["ether-options", "gigether-options", "fastether-options"]
.iter()
.find_map(|name| child_block(interface, name))?;
let negotiation =
statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
let flow_control = child_block(options, "configured-flow-control");
Some(EtherOptions {
auto_negotiation: toggle(options, "auto-negotiation"),
remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
flow_control: toggle(options, "flow-control"),
loopback: toggle(options, "loopback"),
source_filtering: toggle(options, "source-filtering"),
speed: arg_of(options, "speed").map(str::to_string),
link_mode: arg_of(options, "link-mode").map(str::to_string),
fec: arg_of(options, "fec").map(str::to_string),
ieee_802_3ad: parse_ieee_802_3ad(options),
mdi_mode: arg_of(options, "mdi-mode").map(str::to_string),
no_auto_mdix: has_leaf(options, "no-auto-mdix"),
autostate_exclude: has_leaf(options, "autostate-exclude"),
ieee_802_3az_eee: has_leaf(options, "ieee-802-3az-eee"),
configured_flow_control_rx_buffers: flow_control.and_then(|f| arg_of(f, "rx-buffers")).map(|v| v == "on"),
configured_flow_control_tx_buffers: flow_control.and_then(|f| arg_of(f, "tx-buffers")).map(|v| v == "on"),
ethernet_switch_profile: child_block(options, "ethernet-switch-profile").map(parse_ethernet_switch_profile),
redundant_parent: arg_of(options, "redundant-parent").map(str::to_string),
mru: arg_of(options, "mru").map(str::to_string),
loopback_remote: has_leaf(options, "loopback-remote"),
ignore_l3_incompletes: has_leaf(options, "ignore-l3-incompletes"),
asynchronous_notification: toggle(options, "asynchronous-notification"),
ingress_rate_limit: arg_of(options, "ingress-rate-limit").map(str::to_string),
source_address_filter: source_address_filter(options),
})
}
#[cfg(test)]
mod tests {
use crate::interfaces::interfaces;
use asterust::config::juniper;
const CONFIG: &str = r#"
interfaces {
ge-0/0/0 {
gigether-options {
802.3ad ae0;
no-auto-negotiation;
no-flow-control;
loopback;
source-filtering;
speed 1g;
}
}
ge-0/0/1 {
gigether-options {
802.3ad ae1 {
primary;
link-index 2;
distribution-list DL1;
lacp {
port-priority 64;
}
}
auto-negotiation remote-fault local-interface-offline;
}
}
xe-0/0/0 {
ether-options {
802.3ad {
ae2;
backup;
}
link-mode full-duplex;
fec fec91;
no-loopback;
no-source-filtering;
flow-control;
}
}
}
"#;
#[test]
fn extracts_ether_options_and_bundle_membership() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let options = ifaces[0].ether_options.as_ref().unwrap();
assert_eq!(options.auto_negotiation, Some(false));
assert_eq!(options.remote_fault, None);
assert_eq!(options.flow_control, Some(false));
assert_eq!(options.loopback, Some(true));
assert_eq!(options.source_filtering, Some(true));
assert_eq!(options.speed.as_deref(), Some("1g"));
assert_eq!(options.link_mode, None);
let bundle = options.ieee_802_3ad.as_ref().unwrap();
assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
assert_eq!(bundle.role, None);
}
#[test]
fn extracts_802_3ad_block_and_remote_fault() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let options = ifaces[1].ether_options.as_ref().unwrap();
assert_eq!(options.auto_negotiation, Some(true));
assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));
let bundle = options.ieee_802_3ad.as_ref().unwrap();
assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
assert_eq!(bundle.role.as_deref(), Some("primary"));
assert_eq!(bundle.link_index.as_deref(), Some("2"));
assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
}
#[test]
fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let options = ifaces[2].ether_options.as_ref().unwrap();
assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
assert_eq!(options.fec.as_deref(), Some("fec91"));
assert_eq!(options.loopback, Some(false));
assert_eq!(options.source_filtering, Some(false));
assert_eq!(options.flow_control, Some(true));
assert_eq!(options.auto_negotiation, None);
let bundle = options.ieee_802_3ad.as_ref().unwrap();
assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
assert_eq!(bundle.role.as_deref(), Some("backup"));
}
#[test]
fn extracts_the_other_ethernet_options() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
ge-0/0/5 {
gigether-options {
redundant-parent reth0;
mru 9216;
loopback-remote;
no-asynchronous-notification;
ignore-l3-incompletes;
source-address-filter {
00:11:22:33:44:55;
00:11:22:33:44:66;
}
}
}
fe-0/0/6 {
fastether-options {
ingress-rate-limit 100;
redundant-parent reth1;
}
}
xe-0/0/7 {
ether-options {
ieee-802-3az-eee;
autostate-exclude;
configured-flow-control {
rx-buffers on;
tx-buffers off;
}
802.3ad {
ae3;
link-protection-sub-group grp1;
}
}
}
}
"#,
));
let gig = ifaces[0].ether_options.as_ref().unwrap();
assert_eq!(gig.redundant_parent.as_deref(), Some("reth0"));
assert_eq!(gig.mru.as_deref(), Some("9216"));
assert!(gig.loopback_remote);
assert_eq!(gig.asynchronous_notification, Some(false));
assert!(gig.ignore_l3_incompletes);
assert_eq!(gig.source_address_filter, ["00:11:22:33:44:55", "00:11:22:33:44:66"]);
let fast = ifaces[1].ether_options.as_ref().unwrap();
assert_eq!(fast.ingress_rate_limit.as_deref(), Some("100"));
assert_eq!(fast.redundant_parent.as_deref(), Some("reth1"));
let xe = ifaces[2].ether_options.as_ref().unwrap();
assert!(xe.ieee_802_3az_eee);
assert!(xe.autostate_exclude);
assert_eq!(xe.configured_flow_control_rx_buffers, Some(true));
assert_eq!(xe.configured_flow_control_tx_buffers, Some(false));
assert_eq!(
xe.ieee_802_3ad.as_ref().unwrap().link_protection_sub_group.as_deref(),
Some("grp1")
);
assert!(!gig.autostate_exclude);
assert_eq!(gig.configured_flow_control_rx_buffers, None);
assert!(gig.ethernet_switch_profile.is_none());
}
#[test]
fn ether_options_are_optional() {
let ifaces = interfaces(&juniper::parse_braces(
"interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n",
));
assert!(ifaces[0].ether_options.is_none());
}
}