Skip to main content

asterust_junos/system/
packet_forwarding_options.rs

1//! `packet-forwarding-options`
2//! `system { packet-forwarding-options { ... } }`.
3//!
4//! Most of these statements are platform specific and restart the Packet Forwarding Engine.
5//!
6//! # See also
7//!
8//! - [eracl-ip6-match](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/eracl-ip6-match-edit-system.html)
9//! - [flow-export-timer](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flow-export-timer-edit-system-packet-fo.html)
10//! - [hw-db-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-hw-db-profile.html)
11//! - [mcast stats-enable](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mcast-stats-enable-edit-system-packet-forwarding-options.html)
12//! - [mcast-repl-type ingress-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mcast-repl-type-ingress-only.html)
13//! - [mcast-repl-type ingress-egress-recycle](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mcast-repl-type-ingress-egress-recycle.html)
14//! - [mef-forwarding-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mef-forwarding-profile-edit-system-packet-forwarding-options.html)
15//! - [mpls (packet-forwarding-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mpls.html)
16//! - [overlay (Packet Forwarding Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/overlay-edit-system-packet-forwarding-options.html)
17//! - [packet-format-match](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-packet-format-match.html)
18//! - [recycle-bandwidth-profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-recycle-bandwidth-profiles.html)
19//! - [vxlan-extended](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-system-profile-vxlan-extended.html)
20//! - [vxlan-stitching](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vxlan-stitching-edit-system-packet-forwarding-options-system-profile.html)
21//! - [eracl-profile (packet-forwarding-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-firewall-eracl-profile.html)
22//! - [ethernet-switching (firewall profiles)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ethernet-switching-edit-system-packet-forwarding-options-firewall-profiles.html)
23//! - [polling-interval (Network Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/polling-interval-edit-system-packet-forwarding-options-hw-resource-monitor.html)
24//! - [monitor-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/monitor-profile-edit-system-packet-forwarding-options-hw-resource-monitor.html)
25//! - [resource-list (Network Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resource-list-edit-system-packet-forwarding-options-hw-resource-monitor.html)
26
27use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statements};
28use asterust::config::ConfigLine;
29
30/// A `profile <name>` of `recycle-bandwidth-profiles`.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct RecycleBandwidthProfile {
34    pub name: String,
35    /// `(application, percentage)` pairs such as `("tunnel-services", "20")`.
36    pub applications: Vec<(String, String)>,
37}
38
39/// The `system-profile` block.
40#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct PfeSystemProfile {
43    pub vxlan_extended: bool,
44    pub vxlan_stitching: bool,
45    /// `vxlan-stitching { control-word; }`.
46    pub vxlan_stitching_control_word: bool,
47}
48
49/// The `firewall` block of `packet-forwarding-options`.
50#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct PfeFirewall {
53    /// `eracl-profile { eracl-scale; }`: egress firewall filters in scaled mode.
54    pub eracl_scale: bool,
55    /// The profiles of `profiles ethernet-switching`, e.g. `profile1`, `udf-profile1`.
56    pub ethernet_switching_profiles: Vec<String>,
57    /// The profiles of `profiles ethernet-switching egress`.
58    pub ethernet_switching_egress_profiles: Vec<String>,
59}
60
61/// A `monitor-profile <name>` of `hw-resource-monitor`.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct HwMonitorProfile {
65    pub name: String,
66    pub lower_threshold: Option<String>,
67    /// `alarm`, `none` or `syslog`.
68    pub notification_type: Option<String>,
69    pub upper_threshold: Option<String>,
70}
71
72/// A `resource-list <name>` of `hw-resource-monitor`.
73#[derive(Clone, Debug, PartialEq, Eq)]
74#[non_exhaustive]
75pub struct HwResourceList {
76    pub name: String,
77    /// The `resource-names`, e.g. `lpm-ipv4`.
78    pub resource_names: Vec<String>,
79}
80
81/// The `hw-resource-monitor` block.
82#[derive(Clone, Debug, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct HwResourceMonitor {
85    /// Milliseconds.
86    pub polling_interval: Option<String>,
87    pub monitor_profiles: Vec<HwMonitorProfile>,
88    pub resource_lists: Vec<HwResourceList>,
89}
90
91/// The `system packet-forwarding-options` settings.
92#[derive(Clone, Debug, PartialEq, Eq)]
93#[non_exhaustive]
94pub struct PacketForwardingOptions {
95    /// `srcip6-and-destip6` or `srcip6-only`.
96    pub eracl_ip6_match: Option<String>,
97    /// Seconds.
98    pub flow_export_timer: Option<String>,
99    /// e.g. `lean-edge`, `bng`, `carrier-ethernet` or `cloud-metro`.
100    pub hw_db_profile: Option<String>,
101    /// `mcast { stats-enable; }`.
102    pub mcast_stats_enable: bool,
103    /// `ingress-only` or `ingress-egress-recycle`.
104    pub mcast_repl_type: Option<String>,
105    pub mef_forwarding_profile: bool,
106    pub mpls_lsp_ingress_stats_enable: bool,
107    pub mpls_lsp_transit_stats_enable: bool,
108    /// From `overlay { interface-num }`.
109    pub overlay_interface_num: Option<String>,
110    /// From `overlay { next-hop }`.
111    pub overlay_next_hop: Option<String>,
112    /// e.g. `mpls-packet-format-1`.
113    pub packet_format_match: Option<String>,
114    pub recycle_bandwidth_profiles: Vec<RecycleBandwidthProfile>,
115    pub system_profile: Option<PfeSystemProfile>,
116    pub firewall: Option<PfeFirewall>,
117    pub hw_resource_monitor: Option<HwResourceMonitor>,
118}
119
120fn parse_firewall(firewall: &ConfigLine) -> PfeFirewall {
121    let switching = child_block(firewall, "profiles").and_then(|profiles| child_block(profiles, "ethernet-switching"));
122    PfeFirewall {
123        eracl_scale: child_block(firewall, "eracl-profile").is_some_and(|profile| has_leaf(profile, "eracl-scale")),
124        ethernet_switching_profiles: switching
125            .map(|s| {
126                active_children(s)
127                    .filter(|c| c.text != "egress")
128                    .map(|c| c.text.clone())
129                    .collect()
130            })
131            .unwrap_or_default(),
132        ethernet_switching_egress_profiles: switching
133            .and_then(|s| child_block(s, "egress"))
134            .map(|egress| active_children(egress).map(|c| c.text.clone()).collect())
135            .unwrap_or_default(),
136    }
137}
138
139fn parse_hw_resource_monitor(monitor: &ConfigLine) -> HwResourceMonitor {
140    HwResourceMonitor {
141        polling_interval: arg_of(monitor, "polling-interval").map(str::to_string),
142        monitor_profiles: named_children(monitor, "monitor-profile")
143            .map(|(name, profile)| HwMonitorProfile {
144                name: name.to_string(),
145                lower_threshold: arg_of(profile, "lower-threshold").map(str::to_string),
146                notification_type: arg_of(profile, "notification-type").map(str::to_string),
147                upper_threshold: arg_of(profile, "upper-threshold").map(str::to_string),
148            })
149            .collect(),
150        resource_lists: named_children(monitor, "resource-list")
151            .map(|(name, list)| HwResourceList {
152                name: name.to_string(),
153                resource_names: args_of(list, "resource-names")
154                    .into_iter()
155                    .flat_map(list_values)
156                    .collect(),
157            })
158            .collect(),
159    }
160}
161
162pub(super) fn parse_packet_forwarding_options(options: &ConfigLine) -> PacketForwardingOptions {
163    let overlay = child_block(options, "overlay");
164    let mpls = child_block(options, "mpls");
165    let profile = child_block(options, "system-profile");
166    PacketForwardingOptions {
167        eracl_ip6_match: statements(options, "eracl-ip6-match").first().map(|s| s.to_string()),
168        flow_export_timer: arg_of(options, "flow-export-timer").map(str::to_string),
169        hw_db_profile: statements(options, "hw-db-profile").first().map(|s| s.to_string()),
170        mcast_stats_enable: child_block(options, "mcast").is_some_and(|mcast| has_leaf(mcast, "stats-enable")),
171        mcast_repl_type: statements(options, "mcast-repl-type").first().map(|s| s.to_string()),
172        mef_forwarding_profile: has_leaf(options, "mef-forwarding-profile"),
173        mpls_lsp_ingress_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-ingress-stats-enable")),
174        mpls_lsp_transit_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-transit-stats-enable")),
175        overlay_interface_num: overlay.and_then(|o| arg_of(o, "interface-num")).map(str::to_string),
176        overlay_next_hop: overlay.and_then(|o| arg_of(o, "next-hop")).map(str::to_string),
177        packet_format_match: statements(options, "packet-format-match")
178            .first()
179            .map(|s| s.to_string()),
180        recycle_bandwidth_profiles: named_children(options, "recycle-bandwidth-profiles")
181            .map(|(name, profile)| RecycleBandwidthProfile {
182                name: name.to_string(),
183                applications: active_children(profile)
184                    .filter_map(|c| {
185                        let (application, percentage) = c.text.split_once(char::is_whitespace)?;
186                        Some((application.to_string(), percentage.trim().to_string()))
187                    })
188                    .collect(),
189            })
190            .collect(),
191        system_profile: profile.map(|profile| PfeSystemProfile {
192            vxlan_extended: has_leaf(profile, "vxlan-extended"),
193            vxlan_stitching: child_block(profile, "vxlan-stitching").is_some(),
194            vxlan_stitching_control_word: child_block(profile, "vxlan-stitching")
195                .is_some_and(|stitching| has_leaf(stitching, "control-word")),
196        }),
197        firewall: child_block(options, "firewall").map(parse_firewall),
198        hw_resource_monitor: child_block(options, "hw-resource-monitor").map(parse_hw_resource_monitor),
199    }
200}
201
202#[cfg(test)]
203mod tests {
204    use crate::system::system;
205    use asterust::config::juniper;
206
207    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
208    #[test]
209    fn extracts_the_packet_forwarding_options() {
210        let options = system(&juniper::parse_braces(
211            r#"
212system {
213    packet-forwarding-options {
214        eracl-ip6-match {
215            srcip6-only;
216        }
217        flow-export-timer 30;
218        hw-db-profile {
219            cloud-metro;
220        }
221        mcast {
222            stats-enable;
223        }
224        mcast-repl-type {
225            ingress-only;
226        }
227        mef-forwarding-profile;
228        mpls {
229            lsp-ingress-stats-enable;
230        }
231        overlay {
232            interface-num 2048;
233            next-hop 8192;
234        }
235        packet-format-match {
236            mpls-packet-format-3;
237        }
238        recycle-bandwidth-profiles lab {
239            tunnel-services 20;
240            srv6 30;
241        }
242        system-profile {
243            vxlan-extended;
244            vxlan-stitching {
245                control-word;
246            }
247        }
248        firewall {
249            eracl-profile {
250                eracl-scale;
251            }
252            profiles {
253                ethernet-switching {
254                    profile1;
255                    udf-profile1;
256                    egress {
257                        profile1;
258                    }
259                }
260            }
261        }
262        hw-resource-monitor {
263            polling-interval 5000;
264            monitor-profile alarms {
265                lower-threshold 60;
266                notification-type syslog;
267                upper-threshold 90;
268            }
269            resource-list lpm {
270                resource-names [ lpm-ipv4 lpm-ipv6-64 ];
271            }
272        }
273    }
274}
275"#,
276        ))
277        .unwrap()
278        .packet_forwarding_options
279        .unwrap();
280        assert_eq!(options.eracl_ip6_match.as_deref(), Some("srcip6-only"));
281        assert_eq!(options.flow_export_timer.as_deref(), Some("30"));
282        assert_eq!(options.hw_db_profile.as_deref(), Some("cloud-metro"));
283        assert!(options.mcast_stats_enable);
284        assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-only"));
285        assert!(options.mef_forwarding_profile);
286        assert!(options.mpls_lsp_ingress_stats_enable);
287        assert!(!options.mpls_lsp_transit_stats_enable);
288        assert_eq!(options.overlay_interface_num.as_deref(), Some("2048"));
289        assert_eq!(options.overlay_next_hop.as_deref(), Some("8192"));
290        assert_eq!(options.packet_format_match.as_deref(), Some("mpls-packet-format-3"));
291        assert_eq!(options.recycle_bandwidth_profiles[0].name, "lab");
292        assert_eq!(
293            options.recycle_bandwidth_profiles[0].applications,
294            [
295                ("tunnel-services".to_string(), "20".to_string()),
296                ("srv6".to_string(), "30".to_string())
297            ]
298        );
299        let profile = options.system_profile.unwrap();
300        assert!(profile.vxlan_extended);
301        assert!(profile.vxlan_stitching);
302        assert!(profile.vxlan_stitching_control_word);
303
304        let firewall = options.firewall.unwrap();
305        assert!(firewall.eracl_scale);
306        assert_eq!(firewall.ethernet_switching_profiles, ["profile1", "udf-profile1"]);
307        assert_eq!(firewall.ethernet_switching_egress_profiles, ["profile1"]);
308
309        let monitor = options.hw_resource_monitor.unwrap();
310        assert_eq!(monitor.polling_interval.as_deref(), Some("5000"));
311        assert_eq!(monitor.monitor_profiles[0].name, "alarms");
312        assert_eq!(monitor.monitor_profiles[0].lower_threshold.as_deref(), Some("60"));
313        assert_eq!(monitor.monitor_profiles[0].notification_type.as_deref(), Some("syslog"));
314        assert_eq!(monitor.monitor_profiles[0].upper_threshold.as_deref(), Some("90"));
315        assert_eq!(monitor.resource_lists[0].name, "lpm");
316        assert_eq!(monitor.resource_lists[0].resource_names, ["lpm-ipv4", "lpm-ipv6-64"]);
317    }
318
319    #[test]
320    fn accepts_the_inline_forms() {
321        let options = system(&juniper::parse_braces(
322            "system {\n    packet-forwarding-options {\n        hw-db-profile bng;\n        mcast-repl-type ingress-egress-recycle;\n        mcast {\n            stats-enable;\n        }\n    }\n}\n",
323        ))
324        .unwrap()
325        .packet_forwarding_options
326        .unwrap();
327        assert_eq!(options.hw_db_profile.as_deref(), Some("bng"));
328        assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-egress-recycle"));
329        assert!(options.system_profile.is_none());
330        assert!(options.firewall.is_none());
331    }
332
333    #[test]
334    fn no_packet_forwarding_options_is_none() {
335        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
336        assert!(system.packet_forwarding_options.is_none());
337    }
338}