asterust_junos/class_of_service/
congestion_notification.rs1use super::words;
13use crate::{active_children, arg_of, child_block, list_values, named_children, value_after};
14use asterust::config::ConfigLine;
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct NotificationInput {
20 pub kind: String,
22 pub code_points: Vec<String>,
23 pub pfc: bool,
25 pub pfc_mru: Option<String>,
27 pub xon: Option<String>,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct NotificationOutput {
34 pub kind: String,
35 pub code_points: Vec<String>,
36 pub flow_control_queues: Vec<String>,
37}
38
39#[derive(Clone, Debug, Default, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct PfcWatchdog {
43 pub detection: Option<String>,
44 pub poll_interval: Option<String>,
45 pub recovery: Option<String>,
46 pub drop: bool,
48}
49
50#[derive(Clone, Debug, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct CongestionNotificationProfile {
54 pub name: String,
55 pub input: Vec<NotificationInput>,
56 pub cable_length: Option<String>,
57 pub output: Vec<NotificationOutput>,
58 pub pfc_watchdog: Option<PfcWatchdog>,
59}
60
61#[derive(Clone, Debug, Default, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct DropCongestionNotification {
65 pub trimmable_dscp: Option<String>,
66 pub trimmed_dscp: Option<String>,
67 pub forwarding_class: Option<String>,
68 pub udp_port: Option<String>,
69}
70
71#[derive(Clone, Debug, PartialEq, Eq)]
73#[non_exhaustive]
74pub struct MplsEcnEntry {
75 pub ecn_capable_exp: String,
76 pub congestion_experienced_exp: Option<String>,
77}
78
79fn code_points(entry: &ConfigLine) -> Vec<String> {
80 entry.arg("code-point").map(list_values).unwrap_or_default()
81}
82
83fn parse_input(block: &ConfigLine) -> Vec<NotificationInput> {
84 active_children(block)
85 .filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
86 .flat_map(|kind| {
87 active_children(kind)
88 .filter(|entry| entry.arg("code-point").is_some())
89 .map(|entry| NotificationInput {
90 kind: kind.text.clone(),
91 code_points: code_points(entry),
92 pfc: child_block(entry, "pfc").is_some(),
93 pfc_mru: child_block(entry, "pfc")
94 .and_then(|pfc| arg_of(pfc, "mru"))
95 .map(str::to_string),
96 xon: arg_of(entry, "xon").map(str::to_string),
97 })
98 })
99 .collect()
100}
101
102fn parse_output(block: &ConfigLine) -> Vec<NotificationOutput> {
103 active_children(block)
104 .filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
105 .flat_map(|kind| {
106 active_children(kind)
107 .filter(|entry| entry.arg("code-point").is_some())
108 .map(|entry| NotificationOutput {
109 kind: kind.text.clone(),
110 code_points: code_points(entry),
111 flow_control_queues: arg_of(entry, "flow-control-queue").map(list_values).unwrap_or_default(),
112 })
113 })
114 .collect()
115}
116
117pub(super) fn parse_congestion_notification_profiles(cos: &ConfigLine) -> Vec<CongestionNotificationProfile> {
118 named_children(cos, "congestion-notification-profile")
119 .map(|(name, profile)| CongestionNotificationProfile {
120 name: name.to_string(),
121 input: child_block(profile, "input").map(parse_input).unwrap_or_default(),
122 cable_length: child_block(profile, "input")
123 .and_then(|input| arg_of(input, "cable-length"))
124 .map(str::to_string),
125 output: child_block(profile, "output").map(parse_output).unwrap_or_default(),
126 pfc_watchdog: child_block(profile, "pfc-watchdog").map(|watchdog| PfcWatchdog {
127 detection: arg_of(watchdog, "detection").map(str::to_string),
128 poll_interval: arg_of(watchdog, "poll-interval").map(str::to_string),
129 recovery: arg_of(watchdog, "recovery").map(str::to_string),
130 drop: child_block(watchdog, "watchdog-action").is_some_and(|action| crate::has_leaf(action, "drop")),
131 }),
132 })
133 .collect()
134}
135
136pub(super) fn parse_drop_congestion_notification(cos: &ConfigLine) -> Option<DropCongestionNotification> {
137 let drop = child_block(cos, "drop-congestion-notification")?;
138 let map = crate::statement_words(drop, "dscp-map");
139 Some(DropCongestionNotification {
140 trimmable_dscp: map.as_ref().and_then(|words| value_after(words, "trimmable")),
141 trimmed_dscp: map.as_ref().and_then(|words| value_after(words, "trimmed")),
142 forwarding_class: arg_of(drop, "forwarding-class").map(str::to_string),
143 udp_port: arg_of(drop, "udp-port").map(str::to_string),
144 })
145}
146
147pub(super) fn parse_mpls_ecn_map(cos: &ConfigLine) -> Vec<MplsEcnEntry> {
148 let Some(map) = child_block(cos, "mpls-ecn-map") else {
149 return Vec::new();
150 };
151 named_children(map, "ecn-capable-exp")
152 .map(|(exp, entry)| MplsEcnEntry {
153 ecn_capable_exp: exp.to_string(),
154 congestion_experienced_exp: words(entry, 0)
155 .windows(2)
156 .find(|pair| pair[0] == "congestion-experienced-exp" || pair[0] == "congestion-experienced-exp-value")
158 .map(|pair| pair[1].to_string()),
159 })
160 .collect()
161}
162
163#[cfg(test)]
164mod tests {
165 use crate::class_of_service::class_of_service;
166 use asterust::config::juniper;
167
168 const EXTRA: &str = r#"
170class-of-service {
171 congestion-notification-profile CNP1 {
172 input {
173 ieee-802.1 {
174 code-point [ 011 100 ] {
175 pfc {
176 mru 2500;
177 }
178 xon 150;
179 }
180 }
181 dscp {
182 code-point 110000 {
183 xon 100;
184 }
185 }
186 cable-length 100;
187 }
188 output {
189 ieee-802.1 {
190 code-point 011 {
191 flow-control-queue [ 3 4 ];
192 }
193 }
194 }
195 pfc-watchdog {
196 detection 100;
197 poll-interval 10;
198 recovery 200;
199 watchdog-action {
200 drop;
201 }
202 }
203 }
204 congestion-notification-profile CNP2;
205 drop-congestion-notification {
206 dscp-map {
207 trimmable 8 trimmed 9;
208 }
209 forwarding-class trimmed-fc;
210 udp-port 4791;
211 }
212 mpls-ecn-map {
213 ecn-capable-exp 2 {
214 congestion-experienced-exp-value 3;
215 }
216 }
217}
218"#;
219
220 #[test]
221 fn reads_the_congestion_notification_input() {
222 let profiles = class_of_service(&juniper::parse_braces(EXTRA))
223 .unwrap()
224 .congestion_notification_profiles;
225 assert_eq!(profiles.len(), 2);
226 let profile = &profiles[0];
227 assert_eq!(profile.name, "CNP1");
228 assert_eq!(profile.cable_length.as_deref(), Some("100"));
229 assert_eq!(profile.input.len(), 2);
230 assert_eq!(profile.input[0].kind, "ieee-802.1");
231 assert_eq!(profile.input[0].code_points, ["011", "100"]);
232 assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2500"));
233 assert_eq!(profile.input[0].xon.as_deref(), Some("150"));
234 assert_eq!(profile.input[1].kind, "dscp");
235 assert_eq!(profile.input[1].pfc_mru, None);
236 assert!(profiles[1].input.is_empty() && profiles[1].pfc_watchdog.is_none());
237 }
238
239 #[test]
240 fn reads_the_output_and_the_watchdog() {
241 let profile = class_of_service(&juniper::parse_braces(EXTRA))
242 .unwrap()
243 .congestion_notification_profiles
244 .remove(0);
245 assert_eq!(profile.output[0].code_points, ["011"]);
246 assert_eq!(profile.output[0].flow_control_queues, ["3", "4"]);
247 let watchdog = profile.pfc_watchdog.unwrap();
248 assert_eq!(watchdog.detection.as_deref(), Some("100"));
249 assert_eq!(watchdog.poll_interval.as_deref(), Some("10"));
250 assert_eq!(watchdog.recovery.as_deref(), Some("200"));
251 assert!(watchdog.drop);
252 }
253
254 #[test]
255 fn reads_the_drop_notification_and_the_ecn_map() {
256 let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
257 let drop = cos.drop_congestion_notification.unwrap();
258 assert_eq!(drop.trimmable_dscp.as_deref(), Some("8"));
259 assert_eq!(drop.trimmed_dscp.as_deref(), Some("9"));
260 assert_eq!(drop.forwarding_class.as_deref(), Some("trimmed-fc"));
261 assert_eq!(drop.udp_port.as_deref(), Some("4791"));
262 assert_eq!(cos.mpls_ecn_map.len(), 1);
263 assert_eq!(cos.mpls_ecn_map[0].ecn_capable_exp, "2");
264 assert_eq!(cos.mpls_ecn_map[0].congestion_experienced_exp.as_deref(), Some("3"));
265 }
266
267 const DOC_FCOE_PFC: &str = r#"
272class-of-service {
273 classifiers {
274 ieee-802.1 fcoe-classifier {
275 forwarding-class fcoe {
276 loss-priority low code-points 011;
277 }
278 }
279 }
280 congestion-notification-profile fcoe-cnp {
281 input {
282 ieee-802.1 {
283 code-point 011 {
284 pfc {
285 mru 2240;
286 }
287 }
288 }
289 cable-length 100;
290 }
291 }
292 interfaces {
293 xe-0/0/31 {
294 congestion-notification-profile fcoe-cnp;
295 unit 0 {
296 classifiers {
297 ieee-802.1 fcoe-classifier;
298 }
299 }
300 }
301 }
302}
303"#;
304
305 const DOC_DCQCN: &str = r#"
306class-of-service {
307 congestion-notification-profile cnp {
308 input {
309 dscp {
310 code-point 011010 {
311 pfc;
312 }
313 }
314 }
315 output {
316 ieee-802.1 {
317 code-point 011 {
318 flow-control-queue 4;
319 }
320 }
321 }
322 }
323 interfaces {
324 et-* {
325 congestion-notification-profile cnp;
326 }
327 }
328}
329"#;
330
331 const DOC_MPLS_ECN: &str = r#"
332class-of-service {
333 mpls-ecn-map {
334 ecn-capable-exp 6 {
335 congestion-experienced-exp 7;
336 }
337 ecn-capable-exp 5 {
338 congestion-experienced-exp 4;
339 }
340 }
341}
342"#;
343
344 const DOC_DCN: &str = r#"
345class-of-service {
346 drop-congestion-notification {
347 forwarding-class dcn;
348 udp-port 13742;
349 }
350 interfaces {
351 et-0/0/0 {
352 drop-congestion-notification;
353 }
354 }
355}
356"#;
357
358 #[test]
359 fn reads_the_guide_fcoe_profile() {
360 let cos = class_of_service(&juniper::parse_braces(DOC_FCOE_PFC)).unwrap();
361 let profile = &cos.congestion_notification_profiles[0];
362 assert_eq!(profile.name, "fcoe-cnp");
363 assert_eq!(profile.cable_length.as_deref(), Some("100"));
364 let input = &profile.input[0];
365 assert_eq!(input.kind, "ieee-802.1");
366 assert_eq!(input.code_points, ["011"]);
367 assert!(input.pfc);
368 assert_eq!(input.pfc_mru.as_deref(), Some("2240"));
369 let interface = &cos.interfaces[0];
370 assert_eq!(
371 interface.settings.congestion_notification_profile.as_deref(),
372 Some("fcoe-cnp")
373 );
374 assert_eq!(interface.units[0].settings.classifiers[0].name, "fcoe-classifier");
375 }
376
377 #[test]
378 fn reads_the_guide_dcqcn_profile() {
379 let cos = class_of_service(&juniper::parse_braces(DOC_DCQCN)).unwrap();
380 let profile = &cos.congestion_notification_profiles[0];
381 assert_eq!(profile.input[0].kind, "dscp");
382 assert_eq!(profile.input[0].code_points, ["011010"]);
383 assert!(profile.input[0].pfc && profile.input[0].pfc_mru.is_none());
384 assert_eq!(profile.output[0].code_points, ["011"]);
385 assert_eq!(profile.output[0].flow_control_queues, ["4"]);
386 assert_eq!(cos.interfaces[0].name, "et-*");
387 }
388
389 #[test]
390 fn reads_the_guide_mpls_ecn_map() {
391 let map = class_of_service(&juniper::parse_braces(DOC_MPLS_ECN))
392 .unwrap()
393 .mpls_ecn_map;
394 assert_eq!(map.len(), 2);
395 assert_eq!(map[0].ecn_capable_exp, "6");
396 assert_eq!(map[0].congestion_experienced_exp.as_deref(), Some("7"));
397 assert_eq!(map[1].congestion_experienced_exp.as_deref(), Some("4"));
398 }
399
400 #[test]
401 fn reads_the_guide_drop_congestion_notification() {
402 let cos = class_of_service(&juniper::parse_braces(DOC_DCN)).unwrap();
403 let drop = cos.drop_congestion_notification.unwrap();
404 assert_eq!(drop.forwarding_class.as_deref(), Some("dcn"));
405 assert_eq!(drop.udp_port.as_deref(), Some("13742"));
406 assert_eq!(drop.trimmable_dscp, None);
407 assert!(cos.interfaces[0].settings.drop_congestion_notification);
408 }
409
410 const DOC_OUTPUT_QUEUES: &str = r#"
414class-of-service {
415 congestion-notification-profile fcoe-cnp {
416 input {
417 ieee-802.1 {
418 code-point 011 {
419 pfc {
420 mru 2240;
421 }
422 }
423 }
424 cable-length 100;
425 }
426 output {
427 ieee-802.1 {
428 code-point 011 {
429 flow-control-queue [3 5];
430 }
431 code-point 100 {
432 flow-control-queue 4;
433 }
434 }
435 }
436 }
437 interfaces {
438 xe-0/0/7 {
439 congestion-notification-profile fcoe-cnp;
440 }
441 }
442}
443"#;
444
445 #[test]
446 fn reads_the_guide_output_flow_control_queues() {
447 let cos = class_of_service(&juniper::parse_braces(DOC_OUTPUT_QUEUES)).unwrap();
448 let profile = &cos.congestion_notification_profiles[0];
449 assert_eq!(profile.output.len(), 2);
450 assert_eq!(profile.output[0].kind, "ieee-802.1");
451 assert_eq!(profile.output[0].code_points, ["011"]);
452 assert_eq!(profile.output[0].flow_control_queues, ["3", "5"]);
453 assert_eq!(profile.output[1].code_points, ["100"]);
454 assert_eq!(profile.output[1].flow_control_queues, ["4"]);
455 assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2240"));
456 assert_eq!(profile.cable_length.as_deref(), Some("100"));
457 assert_eq!(
458 cos.interfaces[0].settings.congestion_notification_profile.as_deref(),
459 Some("fcoe-cnp")
460 );
461 }
462}