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