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