use super::words;
use crate::{active_children, arg_of, child_block, list_values, named_children, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NotificationInput {
pub kind: String,
pub code_points: Vec<String>,
pub pfc: bool,
pub pfc_mru: Option<String>,
pub xon: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NotificationOutput {
pub kind: String,
pub code_points: Vec<String>,
pub flow_control_queues: Vec<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfcWatchdog {
pub detection: Option<String>,
pub poll_interval: Option<String>,
pub recovery: Option<String>,
pub drop: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CongestionNotificationProfile {
pub name: String,
pub input: Vec<NotificationInput>,
pub cable_length: Option<String>,
pub output: Vec<NotificationOutput>,
pub pfc_watchdog: Option<PfcWatchdog>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropCongestionNotification {
pub trimmable_dscp: Option<String>,
pub trimmed_dscp: Option<String>,
pub forwarding_class: Option<String>,
pub udp_port: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsEcnEntry {
pub ecn_capable_exp: String,
pub congestion_experienced_exp: Option<String>,
}
fn code_points(entry: &ConfigLine) -> Vec<String> {
entry.arg("code-point").map(list_values).unwrap_or_default()
}
fn parse_input(block: &ConfigLine) -> Vec<NotificationInput> {
active_children(block)
.filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
.flat_map(|kind| {
active_children(kind)
.filter(|entry| entry.arg("code-point").is_some())
.map(|entry| NotificationInput {
kind: kind.text.clone(),
code_points: code_points(entry),
pfc: child_block(entry, "pfc").is_some(),
pfc_mru: child_block(entry, "pfc")
.and_then(|pfc| arg_of(pfc, "mru"))
.map(str::to_string),
xon: arg_of(entry, "xon").map(str::to_string),
})
})
.collect()
}
fn parse_output(block: &ConfigLine) -> Vec<NotificationOutput> {
active_children(block)
.filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
.flat_map(|kind| {
active_children(kind)
.filter(|entry| entry.arg("code-point").is_some())
.map(|entry| NotificationOutput {
kind: kind.text.clone(),
code_points: code_points(entry),
flow_control_queues: arg_of(entry, "flow-control-queue").map(list_values).unwrap_or_default(),
})
})
.collect()
}
pub(super) fn parse_congestion_notification_profiles(cos: &ConfigLine) -> Vec<CongestionNotificationProfile> {
named_children(cos, "congestion-notification-profile")
.map(|(name, profile)| CongestionNotificationProfile {
name: name.to_string(),
input: child_block(profile, "input").map(parse_input).unwrap_or_default(),
cable_length: child_block(profile, "input")
.and_then(|input| arg_of(input, "cable-length"))
.map(str::to_string),
output: child_block(profile, "output").map(parse_output).unwrap_or_default(),
pfc_watchdog: child_block(profile, "pfc-watchdog").map(|watchdog| PfcWatchdog {
detection: arg_of(watchdog, "detection").map(str::to_string),
poll_interval: arg_of(watchdog, "poll-interval").map(str::to_string),
recovery: arg_of(watchdog, "recovery").map(str::to_string),
drop: child_block(watchdog, "watchdog-action").is_some_and(|action| crate::has_leaf(action, "drop")),
}),
})
.collect()
}
pub(super) fn parse_drop_congestion_notification(cos: &ConfigLine) -> Option<DropCongestionNotification> {
let drop = child_block(cos, "drop-congestion-notification")?;
let map = crate::statement_words(drop, "dscp-map");
Some(DropCongestionNotification {
trimmable_dscp: map.as_ref().and_then(|words| value_after(words, "trimmable")),
trimmed_dscp: map.as_ref().and_then(|words| value_after(words, "trimmed")),
forwarding_class: arg_of(drop, "forwarding-class").map(str::to_string),
udp_port: arg_of(drop, "udp-port").map(str::to_string),
})
}
pub(super) fn parse_mpls_ecn_map(cos: &ConfigLine) -> Vec<MplsEcnEntry> {
let Some(map) = child_block(cos, "mpls-ecn-map") else {
return Vec::new();
};
named_children(map, "ecn-capable-exp")
.map(|(exp, entry)| MplsEcnEntry {
ecn_capable_exp: exp.to_string(),
congestion_experienced_exp: words(entry, 0)
.windows(2)
.find(|pair| pair[0] == "congestion-experienced-exp" || pair[0] == "congestion-experienced-exp-value")
.map(|pair| pair[1].to_string()),
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::class_of_service::class_of_service;
use asterust::config::juniper;
const EXTRA: &str = r#"
class-of-service {
congestion-notification-profile CNP1 {
input {
ieee-802.1 {
code-point [ 011 100 ] {
pfc {
mru 2500;
}
xon 150;
}
}
dscp {
code-point 110000 {
xon 100;
}
}
cable-length 100;
}
output {
ieee-802.1 {
code-point 011 {
flow-control-queue [ 3 4 ];
}
}
}
pfc-watchdog {
detection 100;
poll-interval 10;
recovery 200;
watchdog-action {
drop;
}
}
}
congestion-notification-profile CNP2;
drop-congestion-notification {
dscp-map {
trimmable 8 trimmed 9;
}
forwarding-class trimmed-fc;
udp-port 4791;
}
mpls-ecn-map {
ecn-capable-exp 2 {
congestion-experienced-exp-value 3;
}
}
}
"#;
#[test]
fn reads_the_congestion_notification_input() {
let profiles = class_of_service(&juniper::parse_braces(EXTRA))
.unwrap()
.congestion_notification_profiles;
assert_eq!(profiles.len(), 2);
let profile = &profiles[0];
assert_eq!(profile.name, "CNP1");
assert_eq!(profile.cable_length.as_deref(), Some("100"));
assert_eq!(profile.input.len(), 2);
assert_eq!(profile.input[0].kind, "ieee-802.1");
assert_eq!(profile.input[0].code_points, ["011", "100"]);
assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2500"));
assert_eq!(profile.input[0].xon.as_deref(), Some("150"));
assert_eq!(profile.input[1].kind, "dscp");
assert_eq!(profile.input[1].pfc_mru, None);
assert!(profiles[1].input.is_empty() && profiles[1].pfc_watchdog.is_none());
}
#[test]
fn reads_the_output_and_the_watchdog() {
let profile = class_of_service(&juniper::parse_braces(EXTRA))
.unwrap()
.congestion_notification_profiles
.remove(0);
assert_eq!(profile.output[0].code_points, ["011"]);
assert_eq!(profile.output[0].flow_control_queues, ["3", "4"]);
let watchdog = profile.pfc_watchdog.unwrap();
assert_eq!(watchdog.detection.as_deref(), Some("100"));
assert_eq!(watchdog.poll_interval.as_deref(), Some("10"));
assert_eq!(watchdog.recovery.as_deref(), Some("200"));
assert!(watchdog.drop);
}
#[test]
fn reads_the_drop_notification_and_the_ecn_map() {
let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
let drop = cos.drop_congestion_notification.unwrap();
assert_eq!(drop.trimmable_dscp.as_deref(), Some("8"));
assert_eq!(drop.trimmed_dscp.as_deref(), Some("9"));
assert_eq!(drop.forwarding_class.as_deref(), Some("trimmed-fc"));
assert_eq!(drop.udp_port.as_deref(), Some("4791"));
assert_eq!(cos.mpls_ecn_map.len(), 1);
assert_eq!(cos.mpls_ecn_map[0].ecn_capable_exp, "2");
assert_eq!(cos.mpls_ecn_map[0].congestion_experienced_exp.as_deref(), Some("3"));
}
const DOC_FCOE_PFC: &str = r#"
class-of-service {
classifiers {
ieee-802.1 fcoe-classifier {
forwarding-class fcoe {
loss-priority low code-points 011;
}
}
}
congestion-notification-profile fcoe-cnp {
input {
ieee-802.1 {
code-point 011 {
pfc {
mru 2240;
}
}
}
cable-length 100;
}
}
interfaces {
xe-0/0/31 {
congestion-notification-profile fcoe-cnp;
unit 0 {
classifiers {
ieee-802.1 fcoe-classifier;
}
}
}
}
}
"#;
const DOC_DCQCN: &str = r#"
class-of-service {
congestion-notification-profile cnp {
input {
dscp {
code-point 011010 {
pfc;
}
}
}
output {
ieee-802.1 {
code-point 011 {
flow-control-queue 4;
}
}
}
}
interfaces {
et-* {
congestion-notification-profile cnp;
}
}
}
"#;
const DOC_MPLS_ECN: &str = r#"
class-of-service {
mpls-ecn-map {
ecn-capable-exp 6 {
congestion-experienced-exp 7;
}
ecn-capable-exp 5 {
congestion-experienced-exp 4;
}
}
}
"#;
const DOC_DCN: &str = r#"
class-of-service {
drop-congestion-notification {
forwarding-class dcn;
udp-port 13742;
}
interfaces {
et-0/0/0 {
drop-congestion-notification;
}
}
}
"#;
#[test]
fn reads_the_guide_fcoe_profile() {
let cos = class_of_service(&juniper::parse_braces(DOC_FCOE_PFC)).unwrap();
let profile = &cos.congestion_notification_profiles[0];
assert_eq!(profile.name, "fcoe-cnp");
assert_eq!(profile.cable_length.as_deref(), Some("100"));
let input = &profile.input[0];
assert_eq!(input.kind, "ieee-802.1");
assert_eq!(input.code_points, ["011"]);
assert!(input.pfc);
assert_eq!(input.pfc_mru.as_deref(), Some("2240"));
let interface = &cos.interfaces[0];
assert_eq!(
interface.settings.congestion_notification_profile.as_deref(),
Some("fcoe-cnp")
);
assert_eq!(interface.units[0].settings.classifiers[0].name, "fcoe-classifier");
}
#[test]
fn reads_the_guide_dcqcn_profile() {
let cos = class_of_service(&juniper::parse_braces(DOC_DCQCN)).unwrap();
let profile = &cos.congestion_notification_profiles[0];
assert_eq!(profile.input[0].kind, "dscp");
assert_eq!(profile.input[0].code_points, ["011010"]);
assert!(profile.input[0].pfc && profile.input[0].pfc_mru.is_none());
assert_eq!(profile.output[0].code_points, ["011"]);
assert_eq!(profile.output[0].flow_control_queues, ["4"]);
assert_eq!(cos.interfaces[0].name, "et-*");
}
#[test]
fn reads_the_guide_mpls_ecn_map() {
let map = class_of_service(&juniper::parse_braces(DOC_MPLS_ECN))
.unwrap()
.mpls_ecn_map;
assert_eq!(map.len(), 2);
assert_eq!(map[0].ecn_capable_exp, "6");
assert_eq!(map[0].congestion_experienced_exp.as_deref(), Some("7"));
assert_eq!(map[1].congestion_experienced_exp.as_deref(), Some("4"));
}
#[test]
fn reads_the_guide_drop_congestion_notification() {
let cos = class_of_service(&juniper::parse_braces(DOC_DCN)).unwrap();
let drop = cos.drop_congestion_notification.unwrap();
assert_eq!(drop.forwarding_class.as_deref(), Some("dcn"));
assert_eq!(drop.udp_port.as_deref(), Some("13742"));
assert_eq!(drop.trimmable_dscp, None);
assert!(cos.interfaces[0].settings.drop_congestion_notification);
}
const DOC_OUTPUT_QUEUES: &str = r#"
class-of-service {
congestion-notification-profile fcoe-cnp {
input {
ieee-802.1 {
code-point 011 {
pfc {
mru 2240;
}
}
}
cable-length 100;
}
output {
ieee-802.1 {
code-point 011 {
flow-control-queue [3 5];
}
code-point 100 {
flow-control-queue 4;
}
}
}
}
interfaces {
xe-0/0/7 {
congestion-notification-profile fcoe-cnp;
}
}
}
"#;
#[test]
fn reads_the_guide_output_flow_control_queues() {
let cos = class_of_service(&juniper::parse_braces(DOC_OUTPUT_QUEUES)).unwrap();
let profile = &cos.congestion_notification_profiles[0];
assert_eq!(profile.output.len(), 2);
assert_eq!(profile.output[0].kind, "ieee-802.1");
assert_eq!(profile.output[0].code_points, ["011"]);
assert_eq!(profile.output[0].flow_control_queues, ["3", "5"]);
assert_eq!(profile.output[1].code_points, ["100"]);
assert_eq!(profile.output[1].flow_control_queues, ["4"]);
assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2240"));
assert_eq!(profile.cable_length.as_deref(), Some("100"));
assert_eq!(
cos.interfaces[0].settings.congestion_notification_profile.as_deref(),
Some("fcoe-cnp")
);
}
}