1use crate::{
38 active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statements, value_after,
39};
40use asterust::config::ConfigLine;
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct RecycleBandwidthProfile {
46 pub name: String,
47 pub applications: Vec<(String, String)>,
49}
50
51#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct CounterProfileApp {
55 pub name: String,
56 pub counter_4k: Option<String>,
58 pub counter_8k: Option<String>,
59 pub counter_16k: Option<String>,
60}
61
62#[derive(Clone, Debug, PartialEq, Eq)]
64#[non_exhaustive]
65pub struct CounterProfile {
66 pub name: String,
67 pub apps: Vec<CounterProfileApp>,
68}
69
70#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct HwProfile {
74 pub name: String,
75 pub counter_profile: Option<String>,
76}
77
78#[derive(Clone, Debug, PartialEq, Eq)]
80#[non_exhaustive]
81pub struct PfeSystemProfile {
82 pub vxlan_extended: bool,
83 pub vxlan_stitching: bool,
84 pub vxlan_stitching_control_word: bool,
86}
87
88#[derive(Clone, Debug, PartialEq, Eq)]
90#[non_exhaustive]
91pub struct PfeFirewall {
92 pub eracl_scale: bool,
94 pub force_fbf_terms: bool,
95 pub ethernet_switching_profiles: Vec<String>,
97 pub ethernet_switching_egress_profiles: Vec<String>,
99}
100
101#[derive(Clone, Debug, PartialEq, Eq)]
103#[non_exhaustive]
104pub struct HwMonitorProfile {
105 pub name: String,
106 pub lower_threshold: Option<String>,
107 pub notification_type: Option<String>,
109 pub upper_threshold: Option<String>,
110}
111
112#[derive(Clone, Debug, PartialEq, Eq)]
114#[non_exhaustive]
115pub struct HwResourceList {
116 pub name: String,
117 pub resource_names: Vec<String>,
119}
120
121#[derive(Clone, Debug, PartialEq, Eq)]
123#[non_exhaustive]
124pub struct HwResourceMonitor {
125 pub polling_interval: Option<String>,
127 pub monitor_profiles: Vec<HwMonitorProfile>,
128 pub resource_lists: Vec<HwResourceList>,
129}
130
131#[derive(Clone, Debug, PartialEq, Eq)]
133#[non_exhaustive]
134pub struct PacketForwardingOptions {
135 pub eracl_ip6_match: Option<String>,
137 pub flow_export_timer: Option<String>,
139 pub hw_db_profile: Option<String>,
141 pub mcast_stats_enable: bool,
143 pub mcast_repl_type: Option<String>,
145 pub mef_forwarding_profile: bool,
146 pub mpls_lsp_ingress_stats_enable: bool,
147 pub mpls_lsp_transit_stats_enable: bool,
148 pub overlay_interface_num: Option<String>,
150 pub overlay_next_hop: Option<String>,
152 pub packet_format_match: Option<String>,
154 pub recycle_bandwidth_profiles: Vec<RecycleBandwidthProfile>,
155 pub system_profile: Option<PfeSystemProfile>,
156 pub firewall: Option<PfeFirewall>,
157 pub hw_resource_monitor: Option<HwResourceMonitor>,
158 pub counter_profiles: Vec<CounterProfile>,
159 pub hw_profiles: Vec<HwProfile>,
160 pub hw_profile: Option<String>,
162 pub internal_forwarding_bandwidth: Option<String>,
164 pub internal_forwarding_reserve_ingress_egress_mcast: Option<String>,
166 pub ipfix_sw_mode: bool,
167 pub no_ip_tos_rewrite: bool,
168 pub multicast_statistics: bool,
169 pub egress_l2_extended_match: bool,
171}
172
173fn parse_firewall(firewall: &ConfigLine) -> PfeFirewall {
174 let switching = child_block(firewall, "profiles").and_then(|profiles| child_block(profiles, "ethernet-switching"));
175 PfeFirewall {
176 eracl_scale: child_block(firewall, "eracl-profile").is_some_and(|profile| has_leaf(profile, "eracl-scale")),
177 force_fbf_terms: has_leaf(firewall, "force-fbf-terms"),
178 ethernet_switching_profiles: switching
179 .map(|s| {
180 active_children(s)
181 .filter(|c| c.text != "egress")
182 .map(|c| c.text.clone())
183 .collect()
184 })
185 .unwrap_or_default(),
186 ethernet_switching_egress_profiles: switching
187 .and_then(|s| child_block(s, "egress"))
188 .map(|egress| active_children(egress).map(|c| c.text.clone()).collect())
189 .unwrap_or_default(),
190 }
191}
192
193fn parse_hw_resource_monitor(monitor: &ConfigLine) -> HwResourceMonitor {
194 HwResourceMonitor {
195 polling_interval: arg_of(monitor, "polling-interval").map(str::to_string),
196 monitor_profiles: named_children(monitor, "monitor-profile")
197 .map(|(name, profile)| HwMonitorProfile {
198 name: name.to_string(),
199 lower_threshold: arg_of(profile, "lower-threshold").map(str::to_string),
200 notification_type: arg_of(profile, "notification-type").map(str::to_string),
201 upper_threshold: arg_of(profile, "upper-threshold").map(str::to_string),
202 })
203 .collect(),
204 resource_lists: named_children(monitor, "resource-list")
205 .map(|(name, list)| HwResourceList {
206 name: name.to_string(),
207 resource_names: args_of(list, "resource-names")
208 .into_iter()
209 .flat_map(list_values)
210 .collect(),
211 })
212 .collect(),
213 }
214}
215
216fn entry_words(entry: &ConfigLine) -> Vec<&str> {
218 entry
219 .text
220 .split_whitespace()
221 .chain(active_children(entry).flat_map(|c| c.text.split_whitespace()))
222 .collect()
223}
224
225pub(super) fn parse_packet_forwarding_options(options: &ConfigLine) -> PacketForwardingOptions {
226 let overlay = child_block(options, "overlay");
227 let mpls = child_block(options, "mpls");
228 let profile = child_block(options, "system-profile");
229 let custom = child_block(options, "custom-profiles");
230 let bandwidth = child_block(options, "internal-forwarding-bandwidth");
231 PacketForwardingOptions {
232 eracl_ip6_match: statements(options, "eracl-ip6-match").first().map(|s| s.to_string()),
233 flow_export_timer: arg_of(options, "flow-export-timer").map(str::to_string),
234 hw_db_profile: statements(options, "hw-db-profile").first().map(|s| s.to_string()),
235 mcast_stats_enable: child_block(options, "mcast").is_some_and(|mcast| has_leaf(mcast, "stats-enable")),
236 mcast_repl_type: statements(options, "mcast-repl-type").first().map(|s| s.to_string()),
237 mef_forwarding_profile: has_leaf(options, "mef-forwarding-profile"),
238 mpls_lsp_ingress_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-ingress-stats-enable")),
239 mpls_lsp_transit_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-transit-stats-enable")),
240 overlay_interface_num: overlay.and_then(|o| arg_of(o, "interface-num")).map(str::to_string),
241 overlay_next_hop: overlay.and_then(|o| arg_of(o, "next-hop")).map(str::to_string),
242 packet_format_match: statements(options, "packet-format-match")
243 .first()
244 .map(|s| s.to_string()),
245 recycle_bandwidth_profiles: named_children(options, "recycle-bandwidth-profiles")
246 .map(|(name, profile)| RecycleBandwidthProfile {
247 name: name.to_string(),
248 applications: active_children(profile)
249 .filter_map(|c| {
250 let (application, percentage) = c.text.split_once(char::is_whitespace)?;
251 Some((application.to_string(), percentage.trim().to_string()))
252 })
253 .collect(),
254 })
255 .collect(),
256 system_profile: profile.map(|profile| PfeSystemProfile {
257 vxlan_extended: has_leaf(profile, "vxlan-extended"),
258 vxlan_stitching: child_block(profile, "vxlan-stitching").is_some(),
259 vxlan_stitching_control_word: child_block(profile, "vxlan-stitching")
260 .is_some_and(|stitching| has_leaf(stitching, "control-word")),
261 }),
262 firewall: child_block(options, "firewall").map(parse_firewall),
263 hw_resource_monitor: child_block(options, "hw-resource-monitor").map(parse_hw_resource_monitor),
264 counter_profiles: custom
265 .map(|custom| {
266 named_children(custom, "counter-profiles")
267 .map(|(name, profile)| CounterProfile {
268 name: name.to_string(),
269 apps: child_block(profile, "app")
270 .map(|app| {
271 active_children(app)
272 .map(|entry| {
273 let words = entry_words(entry);
274 CounterProfileApp {
275 name: words.first().unwrap_or(&"").to_string(),
276 counter_4k: value_after(&words, "counter-4k"),
277 counter_8k: value_after(&words, "counter-8k"),
278 counter_16k: value_after(&words, "counter-16k"),
279 }
280 })
281 .collect()
282 })
283 .unwrap_or_default(),
284 })
285 .collect()
286 })
287 .unwrap_or_default(),
288 hw_profiles: custom
289 .map(|custom| {
290 named_children(custom, "hw-profiles")
291 .map(|(name, profile)| HwProfile {
292 name: name.to_string(),
293 counter_profile: arg_of(profile, "counter-profile").map(str::to_string),
294 })
295 .collect()
296 })
297 .unwrap_or_default(),
298 hw_profile: arg_of(options, "hw-profile").map(str::to_string),
299 internal_forwarding_bandwidth: arg_of(options, "internal-forwarding-bandwidth").map(str::to_string),
300 internal_forwarding_reserve_ingress_egress_mcast: bandwidth
301 .and_then(|b| arg_of(b, "reserve"))
302 .and_then(|reserve| value_after(&reserve.split_whitespace().collect::<Vec<_>>(), "ingress-egress-mcast")),
303 ipfix_sw_mode: has_leaf(options, "ipfix-sw-mode"),
304 no_ip_tos_rewrite: has_leaf(options, "no-ip-tos-rewrite"),
305 multicast_statistics: has_leaf(options, "multicast-statistics"),
306 egress_l2_extended_match: child_block(options, "firewall-profile")
307 .is_some_and(|profile| has_leaf(profile, "egress-l2-extended-match")),
308 }
309}
310
311#[cfg(test)]
312mod tests {
313 use crate::system::system;
314 use asterust::config::juniper;
315
316 #[test]
318 fn extracts_the_packet_forwarding_options() {
319 let options = system(&juniper::parse_braces(
320 r#"
321system {
322 packet-forwarding-options {
323 eracl-ip6-match {
324 srcip6-only;
325 }
326 flow-export-timer 30;
327 hw-db-profile {
328 cloud-metro;
329 }
330 mcast {
331 stats-enable;
332 }
333 mcast-repl-type {
334 ingress-only;
335 }
336 mef-forwarding-profile;
337 mpls {
338 lsp-ingress-stats-enable;
339 }
340 overlay {
341 interface-num 2048;
342 next-hop 8192;
343 }
344 packet-format-match {
345 mpls-packet-format-3;
346 }
347 recycle-bandwidth-profiles lab {
348 tunnel-services 20;
349 srv6 30;
350 }
351 system-profile {
352 vxlan-extended;
353 vxlan-stitching {
354 control-word;
355 }
356 }
357 firewall {
358 eracl-profile {
359 eracl-scale;
360 }
361 profiles {
362 ethernet-switching {
363 profile1;
364 udf-profile1;
365 egress {
366 profile1;
367 }
368 }
369 }
370 }
371 hw-resource-monitor {
372 polling-interval 5000;
373 monitor-profile alarms {
374 lower-threshold 60;
375 notification-type syslog;
376 upper-threshold 90;
377 }
378 resource-list lpm {
379 resource-names [ lpm-ipv4 lpm-ipv6-64 ];
380 }
381 }
382 }
383}
384"#,
385 ))
386 .unwrap()
387 .packet_forwarding_options
388 .unwrap();
389 assert_eq!(options.eracl_ip6_match.as_deref(), Some("srcip6-only"));
390 assert_eq!(options.flow_export_timer.as_deref(), Some("30"));
391 assert_eq!(options.hw_db_profile.as_deref(), Some("cloud-metro"));
392 assert!(options.mcast_stats_enable);
393 assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-only"));
394 assert!(options.mef_forwarding_profile);
395 assert!(options.mpls_lsp_ingress_stats_enable);
396 assert!(!options.mpls_lsp_transit_stats_enable);
397 assert_eq!(options.overlay_interface_num.as_deref(), Some("2048"));
398 assert_eq!(options.overlay_next_hop.as_deref(), Some("8192"));
399 assert_eq!(options.packet_format_match.as_deref(), Some("mpls-packet-format-3"));
400 assert_eq!(options.recycle_bandwidth_profiles[0].name, "lab");
401 assert_eq!(
402 options.recycle_bandwidth_profiles[0].applications,
403 [
404 ("tunnel-services".to_string(), "20".to_string()),
405 ("srv6".to_string(), "30".to_string())
406 ]
407 );
408 let profile = options.system_profile.unwrap();
409 assert!(profile.vxlan_extended);
410 assert!(profile.vxlan_stitching);
411 assert!(profile.vxlan_stitching_control_word);
412
413 let firewall = options.firewall.unwrap();
414 assert!(firewall.eracl_scale);
415 assert_eq!(firewall.ethernet_switching_profiles, ["profile1", "udf-profile1"]);
416 assert_eq!(firewall.ethernet_switching_egress_profiles, ["profile1"]);
417
418 let monitor = options.hw_resource_monitor.unwrap();
419 assert_eq!(monitor.polling_interval.as_deref(), Some("5000"));
420 assert_eq!(monitor.monitor_profiles[0].name, "alarms");
421 assert_eq!(monitor.monitor_profiles[0].lower_threshold.as_deref(), Some("60"));
422 assert_eq!(monitor.monitor_profiles[0].notification_type.as_deref(), Some("syslog"));
423 assert_eq!(monitor.monitor_profiles[0].upper_threshold.as_deref(), Some("90"));
424 assert_eq!(monitor.resource_lists[0].name, "lpm");
425 assert_eq!(monitor.resource_lists[0].resource_names, ["lpm-ipv4", "lpm-ipv6-64"]);
426 }
427
428 #[test]
429 fn extracts_the_custom_profiles_and_other_statements() {
430 let options = system(&juniper::parse_braces(
431 r#"
432system {
433 packet-forwarding-options {
434 custom-profiles {
435 counter-profiles flows {
436 app {
437 acl {
438 counter-4k 2;
439 counter-8k 1;
440 }
441 telemetry {
442 counter-16k 4;
443 }
444 }
445 }
446 hw-profiles edge {
447 counter-profile flows;
448 }
449 }
450 hw-profile edge;
451 internal-forwarding-bandwidth {
452 reserve ingress-egress-mcast 40;
453 }
454 ipfix-sw-mode;
455 no-ip-tos-rewrite;
456 multicast-statistics;
457 firewall {
458 force-fbf-terms;
459 }
460 firewall-profile {
461 egress-l2-extended-match;
462 }
463 }
464}
465"#,
466 ))
467 .unwrap()
468 .packet_forwarding_options
469 .unwrap();
470 assert_eq!(options.counter_profiles[0].name, "flows");
471 let acl = &options.counter_profiles[0].apps[0];
472 assert_eq!(acl.name, "acl");
473 assert_eq!(acl.counter_4k.as_deref(), Some("2"));
474 assert_eq!(acl.counter_8k.as_deref(), Some("1"));
475 assert_eq!(acl.counter_16k, None);
476 assert_eq!(options.counter_profiles[0].apps[1].counter_16k.as_deref(), Some("4"));
477 assert_eq!(options.hw_profiles[0].name, "edge");
478 assert_eq!(options.hw_profiles[0].counter_profile.as_deref(), Some("flows"));
479 assert_eq!(options.hw_profile.as_deref(), Some("edge"));
480 assert_eq!(options.internal_forwarding_bandwidth, None);
481 assert_eq!(
482 options.internal_forwarding_reserve_ingress_egress_mcast.as_deref(),
483 Some("40")
484 );
485 assert!(options.ipfix_sw_mode);
486 assert!(options.no_ip_tos_rewrite);
487 assert!(options.multicast_statistics);
488 assert!(options.firewall.unwrap().force_fbf_terms);
489 assert!(options.egress_l2_extended_match);
490 }
491
492 #[test]
493 fn reads_the_inline_internal_forwarding_bandwidth() {
494 let options = system(&juniper::parse_braces(
495 "system {\n packet-forwarding-options {\n internal-forwarding-bandwidth 50g;\n }\n}\n",
496 ))
497 .unwrap()
498 .packet_forwarding_options
499 .unwrap();
500 assert_eq!(options.internal_forwarding_bandwidth.as_deref(), Some("50g"));
501 assert_eq!(options.internal_forwarding_reserve_ingress_egress_mcast, None);
502 assert!(options.counter_profiles.is_empty());
503 }
504
505 #[test]
506 fn accepts_the_inline_forms() {
507 let options = system(&juniper::parse_braces(
508 "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",
509 ))
510 .unwrap()
511 .packet_forwarding_options
512 .unwrap();
513 assert_eq!(options.hw_db_profile.as_deref(), Some("bng"));
514 assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-egress-recycle"));
515 assert!(options.system_profile.is_none());
516 assert!(options.firewall.is_none());
517 }
518
519 #[test]
520 fn no_packet_forwarding_options_is_none() {
521 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
522 assert!(system.packet_forwarding_options.is_none());
523 }
524}