Skip to main content

asterust_junos/interfaces/unit/family/
mod.rs

1//! `interfaces { <name> { unit <n> { family <name> { ... } } } }`.
2
3mod address;
4mod filter;
5mod policer;
6mod rpf_check;
7mod sampling;
8
9pub use address::{
10    AddressArp, BandwidthThreshold, TrackedInterface, TrackedRoute, UnitAddress, VrrpGroup, VrrpInheritFrom, VrrpTrack,
11};
12pub use filter::FamilyFilter;
13pub use policer::FamilyPolicer;
14pub use rpf_check::RpfCheck;
15pub use sampling::FamilySampling;
16
17use crate::arg_of;
18use address::parse_addresses;
19use asterust::config::ConfigLine;
20use filter::parse_filter;
21use policer::parse_policer;
22use rpf_check::parse_rpf_check;
23use sampling::parse_sampling;
24
25/// A `family <name>` of a logical unit, with the settings that apply to it.
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct UnitFamily {
29    /// e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
30    pub name: String,
31    pub mtu: Option<String>,
32    pub filter: Option<FamilyFilter>,
33    pub rpf_check: Option<RpfCheck>,
34    pub policer: Option<FamilyPolicer>,
35    pub sampling: Option<FamilySampling>,
36    pub addresses: Vec<UnitAddress>,
37}
38
39pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
40    UnitFamily {
41        name: name.to_string(),
42        mtu: arg_of(family, "mtu").map(str::to_string),
43        filter: parse_filter(family),
44        rpf_check: parse_rpf_check(family),
45        policer: parse_policer(family),
46        sampling: parse_sampling(family),
47        addresses: parse_addresses(family),
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::interfaces::interfaces;
54    use asterust::config::juniper;
55
56    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
57    const CONFIG: &str = r#"
58interfaces {
59    ge-0/0/0 {
60        unit 0 {
61            family inet {
62                mtu 1500;
63                filter {
64                    group 5;
65                    input PROTECT-IN;
66                    output PROTECT-OUT;
67                    input-list [ A B ];
68                    output-list C;
69                }
70                rpf-check {
71                    fail-filter RPF-FAIL;
72                    mode loose;
73                }
74                policer {
75                    arp ARP-POLICER;
76                    input IN-POLICER;
77                    output OUT-POLICER;
78                }
79                sampling {
80                    input;
81                    output;
82                }
83                address 192.0.2.1/24 {
84                    primary;
85                    preferred;
86                    destination 192.0.2.2;
87                    broadcast 192.0.2.255;
88                    arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
89                    arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
90                }
91                address 192.0.2.9/24;
92            }
93            family inet6 {
94                rpf-check;
95                sampling input;
96                address 2001:db8::1/64 {
97                    eui-64;
98                }
99            }
100            family iso;
101        }
102    }
103}
104"#;
105
106    #[test]
107    fn extracts_family_filters_policers_and_sampling() {
108        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
109        let inet = &ifaces[0].units[0].family_settings[0];
110        assert_eq!(inet.name, "inet");
111        assert_eq!(inet.mtu.as_deref(), Some("1500"));
112
113        let filter = inet.filter.as_ref().unwrap();
114        assert_eq!(filter.group.as_deref(), Some("5"));
115        assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
116        assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
117        assert_eq!(filter.input_list, ["A", "B"]);
118        assert_eq!(filter.output_list, ["C"]);
119
120        let policer = inet.policer.as_ref().unwrap();
121        assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
122        assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
123        assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));
124
125        let sampling = inet.sampling.as_ref().unwrap();
126        assert!(sampling.input);
127        assert!(sampling.output);
128    }
129
130    #[test]
131    fn extracts_rpf_check_in_block_and_flag_form() {
132        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
133        let families = &ifaces[0].units[0].family_settings;
134
135        let rpf = families[0].rpf_check.as_ref().unwrap();
136        assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
137        assert_eq!(rpf.mode.as_deref(), Some("loose"));
138
139        let bare = families[1].rpf_check.as_ref().unwrap();
140        assert_eq!(bare.fail_filter, None);
141        assert_eq!(bare.mode, None);
142    }
143
144    #[test]
145    fn extracts_address_options_and_arp_entries() {
146        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
147        let addresses = &ifaces[0].units[0].family_settings[0].addresses;
148        assert_eq!(addresses.len(), 2);
149
150        let first = &addresses[0];
151        assert_eq!(first.address, "192.0.2.1/24");
152        assert!(first.primary);
153        assert!(first.preferred);
154        assert!(!first.master_only);
155        assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
156        assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
157        assert_eq!(first.arps.len(), 2);
158        assert_eq!(first.arps[0].address, "192.0.2.50");
159        assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
160        assert!(first.arps[0].publish);
161        assert!(!first.arps[0].multicast_mac);
162        assert!(first.arps[1].multicast_mac);
163        assert!(!first.arps[1].publish);
164
165        let second = &addresses[1];
166        assert_eq!(second.address, "192.0.2.9/24");
167        assert!(!second.primary);
168        assert!(second.arps.is_empty());
169    }
170
171    #[test]
172    fn extracts_inline_sampling_and_eui_64() {
173        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
174        let inet6 = &ifaces[0].units[0].family_settings[1];
175        let sampling = inet6.sampling.as_ref().unwrap();
176        assert!(sampling.input);
177        assert!(!sampling.output);
178        assert!(inet6.addresses[0].eui_64);
179    }
180
181    #[test]
182    fn family_without_settings_has_none() {
183        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
184        let iso = &ifaces[0].units[0].family_settings[2];
185        assert_eq!(iso.name, "iso");
186        assert!(iso.mtu.is_none());
187        assert!(iso.filter.is_none());
188        assert!(iso.rpf_check.is_none());
189        assert!(iso.policer.is_none());
190        assert!(iso.sampling.is_none());
191        assert!(iso.addresses.is_empty());
192    }
193}