1mod interface;
37mod session;
38
39pub use interface::{LdpInterface, LdpLinkProtection};
40pub use session::LdpSession;
41
42use super::traceoptions::{parse_traceoptions, Traceoptions};
43use crate::{
44 active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, toggle,
45 top_level, value_after,
46};
47use asterust::config::Config;
48use interface::parse_interface;
49use session::parse_session;
50
51#[derive(Clone, Debug, Default, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct LdpGracefulRestart {
55 pub disable: bool,
56 pub helper_disable: bool,
57 pub maximum_neighbor_recovery_time: Option<String>,
59 pub reconnect_time: Option<String>,
61 pub recovery_time: Option<String>,
63}
64
65#[derive(Clone, Debug, Default, PartialEq, Eq)]
67#[non_exhaustive]
68pub struct LdpDualTransport {
69 pub inet_lsr_id: Option<String>,
70 pub inet6_lsr_id: Option<String>,
71}
72
73#[derive(Clone, Debug, Default, PartialEq, Eq)]
75#[non_exhaustive]
76pub struct LdpTargetedHello {
77 pub hello_interval: Option<String>,
78 pub hold_time: Option<String>,
79}
80
81#[derive(Clone, Debug, Default, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct LdpAutoTargetedSession {
85 pub maximum_sessions: Option<String>,
86 pub teardown_delay: Option<String>,
88}
89
90#[derive(Clone, Debug, Default, PartialEq, Eq)]
92#[non_exhaustive]
93pub struct LdpMakeBeforeBreak {
94 pub timeout: Option<String>,
95 pub switchover_delay: Option<String>,
96}
97
98#[derive(Clone, Debug, Default, PartialEq, Eq)]
101#[non_exhaustive]
102pub struct Ldp {
103 pub disable: bool,
104 pub interfaces: Vec<LdpInterface>,
105 pub sessions: Vec<LdpSession>,
106 pub session_groups: Vec<LdpSession>,
107 pub keepalive_interval: Option<String>,
109 pub keepalive_timeout: Option<String>,
111 pub preference: Option<String>,
112 pub families: Vec<String>,
114 pub dual_transport: Option<LdpDualTransport>,
115 pub transport_address: Option<String>,
117 pub transport_preference: Option<String>,
119 pub deaggregate: Option<bool>,
121 pub mtu_discovery: Option<bool>,
123 pub explicit_null: bool,
124 pub no_forwarding: bool,
125 pub track_igp_metric: bool,
126 pub l2_smart_policy: bool,
127 pub strict_targeted_hellos: bool,
128 pub upstream_label_assignment: bool,
129 pub p2mp: bool,
130 pub p2mp_no_rsvp_tunneling: bool,
131 pub longest_match: Option<Vec<String>>,
133 pub export_policies: Vec<String>,
134 pub import_policies: Vec<String>,
135 pub egress_policies: Vec<String>,
136 pub dod_request_policy: Option<String>,
137 pub igp_synchronization_holddown_interval: Option<String>,
139 pub graceful_restart: Option<LdpGracefulRestart>,
140 pub session_protection: Option<Option<String>>,
142 pub targeted_hello: Option<LdpTargetedHello>,
143 pub auto_targeted_session: Option<LdpAutoTargetedSession>,
144 pub make_before_break: Option<LdpMakeBeforeBreak>,
145 pub label_withdrawal_delay: Option<String>,
146 pub log_updown_trap_disabled: bool,
148 pub traceoptions: Option<Traceoptions>,
149}
150
151pub fn ldp(config: &Config) -> Option<Ldp> {
165 let ldp = child_block(top_level(config, "protocols")?, "ldp")?;
166 let field = |keyword: &str| arg_of(ldp, keyword).map(str::to_string);
167 let policies = |keyword: &str| -> Vec<String> { args_of(ldp, keyword).into_iter().flat_map(list_values).collect() };
168 let p2mp = child_block(ldp, "p2mp");
169 Some(Ldp {
170 disable: has_leaf(ldp, "disable"),
171 interfaces: named_children(ldp, "interface")
172 .map(|(name, interface)| parse_interface(name, interface))
173 .collect(),
174 sessions: named_children(ldp, "session")
175 .map(|(name, session)| parse_session(name, session))
176 .collect(),
177 session_groups: named_children(ldp, "session-group")
178 .map(|(name, group)| parse_session(name, group))
179 .collect(),
180 keepalive_interval: field("keepalive-interval"),
181 keepalive_timeout: field("keepalive-timeout"),
182 preference: field("preference"),
183 families: child_block(ldp, "family")
184 .map(|family| active_children(family).map(|c| c.text.clone()).collect())
185 .unwrap_or_default(),
186 dual_transport: child_block(ldp, "dual-transport").map(|block| LdpDualTransport {
187 inet_lsr_id: arg_of(block, "inet-lsr-id").map(str::to_string),
188 inet6_lsr_id: arg_of(block, "inet6-lsr-id").map(str::to_string),
189 }),
190 transport_address: field("transport-address"),
191 transport_preference: field("transport-preference"),
192 deaggregate: toggle(ldp, "deaggregate"),
193 mtu_discovery: toggle(ldp, "mtu-discovery"),
194 explicit_null: has_leaf(ldp, "explicit-null") || arg_of(ldp, "explicit-null").is_some(),
195 no_forwarding: has_leaf(ldp, "no-forwarding"),
196 track_igp_metric: has_leaf(ldp, "track-igp-metric"),
197 l2_smart_policy: has_leaf(ldp, "l2-smart-policy"),
198 strict_targeted_hellos: has_leaf(ldp, "strict-targeted-hellos"),
199 upstream_label_assignment: has_leaf(ldp, "upstream-label-assignment"),
200 p2mp: p2mp.is_some(),
201 p2mp_no_rsvp_tunneling: p2mp.is_some_and(|block| has_leaf(block, "no-rsvp-tunneling")),
202 longest_match: child_block(ldp, "longest-match").map(|block| {
203 let policy = child_block(block, "policy");
204 policy
205 .map(|p| active_children(p).flat_map(|c| list_values(&c.text)).collect())
206 .unwrap_or_else(|| args_of(block, "policy").into_iter().flat_map(list_values).collect())
207 }),
208 export_policies: policies("export"),
209 import_policies: policies("import"),
210 egress_policies: policies("egress-policy"),
211 dod_request_policy: field("dod-request-policy"),
212 igp_synchronization_holddown_interval: statement_words(ldp, "igp-synchronization")
213 .and_then(|words| value_after(&words, "holddown-interval")),
214 graceful_restart: child_block(ldp, "graceful-restart").map(|block| LdpGracefulRestart {
215 disable: has_leaf(block, "disable"),
216 helper_disable: has_leaf(block, "helper-disable"),
217 maximum_neighbor_recovery_time: arg_of(block, "maximum-neighbor-recovery-time").map(str::to_string),
218 reconnect_time: arg_of(block, "reconnect-time").map(str::to_string),
219 recovery_time: arg_of(block, "recovery-time").map(str::to_string),
220 }),
221 session_protection: statement_words(ldp, "session-protection").map(|words| value_after(&words, "timeout")),
222 targeted_hello: child_block(ldp, "targeted-hello").map(|block| LdpTargetedHello {
223 hello_interval: arg_of(block, "hello-interval").map(str::to_string),
224 hold_time: arg_of(block, "hold-time").map(str::to_string),
225 }),
226 auto_targeted_session: child_block(ldp, "auto-targeted-session").map(|block| LdpAutoTargetedSession {
227 maximum_sessions: arg_of(block, "maximum-sessions").map(str::to_string),
228 teardown_delay: arg_of(block, "teardown-delay").map(str::to_string),
229 }),
230 make_before_break: child_block(ldp, "make-before-break").map(|block| LdpMakeBeforeBreak {
231 timeout: arg_of(block, "timeout").map(str::to_string),
232 switchover_delay: arg_of(block, "switchover-delay").map(str::to_string),
233 }),
234 label_withdrawal_delay: field("label-withdrawal-delay"),
235 log_updown_trap_disabled: statement_words(ldp, "log-updown").is_some_and(|words| words.contains(&"disable")),
236 traceoptions: parse_traceoptions(ldp),
237 })
238}
239
240#[cfg(test)]
241mod tests {
242 use super::*;
243 use asterust::config::juniper;
244
245 const DOC_LINK_PROTECTION: &str = r#"
248protocols {
249 ldp {
250 interface all {
251 link-protection {
252 dynamic-rsvp-lsp;
253 }
254 }
255 interface fxp0.0 {
256 disable;
257 }
258 p2mp;
259 }
260}
261"#;
262
263 const DOC_IPV6: &str = r#"
264protocols {
265 ldp {
266 deaggregate;
267 interface ge-1/0/0.0;
268 interface lo0.0;
269 family {
270 inet6;
271 inet;
272 }
273 transport-preference ipv4;
274 }
275}
276"#;
277
278 const DOC_DUAL_TRANSPORT: &str = r#"
279protocols {
280 ldp {
281 deaggregate;
282 interface ge-1/0/0.0;
283 interface lo0.0;
284 family {
285 inet6;
286 inet;
287 }
288 dual-transport {
289 inet-lsr-id 10.255.0.1;
290 inet6-lsr-id 10.1.1.1;
291 }
292 }
293}
294"#;
295
296 const DOC_DOWNSTREAM_ON_DEMAND: &str = r#"
297protocols {
298 ldp {
299 dod-request-policy DOD-Request-Loopbacks;
300 session 172.16.1.1 {
301 downstream-on-demand;
302 }
303 }
304}
305"#;
306
307 const DOC_LONGEST_MATCH: &str = r#"
308protocols {
309 ldp {
310 longest-match;
311 interface ge-0/0/2.0;
312 interface lo0.0;
313 }
314}
315"#;
316
317 const DOC_TRACEOPTIONS: &str = r#"
318protocols {
319 ldp {
320 traceoptions {
321 file ldp size 10m files 5 world-readable;
322 flag packets receive;
323 flag binding;
324 }
325 interface all {
326 }
327 }
328}
329"#;
330
331 const DOC_POLICIES: &str = r#"
332protocols {
333 ldp {
334 egress-policy connected-only;
335 export block-one;
336 import only-32;
337 graceful-restart {
338 helper-disable;
339 }
340 }
341}
342"#;
343
344 const EXTRA: &str = r#"
346protocols {
347 ldp {
348 keepalive-interval 10;
349 keepalive-timeout 30;
350 preference 9;
351 transport-address router-id;
352 no-deaggregate;
353 no-mtu-discovery;
354 explicit-null;
355 no-forwarding;
356 track-igp-metric;
357 l2-smart-policy;
358 strict-targeted-hellos;
359 upstream-label-assignment;
360 label-withdrawal-delay 60;
361 export [ a b ];
362 import [ c d ];
363 egress-policy [ e f ];
364 longest-match {
365 policy [ lm1 lm2 ];
366 }
367 igp-synchronization {
368 holddown-interval 20;
369 }
370 graceful-restart {
371 disable;
372 maximum-neighbor-recovery-time 140;
373 reconnect-time 60;
374 recovery-time 100;
375 }
376 session-protection {
377 timeout 120;
378 }
379 targeted-hello {
380 hello-interval 5;
381 hold-time 15;
382 }
383 auto-targeted-session {
384 maximum-sessions 200;
385 teardown-delay 30;
386 }
387 make-before-break {
388 timeout 60;
389 switchover-delay 5;
390 }
391 p2mp {
392 no-rsvp-tunneling;
393 }
394 log-updown {
395 trap disable;
396 }
397 interface ge-0/0/0.0 {
398 hello-interval 3;
399 hold-time 9;
400 transport-address loopback;
401 allow-subnet-mismatch;
402 link-protection {
403 disable;
404 dynamic-rsvp-lsp;
405 }
406 }
407 interface ge-0/0/1.0 {
408 link-protection;
409 }
410 session 10.0.0.2 {
411 authentication-algorithm hmac-sha-1-96;
412 authentication-key "$9$secret"; ## SECRET-DATA
413 authentication-key-chain chain1;
414 no-mtu-discovery;
415 transport-address 10.255.0.1;
416 }
417 session-group 10.1.0.0/16 {
418 downstream-on-demand;
419 mtu-discovery;
420 }
421 }
422}
423"#;
424
425 fn ldp_of(text: &str) -> Ldp {
426 ldp(&juniper::parse_braces(text)).unwrap()
427 }
428
429 #[test]
430 fn reads_the_guide_link_protection() {
431 let ldp = ldp_of(DOC_LINK_PROTECTION);
432 assert_eq!(ldp.interfaces.len(), 2);
433 assert_eq!(ldp.interfaces[0].name, "all");
434 let protection = ldp.interfaces[0].link_protection.as_ref().unwrap();
435 assert!(protection.dynamic_rsvp_lsp);
436 assert!(!protection.disable);
437 assert_eq!(ldp.interfaces[1].name, "fxp0.0");
438 assert!(ldp.interfaces[1].disable);
439 assert!(ldp.p2mp);
440 assert!(!ldp.p2mp_no_rsvp_tunneling);
441 }
442
443 #[test]
444 fn reads_the_guide_ipv6_transport() {
445 let ldp = ldp_of(DOC_IPV6);
446 assert_eq!(ldp.deaggregate, Some(true));
447 assert_eq!(ldp.families, ["inet6", "inet"]);
448 assert_eq!(ldp.transport_preference.as_deref(), Some("ipv4"));
449 assert_eq!(ldp.dual_transport, None);
450
451 let dual = ldp_of(DOC_DUAL_TRANSPORT).dual_transport.unwrap();
452 assert_eq!(dual.inet_lsr_id.as_deref(), Some("10.255.0.1"));
453 assert_eq!(dual.inet6_lsr_id.as_deref(), Some("10.1.1.1"));
454 }
455
456 #[test]
457 fn reads_the_guide_downstream_on_demand_session() {
458 let ldp = ldp_of(DOC_DOWNSTREAM_ON_DEMAND);
459 assert_eq!(ldp.dod_request_policy.as_deref(), Some("DOD-Request-Loopbacks"));
460 assert_eq!(ldp.sessions.len(), 1);
461 assert_eq!(ldp.sessions[0].name, "172.16.1.1");
462 assert!(ldp.sessions[0].downstream_on_demand);
463 assert!(ldp.session_groups.is_empty());
464 }
465
466 #[test]
467 fn reads_the_guide_longest_match_policies_and_tracing() {
468 let ldp = ldp_of(DOC_LONGEST_MATCH);
469 assert_eq!(ldp.longest_match, Some(Vec::new()));
470 assert_eq!(ldp.interfaces.len(), 2);
471
472 let ldp = ldp_of(DOC_POLICIES);
473 assert_eq!(ldp.egress_policies, ["connected-only"]);
474 assert_eq!(ldp.export_policies, ["block-one"]);
475 assert_eq!(ldp.import_policies, ["only-32"]);
476 let restart = ldp.graceful_restart.unwrap();
477 assert!(restart.helper_disable);
478 assert!(!restart.disable);
479
480 let ldp = ldp_of(DOC_TRACEOPTIONS);
481 let trace = ldp.traceoptions.unwrap();
482 assert_eq!(trace.file.as_deref(), Some("ldp"));
483 assert_eq!(trace.size.as_deref(), Some("10m"));
484 assert_eq!(trace.files.as_deref(), Some("5"));
485 assert_eq!(trace.world_readable, Some(true));
486 assert_eq!(trace.flags[0].name, "packets");
487 assert_eq!(trace.flags[0].modifiers, ["receive"]);
488 assert_eq!(trace.flags[1].name, "binding");
489 assert_eq!(ldp.interfaces[0].name, "all");
490 }
491
492 #[test]
493 fn reads_the_global_options() {
494 let ldp = ldp_of(EXTRA);
495 assert!(!ldp.disable);
496 assert_eq!(ldp.keepalive_interval.as_deref(), Some("10"));
497 assert_eq!(ldp.keepalive_timeout.as_deref(), Some("30"));
498 assert_eq!(ldp.preference.as_deref(), Some("9"));
499 assert_eq!(ldp.transport_address.as_deref(), Some("router-id"));
500 assert_eq!(ldp.deaggregate, Some(false));
501 assert_eq!(ldp.mtu_discovery, Some(false));
502 assert!(ldp.explicit_null);
503 assert!(ldp.no_forwarding);
504 assert!(ldp.track_igp_metric);
505 assert!(ldp.l2_smart_policy);
506 assert!(ldp.strict_targeted_hellos);
507 assert!(ldp.upstream_label_assignment);
508 assert_eq!(ldp.label_withdrawal_delay.as_deref(), Some("60"));
509 assert!(ldp.log_updown_trap_disabled);
510 assert!(ldp.p2mp);
511 assert!(ldp.p2mp_no_rsvp_tunneling);
512 assert_eq!(ldp.export_policies, ["a", "b"]);
513 assert_eq!(ldp.import_policies, ["c", "d"]);
514 assert_eq!(ldp.egress_policies, ["e", "f"]);
515 assert_eq!(ldp.longest_match, Some(vec!["lm1".to_string(), "lm2".to_string()]));
516 assert_eq!(ldp.igp_synchronization_holddown_interval.as_deref(), Some("20"));
517 }
518
519 #[test]
520 fn reads_the_grouped_options() {
521 let ldp = ldp_of(EXTRA);
522 let restart = ldp.graceful_restart.unwrap();
523 assert!(restart.disable);
524 assert_eq!(restart.maximum_neighbor_recovery_time.as_deref(), Some("140"));
525 assert_eq!(restart.reconnect_time.as_deref(), Some("60"));
526 assert_eq!(restart.recovery_time.as_deref(), Some("100"));
527 assert_eq!(ldp.session_protection, Some(Some("120".to_string())));
528 let hello = ldp.targeted_hello.unwrap();
529 assert_eq!(hello.hello_interval.as_deref(), Some("5"));
530 assert_eq!(hello.hold_time.as_deref(), Some("15"));
531 let auto = ldp.auto_targeted_session.unwrap();
532 assert_eq!(auto.maximum_sessions.as_deref(), Some("200"));
533 assert_eq!(auto.teardown_delay.as_deref(), Some("30"));
534 let mbb = ldp.make_before_break.unwrap();
535 assert_eq!(mbb.timeout.as_deref(), Some("60"));
536 assert_eq!(mbb.switchover_delay.as_deref(), Some("5"));
537 }
538
539 #[test]
540 fn reads_the_interface_options() {
541 let ldp = ldp_of(EXTRA);
542 let interface = &ldp.interfaces[0];
543 assert_eq!(interface.hello_interval.as_deref(), Some("3"));
544 assert_eq!(interface.hold_time.as_deref(), Some("9"));
545 assert_eq!(interface.transport_address.as_deref(), Some("loopback"));
546 assert!(interface.allow_subnet_mismatch);
547 let protection = interface.link_protection.as_ref().unwrap();
548 assert!(protection.disable);
549 assert!(protection.dynamic_rsvp_lsp);
550 assert_eq!(ldp.interfaces[1].link_protection, Some(LdpLinkProtection::default()));
551 }
552
553 #[test]
554 fn reads_sessions_without_the_key() {
555 let ldp = ldp_of(EXTRA);
556 let session = &ldp.sessions[0];
557 assert_eq!(session.name, "10.0.0.2");
558 assert_eq!(session.authentication_algorithm.as_deref(), Some("hmac-sha-1-96"));
559 assert!(session.has_authentication_key);
560 assert_eq!(session.authentication_key_chain.as_deref(), Some("chain1"));
561 assert_eq!(session.mtu_discovery, Some(false));
562 assert_eq!(session.transport_address.as_deref(), Some("10.255.0.1"));
563 assert!(!session.downstream_on_demand);
564
565 let group = &ldp.session_groups[0];
566 assert_eq!(group.name, "10.1.0.0/16");
567 assert!(group.downstream_on_demand);
568 assert_eq!(group.mtu_discovery, Some(true));
569 assert!(!group.has_authentication_key);
570 }
571
572 #[test]
573 fn bare_statements_and_absent_values() {
574 let ldp = ldp_of("protocols {\n ldp {\n session-protection;\n disable;\n }\n}\n");
575 assert!(ldp.disable);
576 assert_eq!(ldp.session_protection, Some(None));
577 assert_eq!(ldp.graceful_restart, None);
578 assert_eq!(ldp.longest_match, None);
579 assert!(!ldp.p2mp);
580 assert!(!ldp.log_updown_trap_disabled);
581 }
582
583 #[test]
584 fn no_ldp_is_none() {
585 let config = juniper::parse_braces("protocols {\n ospf {\n area 0.0.0.0;\n }\n}\n");
586 assert_eq!(ldp(&config), None);
587 }
588}