Skip to main content

asterust_junos/interfaces/unit/family/
mod.rs

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