Skip to main content

asterust_junos/protocols/source_packet_routing/
mod.rs

1//! `source-packet-routing`
2//! `protocols { source-packet-routing { segment-list <name> { ... } source-routing-path <name> { ... } } }`.
3//!
4//! This is segment routing traffic engineering (SR-TE), including static SRv6-TE policies. The IGP statements of the
5//! same name are in [`crate::protocols::isis`] and [`crate::protocols::ospf`].
6//!
7//! # See also
8//!
9//! - [source-packet-routing (Segment Routing Traffic Engineering)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autogen-protocols-source-packet-routing.html)
10//! - [preserve-nexthop-hierarchy (SRv6 TE)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preserve-nexthop-hierarchy-srv6-te.html)
11//! - [BGP Egress Traffic Engineering](https://networkingdocs.hpe.com/documentation/us/en/software/junos/bgp/topics/topic-map/bgp-egress-traffic-engineering.html),
12//!   which has "Overview of SR-TE Policy for SRv6 Tunnel" and "Example: Configuring Static SR-TE Policy for an SRv6
13//!   Tunnel"
14
15mod segment_list;
16mod source_routing_path;
17
18pub use segment_list::{SegmentHop, SegmentList};
19pub use source_routing_path::{PathSegmentList, SourceRoutingPath};
20
21use crate::{arg_of, child_block, has_leaf, named_children, top_level};
22use asterust::config::Config;
23use segment_list::parse_segment_list;
24use source_routing_path::parse_source_routing_path;
25
26/// The `protocols source-packet-routing` configuration. The `telemetry`, `traceoptions`, `traffic-engineering`,
27/// `source-routing-path-template` and `compute-profile` statements are not extracted.
28#[derive(Clone, Debug, Default, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct SourcePacketRouting {
31    pub inherit_label_nexthops: bool,
32    /// The name of the external path computing entity.
33    pub lsp_external_controller: Option<String>,
34    /// 1 through 16.
35    pub maximum_segment_list_depth: Option<String>,
36    /// 1 through 6, or 1 through 5 with `no-reduced-srh`.
37    pub maximum_srv6_te_segment_list_depth: Option<String>,
38    pub preference: Option<String>,
39    pub preserve_nexthop_hierarchy: bool,
40    /// Seconds.
41    pub retry_timer: Option<String>,
42    pub sbfd_frr: bool,
43    pub sr_preference: Option<String>,
44    pub sr_preference_override: Option<String>,
45    /// SRv6 data plane for statically configured SR-TE policies.
46    pub srv6: bool,
47    pub tunnel_tracking: bool,
48    pub segment_lists: Vec<SegmentList>,
49    pub source_routing_paths: Vec<SourceRoutingPath>,
50}
51
52/// Returns the top-level `protocols source-packet-routing` block, or `None` if it is absent.
53///
54/// # Examples
55///
56/// ```
57/// use asterust::config::juniper;
58/// use asterust_junos::protocols::source_packet_routing::source_packet_routing;
59///
60/// let config = juniper::parse_braces("protocols {\n    source-packet-routing {\n        srv6;\n        segment-list SL {\n            srv6;\n            hop1 srv6-sid 2001:db8::1;\n        }\n    }\n}\n");
61/// let spr = source_packet_routing(&config).unwrap();
62/// assert!(spr.srv6);
63/// assert_eq!(spr.segment_lists[0].hops[0].srv6_sid.as_deref(), Some("2001:db8::1"));
64/// ```
65pub fn source_packet_routing(config: &Config) -> Option<SourcePacketRouting> {
66    let spr = child_block(top_level(config, "protocols")?, "source-packet-routing")?;
67    let field = |keyword: &str| arg_of(spr, keyword).map(str::to_string);
68    Some(SourcePacketRouting {
69        inherit_label_nexthops: has_leaf(spr, "inherit-label-nexthops"),
70        lsp_external_controller: field("lsp-external-controller"),
71        maximum_segment_list_depth: field("maximum-segment-list-depth"),
72        maximum_srv6_te_segment_list_depth: field("maximum-srv6-te-segment-list-depth"),
73        preference: field("preference"),
74        preserve_nexthop_hierarchy: has_leaf(spr, "preserve-nexthop-hierarchy"),
75        retry_timer: field("retry-timer"),
76        sbfd_frr: has_leaf(spr, "sbfd-frr"),
77        sr_preference: field("sr-preference"),
78        sr_preference_override: field("sr-preference-override"),
79        srv6: has_leaf(spr, "srv6"),
80        tunnel_tracking: has_leaf(spr, "tunnel-tracking"),
81        segment_lists: named_children(spr, "segment-list")
82            .map(|(name, list)| parse_segment_list(name, list))
83            .collect(),
84        source_routing_paths: named_children(spr, "source-routing-path")
85            .map(|(name, path)| parse_source_routing_path(name, path))
86            .collect(),
87    })
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93    use asterust::config::juniper;
94
95    // Device R1 `show protocols` (source-packet-routing part) of "Example: Configuring Static SR-TE Policy for an SRv6
96    // Tunnel" (BGP User Guide, BGP Egress Traffic Engineering), with the guide's column padding removed; see the README
97    // "Sources".
98    const DOC_SRV6_TE: &str = r#"
99protocols {
100    source-packet-routing {
101        segment-list end-sids-segment {
102            srv6;
103            hop1 srv6-sid 2001:db8:0:a4::d04;
104            hop2 srv6-sid 2001:db8:0:a5::d05;
105            hop3 srv6-sid 2001:db8:0:a6::d06;
106        }
107        segment-list end-x-sids-segment-last-sid-end-sid {
108            srv6;
109            hop1 srv6-sid 2001:db8:0:a2::1a23;
110            hop2 srv6-sid 2001:db8:0:a3::1a34;
111            hop3 srv6-sid 2001:db8:0:a6::d06;
112        }
113        srv6;
114        source-routing-path nc_path_R1R6 {
115            srv6;
116            to 2001:db8:0:a6::d06;
117            from 2001:db8:1:255::1;
118            primary {
119                end-sids-segment weight 40;
120                end-x-sids-segment-last-sid-end-sid weight 30;
121            }
122        }
123        source-routing-path c_path_R1R6 {
124            srv6;
125            to 2001:db8:0:a6::d06;
126            from 2001:db8:1:255::1;
127            color 6;
128            primary {
129                end-sids-segment weight 40;
130                end-x-sids-segment-last-sid-end-sid weight 30;
131            }
132        }
133    }
134}
135"#;
136
137    // Not from documentation: written from the CLI Reference (`source-packet-routing`, `segment-list`,
138    // `source-routing-path`). See the README "Sources".
139    const EXTRA: &str = r#"
140protocols {
141    source-packet-routing {
142        inherit-label-nexthops;
143        lsp-external-controller PCE1;
144        maximum-segment-list-depth 8;
145        maximum-srv6-te-segment-list-depth 5;
146        preference 8;
147        preserve-nexthop-hierarchy;
148        retry-timer 60;
149        sbfd-frr;
150        sr-preference 200;
151        sr-preference-override 300;
152        tunnel-tracking;
153        segment-list MPLS-SL {
154            auto-translate {
155                protected mandatory;
156            }
157            compute;
158            dynamic;
159            inherit-label-nexthops;
160            hop1 {
161                ip-address 192.0.2.1;
162                label 801001;
163                label-type node;
164                strict;
165            }
166            hop2 {
167                label 801002;
168                loose;
169            }
170        }
171        source-routing-path MPLS-PATH {
172            to 192.0.2.7;
173            color 1200;
174            binding-sid 1200;
175            metric 500;
176            no-ingress;
177            sr-preference 70;
178            te-group-id 5;
179            lsp-external-controller PCE1;
180            primary sr1 {
181                weight 1;
182                return-path sr1-return;
183            }
184            primary sr4 {
185                compute PROFILE-A;
186                weight 2;
187            }
188            secondary sr9 {
189                weight 3;
190            }
191        }
192    }
193}
194"#;
195
196    #[test]
197    fn reads_the_guide_segment_lists() {
198        let spr = source_packet_routing(&juniper::parse_braces(DOC_SRV6_TE)).unwrap();
199        assert!(spr.srv6);
200        assert_eq!(spr.segment_lists.len(), 2);
201        let first = &spr.segment_lists[0];
202        assert_eq!(first.name, "end-sids-segment");
203        assert!(first.srv6);
204        assert_eq!(first.hops.len(), 3);
205        assert_eq!(first.hops[0].name, "hop1");
206        assert_eq!(first.hops[0].srv6_sid.as_deref(), Some("2001:db8:0:a4::d04"));
207        assert_eq!(first.hops[2].srv6_sid.as_deref(), Some("2001:db8:0:a6::d06"));
208        assert_eq!(first.hops[0].label, None);
209    }
210
211    #[test]
212    fn reads_the_guide_source_routing_paths() {
213        let spr = source_packet_routing(&juniper::parse_braces(DOC_SRV6_TE)).unwrap();
214        assert_eq!(spr.source_routing_paths.len(), 2);
215
216        let uncolored = &spr.source_routing_paths[0];
217        assert_eq!(uncolored.name, "nc_path_R1R6");
218        assert!(uncolored.srv6);
219        assert_eq!(uncolored.to.as_deref(), Some("2001:db8:0:a6::d06"));
220        assert_eq!(uncolored.from.as_deref(), Some("2001:db8:1:255::1"));
221        assert_eq!(uncolored.color, None);
222        assert_eq!(uncolored.primary.len(), 2);
223        assert_eq!(uncolored.primary[0].name, "end-sids-segment");
224        assert_eq!(uncolored.primary[0].weight.as_deref(), Some("40"));
225        assert_eq!(uncolored.primary[1].name, "end-x-sids-segment-last-sid-end-sid");
226        assert_eq!(uncolored.primary[1].weight.as_deref(), Some("30"));
227
228        assert_eq!(spr.source_routing_paths[1].color.as_deref(), Some("6"));
229    }
230
231    #[test]
232    fn reads_the_global_options() {
233        let spr = source_packet_routing(&juniper::parse_braces(EXTRA)).unwrap();
234        assert!(spr.inherit_label_nexthops);
235        assert_eq!(spr.lsp_external_controller.as_deref(), Some("PCE1"));
236        assert_eq!(spr.maximum_segment_list_depth.as_deref(), Some("8"));
237        assert_eq!(spr.maximum_srv6_te_segment_list_depth.as_deref(), Some("5"));
238        assert_eq!(spr.preference.as_deref(), Some("8"));
239        assert!(spr.preserve_nexthop_hierarchy);
240        assert_eq!(spr.retry_timer.as_deref(), Some("60"));
241        assert!(spr.sbfd_frr);
242        assert_eq!(spr.sr_preference.as_deref(), Some("200"));
243        assert_eq!(spr.sr_preference_override.as_deref(), Some("300"));
244        assert!(spr.tunnel_tracking);
245        assert!(!spr.srv6);
246    }
247
248    #[test]
249    fn reads_hop_blocks_and_list_options() {
250        let spr = source_packet_routing(&juniper::parse_braces(EXTRA)).unwrap();
251        let list = &spr.segment_lists[0];
252        assert!(list.auto_translate);
253        assert!(list.compute);
254        assert!(list.dynamic);
255        assert!(list.inherit_label_nexthops);
256        assert!(!list.srv6);
257        assert_eq!(list.hops.len(), 2);
258
259        let first = &list.hops[0];
260        assert_eq!(first.ip_address.as_deref(), Some("192.0.2.1"));
261        assert_eq!(first.label.as_deref(), Some("801001"));
262        assert_eq!(first.label_type.as_deref(), Some("node"));
263        assert!(first.strict);
264        assert!(!first.loose);
265
266        assert!(list.hops[1].loose);
267        assert_eq!(list.hops[1].ip_address, None);
268    }
269
270    #[test]
271    fn reads_path_options_and_named_primaries() {
272        let spr = source_packet_routing(&juniper::parse_braces(EXTRA)).unwrap();
273        let path = &spr.source_routing_paths[0];
274        assert!(!path.srv6);
275        assert_eq!(path.to.as_deref(), Some("192.0.2.7"));
276        assert_eq!(path.binding_sid.as_deref(), Some("1200"));
277        assert_eq!(path.metric.as_deref(), Some("500"));
278        assert!(path.no_ingress);
279        assert_eq!(path.sr_preference.as_deref(), Some("70"));
280        assert_eq!(path.te_group_id.as_deref(), Some("5"));
281        assert_eq!(path.lsp_external_controller.as_deref(), Some("PCE1"));
282
283        assert_eq!(path.primary.len(), 2);
284        assert_eq!(path.primary[0].name, "sr1");
285        assert_eq!(path.primary[0].weight.as_deref(), Some("1"));
286        assert_eq!(path.primary[0].return_path.as_deref(), Some("sr1-return"));
287        assert_eq!(path.primary[1].compute.as_deref(), Some("PROFILE-A"));
288        assert_eq!(path.secondary.len(), 1);
289        assert_eq!(path.secondary[0].name, "sr9");
290        assert_eq!(path.secondary[0].weight.as_deref(), Some("3"));
291    }
292
293    #[test]
294    fn no_source_packet_routing_is_none() {
295        let config = juniper::parse_braces("protocols {\n    ospf {\n        area 0.0.0.0;\n    }\n}\n");
296        assert_eq!(source_packet_routing(&config), None);
297    }
298}