Skip to main content

asterust_junos/protocols/bgp/peer_options/
bfd_liveness_detection.rs

1//! `bfd-liveness-detection`
2//! `protocols { bgp { group <name> { bfd-liveness-detection { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [bfd-liveness-detection (BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bfd-liveness-detection-edit-protocols-bgp.html)
7//! - [authentication (BGP BFD Liveness Detection)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-protocols-bgp-liveness-detection.html)
8//! - [detection-time (BFD Liveness Detection)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/detection-time-edit-protocols-bgp-bfd-liveness-detection.html)
9//! - [transmit-interval (BFD Liveness Detection)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/transmit-interval-edit-protocols-bgp-liveness-detection.html)
10//! - [BFD strict mode for BGP peer sessions](https://supportportal.juniper.net/s/article/BFD-strict-mode-for-BGP-peer-sessions)
11
12use crate::{arg_of, child_block, has_leaf, statement_words, value_after};
13use asterust::config::ConfigLine;
14
15/// The BFD session of a BGP peer.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct BgpBfd {
19    /// Milliseconds.
20    pub minimum_interval: Option<String>,
21    pub minimum_receive_interval: Option<String>,
22    pub multiplier: Option<String>,
23    /// `1` or `automatic`.
24    pub version: Option<String>,
25    /// `automatic`, `multihop` or `single-hop`.
26    pub session_mode: Option<String>,
27    pub pdu_size: Option<String>,
28    pub holddown_interval: Option<String>,
29    pub no_adaptation: bool,
30    /// From `detection-time { threshold ...; }`.
31    pub detection_time_threshold: Option<String>,
32    /// From `transmit-interval { minimum-interval ...; }`.
33    pub transmit_interval_minimum: Option<String>,
34    /// From `transmit-interval { threshold ...; }`.
35    pub transmit_interval_threshold: Option<String>,
36    /// From `authentication { algorithm ...; }`, e.g. `keyed-sha-1`.
37    pub authentication_algorithm: Option<String>,
38    /// The name of the key chain; the keys are not extracted.
39    pub authentication_key_chain: Option<String>,
40    pub authentication_loose_check: bool,
41    /// `strict-bfd` (CLI Reference) or `strict-mode` (support article): no session until BFD is up.
42    pub strict_mode: bool,
43    /// Seconds, from `bfd-up-wait-interval` or `bfd-wait-timeout`.
44    pub strict_mode_wait_timeout: Option<String>,
45}
46
47pub(super) fn parse_bfd(line: &ConfigLine) -> Option<BgpBfd> {
48    let bfd = child_block(line, "bfd-liveness-detection")?;
49    let strict = statement_words(bfd, "strict-bfd").or_else(|| statement_words(bfd, "strict-mode"));
50    let field = |keyword: &str| arg_of(bfd, keyword).map(str::to_string);
51    let nested = |block: &str, keyword: &str| {
52        child_block(bfd, block)
53            .and_then(|block| arg_of(block, keyword))
54            .map(str::to_string)
55    };
56    Some(BgpBfd {
57        minimum_interval: field("minimum-interval"),
58        minimum_receive_interval: field("minimum-receive-interval"),
59        multiplier: field("multiplier"),
60        version: field("version"),
61        session_mode: field("session-mode"),
62        pdu_size: field("pdu-size"),
63        holddown_interval: field("holddown-interval"),
64        no_adaptation: has_leaf(bfd, "no-adaptation"),
65        detection_time_threshold: nested("detection-time", "threshold"),
66        transmit_interval_minimum: nested("transmit-interval", "minimum-interval"),
67        transmit_interval_threshold: nested("transmit-interval", "threshold"),
68        authentication_algorithm: nested("authentication", "algorithm"),
69        authentication_key_chain: nested("authentication", "key-chain"),
70        authentication_loose_check: child_block(bfd, "authentication")
71            .is_some_and(|auth| has_leaf(auth, "loose-check")),
72        strict_mode: strict.is_some(),
73        strict_mode_wait_timeout: strict.and_then(|words| {
74            value_after(&words, "bfd-up-wait-interval").or_else(|| value_after(&words, "bfd-wait-timeout"))
75        }),
76    })
77}
78
79#[cfg(test)]
80mod tests {
81    use crate::protocols::bgp::bgp;
82    use asterust::config::juniper;
83
84    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
85    const CONFIG: &str = r#"
86protocols {
87    bgp {
88        group EBGP {
89            bfd-liveness-detection {
90                minimum-interval 300;
91                multiplier 3;
92                session-mode single-hop;
93                no-adaptation;
94                detection-time {
95                    threshold 900;
96                }
97                transmit-interval {
98                    minimum-interval 100;
99                    threshold 500;
100                }
101                authentication {
102                    algorithm keyed-sha-1;
103                    key-chain BFD-KEYS;
104                    loose-check;
105                }
106            }
107            neighbor 192.0.2.2 {
108                bfd-liveness-detection {
109                    minimum-interval 50;
110                }
111            }
112            neighbor 192.0.2.3;
113        }
114    }
115}
116"#;
117
118    #[test]
119    fn reads_the_session_and_its_nested_blocks() {
120        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
121        let group = bgp.groups[0].options.bfd_liveness_detection.as_ref().unwrap();
122        assert_eq!(group.minimum_interval.as_deref(), Some("300"));
123        assert_eq!(group.multiplier.as_deref(), Some("3"));
124        assert_eq!(group.session_mode.as_deref(), Some("single-hop"));
125        assert!(group.no_adaptation);
126        assert_eq!(group.detection_time_threshold.as_deref(), Some("900"));
127        assert_eq!(group.transmit_interval_minimum.as_deref(), Some("100"));
128        assert_eq!(group.transmit_interval_threshold.as_deref(), Some("500"));
129        assert_eq!(group.authentication_algorithm.as_deref(), Some("keyed-sha-1"));
130        assert_eq!(group.authentication_key_chain.as_deref(), Some("BFD-KEYS"));
131        assert!(group.authentication_loose_check);
132    }
133
134    #[test]
135    fn reads_the_neighbor_level_and_leaves_others_unset() {
136        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
137        let neighbors = &bgp.groups[0].neighbors;
138        let bfd = neighbors[0].options.bfd_liveness_detection.as_ref().unwrap();
139        assert_eq!(bfd.minimum_interval.as_deref(), Some("50"));
140        assert!(!bfd.no_adaptation);
141        assert_eq!(bfd.authentication_algorithm, None);
142        assert_eq!(neighbors[1].options.bfd_liveness_detection, None);
143    }
144
145    // Not from documentation: written from the CLI Reference and the support article. See the README "Sources".
146    const STRICT: &str = r#"
147protocols {
148    bgp {
149        group EBGP {
150            neighbor 192.0.2.2 {
151                bfd-liveness-detection {
152                    strict-bfd {
153                        bfd-up-wait-interval 30;
154                    }
155                }
156            }
157            neighbor 192.0.2.3 {
158                bfd-liveness-detection {
159                    strict-mode bfd-wait-timeout 20;
160                }
161            }
162            neighbor 192.0.2.4 {
163                bfd-liveness-detection {
164                    strict-mode;
165                }
166            }
167        }
168    }
169}
170"#;
171
172    #[test]
173    fn reads_strict_mode_in_both_spellings() {
174        let bgp = bgp(&juniper::parse_braces(STRICT)).unwrap();
175        let bfd = |index: usize| {
176            bgp.groups[0].neighbors[index]
177                .options
178                .bfd_liveness_detection
179                .clone()
180                .unwrap()
181        };
182        assert!(bfd(0).strict_mode);
183        assert_eq!(bfd(0).strict_mode_wait_timeout.as_deref(), Some("30"));
184        assert!(bfd(1).strict_mode);
185        assert_eq!(bfd(1).strict_mode_wait_timeout.as_deref(), Some("20"));
186        assert!(bfd(2).strict_mode);
187        assert_eq!(bfd(2).strict_mode_wait_timeout, None);
188    }
189}