mod accounting;
mod address;
mod dhcp;
mod ethernet_switching;
mod filter;
mod next_hop_tunnel;
mod policer;
mod rpf_check;
mod sampling;
pub use accounting::{FamilyAccounting, TargetedBroadcast, UnnumberedAddress, VlanTranslation};
pub use address::{
AddressArp, BandwidthThreshold, TrackedInterface, TrackedRoute, UnitAddress, VrrpGroup, VrrpInheritFrom, VrrpTrack,
};
pub use dhcp::{DhcpClient, DhcpClientIdentifier, Dhcpv6Client, PrefixDelegating, RouterAdvertisementInterface};
pub use ethernet_switching::EthernetSwitching;
pub use filter::FamilyFilter;
pub use next_hop_tunnel::NextHopTunnel;
pub use policer::FamilyPolicer;
pub use rpf_check::RpfCheck;
pub use sampling::FamilySampling;
use super::demux_options::demux_prefixes;
use super::pppoe_underlying_options::{parse_pppoe_underlying_options, PppoeUnderlyingOptions};
use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
use accounting::{parse_accounting, parse_targeted_broadcast, parse_unnumbered_address, parse_vlan_rewrite};
use address::parse_addresses;
use asterust::config::ConfigLine;
use dhcp::{parse_dhcp_client, parse_dhcpv6_client};
use ethernet_switching::parse_ethernet_switching;
use filter::parse_filter;
use next_hop_tunnel::parse_next_hop_tunnels;
use policer::parse_policer;
use rpf_check::parse_rpf_check;
use sampling::parse_sampling;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnitFamily {
pub name: String,
pub mtu: Option<String>,
pub filter: Option<FamilyFilter>,
pub rpf_check: Option<RpfCheck>,
pub policer: Option<FamilyPolicer>,
pub sampling: Option<FamilySampling>,
pub vlan_id: Option<String>,
pub vlan_id_list: Vec<String>,
pub addresses: Vec<UnitAddress>,
pub destination_udp_port: Option<String>,
pub ethernet_switching: Option<EthernetSwitching>,
pub nd6_max_cache: Option<String>,
pub nd6_stale_time: Option<String>,
pub nd6_new_hold_limit: Option<String>,
pub pppoe: Option<PppoeUnderlyingOptions>,
pub demux_source: Vec<String>,
pub demux_destination: Vec<String>,
pub dhcp: Option<DhcpClient>,
pub dhcp_client: Option<DhcpClient>,
pub dhcpv6_client: Option<Dhcpv6Client>,
pub no_redirects: bool,
pub no_redirects_ipv6: bool,
pub core_facing: bool,
pub receive_options_packets: bool,
pub receive_ttl_exceeded: bool,
pub multicast_only: bool,
pub unconditional_src_learn: bool,
pub tunnel_loopback: bool,
pub mac_validate: Option<String>,
pub tcp_mss: Option<String>,
pub arp_max_cache: Option<String>,
pub dad_proxy: Option<String>,
pub ndp_proxy: Option<String>,
pub proxy_inet_address: Option<String>,
pub unnumbered_address: Option<UnnumberedAddress>,
pub accounting: Option<FamilyAccounting>,
pub targeted_broadcast: Option<TargetedBroadcast>,
pub vlan_rewrite: Vec<VlanTranslation>,
pub next_hop_tunnel: Vec<NextHopTunnel>,
}
fn proxy_mode(family: &ConfigLine, keyword: &str) -> Option<String> {
statement_words(family, keyword)?.first().map(|mode| mode.to_string())
}
pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
UnitFamily {
name: name.to_string(),
mtu: arg_of(family, "mtu").map(str::to_string),
filter: parse_filter(family),
rpf_check: parse_rpf_check(family),
policer: parse_policer(family),
sampling: parse_sampling(family),
vlan_id: arg_of(family, "vlan-id").map(str::to_string),
vlan_id_list: args_of(family, "vlan-id-list")
.into_iter()
.flat_map(list_values)
.collect(),
addresses: parse_addresses(family),
destination_udp_port: arg_of(family, "destination-udp-port").map(str::to_string),
ethernet_switching: (name == "ethernet-switching").then(|| parse_ethernet_switching(family)),
nd6_max_cache: arg_of(family, "nd6-max-cache").map(str::to_string),
nd6_stale_time: arg_of(family, "nd6-stale-time").map(str::to_string),
nd6_new_hold_limit: arg_of(family, "nd6-new-hold-limit").map(str::to_string),
pppoe: (name == "pppoe").then(|| parse_pppoe_underlying_options(family)),
demux_source: demux_prefixes(family, "demux-source"),
demux_destination: demux_prefixes(family, "demux-destination"),
dhcp: child_block(family, "dhcp").map(parse_dhcp_client),
dhcp_client: child_block(family, "dhcp-client").map(parse_dhcp_client),
dhcpv6_client: child_block(family, "dhcpv6-client").map(parse_dhcpv6_client),
no_redirects: has_leaf(family, "no-redirects"),
no_redirects_ipv6: has_leaf(family, "no-redirects-ipv6"),
core_facing: has_leaf(family, "core-facing"),
receive_options_packets: has_leaf(family, "receive-options-packets"),
receive_ttl_exceeded: has_leaf(family, "receive-ttl-exceeded"),
multicast_only: has_leaf(family, "multicast-only"),
unconditional_src_learn: has_leaf(family, "unconditional-src-learn"),
tunnel_loopback: has_leaf(family, "tunnel-loopback"),
mac_validate: arg_of(family, "mac-validate").map(str::to_string),
tcp_mss: arg_of(family, "tcp-mss").map(str::to_string),
arp_max_cache: arg_of(family, "arp-max-cache").map(str::to_string),
dad_proxy: proxy_mode(family, "dad-proxy"),
ndp_proxy: proxy_mode(family, "ndp-proxy"),
proxy_inet_address: statement_words(family, "proxy").and_then(|words| value_after(&words, "inet-address")),
unnumbered_address: parse_unnumbered_address(family),
accounting: child_block(family, "accounting").map(parse_accounting),
targeted_broadcast: child_block(family, "targeted-broadcast").map(parse_targeted_broadcast),
vlan_rewrite: parse_vlan_rewrite(family),
next_hop_tunnel: parse_next_hop_tunnels(family),
}
}
#[cfg(test)]
mod tests {
use crate::interfaces::interfaces;
use asterust::config::juniper;
const CONFIG: &str = r#"
interfaces {
ge-0/0/0 {
unit 0 {
family inet {
mtu 1500;
filter {
group 5;
input PROTECT-IN;
output PROTECT-OUT;
input-list [ A B ];
output-list C;
}
rpf-check {
fail-filter RPF-FAIL;
mode loose;
}
policer {
arp ARP-POLICER;
input IN-POLICER;
output OUT-POLICER;
}
sampling {
input;
output;
}
address 192.0.2.1/24 {
primary;
preferred;
destination 192.0.2.2;
broadcast 192.0.2.255;
arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
}
address 192.0.2.9/24;
}
family inet6 {
rpf-check;
sampling input;
address 2001:db8::1/64 {
eui-64;
}
}
family iso;
}
}
}
"#;
#[test]
fn extracts_more_family_statements() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
ge-0/0/0 {
unit 0 {
family inet {
no-redirects;
core-facing;
receive-options-packets;
receive-ttl-exceeded;
multicast-only;
unconditional-src-learn;
tunnel-loopback;
mac-validate strict;
tcp-mss 1400;
arp-max-cache 1000;
unnumbered-address lo0.0 preferred-source-address 10.0.0.1;
accounting {
destination-class-usage;
source-class-usage {
input;
output;
}
}
targeted-broadcast {
forward-and-send-to-re;
}
}
family inet6 {
no-redirects-ipv6;
tcp-mss 1300;
dad-proxy {
interface-restricted;
}
ndp-proxy {
interface-unrestricted;
}
unnumbered-address lo0.1;
}
}
unit 1 {
family tcc {
proxy inet-address 192.0.2.9;
}
family bridge {
vlan-rewrite {
translate 100 200;
translate 300 400;
}
}
family ethernet-switching {
interface-mode trunk;
inter-switch-link vlan members primary-vlan;
}
}
unit 2 {
family inet {
unnumbered-address ge-0/0/1.0 destination 192.0.2.1 destination-profile prof1;
accounting {
source-class-usage output;
}
targeted-broadcast {
forward-only;
}
}
}
unit 3 {
family inet {
mtu 1500;
}
}
}
}
"#,
));
let units = &ifaces[0].units;
let inet = &units[0].family_settings[0];
assert!(inet.no_redirects && inet.core_facing && inet.receive_options_packets && inet.receive_ttl_exceeded);
assert!(inet.multicast_only && inet.unconditional_src_learn && inet.tunnel_loopback);
assert_eq!(inet.mac_validate.as_deref(), Some("strict"));
assert_eq!(inet.tcp_mss.as_deref(), Some("1400"));
assert_eq!(inet.arp_max_cache.as_deref(), Some("1000"));
let unnumbered = inet.unnumbered_address.as_ref().unwrap();
assert_eq!(unnumbered.interface, "lo0.0");
assert_eq!(unnumbered.preferred_source_address.as_deref(), Some("10.0.0.1"));
assert!(unnumbered.destination.is_none() && unnumbered.destination_profile.is_none());
let accounting = inet.accounting.as_ref().unwrap();
assert!(accounting.destination_class_usage && accounting.source_class_usage);
assert_eq!(accounting.source_class_usage_directions, ["input", "output"]);
let broadcast = inet.targeted_broadcast.as_ref().unwrap();
assert!(broadcast.forward_and_send_to_re && !broadcast.forward_only);
let inet6 = &units[0].family_settings[1];
assert!(inet6.no_redirects_ipv6 && !inet6.no_redirects);
assert_eq!(inet6.tcp_mss.as_deref(), Some("1300"));
assert_eq!(inet6.dad_proxy.as_deref(), Some("interface-restricted"));
assert_eq!(inet6.ndp_proxy.as_deref(), Some("interface-unrestricted"));
assert_eq!(inet6.unnumbered_address.as_ref().unwrap().interface, "lo0.1");
assert_eq!(
units[1].family_settings[0].proxy_inet_address.as_deref(),
Some("192.0.2.9")
);
let rewrite = &units[1].family_settings[1].vlan_rewrite;
assert_eq!(rewrite.len(), 2);
assert_eq!((rewrite[1].from.as_str(), rewrite[1].to.as_str()), ("300", "400"));
let switching = units[1].family_settings[2].ethernet_switching.as_ref().unwrap();
assert_eq!(switching.inter_switch_link_vlan.as_deref(), Some("primary-vlan"));
let ppp = units[2].family_settings[0].unnumbered_address.as_ref().unwrap();
assert_eq!(ppp.interface, "ge-0/0/1.0");
assert_eq!(ppp.destination.as_deref(), Some("192.0.2.1"));
assert_eq!(ppp.destination_profile.as_deref(), Some("prof1"));
let source = units[2].family_settings[0].accounting.as_ref().unwrap();
assert_eq!(source.source_class_usage_directions, ["output"]);
assert!(
units[2].family_settings[0]
.targeted_broadcast
.as_ref()
.unwrap()
.forward_only
);
let plain = &units[3].family_settings[0];
assert!(!plain.no_redirects && !plain.multicast_only && plain.mac_validate.is_none());
assert!(plain.unnumbered_address.is_none() && plain.accounting.is_none() && plain.targeted_broadcast.is_none());
assert!(plain.vlan_rewrite.is_empty() && plain.dad_proxy.is_none() && plain.proxy_inet_address.is_none());
}
#[test]
fn extracts_family_filters_policers_and_sampling() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let inet = &ifaces[0].units[0].family_settings[0];
assert_eq!(inet.name, "inet");
assert_eq!(inet.mtu.as_deref(), Some("1500"));
let filter = inet.filter.as_ref().unwrap();
assert_eq!(filter.group.as_deref(), Some("5"));
assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
assert_eq!(filter.input_list, ["A", "B"]);
assert_eq!(filter.output_list, ["C"]);
let policer = inet.policer.as_ref().unwrap();
assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));
let sampling = inet.sampling.as_ref().unwrap();
assert!(sampling.input);
assert!(sampling.output);
}
#[test]
fn extracts_rpf_check_in_block_and_flag_form() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let families = &ifaces[0].units[0].family_settings;
let rpf = families[0].rpf_check.as_ref().unwrap();
assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
assert_eq!(rpf.mode.as_deref(), Some("loose"));
let bare = families[1].rpf_check.as_ref().unwrap();
assert_eq!(bare.fail_filter, None);
assert_eq!(bare.mode, None);
}
#[test]
fn extracts_address_options_and_arp_entries() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let addresses = &ifaces[0].units[0].family_settings[0].addresses;
assert_eq!(addresses.len(), 2);
let first = &addresses[0];
assert_eq!(first.address, "192.0.2.1/24");
assert!(first.primary);
assert!(first.preferred);
assert!(!first.master_only);
assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
assert_eq!(first.arps.len(), 2);
assert_eq!(first.arps[0].address, "192.0.2.50");
assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
assert!(first.arps[0].publish);
assert!(!first.arps[0].multicast_mac);
assert!(first.arps[1].multicast_mac);
assert!(!first.arps[1].publish);
let second = &addresses[1];
assert_eq!(second.address, "192.0.2.9/24");
assert!(!second.primary);
assert!(second.arps.is_empty());
}
#[test]
fn extracts_inline_sampling_and_eui_64() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let inet6 = &ifaces[0].units[0].family_settings[1];
let sampling = inet6.sampling.as_ref().unwrap();
assert!(sampling.input);
assert!(!sampling.output);
assert!(inet6.addresses[0].eui_64);
}
#[test]
fn family_without_settings_has_none() {
let ifaces = interfaces(&juniper::parse_braces(CONFIG));
let iso = &ifaces[0].units[0].family_settings[2];
assert_eq!(iso.name, "iso");
assert!(iso.mtu.is_none());
assert!(iso.filter.is_none());
assert!(iso.rpf_check.is_none());
assert!(iso.policer.is_none());
assert!(iso.sampling.is_none());
assert!(iso.addresses.is_empty());
}
}