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//! - [no-redirects](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-redirects-edit-system.html)
8//! - [no-redirects-ipv6](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-redirects-ipv6.html)
9//! - [core-facing](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/core-facing-edit-interfaces.html)
10//! - [receive-options-packets](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/receive-options-packets-edit-interfaces-ni.html)
11//! - [receive-ttl-exceeded](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/receive-ttl-exceeded-edit-interfaces-ni.html)
12//! - [multicast-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multicast-only-edit-interfaces.html)
13//! - [unconditional-src-learn](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unconditional-src-learn-edit-interfaces.html)
14//! - [tunnel-loopback](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tunnel-loopback-edit-interfaces-unit-family-inet.html)
15//! - [mac-validate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-validate-edit-interfaces.html)
16//! - [tcp-mss (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tcp-mss-edit-interfaces-unit-family.html)
17//! - [arp-max-cache](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/arp-max-cache-edit-system.html)
18//! - [dad-proxy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-family-inet6-dad-proxy.html)
19//! - [ndp-proxy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-family-inet6-ndp-proxy.html)
20//! - [proxy (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/proxy-edit-interfaces.html)
21//! - [mtu (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-edit-interfaces-ni.html)
22//! - [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)
23//! - [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)
24
25mod accounting;
26mod address;
27mod dhcp;
28mod ethernet_switching;
29mod filter;
30mod next_hop_tunnel;
31mod policer;
32mod rpf_check;
33mod sampling;
34
35pub use accounting::{FamilyAccounting, TargetedBroadcast, UnnumberedAddress, VlanTranslation};
36pub use address::{
37    AddressArp, BandwidthThreshold, TrackedInterface, TrackedRoute, UnitAddress, VrrpGroup, VrrpInheritFrom, VrrpTrack,
38};
39pub use dhcp::{DhcpClient, DhcpClientIdentifier, Dhcpv6Client, PrefixDelegating, RouterAdvertisementInterface};
40pub use ethernet_switching::EthernetSwitching;
41pub use filter::FamilyFilter;
42pub use next_hop_tunnel::NextHopTunnel;
43pub use policer::FamilyPolicer;
44pub use rpf_check::RpfCheck;
45pub use sampling::FamilySampling;
46
47use super::demux_options::demux_prefixes;
48use super::pppoe_underlying_options::{parse_pppoe_underlying_options, PppoeUnderlyingOptions};
49use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
50use accounting::{parse_accounting, parse_targeted_broadcast, parse_unnumbered_address, parse_vlan_rewrite};
51use address::parse_addresses;
52use asterust::config::ConfigLine;
53use dhcp::{parse_dhcp_client, parse_dhcpv6_client};
54use ethernet_switching::parse_ethernet_switching;
55use filter::parse_filter;
56use next_hop_tunnel::parse_next_hop_tunnels;
57use policer::parse_policer;
58use rpf_check::parse_rpf_check;
59use sampling::parse_sampling;
60
61/// A `family <name>` of a logical unit, with the settings that apply to it.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct UnitFamily {
65    /// e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
66    pub name: String,
67    pub mtu: Option<String>,
68    pub filter: Option<FamilyFilter>,
69    pub rpf_check: Option<RpfCheck>,
70    pub policer: Option<FamilyPolicer>,
71    pub sampling: Option<FamilySampling>,
72    /// `vlan-id` of `family bridge`.
73    pub vlan_id: Option<String>,
74    /// `vlan-id-list` of `family bridge`: IDs and `<from>-<to>` ranges.
75    pub vlan_id_list: Vec<String>,
76    pub addresses: Vec<UnitAddress>,
77    /// `destination-udp-port` of a family on a UDP tunnel such as `fti`.
78    pub destination_udp_port: Option<String>,
79    /// The port settings of `family ethernet-switching`; `None` for other families.
80    pub ethernet_switching: Option<EthernetSwitching>,
81    /// The `nd6-max-cache` of `family inet6`.
82    pub nd6_max_cache: Option<String>,
83    /// Seconds; the `nd6-stale-time` of `family inet6`.
84    pub nd6_stale_time: Option<String>,
85    /// The `nd6-new-hold-limit` of `family inet6`.
86    pub nd6_new_hold_limit: Option<String>,
87    /// The settings of `family pppoe`, which are those of `pppoe-underlying-options`.
88    pub pppoe: Option<PppoeUnderlyingOptions>,
89    /// The prefixes of `demux-source`.
90    pub demux_source: Vec<String>,
91    /// The prefixes of `demux-destination`.
92    pub demux_destination: Vec<String>,
93    /// The `dhcp` block of `family inet`.
94    pub dhcp: Option<DhcpClient>,
95    pub dhcp_client: Option<DhcpClient>,
96    pub dhcpv6_client: Option<Dhcpv6Client>,
97    pub no_redirects: bool,
98    pub no_redirects_ipv6: bool,
99    pub core_facing: bool,
100    pub receive_options_packets: bool,
101    pub receive_ttl_exceeded: bool,
102    pub multicast_only: bool,
103    pub unconditional_src_learn: bool,
104    pub tunnel_loopback: bool,
105    /// `loose` or `strict`.
106    pub mac_validate: Option<String>,
107    pub tcp_mss: Option<String>,
108    pub arp_max_cache: Option<String>,
109    /// `interface-restricted` or `interface-unrestricted`, from `dad-proxy`.
110    pub dad_proxy: Option<String>,
111    /// `interface-restricted` or `interface-unrestricted`, from `ndp-proxy`.
112    pub ndp_proxy: Option<String>,
113    /// The `inet-address` of `proxy` in `family tcc`.
114    pub proxy_inet_address: Option<String>,
115    pub unnumbered_address: Option<UnnumberedAddress>,
116    pub accounting: Option<FamilyAccounting>,
117    pub targeted_broadcast: Option<TargetedBroadcast>,
118    pub vlan_rewrite: Vec<VlanTranslation>,
119    pub next_hop_tunnel: Vec<NextHopTunnel>,
120}
121
122/// The mode of `dad-proxy` or `ndp-proxy`, written inside a block or after the keyword.
123fn proxy_mode(family: &ConfigLine, keyword: &str) -> Option<String> {
124    statement_words(family, keyword)?.first().map(|mode| mode.to_string())
125}
126
127pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
128    UnitFamily {
129        name: name.to_string(),
130        mtu: arg_of(family, "mtu").map(str::to_string),
131        filter: parse_filter(family),
132        rpf_check: parse_rpf_check(family),
133        policer: parse_policer(family),
134        sampling: parse_sampling(family),
135        vlan_id: arg_of(family, "vlan-id").map(str::to_string),
136        vlan_id_list: args_of(family, "vlan-id-list")
137            .into_iter()
138            .flat_map(list_values)
139            .collect(),
140        addresses: parse_addresses(family),
141        destination_udp_port: arg_of(family, "destination-udp-port").map(str::to_string),
142        ethernet_switching: (name == "ethernet-switching").then(|| parse_ethernet_switching(family)),
143        nd6_max_cache: arg_of(family, "nd6-max-cache").map(str::to_string),
144        nd6_stale_time: arg_of(family, "nd6-stale-time").map(str::to_string),
145        nd6_new_hold_limit: arg_of(family, "nd6-new-hold-limit").map(str::to_string),
146        pppoe: (name == "pppoe").then(|| parse_pppoe_underlying_options(family)),
147        demux_source: demux_prefixes(family, "demux-source"),
148        demux_destination: demux_prefixes(family, "demux-destination"),
149        dhcp: child_block(family, "dhcp").map(parse_dhcp_client),
150        dhcp_client: child_block(family, "dhcp-client").map(parse_dhcp_client),
151        dhcpv6_client: child_block(family, "dhcpv6-client").map(parse_dhcpv6_client),
152        no_redirects: has_leaf(family, "no-redirects"),
153        no_redirects_ipv6: has_leaf(family, "no-redirects-ipv6"),
154        core_facing: has_leaf(family, "core-facing"),
155        receive_options_packets: has_leaf(family, "receive-options-packets"),
156        receive_ttl_exceeded: has_leaf(family, "receive-ttl-exceeded"),
157        multicast_only: has_leaf(family, "multicast-only"),
158        unconditional_src_learn: has_leaf(family, "unconditional-src-learn"),
159        tunnel_loopback: has_leaf(family, "tunnel-loopback"),
160        mac_validate: arg_of(family, "mac-validate").map(str::to_string),
161        tcp_mss: arg_of(family, "tcp-mss").map(str::to_string),
162        arp_max_cache: arg_of(family, "arp-max-cache").map(str::to_string),
163        dad_proxy: proxy_mode(family, "dad-proxy"),
164        ndp_proxy: proxy_mode(family, "ndp-proxy"),
165        proxy_inet_address: statement_words(family, "proxy").and_then(|words| value_after(&words, "inet-address")),
166        unnumbered_address: parse_unnumbered_address(family),
167        accounting: child_block(family, "accounting").map(parse_accounting),
168        targeted_broadcast: child_block(family, "targeted-broadcast").map(parse_targeted_broadcast),
169        vlan_rewrite: parse_vlan_rewrite(family),
170        next_hop_tunnel: parse_next_hop_tunnels(family),
171    }
172}
173
174#[cfg(test)]
175mod tests {
176    use crate::interfaces::interfaces;
177    use asterust::config::juniper;
178
179    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
180    const CONFIG: &str = r#"
181interfaces {
182    ge-0/0/0 {
183        unit 0 {
184            family inet {
185                mtu 1500;
186                filter {
187                    group 5;
188                    input PROTECT-IN;
189                    output PROTECT-OUT;
190                    input-list [ A B ];
191                    output-list C;
192                }
193                rpf-check {
194                    fail-filter RPF-FAIL;
195                    mode loose;
196                }
197                policer {
198                    arp ARP-POLICER;
199                    input IN-POLICER;
200                    output OUT-POLICER;
201                }
202                sampling {
203                    input;
204                    output;
205                }
206                address 192.0.2.1/24 {
207                    primary;
208                    preferred;
209                    destination 192.0.2.2;
210                    broadcast 192.0.2.255;
211                    arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
212                    arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
213                }
214                address 192.0.2.9/24;
215            }
216            family inet6 {
217                rpf-check;
218                sampling input;
219                address 2001:db8::1/64 {
220                    eui-64;
221                }
222            }
223            family iso;
224        }
225    }
226}
227"#;
228
229    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
230    #[test]
231    fn extracts_more_family_statements() {
232        let ifaces = interfaces(&juniper::parse_braces(
233            r#"
234interfaces {
235    ge-0/0/0 {
236        unit 0 {
237            family inet {
238                no-redirects;
239                core-facing;
240                receive-options-packets;
241                receive-ttl-exceeded;
242                multicast-only;
243                unconditional-src-learn;
244                tunnel-loopback;
245                mac-validate strict;
246                tcp-mss 1400;
247                arp-max-cache 1000;
248                unnumbered-address lo0.0 preferred-source-address 10.0.0.1;
249                accounting {
250                    destination-class-usage;
251                    source-class-usage {
252                        input;
253                        output;
254                    }
255                }
256                targeted-broadcast {
257                    forward-and-send-to-re;
258                }
259            }
260            family inet6 {
261                no-redirects-ipv6;
262                tcp-mss 1300;
263                dad-proxy {
264                    interface-restricted;
265                }
266                ndp-proxy {
267                    interface-unrestricted;
268                }
269                unnumbered-address lo0.1;
270            }
271        }
272        unit 1 {
273            family tcc {
274                proxy inet-address 192.0.2.9;
275            }
276            family bridge {
277                vlan-rewrite {
278                    translate 100 200;
279                    translate 300 400;
280                }
281            }
282            family ethernet-switching {
283                interface-mode trunk;
284                inter-switch-link vlan members primary-vlan;
285            }
286        }
287        unit 2 {
288            family inet {
289                unnumbered-address ge-0/0/1.0 destination 192.0.2.1 destination-profile prof1;
290                accounting {
291                    source-class-usage output;
292                }
293                targeted-broadcast {
294                    forward-only;
295                }
296            }
297        }
298        unit 3 {
299            family inet {
300                mtu 1500;
301            }
302        }
303    }
304}
305"#,
306        ));
307        let units = &ifaces[0].units;
308        let inet = &units[0].family_settings[0];
309        assert!(inet.no_redirects && inet.core_facing && inet.receive_options_packets && inet.receive_ttl_exceeded);
310        assert!(inet.multicast_only && inet.unconditional_src_learn && inet.tunnel_loopback);
311        assert_eq!(inet.mac_validate.as_deref(), Some("strict"));
312        assert_eq!(inet.tcp_mss.as_deref(), Some("1400"));
313        assert_eq!(inet.arp_max_cache.as_deref(), Some("1000"));
314        let unnumbered = inet.unnumbered_address.as_ref().unwrap();
315        assert_eq!(unnumbered.interface, "lo0.0");
316        assert_eq!(unnumbered.preferred_source_address.as_deref(), Some("10.0.0.1"));
317        assert!(unnumbered.destination.is_none() && unnumbered.destination_profile.is_none());
318        let accounting = inet.accounting.as_ref().unwrap();
319        assert!(accounting.destination_class_usage && accounting.source_class_usage);
320        assert_eq!(accounting.source_class_usage_directions, ["input", "output"]);
321        let broadcast = inet.targeted_broadcast.as_ref().unwrap();
322        assert!(broadcast.forward_and_send_to_re && !broadcast.forward_only);
323
324        let inet6 = &units[0].family_settings[1];
325        assert!(inet6.no_redirects_ipv6 && !inet6.no_redirects);
326        assert_eq!(inet6.tcp_mss.as_deref(), Some("1300"));
327        assert_eq!(inet6.dad_proxy.as_deref(), Some("interface-restricted"));
328        assert_eq!(inet6.ndp_proxy.as_deref(), Some("interface-unrestricted"));
329        assert_eq!(inet6.unnumbered_address.as_ref().unwrap().interface, "lo0.1");
330
331        assert_eq!(
332            units[1].family_settings[0].proxy_inet_address.as_deref(),
333            Some("192.0.2.9")
334        );
335        let rewrite = &units[1].family_settings[1].vlan_rewrite;
336        assert_eq!(rewrite.len(), 2);
337        assert_eq!((rewrite[1].from.as_str(), rewrite[1].to.as_str()), ("300", "400"));
338        let switching = units[1].family_settings[2].ethernet_switching.as_ref().unwrap();
339        assert_eq!(switching.inter_switch_link_vlan.as_deref(), Some("primary-vlan"));
340
341        let ppp = units[2].family_settings[0].unnumbered_address.as_ref().unwrap();
342        assert_eq!(ppp.interface, "ge-0/0/1.0");
343        assert_eq!(ppp.destination.as_deref(), Some("192.0.2.1"));
344        assert_eq!(ppp.destination_profile.as_deref(), Some("prof1"));
345        let source = units[2].family_settings[0].accounting.as_ref().unwrap();
346        assert_eq!(source.source_class_usage_directions, ["output"]);
347        assert!(
348            units[2].family_settings[0]
349                .targeted_broadcast
350                .as_ref()
351                .unwrap()
352                .forward_only
353        );
354
355        let plain = &units[3].family_settings[0];
356        assert!(!plain.no_redirects && !plain.multicast_only && plain.mac_validate.is_none());
357        assert!(plain.unnumbered_address.is_none() && plain.accounting.is_none() && plain.targeted_broadcast.is_none());
358        assert!(plain.vlan_rewrite.is_empty() && plain.dad_proxy.is_none() && plain.proxy_inet_address.is_none());
359    }
360
361    #[test]
362    fn extracts_family_filters_policers_and_sampling() {
363        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
364        let inet = &ifaces[0].units[0].family_settings[0];
365        assert_eq!(inet.name, "inet");
366        assert_eq!(inet.mtu.as_deref(), Some("1500"));
367
368        let filter = inet.filter.as_ref().unwrap();
369        assert_eq!(filter.group.as_deref(), Some("5"));
370        assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
371        assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
372        assert_eq!(filter.input_list, ["A", "B"]);
373        assert_eq!(filter.output_list, ["C"]);
374
375        let policer = inet.policer.as_ref().unwrap();
376        assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
377        assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
378        assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));
379
380        let sampling = inet.sampling.as_ref().unwrap();
381        assert!(sampling.input);
382        assert!(sampling.output);
383    }
384
385    #[test]
386    fn extracts_rpf_check_in_block_and_flag_form() {
387        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
388        let families = &ifaces[0].units[0].family_settings;
389
390        let rpf = families[0].rpf_check.as_ref().unwrap();
391        assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
392        assert_eq!(rpf.mode.as_deref(), Some("loose"));
393
394        let bare = families[1].rpf_check.as_ref().unwrap();
395        assert_eq!(bare.fail_filter, None);
396        assert_eq!(bare.mode, None);
397    }
398
399    #[test]
400    fn extracts_address_options_and_arp_entries() {
401        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
402        let addresses = &ifaces[0].units[0].family_settings[0].addresses;
403        assert_eq!(addresses.len(), 2);
404
405        let first = &addresses[0];
406        assert_eq!(first.address, "192.0.2.1/24");
407        assert!(first.primary);
408        assert!(first.preferred);
409        assert!(!first.master_only);
410        assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
411        assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
412        assert_eq!(first.arps.len(), 2);
413        assert_eq!(first.arps[0].address, "192.0.2.50");
414        assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
415        assert!(first.arps[0].publish);
416        assert!(!first.arps[0].multicast_mac);
417        assert!(first.arps[1].multicast_mac);
418        assert!(!first.arps[1].publish);
419
420        let second = &addresses[1];
421        assert_eq!(second.address, "192.0.2.9/24");
422        assert!(!second.primary);
423        assert!(second.arps.is_empty());
424    }
425
426    #[test]
427    fn extracts_inline_sampling_and_eui_64() {
428        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
429        let inet6 = &ifaces[0].units[0].family_settings[1];
430        let sampling = inet6.sampling.as_ref().unwrap();
431        assert!(sampling.input);
432        assert!(!sampling.output);
433        assert!(inet6.addresses[0].eui_64);
434    }
435
436    #[test]
437    fn family_without_settings_has_none() {
438        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
439        let iso = &ifaces[0].units[0].family_settings[2];
440        assert_eq!(iso.name, "iso");
441        assert!(iso.mtu.is_none());
442        assert!(iso.filter.is_none());
443        assert!(iso.rpf_check.is_none());
444        assert!(iso.policer.is_none());
445        assert!(iso.sampling.is_none());
446        assert!(iso.addresses.is_empty());
447    }
448}