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    /// The `nd6-max-cache` of `family inet6`.
57    pub nd6_max_cache: Option<String>,
58    /// Seconds; the `nd6-stale-time` of `family inet6`.
59    pub nd6_stale_time: Option<String>,
60    /// The `nd6-new-hold-limit` of `family inet6`.
61    pub nd6_new_hold_limit: Option<String>,
62}
63
64pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
65    UnitFamily {
66        name: name.to_string(),
67        mtu: arg_of(family, "mtu").map(str::to_string),
68        filter: parse_filter(family),
69        rpf_check: parse_rpf_check(family),
70        policer: parse_policer(family),
71        sampling: parse_sampling(family),
72        vlan_id: arg_of(family, "vlan-id").map(str::to_string),
73        vlan_id_list: args_of(family, "vlan-id-list")
74            .into_iter()
75            .flat_map(list_values)
76            .collect(),
77        addresses: parse_addresses(family),
78        destination_udp_port: arg_of(family, "destination-udp-port").map(str::to_string),
79        ethernet_switching: (name == "ethernet-switching").then(|| parse_ethernet_switching(family)),
80        nd6_max_cache: arg_of(family, "nd6-max-cache").map(str::to_string),
81        nd6_stale_time: arg_of(family, "nd6-stale-time").map(str::to_string),
82        nd6_new_hold_limit: arg_of(family, "nd6-new-hold-limit").map(str::to_string),
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use crate::interfaces::interfaces;
89    use asterust::config::juniper;
90
91    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
92    const CONFIG: &str = r#"
93interfaces {
94    ge-0/0/0 {
95        unit 0 {
96            family inet {
97                mtu 1500;
98                filter {
99                    group 5;
100                    input PROTECT-IN;
101                    output PROTECT-OUT;
102                    input-list [ A B ];
103                    output-list C;
104                }
105                rpf-check {
106                    fail-filter RPF-FAIL;
107                    mode loose;
108                }
109                policer {
110                    arp ARP-POLICER;
111                    input IN-POLICER;
112                    output OUT-POLICER;
113                }
114                sampling {
115                    input;
116                    output;
117                }
118                address 192.0.2.1/24 {
119                    primary;
120                    preferred;
121                    destination 192.0.2.2;
122                    broadcast 192.0.2.255;
123                    arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
124                    arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
125                }
126                address 192.0.2.9/24;
127            }
128            family inet6 {
129                rpf-check;
130                sampling input;
131                address 2001:db8::1/64 {
132                    eui-64;
133                }
134            }
135            family iso;
136        }
137    }
138}
139"#;
140
141    #[test]
142    fn extracts_family_filters_policers_and_sampling() {
143        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
144        let inet = &ifaces[0].units[0].family_settings[0];
145        assert_eq!(inet.name, "inet");
146        assert_eq!(inet.mtu.as_deref(), Some("1500"));
147
148        let filter = inet.filter.as_ref().unwrap();
149        assert_eq!(filter.group.as_deref(), Some("5"));
150        assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
151        assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
152        assert_eq!(filter.input_list, ["A", "B"]);
153        assert_eq!(filter.output_list, ["C"]);
154
155        let policer = inet.policer.as_ref().unwrap();
156        assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
157        assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
158        assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));
159
160        let sampling = inet.sampling.as_ref().unwrap();
161        assert!(sampling.input);
162        assert!(sampling.output);
163    }
164
165    #[test]
166    fn extracts_rpf_check_in_block_and_flag_form() {
167        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
168        let families = &ifaces[0].units[0].family_settings;
169
170        let rpf = families[0].rpf_check.as_ref().unwrap();
171        assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
172        assert_eq!(rpf.mode.as_deref(), Some("loose"));
173
174        let bare = families[1].rpf_check.as_ref().unwrap();
175        assert_eq!(bare.fail_filter, None);
176        assert_eq!(bare.mode, None);
177    }
178
179    #[test]
180    fn extracts_address_options_and_arp_entries() {
181        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
182        let addresses = &ifaces[0].units[0].family_settings[0].addresses;
183        assert_eq!(addresses.len(), 2);
184
185        let first = &addresses[0];
186        assert_eq!(first.address, "192.0.2.1/24");
187        assert!(first.primary);
188        assert!(first.preferred);
189        assert!(!first.master_only);
190        assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
191        assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
192        assert_eq!(first.arps.len(), 2);
193        assert_eq!(first.arps[0].address, "192.0.2.50");
194        assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
195        assert!(first.arps[0].publish);
196        assert!(!first.arps[0].multicast_mac);
197        assert!(first.arps[1].multicast_mac);
198        assert!(!first.arps[1].publish);
199
200        let second = &addresses[1];
201        assert_eq!(second.address, "192.0.2.9/24");
202        assert!(!second.primary);
203        assert!(second.arps.is_empty());
204    }
205
206    #[test]
207    fn extracts_inline_sampling_and_eui_64() {
208        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
209        let inet6 = &ifaces[0].units[0].family_settings[1];
210        let sampling = inet6.sampling.as_ref().unwrap();
211        assert!(sampling.input);
212        assert!(!sampling.output);
213        assert!(inet6.addresses[0].eui_64);
214    }
215
216    #[test]
217    fn family_without_settings_has_none() {
218        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
219        let iso = &ifaces[0].units[0].family_settings[2];
220        assert_eq!(iso.name, "iso");
221        assert!(iso.mtu.is_none());
222        assert!(iso.filter.is_none());
223        assert!(iso.rpf_check.is_none());
224        assert!(iso.policer.is_none());
225        assert!(iso.sampling.is_none());
226        assert!(iso.addresses.is_empty());
227    }
228}