Skip to main content

asterust_junos/interfaces/unit/
accept_source_mac.rs

1//! `accept-source-mac` and `policer-overhead`
2//! `interfaces { <name> { unit <n> { accept-source-mac { ... } policer-overhead { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [accept-source-mac](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-source-mac-edit-interfaces.html)
7//! - [accept-source-mac (SRX)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-accept-source-mac-srx.html)
8//! - [mac-address (Accept Source Mac)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-address-edit-interfaces.html)
9//! - [policer-overhead](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-policer-overhead.html)
10
11use crate::{active_children, arg_of, child_block, split_name};
12use asterust::config::ConfigLine;
13
14/// A `mac-address` accepted as a source by `accept-source-mac`, with its policers.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct AcceptedMac {
18    pub address: String,
19    pub policer_input: Option<String>,
20    pub policer_output: Option<String>,
21}
22
23/// The `policer-overhead` of a unit, in bytes.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct PolicerOverhead {
27    pub egress: Option<String>,
28    pub ingress: Option<String>,
29}
30
31/// The `mac-address` entries of `accept-source-mac`.
32pub(super) fn parse_accept_source_mac(unit: &ConfigLine) -> Vec<AcceptedMac> {
33    let Some(accept) = child_block(unit, "accept-source-mac") else {
34        return Vec::new();
35    };
36    active_children(accept)
37        .filter_map(|entry| {
38            // Written `mac-address <mac> [policer ...]` as a leaf or a block.
39            let (address, _) = split_name(entry.arg("mac-address")?);
40            let policer = child_block(entry, "policer");
41            Some(AcceptedMac {
42                address: address.to_string(),
43                policer_input: policer.and_then(|p| arg_of(p, "input")).map(str::to_string),
44                policer_output: policer.and_then(|p| arg_of(p, "output")).map(str::to_string),
45            })
46        })
47        .collect()
48}
49
50pub(super) fn parse_policer_overhead(overhead: &ConfigLine) -> PolicerOverhead {
51    PolicerOverhead {
52        egress: arg_of(overhead, "egress").map(str::to_string),
53        ingress: arg_of(overhead, "ingress").map(str::to_string),
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::interfaces::interfaces;
60    use asterust::config::juniper;
61
62    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
63    #[test]
64    fn extracts_accept_source_mac_and_policer_overhead() {
65        let ifaces = interfaces(&juniper::parse_braces(
66            r#"
67interfaces {
68    ge-0/0/0 {
69        unit 0 {
70            accept-source-mac {
71                mac-address 00:11:22:33:44:55 {
72                    policer {
73                        input pin;
74                        output pout;
75                    }
76                }
77                mac-address 00:11:22:33:44:66;
78            }
79            policer-overhead {
80                egress 20;
81                ingress 8;
82            }
83        }
84        unit 1 {
85            description plain;
86        }
87    }
88}
89"#,
90        ));
91        let units = &ifaces[0].units;
92        let macs = &units[0].accept_source_mac;
93        assert_eq!(macs.len(), 2);
94        assert_eq!(macs[0].address, "00:11:22:33:44:55");
95        assert_eq!(macs[0].policer_input.as_deref(), Some("pin"));
96        assert_eq!(macs[0].policer_output.as_deref(), Some("pout"));
97        assert_eq!(macs[1].address, "00:11:22:33:44:66");
98        assert!(macs[1].policer_input.is_none());
99        let overhead = units[0].policer_overhead.as_ref().unwrap();
100        assert_eq!(overhead.egress.as_deref(), Some("20"));
101        assert_eq!(overhead.ingress.as_deref(), Some("8"));
102        assert!(units[1].accept_source_mac.is_empty() && units[1].policer_overhead.is_none());
103    }
104}