mod container_lsp;
mod interface;
mod label_switched_path;
mod log_updown;
mod lsp_path;
mod path;
mod path_mtu;
mod static_lsp;
pub use container_lsp::MplsContainerLsp;
pub use interface::MplsInterface;
pub use label_switched_path::{MplsFastReroute, MplsLabelSwitchedPath};
pub use log_updown::MplsLogUpdown;
pub use lsp_path::{MplsAdminGroupConstraints, MplsLspPath};
pub use path::{MplsPath, MplsPathHop};
pub use path_mtu::MplsPathMtu;
pub use static_lsp::{MplsStaticBypass, MplsStaticIngress, MplsStaticLsp, MplsStaticNodeProtection, MplsStaticTransit};
use crate::{arg_of, child_block, has_leaf, named_children, split_name, top_level};
use asterust::config::{Config, ConfigLine};
use container_lsp::parse_container_lsp;
use interface::parse_interface;
use label_switched_path::parse_label_switched_path;
use log_updown::parse_log_updown;
use path::parse_path;
use path_mtu::parse_path_mtu;
use static_lsp::parse_static_lsp;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsAdminGroup {
pub name: String,
pub value: String,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Mpls {
pub disable: bool,
pub interfaces: Vec<MplsInterface>,
pub admin_groups: Vec<MplsAdminGroup>,
pub explicit_null: bool,
pub no_cspf: bool,
pub no_propagate_ttl: bool,
pub no_decrement_ttl: bool,
pub ipv6_tunneling: bool,
pub expand_loose_hop: bool,
pub traffic_engineering: Option<String>,
pub optimize_timer: Option<String>,
pub optimize_switchover_delay: Option<String>,
pub optimize_hold_dead_delay: Option<String>,
pub smart_optimize_timer: Option<String>,
pub rsvp_error_hold_time: Option<String>,
pub path_mtu: Option<MplsPathMtu>,
pub log_updown: Option<MplsLogUpdown>,
pub label_switched_paths: Vec<MplsLabelSwitchedPath>,
pub paths: Vec<MplsPath>,
pub static_label_switched_paths: Vec<MplsStaticLsp>,
pub container_label_switched_paths: Vec<MplsContainerLsp>,
}
pub fn mpls(config: &Config) -> Option<Mpls> {
let mpls = child_block(top_level(config, "protocols")?, "mpls")?;
let field = |keyword: &str| arg_of(mpls, keyword).map(str::to_string);
Some(Mpls {
disable: has_leaf(mpls, "disable"),
interfaces: named_children(mpls, "interface")
.map(|(name, interface)| parse_interface(name, interface))
.collect(),
admin_groups: parse_admin_groups(mpls),
explicit_null: has_leaf(mpls, "explicit-null"),
no_cspf: has_leaf(mpls, "no-cspf"),
no_propagate_ttl: has_leaf(mpls, "no-propagate-ttl"),
no_decrement_ttl: has_leaf(mpls, "no-decrement-ttl"),
ipv6_tunneling: has_leaf(mpls, "ipv6-tunneling"),
expand_loose_hop: has_leaf(mpls, "expand-loose-hop"),
traffic_engineering: field("traffic-engineering"),
optimize_timer: field("optimize-timer"),
optimize_switchover_delay: field("optimize-switchover-delay"),
optimize_hold_dead_delay: field("optimize-hold-dead-delay"),
smart_optimize_timer: field("smart-optimize-timer"),
rsvp_error_hold_time: field("rsvp-error-hold-time"),
path_mtu: parse_path_mtu(mpls),
log_updown: parse_log_updown(mpls),
label_switched_paths: named_children(mpls, "label-switched-path")
.map(|(name, lsp)| parse_label_switched_path(name, lsp))
.collect(),
paths: named_children(mpls, "path")
.map(|(name, path)| parse_path(name, path))
.collect(),
static_label_switched_paths: named_children(mpls, "static-label-switched-path")
.map(|(name, lsp)| parse_static_lsp(name, lsp))
.collect(),
container_label_switched_paths: named_children(mpls, "container-label-switched-path")
.map(|(name, lsp)| parse_container_lsp(name, lsp))
.collect(),
})
}
fn parse_admin_groups(mpls: &ConfigLine) -> Vec<MplsAdminGroup> {
let Some(block) = child_block(mpls, "admin-groups") else {
return Vec::new();
};
crate::active_children(block)
.map(|group| {
let (name, value) = split_name(&group.text);
MplsAdminGroup {
name: name.to_string(),
value: value.to_string(),
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
const DOC_BASIC: &str = r#"
protocols {
rsvp {
interface lo0.0;
interface ge-0/0/5.0;
}
mpls {
label-switched-path lsp_to_pe2_ge1 {
to 127.1.1.3;
}
interface ge-0/0/5.0;
interface ge-0/0/6.0;
interface ae0.0;
}
ospf {
traffic-engineering;
}
}
"#;
const EXTRA: &str = r#"
protocols {
mpls {
explicit-null;
no-cspf;
no-propagate-ttl;
no-decrement-ttl;
ipv6-tunneling;
expand-loose-hop;
traffic-engineering bgp-igp;
optimize-timer 3600;
optimize-switchover-delay 5;
optimize-hold-dead-delay 10;
smart-optimize-timer 180;
rsvp-error-hold-time 25;
admin-groups {
gold 1;
silver 2;
}
path-mtu {
allow-fragmentation;
rsvp {
mtu-signaling;
}
}
log-updown {
no-trap {
mpls-lsp-traps;
}
no-syslog;
trap;
trap-path-down;
}
interface all {
admin-group [ gold silver ];
srlg SRLG-A;
}
interface fxp0.0 {
disable;
}
}
}
"#;
#[test]
fn reads_the_guide_interfaces() {
let mpls = mpls(&juniper::parse_braces(DOC_BASIC)).unwrap();
assert!(!mpls.disable);
let names: Vec<&str> = mpls.interfaces.iter().map(|i| i.name.as_str()).collect();
assert_eq!(names, ["ge-0/0/5.0", "ge-0/0/6.0", "ae0.0"]);
assert!(mpls.interfaces.iter().all(|i| !i.disable));
assert!(!mpls.explicit_null);
assert_eq!(mpls.path_mtu, None);
}
#[test]
fn reads_the_guide_label_switched_path() {
let mpls = mpls(&juniper::parse_braces(DOC_BASIC)).unwrap();
assert_eq!(mpls.label_switched_paths.len(), 1);
let lsp = &mpls.label_switched_paths[0];
assert_eq!(lsp.name, "lsp_to_pe2_ge1");
assert_eq!(lsp.to.as_deref(), Some("127.1.1.3"));
assert_eq!(lsp.from, None);
assert!(lsp.primary.is_empty());
assert_eq!(lsp.fast_reroute, None);
}
const LSPS: &str = r#"
protocols {
mpls {
label-switched-path to-R3 {
description "to R3";
to 10.255.0.3;
from 10.255.0.1;
adaptive;
bandwidth 100m;
class-of-service 3;
metric 50;
preference 5;
priority 3 3;
no-record;
random;
retry-limit 10;
retry-timer 30;
revert-timer 120;
optimize-timer 600;
link-protection;
soft-preemption;
ldp-tunneling;
install 10.9.0.0/24 active;
install 10.9.1.0/24;
admin-group {
include-any [ gold silver ];
exclude red;
}
fast-reroute {
bandwidth 10m;
hop-limit 5;
include-all [ gold ];
}
primary via-A {
bandwidth 50m;
hop-limit 8;
priority 4 4;
record;
select manual;
standby;
}
secondary via-B;
secondary via-C standby;
}
label-switched-path DOWN {
to 10.255.0.4;
admin-down;
disable;
no-cspf;
node-link-protection;
fast-reroute;
most-fill;
no-self-ping;
}
}
}
"#;
#[test]
fn reads_the_label_switched_path_options() {
let mpls = mpls(&juniper::parse_braces(LSPS)).unwrap();
assert_eq!(mpls.label_switched_paths.len(), 2);
let lsp = &mpls.label_switched_paths[0];
assert_eq!(lsp.description.as_deref(), Some("to R3"));
assert_eq!(lsp.to.as_deref(), Some("10.255.0.3"));
assert_eq!(lsp.from.as_deref(), Some("10.255.0.1"));
assert!(lsp.adaptive);
assert_eq!(lsp.bandwidth.as_deref(), Some("100m"));
assert_eq!(lsp.class_of_service.as_deref(), Some("3"));
assert_eq!(lsp.metric.as_deref(), Some("50"));
assert_eq!(lsp.preference.as_deref(), Some("5"));
assert_eq!(lsp.priority.as_deref(), Some("3 3"));
assert_eq!(lsp.record, Some(false));
assert_eq!(lsp.path_selection.as_deref(), Some("random"));
assert_eq!(lsp.retry_limit.as_deref(), Some("10"));
assert_eq!(lsp.retry_timer.as_deref(), Some("30"));
assert_eq!(lsp.revert_timer.as_deref(), Some("120"));
assert_eq!(lsp.optimize_timer.as_deref(), Some("600"));
assert!(lsp.link_protection);
assert!(lsp.soft_preemption);
assert!(lsp.ldp_tunneling);
assert_eq!(lsp.install, ["10.9.0.0/24", "10.9.1.0/24"]);
assert_eq!(lsp.admin_group.include_any, ["gold", "silver"]);
assert_eq!(lsp.admin_group.exclude, ["red"]);
assert!(lsp.admin_group.include_all.is_empty());
}
#[test]
fn reads_fast_reroute_and_flags() {
let mpls = mpls(&juniper::parse_braces(LSPS)).unwrap();
let reroute = mpls.label_switched_paths[0].fast_reroute.as_ref().unwrap();
assert_eq!(reroute.bandwidth.as_deref(), Some("10m"));
assert_eq!(reroute.hop_limit.as_deref(), Some("5"));
assert_eq!(reroute.include_all, ["gold"]);
assert_eq!(reroute.bandwidth_percent, None);
let down = &mpls.label_switched_paths[1];
assert!(down.admin_down);
assert!(down.disable);
assert!(down.no_cspf);
assert!(down.node_link_protection);
assert!(down.no_self_ping);
assert_eq!(down.path_selection.as_deref(), Some("most-fill"));
assert_eq!(down.fast_reroute, Some(MplsFastReroute::default()));
assert_eq!(down.record, None);
}
#[test]
fn reads_primary_and_secondary_paths() {
let mpls = mpls(&juniper::parse_braces(LSPS)).unwrap();
let lsp = &mpls.label_switched_paths[0];
assert_eq!(lsp.primary.len(), 1);
let primary = &lsp.primary[0];
assert_eq!(primary.name, "via-A");
assert_eq!(primary.bandwidth.as_deref(), Some("50m"));
assert_eq!(primary.hop_limit.as_deref(), Some("8"));
assert_eq!(primary.priority.as_deref(), Some("4 4"));
assert_eq!(primary.record, Some(true));
assert_eq!(primary.select.as_deref(), Some("manual"));
assert!(primary.standby);
assert_eq!(lsp.secondary.len(), 2);
assert_eq!(lsp.secondary[0].name, "via-B");
assert!(!lsp.secondary[0].standby);
assert_eq!(lsp.secondary[1].name, "via-C");
assert!(lsp.secondary[1].standby);
}
const STATIC_AND_PATHS: &str = r#"
protocols {
mpls {
path via-A {
10.0.0.2 strict;
10.0.0.3 loose;
core-hop abstract;
r5.example.net;
}
path unconstrained;
static-label-switched-path ingress-lsp {
ingress {
to 10.255.0.9;
push 1000001;
next-hop 10.1.1.2;
bandwidth 10m;
class-of-service 2;
description "static ingress";
metric 5;
preference 4;
no-install-to-address;
install 10.7.0.0/24 active;
link-protection bypass-name byp1;
node-protection bypass-name byp2 next-next-label 3000;
}
}
static-label-switched-path transit-lsp {
transit 1000001 {
swap 1000002;
next-hop ge-0/0/1.0;
bandwidth 5m;
link-protection bypass-name byp1;
}
transit 1000009 {
pop;
next-hop 10.1.2.2/ge-0/0/2.0;
}
bypass byp1 {
to 10.255.0.4;
next-hop 10.1.3.2;
next-table inet.0 push 2000;
description "backup";
}
}
container-label-switched-path CONT {
to 10.255.0.7;
description "multi-LSP";
suffix -member;
label-switched-path-template TEMPLATE-A;
splitting-merging {
maximum-member-lsps 4;
}
}
container-label-switched-path CONT-OFF {
disable;
}
}
}
"#;
#[test]
fn reads_paths() {
let mpls = mpls(&juniper::parse_braces(STATIC_AND_PATHS)).unwrap();
assert_eq!(mpls.paths.len(), 2);
let path = &mpls.paths[0];
assert_eq!(path.name, "via-A");
let hops: Vec<(&str, Option<&str>)> = path
.hops
.iter()
.map(|h| (h.address.as_str(), h.qualifier.as_deref()))
.collect();
assert_eq!(
hops,
[
("10.0.0.2", Some("strict")),
("10.0.0.3", Some("loose")),
("core-hop", Some("abstract")),
("r5.example.net", None),
]
);
assert_eq!(mpls.paths[1].name, "unconstrained");
assert!(mpls.paths[1].hops.is_empty());
}
#[test]
fn reads_static_ingress() {
let mpls = mpls(&juniper::parse_braces(STATIC_AND_PATHS)).unwrap();
assert_eq!(mpls.static_label_switched_paths.len(), 2);
let lsp = &mpls.static_label_switched_paths[0];
assert_eq!(lsp.name, "ingress-lsp");
assert!(lsp.transit.is_empty());
let ingress = lsp.ingress.as_ref().unwrap();
assert_eq!(ingress.to.as_deref(), Some("10.255.0.9"));
assert_eq!(ingress.push.as_deref(), Some("1000001"));
assert_eq!(ingress.next_hop.as_deref(), Some("10.1.1.2"));
assert_eq!(ingress.bandwidth.as_deref(), Some("10m"));
assert_eq!(ingress.class_of_service.as_deref(), Some("2"));
assert_eq!(ingress.description.as_deref(), Some("static ingress"));
assert_eq!(ingress.metric.as_deref(), Some("5"));
assert_eq!(ingress.preference.as_deref(), Some("4"));
assert!(ingress.no_install_to_address);
assert_eq!(ingress.install, ["10.7.0.0/24"]);
assert_eq!(ingress.link_protection.as_deref(), Some("byp1"));
let node = ingress.node_protection.as_ref().unwrap();
assert_eq!(node.bypass_name.as_deref(), Some("byp2"));
assert_eq!(node.next_next_label.as_deref(), Some("3000"));
}
#[test]
fn reads_static_transit_and_bypass() {
let mpls = mpls(&juniper::parse_braces(STATIC_AND_PATHS)).unwrap();
let lsp = &mpls.static_label_switched_paths[1];
assert_eq!(lsp.ingress, None);
assert_eq!(lsp.transit.len(), 2);
assert_eq!(lsp.transit[0].incoming_label, "1000001");
assert_eq!(lsp.transit[0].swap.as_deref(), Some("1000002"));
assert_eq!(lsp.transit[0].next_hop.as_deref(), Some("ge-0/0/1.0"));
assert_eq!(lsp.transit[0].bandwidth.as_deref(), Some("5m"));
assert_eq!(lsp.transit[0].link_protection.as_deref(), Some("byp1"));
assert!(!lsp.transit[0].pop);
assert!(lsp.transit[1].pop);
assert_eq!(lsp.transit[1].next_hop.as_deref(), Some("10.1.2.2/ge-0/0/2.0"));
assert_eq!(lsp.bypasses.len(), 1);
let bypass = &lsp.bypasses[0];
assert_eq!(bypass.name, "byp1");
assert_eq!(bypass.to.as_deref(), Some("10.255.0.4"));
assert_eq!(bypass.next_hop.as_deref(), Some("10.1.3.2"));
assert_eq!(bypass.next_table.as_deref(), Some("inet.0 push 2000"));
assert_eq!(bypass.description.as_deref(), Some("backup"));
}
#[test]
fn reads_container_lsps() {
let mpls = mpls(&juniper::parse_braces(STATIC_AND_PATHS)).unwrap();
assert_eq!(mpls.container_label_switched_paths.len(), 2);
let container = &mpls.container_label_switched_paths[0];
assert_eq!(container.name, "CONT");
assert_eq!(container.to.as_deref(), Some("10.255.0.7"));
assert_eq!(container.description.as_deref(), Some("multi-LSP"));
assert_eq!(container.suffix.as_deref(), Some("-member"));
assert_eq!(container.label_switched_path_template.as_deref(), Some("TEMPLATE-A"));
assert!(container.splitting_merging);
assert!(!container.disable);
let off = &mpls.container_label_switched_paths[1];
assert!(off.disable);
assert!(!off.splitting_merging);
}
#[test]
fn reads_the_flags_and_timers() {
let mpls = mpls(&juniper::parse_braces(EXTRA)).unwrap();
assert!(mpls.explicit_null);
assert!(mpls.no_cspf);
assert!(mpls.no_propagate_ttl);
assert!(mpls.no_decrement_ttl);
assert!(mpls.ipv6_tunneling);
assert!(mpls.expand_loose_hop);
assert_eq!(mpls.traffic_engineering.as_deref(), Some("bgp-igp"));
assert_eq!(mpls.optimize_timer.as_deref(), Some("3600"));
assert_eq!(mpls.optimize_switchover_delay.as_deref(), Some("5"));
assert_eq!(mpls.optimize_hold_dead_delay.as_deref(), Some("10"));
assert_eq!(mpls.smart_optimize_timer.as_deref(), Some("180"));
assert_eq!(mpls.rsvp_error_hold_time.as_deref(), Some("25"));
}
#[test]
fn reads_admin_groups_and_interface_options() {
let mpls = mpls(&juniper::parse_braces(EXTRA)).unwrap();
assert_eq!(mpls.admin_groups.len(), 2);
assert_eq!(mpls.admin_groups[0].name, "gold");
assert_eq!(mpls.admin_groups[0].value, "1");
let all = &mpls.interfaces[0];
assert_eq!(all.name, "all");
assert_eq!(all.admin_groups, ["gold", "silver"]);
assert_eq!(all.srlg, ["SRLG-A"]);
assert!(mpls.interfaces[1].disable);
}
#[test]
fn reads_path_mtu_and_log_updown() {
let mpls = mpls(&juniper::parse_braces(EXTRA)).unwrap();
let mtu = mpls.path_mtu.unwrap();
assert!(mtu.allow_fragmentation);
assert!(mtu.rsvp_mtu_signaling);
let log = mpls.log_updown.unwrap();
assert_eq!(log.syslog, Some(false));
assert!(log.trap);
assert!(log.trap_path_down);
assert!(!log.trap_path_up);
assert!(log.no_trap_mpls_lsp_traps);
assert!(!log.no_trap_rfc3812_traps);
}
#[test]
fn reads_a_bare_mpls_block() {
let config = juniper::parse_braces("protocols {\n mpls;\n}\n");
assert_eq!(mpls(&config), Some(Mpls::default()));
}
#[test]
fn no_mpls_is_none() {
let config = juniper::parse_braces("protocols {\n ospf {\n area 0.0.0.0;\n }\n}\n");
assert_eq!(mpls(&config), None);
}
}