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