mod aggregated_ether_options;
mod aggregated_inline_services_options;
mod auto_configure;
mod es_options;
mod esi;
mod ether_options;
mod forwarding_class_accounting;
mod hierarchical_scheduler;
mod hold_time;
mod interface_range;
mod interface_set;
mod keepalives;
mod layer2_policer;
mod link_degrade_monitor;
mod load_balancing_options;
mod logical_tunnel_options;
mod multi_chassis_protection;
mod multiservice_options;
mod optics_options;
mod otn_options;
mod ppp_options;
mod radius_options;
mod redundancy_options;
mod redundant_ether_options;
mod services_options;
mod targeted_options;
mod tca;
mod unit;
pub use aggregated_ether_options::{
AggregatedEtherOptions, BfdDamping, BfdLivenessDetection, Dlb, Lacp, LinkProtection, LinkProtectionSubGroup,
LoadBalance, McAe, McAeEvents, RebalancePeriodic,
};
pub use aggregated_inline_services_options::AggregatedInlineServicesOptions;
pub use auto_configure::{AutoConfigure, AutoConfigureProfile, AutoConfigureRanges};
pub use es_options::EsOptions;
pub use esi::{Esi, EsiAutoDerive, EsiDfElectionType, EsiDfGranularity, EsiPreference, EsiSystemMac};
pub use ether_options::{
EtherOptions, EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile, Ieee8023ad,
};
pub use forwarding_class_accounting::{EnhancedForwardingClassAccounting, ForwardingClassAccounting};
pub use hierarchical_scheduler::{hierarchical_scheduler, HierarchicalScheduler};
pub use hold_time::HoldTime;
pub use interface_range::{interface_ranges, InterfaceRange, MemberRange};
pub use interface_set::{interface_sets, InterfaceSet, InterfaceSetMember};
pub use keepalives::Keepalives;
pub use layer2_policer::Layer2Policer;
pub use link_degrade_monitor::{LinkDegradeMonitor, LinkDegradeThresholds};
pub use load_balancing_options::LoadBalancingOptions;
pub use logical_tunnel_options::{logical_tunnel_options, LogicalTunnelOptions};
pub use multi_chassis_protection::MultiChassisProtection;
pub use multiservice_options::MultiserviceOptions;
pub use optics_options::{OpticsApplication, OpticsLowLight, OpticsOptions};
pub use otn_options::{OduDelayManagement, OtnOptions, OtnRate, OtnTrigger, PreemptiveFastReroute, SignalDegrade};
pub use ppp_options::{PppChap, PppCompression, PppOptions, PppPap};
pub use radius_options::{NasPortExtendedFormat, NasPortOptions, RadiusOptions};
pub use redundancy_options::RedundancyOptions;
pub use redundant_ether_options::RedundantEtherOptions;
pub use services_options::{ServicesOptions, ServicesSyslog, ServicesSyslogHost, SessionLimit};
pub use targeted_options::TargetedOptions;
pub use tca::Tca;
pub use unit::{
AcceptedMac, AddressArp, AdvisoryOptions, BandwidthThreshold, Demux, DemuxAddressRanges, DemuxDynamicProfile,
DemuxFamily, DemuxNetwork, DemuxOptions, DemuxRange, DemuxUsernameInclude, DhcpClient, DhcpClientIdentifier,
Dhcpv6Client, DuplicateProtection, EthernetSwitching, FamilyAccounting, FamilyFilter, FamilyPolicer,
FamilySampling, InterfaceUnit, NextHopTunnel, PolicerOverhead, PppoeOptions, PppoeUnderlyingOptions,
PrefixDelegating, RouterAdvertisementInterface, RpfCheck, ShortCycleProtection, TargetedBroadcast,
TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, UnitTunnel, UnnumberedAddress, VirtualGatewayEsi, VlanMap,
VlanTags, VlanTranslation, VrrpGroup, VrrpInheritFrom, VrrpTrack,
};
use crate::{active_children, arg_of, child_block, has_leaf, named_children, toggle, top_level};
use aggregated_ether_options::parse_aggregated_ether_options;
use aggregated_inline_services_options::parse_aggregated_inline_services_options;
use asterust::config::{Config, ConfigLine};
use auto_configure::parse_auto_configure;
use es_options::parse_es_options;
use esi::parse_esi;
use ether_options::parse_ether_options;
use forwarding_class_accounting::parse_forwarding_class_accounting;
use hierarchical_scheduler::parse_hierarchical_scheduler;
use hold_time::parse_hold_time;
use keepalives::parse_keepalives;
use layer2_policer::parse_layer2_policer;
use link_degrade_monitor::parse_link_degrade_monitor;
use load_balancing_options::parse_load_balancing_options;
use multi_chassis_protection::parse_multi_chassis_protection;
use multiservice_options::parse_multiservice_options;
use optics_options::parse_optics_options;
use otn_options::parse_otn_options;
use ppp_options::parse_ppp_options;
use radius_options::parse_radius_options;
use redundancy_options::parse_redundancy_options;
use redundant_ether_options::parse_redundant_ether_options;
use services_options::parse_services_options;
use targeted_options::parse_targeted_options;
use unit::parse_unit;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Interface {
pub name: String,
pub description: Option<String>,
pub disabled: bool,
pub mtu: Option<String>,
pub speed: Option<String>,
pub encapsulation: Option<String>,
pub media_type: Option<String>,
pub vlan_tagging: bool,
pub flexible_vlan_tagging: bool,
pub native_vlan_id: Option<String>,
pub per_unit_scheduler: bool,
pub no_gratuitous_arp_request: bool,
pub hold_time: Option<HoldTime>,
pub ether_options: Option<EtherOptions>,
pub aggregated_ether_options: Option<AggregatedEtherOptions>,
pub auto_configure: Option<AutoConfigure>,
pub optics_options: Option<OpticsOptions>,
pub otn_options: Option<OtnOptions>,
pub link_degrade_monitor: Option<LinkDegradeMonitor>,
pub bypass_queuing_chip: bool,
pub cascade_port: bool,
pub no_per_unit_scheduler: bool,
pub no_keepalives: bool,
pub stacked_vlan_tagging: bool,
pub oam_on_svlan: bool,
pub promiscuous_mode: bool,
pub no_native_vlan_insert: bool,
pub shared_scheduler: bool,
pub unidirectional: bool,
pub gratuitous_arp_reply: Option<bool>,
pub traps: Option<bool>,
pub input_native_vlan_push: Option<bool>,
pub number_of_sub_ports: Option<String>,
pub accounting_profile: Option<String>,
pub keepalives: Option<Keepalives>,
pub hierarchical_scheduler: Option<HierarchicalScheduler>,
pub layer2_policer: Option<Layer2Policer>,
pub redundant_ether_options: Option<RedundantEtherOptions>,
pub es_options: Option<EsOptions>,
pub multi_chassis_protection: Option<MultiChassisProtection>,
pub esi: Option<Esi>,
pub aggregated_inline_services_options: Option<AggregatedInlineServicesOptions>,
pub forwarding_class_accounting: Option<ForwardingClassAccounting>,
pub ppp_options: Option<PppOptions>,
pub radius_options: Option<RadiusOptions>,
pub targeted_options: Option<TargetedOptions>,
pub load_balancing_options: Option<LoadBalancingOptions>,
pub multiservice_options: Option<MultiserviceOptions>,
pub redundancy_options: Option<RedundancyOptions>,
pub services_options: Option<ServicesOptions>,
pub units: Vec<InterfaceUnit>,
}
const GLOBAL_BLOCKS: [&str; 2] = ["hierarchical-scheduler", "logical-tunnel-options"];
pub fn interfaces(config: &Config) -> Vec<Interface> {
let Some(root) = top_level(config, "interfaces") else {
return Vec::new();
};
active_children(root)
.filter(|c| !c.text.contains(' ') && !GLOBAL_BLOCKS.contains(&c.text.as_str()))
.map(parse_interface)
.collect()
}
fn parse_interface(line: &ConfigLine) -> Interface {
Interface {
name: line.text.clone(),
description: arg_of(line, "description").map(str::to_string),
disabled: has_leaf(line, "disable"),
mtu: arg_of(line, "mtu").map(str::to_string),
speed: arg_of(line, "speed").map(str::to_string),
encapsulation: arg_of(line, "encapsulation").map(str::to_string),
media_type: arg_of(line, "media-type").map(str::to_string),
vlan_tagging: has_leaf(line, "vlan-tagging"),
flexible_vlan_tagging: has_leaf(line, "flexible-vlan-tagging"),
native_vlan_id: arg_of(line, "native-vlan-id").map(str::to_string),
per_unit_scheduler: has_leaf(line, "per-unit-scheduler"),
no_gratuitous_arp_request: has_leaf(line, "no-gratuitous-arp-request"),
hold_time: parse_hold_time(line),
ether_options: parse_ether_options(line),
aggregated_ether_options: child_block(line, "aggregated-ether-options").map(parse_aggregated_ether_options),
auto_configure: child_block(line, "auto-configure").map(parse_auto_configure),
optics_options: child_block(line, "optics-options").map(parse_optics_options),
otn_options: child_block(line, "otn-options").map(parse_otn_options),
link_degrade_monitor: child_block(line, "link-degrade-monitor").map(parse_link_degrade_monitor),
bypass_queuing_chip: has_leaf(line, "bypass-queuing-chip"),
cascade_port: has_leaf(line, "cascade-port"),
no_per_unit_scheduler: has_leaf(line, "no-per-unit-scheduler"),
no_keepalives: has_leaf(line, "no-keepalives"),
stacked_vlan_tagging: has_leaf(line, "stacked-vlan-tagging"),
oam_on_svlan: has_leaf(line, "oam-on-svlan"),
promiscuous_mode: has_leaf(line, "promiscuous-mode"),
no_native_vlan_insert: has_leaf(line, "no-native-vlan-insert"),
shared_scheduler: has_leaf(line, "shared-scheduler"),
unidirectional: has_leaf(line, "unidirectional"),
gratuitous_arp_reply: toggle(line, "gratuitous-arp-reply"),
traps: toggle(line, "traps"),
input_native_vlan_push: arg_of(line, "input-native-vlan-push").map(|v| v == "enable"),
number_of_sub_ports: arg_of(line, "number-of-sub-ports").map(str::to_string),
accounting_profile: arg_of(line, "accounting-profile").map(str::to_string),
keepalives: parse_keepalives(line),
hierarchical_scheduler: parse_hierarchical_scheduler(line),
layer2_policer: child_block(line, "layer2-policer").map(parse_layer2_policer),
redundant_ether_options: child_block(line, "redundant-ether-options").map(parse_redundant_ether_options),
es_options: child_block(line, "es-options").map(parse_es_options),
multi_chassis_protection: parse_multi_chassis_protection(line),
esi: parse_esi(line),
forwarding_class_accounting: child_block(line, "forwarding-class-accounting")
.map(parse_forwarding_class_accounting),
ppp_options: child_block(line, "ppp-options").map(parse_ppp_options),
radius_options: child_block(line, "radius-options").map(parse_radius_options),
targeted_options: child_block(line, "targeted-options").map(parse_targeted_options),
aggregated_inline_services_options: child_block(line, "aggregated-inline-services-options")
.map(parse_aggregated_inline_services_options),
load_balancing_options: child_block(line, "load-balancing-options").map(parse_load_balancing_options),
multiservice_options: child_block(line, "multiservice-options").map(parse_multiservice_options),
redundancy_options: child_block(line, "redundancy-options").map(parse_redundancy_options),
services_options: child_block(line, "services-options").map(parse_services_options),
units: named_children(line, "unit")
.map(|(number, unit)| parse_unit(number, unit))
.collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
const CONFIG: &str = r#"
interfaces {
ge-0/0/0 {
description "to p1";
unit 0 {
description "core link";
family inet {
address 10.0.1.1/30;
address 10.0.2.1/30 {
primary;
}
}
family inet6 {
address 2001:db8:1::1/64;
}
family iso;
family mpls;
}
}
ge-0/0/1 {
disable;
vlan-tagging;
unit 100 {
vlan-id 100;
family inet {
address 192.0.2.1/24;
}
}
}
inactive: ge-0/0/2 {
unit 0;
}
lo0 {
unit 0 {
family inet {
address 10.255.0.1/32;
}
family iso {
address 49.0001.0100.2550.0001.00;
}
}
}
apply-groups GLOBAL;
}
"#;
#[test]
fn extracts_units_families_and_addresses() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);
let core = &ifaces[0];
assert_eq!(core.description.as_deref(), Some("to p1"));
assert!(!core.disabled);
let unit = &core.units[0];
assert_eq!(unit.number, "0");
assert_eq!(unit.description.as_deref(), Some("core link"));
assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
assert!(unit.iso_addresses.is_empty());
}
#[test]
fn disabled_vlan_units_and_loopback_net() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
assert!(ifaces[1].disabled);
assert_eq!(ifaces[1].units[0].number, "100");
assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
}
#[test]
fn missing_interfaces_block_is_empty() {
assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
}
#[test]
fn extracts_physical_interface_settings() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
xe-0/0/0 {
mtu 9192;
speed 10g;
encapsulation flexible-ethernet-services;
vlan-tagging;
flexible-vlan-tagging;
native-vlan-id 10;
per-unit-scheduler;
no-gratuitous-arp-request;
}
inactive: xe-0/0/1 {
mtu 1500;
}
xe-0/0/2 {
inactive: mtu 1500;
}
}
"#,
));
assert_eq!(ifaces.len(), 2);
let xe0 = &ifaces[0];
assert_eq!(xe0.mtu.as_deref(), Some("9192"));
assert_eq!(xe0.speed.as_deref(), Some("10g"));
assert_eq!(xe0.encapsulation.as_deref(), Some("flexible-ethernet-services"));
assert!(xe0.vlan_tagging);
assert!(xe0.flexible_vlan_tagging);
assert_eq!(xe0.native_vlan_id.as_deref(), Some("10"));
assert!(xe0.per_unit_scheduler);
assert!(xe0.no_gratuitous_arp_request);
assert_eq!(ifaces[1].mtu, None);
}
#[test]
fn extracts_more_interface_level_statements() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
xe-0/0/1 {
accounting-profile ap1;
bypass-queuing-chip;
cascade-port;
no-per-unit-scheduler;
no-keepalives;
stacked-vlan-tagging;
oam-on-svlan;
promiscuous-mode;
no-native-vlan-insert;
shared-scheduler;
unidirectional;
gratuitous-arp-reply;
no-traps;
input-native-vlan-push disable;
number-of-sub-ports 4;
keepalives interval 10 down-count 5 up-count 2;
hierarchical-scheduler {
implicit-hierarchy;
maximum-hierarchy-levels 3;
}
layer2-policer {
input-policer p-in;
input-three-color p3-in;
output-policer p-out;
output-three-color p3-out;
input-hierarchical-policer hp;
}
es-options {
backup-interface ge-0/0/9;
}
multi-chassis-protection 10.1.1.1 {
interface ae0;
}
}
reth0 {
keepalives;
input-native-vlan-push enable;
multi-chassis-protection {
peer 10.2.2.2 {
interface ae1;
}
}
redundant-ether-options {
flow-control;
lacp {
active;
periodic fast;
}
link-speed 10g;
no-loopback;
minimum-links 1;
redundancy-group 1;
source-address-filter 00:11:22:33:44:55;
no-source-filtering;
}
}
}
"#,
));
let xe = &ifaces[0];
assert_eq!(xe.accounting_profile.as_deref(), Some("ap1"));
assert!(xe.bypass_queuing_chip && xe.cascade_port && xe.no_per_unit_scheduler && xe.no_keepalives);
assert!(xe.stacked_vlan_tagging && xe.oam_on_svlan && xe.promiscuous_mode && xe.no_native_vlan_insert);
assert!(xe.shared_scheduler && xe.unidirectional);
assert_eq!(xe.gratuitous_arp_reply, Some(true));
assert_eq!(xe.traps, Some(false));
assert_eq!(xe.input_native_vlan_push, Some(false));
assert_eq!(xe.number_of_sub_ports.as_deref(), Some("4"));
let keepalives = xe.keepalives.as_ref().unwrap();
assert_eq!(keepalives.interval.as_deref(), Some("10"));
assert_eq!(keepalives.down_count.as_deref(), Some("5"));
assert_eq!(keepalives.up_count.as_deref(), Some("2"));
let scheduler = xe.hierarchical_scheduler.as_ref().unwrap();
assert!(scheduler.implicit_hierarchy);
assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
let policer = xe.layer2_policer.as_ref().unwrap();
assert_eq!(policer.input_policer.as_deref(), Some("p-in"));
assert_eq!(policer.input_three_color.as_deref(), Some("p3-in"));
assert_eq!(policer.output_policer.as_deref(), Some("p-out"));
assert_eq!(policer.output_three_color.as_deref(), Some("p3-out"));
assert_eq!(policer.input_hierarchical_policer.as_deref(), Some("hp"));
assert_eq!(
xe.es_options.as_ref().unwrap().backup_interface.as_deref(),
Some("ge-0/0/9")
);
let protection = xe.multi_chassis_protection.as_ref().unwrap();
assert_eq!(protection.peer, "10.1.1.1");
assert_eq!(protection.interface.as_deref(), Some("ae0"));
let reth = &ifaces[1];
let bare = reth.keepalives.as_ref().unwrap();
assert!(bare.interval.is_none() && bare.down_count.is_none() && bare.up_count.is_none());
assert_eq!(reth.input_native_vlan_push, Some(true));
let protection = reth.multi_chassis_protection.as_ref().unwrap();
assert_eq!(protection.peer, "10.2.2.2");
assert_eq!(protection.interface.as_deref(), Some("ae1"));
let options = reth.redundant_ether_options.as_ref().unwrap();
assert_eq!(options.flow_control, Some(true));
assert_eq!(options.lacp.as_ref().unwrap().mode.as_deref(), Some("active"));
assert_eq!(options.lacp.as_ref().unwrap().periodic.as_deref(), Some("fast"));
assert_eq!(options.link_speed.as_deref(), Some("10g"));
assert_eq!(options.loopback, Some(false));
assert_eq!(options.minimum_links.as_deref(), Some("1"));
assert_eq!(options.redundancy_group.as_deref(), Some("1"));
assert_eq!(options.source_address_filter, ["00:11:22:33:44:55"]);
assert_eq!(options.source_filtering, Some(false));
assert!(!reth.bypass_queuing_chip && reth.gratuitous_arp_reply.is_none() && reth.traps.is_none());
assert!(reth.hierarchical_scheduler.is_none() && reth.layer2_policer.is_none() && reth.es_options.is_none());
assert!(xe.redundant_ether_options.is_none());
}
#[test]
fn global_blocks_are_not_interfaces() {
let config = juniper::parse_braces(
r#"
interfaces {
hierarchical-scheduler {
implicit-hierarchy;
maximum-hierarchy-levels 3;
}
logical-tunnel-options {
per-unit-mac-disable;
}
interface-range r1 {
member ge-0/0/1;
}
ge-0/0/0 {
mtu 9000;
}
}
"#,
);
let names: Vec<_> = interfaces(&config).into_iter().map(|i| i.name).collect();
assert_eq!(names, ["ge-0/0/0"]);
let scheduler = hierarchical_scheduler(&config).unwrap();
assert!(scheduler.implicit_hierarchy);
assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
assert!(hierarchical_scheduler(&juniper::parse_braces(
"interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n"
))
.is_none());
}
#[test]
fn physical_interface_settings_default_to_off() {
let ifaces = interfaces(&juniper::parse_braces(
"interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n",
));
let ge = &ifaces[0];
assert_eq!(ge.mtu, None);
assert_eq!(ge.speed, None);
assert_eq!(ge.encapsulation, None);
assert!(!ge.vlan_tagging);
assert!(!ge.flexible_vlan_tagging);
assert_eq!(ge.native_vlan_id, None);
assert!(!ge.per_unit_scheduler);
assert!(!ge.no_gratuitous_arp_request);
}
const DOC_POINT_TO_POINT: &str = r#"
interfaces {
ge-1/2/0 {
unit 0 {
description to-A;
family inet {
address 10.10.10.1/30;
}
}
}
ge-0/0/1 {
unit 5 {
description to-B;
family inet {
address 10.10.10.5/30;
}
}
}
}
"#;
const DOC_LINK_LOCAL: &str = r#"
interfaces {
ge-0/0/1 {
description R1-to-R2-Link;
vlan-tagging;
unit 1 {
vlan-id 1;
family inet6 {
address fe80::10/64;
}
}
unit 2 {
vlan-id 2;
family inet6 {
address fe80::10/64;
}
}
}
lo0 {
unit 0 {
family inet {
address 198.51.100.1/24 {
primary;
}
}
}
}
}
"#;
#[test]
fn doc_point_to_point_interfaces() {
let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
assert_eq!(ifaces[0].units[0].number, "0");
assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
assert_eq!(ifaces[1].units[0].number, "5");
assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
}
#[test]
fn doc_link_local_units_vlans_and_primary_address() {
let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
let ge = &ifaces[0];
assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
assert_eq!(ge.units.len(), 2);
assert_eq!(ge.units[0].vlan_id, Some(1));
assert_eq!(ge.units[1].vlan_id, Some(2));
assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);
let lo0 = &ifaces[1];
assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
}
}