Skip to main content

asterust_junos/protocols/bgp/family/
mod.rs

1//! `family`
2//! `protocols { bgp { [group <name> { [neighbor <address> {] family <afi> { <safi> { ... } } } } }`; valid at the `bgp`, `group` and `neighbor` levels.
3//!
4//! # See also
5//!
6//! - [family (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-protocols-bgp.html)
7//! - [labeled-unicast (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/labeled-unicast-edit-protocols-bgp.html)
8//! - [loops (BGP Address Family)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/loops-edit-protocols-bgp-family.html)
9//! - [aigp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/aigp-edit-protocols-bgp.html)
10//! - [damping (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/damping-edit-protocols-bgp.html)
11//! - [resolve-vpn](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resolve-vpn-edit-protocols-bgp.html)
12//! - [no-install](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-install-edit-protocols.html)
13//! - [no-validate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-validate-edit-protocols-bgp.html)
14//! - [rib-group (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-group-edit-protocols-bgp.html)
15//! - [protection (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/protection-edit-protocols-bgp.html)
16//! - [strip-nexthop](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/strip-nexthop-edit-protocols-bgp.html)
17//! - [auto-discovery-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-discovery-only-edit-protocols-bgp.html)
18//! - [defer-initial-multipath-build](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/defer-initial-multipath-build-edit-protocols-bgp-family-inet-unicast.html)
19//! - [per-group-label](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/per-group-label-edit-protocols-bgp.html)
20//! - [explicit-null (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/explicit-null-edit-protocols-bgp.html)
21//! - [extended-nexthop](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autogen-protocols-bgp-group-family-inet-unicast-extended-nexthop.html)
22//! - [entropy-label (protocols bgp)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/entropy-label-edit-protocols-bgp-family-inet-labeled-unicast.html)
23//! - [graceful-restart (Long-Lived for BGP Restarter)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/graceful-restart-long-lived-per-family-edit-protocols-bgp.html)
24//! - [advertise-srv6-service](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advertise-srv6-service-edit-protocols-bgp-family-inet-unicast.html)
25//! - [accept-srv6-service](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-srv6-service-edit-protocols-bgp-family-inet-unicast.html)
26//! - [extended-nexthop-color](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-nexthop-color-edit-protocols-bgp-family-inet.html)
27//! - [extended-nexthop-tunnel](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-nexthop-tunnel.html)
28//! - [BGP Route Prioritization](https://networkingdocs.hpe.com/documentation/us/en/software/junos/bgp/topics/topic-map/bgp-route-prioritization.html)
29
30mod add_path;
31mod entropy_label;
32mod graceful_restart;
33mod prefix_limit;
34
35pub use add_path::{AddPath, AddPathSend};
36pub use entropy_label::EntropyLabel;
37pub use graceful_restart::FamilyGracefulRestart;
38pub use prefix_limit::PrefixLimit;
39
40use crate::{active_children, arg_of, has_leaf, named_children, statement_words, value_after};
41use add_path::parse_add_path;
42use asterust::config::ConfigLine;
43use entropy_label::parse_entropy_label;
44use graceful_restart::parse_family_graceful_restart;
45use prefix_limit::parse_prefix_limit;
46
47const SAFIS: [&str; 7] = [
48    "unicast",
49    "multicast",
50    "labeled-unicast",
51    "any",
52    "flow",
53    "segment-routing-te",
54    "signaling",
55];
56
57/// An address family of a peer, e.g. `family inet { unicast { ... } }`. Families are not inherited between the
58/// `bgp`, `group` and `neighbor` levels, so none is resolved here. Only the more common per-family statements are
59/// extracted.
60#[derive(Clone, Debug, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct BgpFamily {
63    /// `inet`, `inet6`, `inet-vpn`, `l2vpn`, `evpn`, `route-target`, ...
64    pub afi: String,
65    /// `unicast`, `labeled-unicast`, `signaling`, ...; `None` for a family written without one.
66    pub safi: Option<String>,
67    pub prefix_limit: Option<PrefixLimit>,
68    pub accepted_prefix_limit: Option<PrefixLimit>,
69    pub add_path: Option<AddPath>,
70    pub loops: Option<String>,
71    /// `Some(true)` for `aigp`, `Some(false)` for `aigp { disable; }`.
72    pub aigp: Option<bool>,
73    pub rib_group: Option<String>,
74    /// The policy of `no-validate`.
75    pub no_validate: Option<String>,
76    /// Seconds, from `defer-initial-multipath-build { maximum-delay ...; }`.
77    pub defer_initial_multipath_build_max_delay: Option<String>,
78    pub damping: bool,
79    pub resolve_vpn: bool,
80    pub no_install: bool,
81    pub protection: bool,
82    pub strip_nexthop: bool,
83    pub auto_discovery_only: bool,
84    pub per_group_label: bool,
85    pub explicit_null: bool,
86    pub extended_nexthop: bool,
87    /// Sends the SRv6 service SID with the routes of this family.
88    pub advertise_srv6_service: bool,
89    /// Accepts the SRv6 service SID of received routes.
90    pub accept_srv6_service: bool,
91    /// Resolves colored prefixes over `inetcolor.0` or `inet6color.0` for segment routing traffic engineering.
92    pub extended_nexthop_color: bool,
93    pub extended_nexthop_tunnel: bool,
94    /// `expedited` or `priority <n>`, as written.
95    pub output_queue_priority: Option<String>,
96    pub route_refresh_priority: Option<String>,
97    pub withdraw_priority: Option<String>,
98    pub entropy_label: Option<EntropyLabel>,
99    pub graceful_restart: Option<FamilyGracefulRestart>,
100}
101
102/// Reads the `family` statements of a `bgp`, `group` or `neighbor` block, one entry per address and sub-address family.
103pub(crate) fn parse_families(line: &ConfigLine) -> Vec<BgpFamily> {
104    let mut families = Vec::new();
105    for (afi, block) in named_children(line, "family") {
106        let safis: Vec<(&str, &ConfigLine)> = active_children(block)
107            .filter_map(|child| {
108                let name = child.text.split_whitespace().next()?;
109                SAFIS.contains(&name).then_some((name, child))
110            })
111            .collect();
112        if safis.is_empty() {
113            families.push(parse_family(afi, None, block));
114        }
115        for (safi, node) in safis {
116            families.push(parse_family(afi, Some(safi), node));
117        }
118    }
119    families
120}
121
122fn parse_family(afi: &str, safi: Option<&str>, node: &ConfigLine) -> BgpFamily {
123    let field = |keyword: &str| arg_of(node, keyword).map(str::to_string);
124    let defer = statement_words(node, "defer-initial-multipath-build");
125    BgpFamily {
126        afi: afi.to_string(),
127        safi: safi.map(str::to_string),
128        prefix_limit: parse_prefix_limit(node, "prefix-limit"),
129        accepted_prefix_limit: parse_prefix_limit(node, "accepted-prefix-limit"),
130        add_path: parse_add_path(node),
131        loops: field("loops"),
132        aigp: statement_words(node, "aigp").map(|words| !words.contains(&"disable")),
133        rib_group: field("rib-group"),
134        no_validate: field("no-validate"),
135        defer_initial_multipath_build_max_delay: defer.and_then(|words| value_after(&words, "maximum-delay")),
136        damping: has_leaf(node, "damping"),
137        resolve_vpn: has_leaf(node, "resolve-vpn"),
138        no_install: has_leaf(node, "no-install"),
139        protection: has_leaf(node, "protection"),
140        strip_nexthop: has_leaf(node, "strip-nexthop"),
141        auto_discovery_only: has_leaf(node, "auto-discovery-only"),
142        per_group_label: has_leaf(node, "per-group-label"),
143        explicit_null: statement_words(node, "explicit-null").is_some(),
144        extended_nexthop: has_leaf(node, "extended-nexthop"),
145        advertise_srv6_service: has_leaf(node, "advertise-srv6-service"),
146        accept_srv6_service: has_leaf(node, "accept-srv6-service"),
147        extended_nexthop_color: has_leaf(node, "extended-nexthop-color"),
148        extended_nexthop_tunnel: has_leaf(node, "extended-nexthop-tunnel"),
149        output_queue_priority: priority(node, "output-queue-priority"),
150        route_refresh_priority: priority(node, "route-refresh-priority"),
151        withdraw_priority: priority(node, "withdraw-priority"),
152        entropy_label: parse_entropy_label(node),
153        graceful_restart: parse_family_graceful_restart(node),
154    }
155}
156
157/// `expedited` or `priority <n>`, whether written as a leaf or in a block.
158fn priority(node: &ConfigLine, keyword: &str) -> Option<String> {
159    statement_words(node, keyword).map(|words| words.join(" "))
160}
161
162#[cfg(test)]
163mod tests {
164    use crate::protocols::bgp::bgp;
165    use asterust::config::juniper;
166
167    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
168    const CONFIG: &str = r#"
169protocols {
170    bgp {
171        family inet {
172            unicast;
173        }
174        group CORE {
175            type internal;
176            family inet {
177                unicast {
178                    loops 2;
179                    rib-group RG-UNICAST;
180                    aigp;
181                    damping;
182                    defer-initial-multipath-build {
183                        maximum-delay 120;
184                    }
185                }
186                labeled-unicast {
187                    resolve-vpn;
188                    per-group-label;
189                    explicit-null {
190                        connected-only;
191                    }
192                    aigp {
193                        disable;
194                    }
195                }
196            }
197            family inet-vpn {
198                unicast {
199                    no-install;
200                }
201            }
202            family l2vpn {
203                signaling;
204            }
205            family route-target;
206            neighbor 10.0.0.2 {
207                family inet6 {
208                    any;
209                }
210            }
211        }
212    }
213}
214"#;
215
216    #[test]
217    fn reads_one_entry_per_address_and_sub_address_family() {
218        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
219        let global: Vec<_> = bgp
220            .options
221            .address_families
222            .iter()
223            .map(|f| (f.afi.as_str(), f.safi.as_deref()))
224            .collect();
225        assert_eq!(global, [("inet", Some("unicast"))]);
226
227        let group = &bgp.groups[0];
228        let names: Vec<_> = group
229            .options
230            .address_families
231            .iter()
232            .map(|f| (f.afi.as_str(), f.safi.as_deref()))
233            .collect();
234        assert_eq!(
235            names,
236            [
237                ("inet", Some("unicast")),
238                ("inet", Some("labeled-unicast")),
239                ("inet-vpn", Some("unicast")),
240                ("l2vpn", Some("signaling")),
241                ("route-target", None),
242            ]
243        );
244        let neighbor = &group.neighbors[0].options.address_families[0];
245        assert_eq!(
246            (neighbor.afi.as_str(), neighbor.safi.as_deref()),
247            ("inet6", Some("any"))
248        );
249    }
250
251    #[test]
252    fn reads_the_statements_of_each_family() {
253        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
254        let families = &bgp.groups[0].options.address_families;
255
256        let unicast = &families[0];
257        assert_eq!(unicast.loops.as_deref(), Some("2"));
258        assert_eq!(unicast.rib_group.as_deref(), Some("RG-UNICAST"));
259        assert_eq!(unicast.aigp, Some(true));
260        assert!(unicast.damping);
261        assert_eq!(unicast.defer_initial_multipath_build_max_delay.as_deref(), Some("120"));
262        assert!(!unicast.resolve_vpn);
263
264        let labeled = &families[1];
265        assert!(labeled.resolve_vpn);
266        assert!(labeled.per_group_label);
267        assert!(labeled.explicit_null);
268        assert_eq!(labeled.aigp, Some(false));
269
270        assert!(families[2].no_install);
271    }
272
273    #[test]
274    fn leaves_the_settings_unset_when_absent() {
275        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
276        let global = &bgp.options.address_families[0];
277        assert_eq!(global.prefix_limit, None);
278        assert_eq!(global.add_path, None);
279        assert_eq!(global.aigp, None);
280        assert_eq!(global.loops, None);
281    }
282}