Skip to main content

asterust_junos/routing_options/
forwarding_table.rs

1//! `forwarding-table`
2//! `routing-options { forwarding-table { ... } }`.
3//!
4//! # See also
5//!
6//! - [forwarding-table](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forwarding-table-edit-routing-options.html)
7
8use crate::{arg_of, args_of, has_leaf, list_values, toggle};
9use asterust::config::ConfigLine;
10
11/// The global `forwarding-table` settings. `chained-composite-next-hop` and the EVPN statements are not extracted.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ForwardingTable {
15    /// Policies that decide which routes are installed in the forwarding table.
16    pub export: Vec<String>,
17    pub ecmp_fast_reroute: bool,
18    /// `Some(true)` for `indirect-next-hop`, `Some(false)` for `no-indirect-next-hop`.
19    pub indirect_next_hop: Option<bool>,
20    pub indirect_next_hop_change_acknowledgements: Option<bool>,
21    pub krt_nexthop_ack_timeout: Option<String>,
22    /// Seconds.
23    pub remnant_holdtime: Option<String>,
24    /// Seconds.
25    pub multicast_composite_delete_defer_timeout: Option<String>,
26    /// `active-paths` or `feasible-paths`.
27    pub unicast_reverse_path: Option<String>,
28    /// Merges SRv6 chain next hops in the platform layer.
29    pub srv6_chain_merge: bool,
30}
31
32pub(super) fn parse_forwarding_table(table: &ConfigLine) -> ForwardingTable {
33    let field = |keyword: &str| arg_of(table, keyword).map(str::to_string);
34    ForwardingTable {
35        export: args_of(table, "export").into_iter().flat_map(list_values).collect(),
36        ecmp_fast_reroute: has_leaf(table, "ecmp-fast-reroute"),
37        indirect_next_hop: toggle(table, "indirect-next-hop"),
38        indirect_next_hop_change_acknowledgements: toggle(table, "indirect-next-hop-change-acknowledgements"),
39        krt_nexthop_ack_timeout: field("krt-nexthop-ack-timeout"),
40        remnant_holdtime: field("remnant-holdtime"),
41        multicast_composite_delete_defer_timeout: field("multicast-composite-delete-defer-timeout"),
42        unicast_reverse_path: field("unicast-reverse-path"),
43        srv6_chain_merge: has_leaf(table, "srv6-chain-merge"),
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use crate::routing_options::routing_options;
50    use asterust::config::juniper;
51
52    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
53    #[test]
54    fn extracts_forwarding_table_settings() {
55        let options = routing_options(&juniper::parse_braces(
56            r#"
57routing-options {
58    forwarding-table {
59        export [ LOAD-BALANCE ECMP ];
60        ecmp-fast-reroute;
61        no-indirect-next-hop;
62        indirect-next-hop-change-acknowledgements;
63        krt-nexthop-ack-timeout 20;
64        remnant-holdtime 300;
65        multicast-composite-delete-defer-timeout 60;
66        unicast-reverse-path feasible-paths;
67        srv6-chain-merge;
68    }
69}
70"#,
71        ))
72        .unwrap();
73        let table = options.forwarding_table.unwrap();
74        assert_eq!(table.export, ["LOAD-BALANCE", "ECMP"]);
75        assert!(table.ecmp_fast_reroute);
76        assert_eq!(table.indirect_next_hop, Some(false));
77        assert_eq!(table.indirect_next_hop_change_acknowledgements, Some(true));
78        assert_eq!(table.krt_nexthop_ack_timeout.as_deref(), Some("20"));
79        assert_eq!(table.remnant_holdtime.as_deref(), Some("300"));
80        assert_eq!(table.multicast_composite_delete_defer_timeout.as_deref(), Some("60"));
81        assert_eq!(table.unicast_reverse_path.as_deref(), Some("feasible-paths"));
82        assert!(table.srv6_chain_merge);
83    }
84
85    #[test]
86    fn single_export_policy_needs_no_brackets() {
87        let options = routing_options(&juniper::parse_braces(
88            "routing-options {\n    forwarding-table {\n        export LOAD-BALANCE;\n    }\n}\n",
89        ))
90        .unwrap();
91        let table = options.forwarding_table.unwrap();
92        assert_eq!(table.export, ["LOAD-BALANCE"]);
93        assert_eq!(table.indirect_next_hop, None);
94        assert!(!table.ecmp_fast_reroute);
95    }
96
97    #[test]
98    fn no_forwarding_table_is_none() {
99        let options = routing_options(&juniper::parse_braces(
100            "routing-options {\n    router-id 192.0.2.1;\n}\n",
101        ))
102        .unwrap();
103        assert!(options.forwarding_table.is_none());
104    }
105}