Skip to main content

asterust_junos/interfaces/unit/
mod.rs

1//! `unit`
2//! `interfaces { <name> { unit <n> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [unit (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unit-edit-interfaces.html)
7//! - [encapsulation (Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encapsulation-edit-interfaces-logical.html)
8//! - [demux-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-options-edit-interfaces.html)
9//! - [demux (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-edit-interfaces.html)
10//! - [advisory-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advisory-options-edit-interfaces-unit.html)
11//! - [targeted-distribution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-distribution-edit-interfaces.html)
12//! - [pppoe-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-options-edit-interfaces.html)
13//! - [pppoe-underlying-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-underlying-options-edit-interfaces-pppoe.html)
14//! - [ppp-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ppp-options-edit-interfaces.html)
15//! - [vlan-id (VLAN ID to Be Bound to a Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-edit-interfaces.html)
16
17mod demux;
18mod demux_options;
19mod family;
20mod pppoe_options;
21mod pppoe_underlying_options;
22mod tunnel;
23mod vlan_map;
24mod vlan_tags;
25
26pub use demux::{
27    Demux, DemuxAddressRanges, DemuxDynamicProfile, DemuxFamily, DemuxNetwork, DemuxRange, DemuxUsernameInclude,
28};
29pub use demux_options::{AdvisoryOptions, DemuxOptions};
30pub use family::{
31    AddressArp, BandwidthThreshold, DhcpClient, DhcpClientIdentifier, Dhcpv6Client, EthernetSwitching, FamilyFilter,
32    FamilyPolicer, FamilySampling, PrefixDelegating, RouterAdvertisementInterface, RpfCheck, TrackedInterface,
33    TrackedRoute, UnitAddress, UnitFamily, VrrpGroup, VrrpInheritFrom, VrrpTrack,
34};
35pub use pppoe_options::PppoeOptions;
36pub use pppoe_underlying_options::{DuplicateProtection, PppoeUnderlyingOptions, ShortCycleProtection};
37pub use tunnel::UnitTunnel;
38pub use vlan_map::VlanMap;
39pub use vlan_tags::VlanTags;
40
41use super::esi::{parse_esi, Esi};
42use super::ppp_options::{parse_ppp_options, PppOptions};
43use crate::{active_children, arg_of, args_of, child_block, has_leaf, named_children};
44use asterust::config::ConfigLine;
45use demux::parse_demux;
46use demux_options::{parse_advisory_options, parse_demux_options};
47use family::parse_family;
48use pppoe_options::parse_pppoe_options;
49use pppoe_underlying_options::parse_pppoe_underlying_options;
50use tunnel::parse_tunnel;
51use vlan_map::parse_vlan_map;
52use vlan_tags::parse_vlan_tags;
53
54/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g.
55/// `"10.0.0.1/24"`; IS-IS NETs for `family iso`).
56#[derive(Clone, Debug, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct InterfaceUnit {
59    pub number: String,
60    pub description: Option<String>,
61    pub vlan_id: Option<u32>,
62    /// `vlan-id unmatched`.
63    pub vlan_id_unmatched: bool,
64    /// e.g. `"100-200"`.
65    pub vlan_id_range: Option<String>,
66    pub vlan_tags: Option<VlanTags>,
67    pub tunnel: Option<UnitTunnel>,
68    pub esi: Option<Esi>,
69    pub ppp_options: Option<PppOptions>,
70    /// The `pppoe-options` of a `pp0` unit.
71    pub pppoe_options: Option<PppoeOptions>,
72    pub pppoe_underlying_options: Option<PppoeUnderlyingOptions>,
73    pub demux: Option<Demux>,
74    pub demux_options: Option<DemuxOptions>,
75    /// The families of each `demux-source`.
76    pub demux_source: Vec<String>,
77    /// The families of each `demux-destination`.
78    pub demux_destination: Vec<String>,
79    pub advisory_options: Option<AdvisoryOptions>,
80    /// `replicate` or `scale`.
81    pub targeted_distribution: Option<String>,
82    /// `proxy-macip-advertisement` on an IRB unit.
83    pub proxy_macip_advertisement: bool,
84    /// `enhanced-convergence` on an IRB unit.
85    pub enhanced_convergence: bool,
86    pub virtual_gateway_v4_mac: Option<String>,
87    pub virtual_gateway_v6_mac: Option<String>,
88    /// The `virtual-gateway-esi` of an IRB unit.
89    pub virtual_gateway_esi: Option<VirtualGatewayEsi>,
90    /// `proxy-arp` is configured; `proxy_arp_mode` is its `restricted` or `unrestricted` argument.
91    pub proxy_arp: bool,
92    pub proxy_arp_mode: Option<String>,
93    pub ip_address_owner: Option<String>,
94    pub input_vlan_map: Option<VlanMap>,
95    pub output_vlan_map: Option<VlanMap>,
96    /// Logical encapsulation, e.g. `"vlan-bridge"` or `"vlan-ccc"`.
97    pub encapsulation: Option<String>,
98    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
99    pub families: Vec<String>,
100    pub inet_addresses: Vec<String>,
101    pub inet6_addresses: Vec<String>,
102    pub iso_addresses: Vec<String>,
103    /// The settings of each `family`, in the order written.
104    pub family_settings: Vec<UnitFamily>,
105}
106
107/// The `virtual-gateway-esi` of an IRB unit: an ESI identifier and `all-active`.
108#[derive(Clone, Debug, Default, PartialEq, Eq)]
109#[non_exhaustive]
110pub struct VirtualGatewayEsi {
111    pub identifier: Option<String>,
112    pub all_active: bool,
113}
114
115fn parse_virtual_gateway_esi(unit: &ConfigLine) -> Option<VirtualGatewayEsi> {
116    let line =
117        active_children(unit).find(|c| c.text == "virtual-gateway-esi" || c.arg("virtual-gateway-esi").is_some())?;
118    let inline = line.arg("virtual-gateway-esi");
119    Some(VirtualGatewayEsi {
120        identifier: inline.filter(|v| *v != "all-active").map(str::to_string).or_else(|| {
121            active_children(line)
122                .find(|c| !c.text.contains(' ') && c.text != "all-active")
123                .map(|c| c.text.clone())
124        }),
125        all_active: inline == Some("all-active") || has_leaf(line, "all-active"),
126    })
127}
128
129pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
130    let families: Vec<_> = named_children(unit, "family").collect();
131    let addresses = |family: &str| -> Vec<String> {
132        families
133            .iter()
134            .filter(|(name, _)| *name == family)
135            .flat_map(|(_, f)| args_of(f, "address"))
136            .map(str::to_string)
137            .collect()
138    };
139    InterfaceUnit {
140        number: number.to_string(),
141        description: arg_of(unit, "description").map(str::to_string),
142        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
143        vlan_id_unmatched: arg_of(unit, "vlan-id") == Some("unmatched"),
144        vlan_id_range: arg_of(unit, "vlan-id-range").map(str::to_string),
145        vlan_tags: parse_vlan_tags(unit),
146        tunnel: parse_tunnel(unit),
147        esi: parse_esi(unit),
148        ppp_options: child_block(unit, "ppp-options").map(parse_ppp_options),
149        pppoe_options: child_block(unit, "pppoe-options").map(parse_pppoe_options),
150        pppoe_underlying_options: child_block(unit, "pppoe-underlying-options").map(parse_pppoe_underlying_options),
151        demux: child_block(unit, "demux").map(parse_demux),
152        demux_options: child_block(unit, "demux-options").map(parse_demux_options),
153        demux_source: args_of(unit, "demux-source").into_iter().map(str::to_string).collect(),
154        demux_destination: args_of(unit, "demux-destination")
155            .into_iter()
156            .map(str::to_string)
157            .collect(),
158        advisory_options: child_block(unit, "advisory-options").map(parse_advisory_options),
159        targeted_distribution: arg_of(unit, "targeted-distribution").map(str::to_string),
160        proxy_macip_advertisement: has_leaf(unit, "proxy-macip-advertisement"),
161        enhanced_convergence: has_leaf(unit, "enhanced-convergence"),
162        virtual_gateway_v4_mac: arg_of(unit, "virtual-gateway-v4-mac").map(str::to_string),
163        virtual_gateway_v6_mac: arg_of(unit, "virtual-gateway-v6-mac").map(str::to_string),
164        virtual_gateway_esi: parse_virtual_gateway_esi(unit),
165        proxy_arp: has_leaf(unit, "proxy-arp") || arg_of(unit, "proxy-arp").is_some(),
166        proxy_arp_mode: arg_of(unit, "proxy-arp").map(str::to_string),
167        ip_address_owner: arg_of(unit, "ip-address-owner").map(str::to_string),
168        input_vlan_map: parse_vlan_map(unit, "input-vlan-map"),
169        output_vlan_map: parse_vlan_map(unit, "output-vlan-map"),
170        encapsulation: arg_of(unit, "encapsulation").map(str::to_string),
171        inet_addresses: addresses("inet"),
172        inet6_addresses: addresses("inet6"),
173        iso_addresses: addresses("iso"),
174        families: families.iter().map(|(name, _)| name.to_string()).collect(),
175        family_settings: families
176            .iter()
177            .map(|(name, family)| parse_family(name, family))
178            .collect(),
179    }
180}
181
182#[cfg(test)]
183mod tests {
184    use crate::interfaces::interfaces;
185    use asterust::config::juniper;
186
187    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
188    const CONFIG: &str = r#"
189interfaces {
190    ge-0/0/0 {
191        flexible-vlan-tagging;
192        encapsulation flexible-ethernet-services;
193        unit 100 {
194            encapsulation vlan-bridge;
195            vlan-id 100;
196            input-vlan-map {
197                swap;
198                tag-protocol-id 0x8100;
199                vlan-id 200;
200            }
201            output-vlan-map swap;
202        }
203        unit 200 {
204            vlan-tags outer 0x8100.200 inner 0x8100.300;
205            input-vlan-map {
206                push-push;
207                tag-protocol-id 0x9100;
208                vlan-id 10;
209                inner-tag-protocol-id 0x8100;
210                inner-vlan-id 20;
211            }
212            output-vlan-map {
213                pop-pop;
214            }
215        }
216        unit 300 {
217            vlan-id unmatched;
218        }
219        unit 400 {
220            vlan-id-range 400-499;
221            vlan-tags outer 500 inner-range 1-4094;
222        }
223    }
224}
225"#;
226
227    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
228    #[test]
229    fn extracts_unit_ppp_and_pppoe_options() {
230        let ifaces = interfaces(&juniper::parse_braces(
231            r#"
232interfaces {
233    pp0 {
234        unit 0 {
235            pppoe-options {
236                access-concentrator AC1;
237                auto-reconnect 10;
238                client;
239                ignore-eol-tag;
240                service-name isp;
241                underlying-interface ge-0/0/0.0;
242                ppp-max-payload 1500;
243            }
244            ppp-options {
245                peer-ip-address-optional;
246                initiate-ncp ipv6;
247                chap {
248                    challenge-length minimum 8 maximum 16;
249                }
250            }
251        }
252        unit 1 {
253            pppoe-options {
254                server;
255            }
256        }
257    }
258    ge-0/0/1 {
259        unit 100 {
260            pppoe-underlying-options {
261                access-concentrator AC2;
262                dynamic-profile pppoe-prof;
263                direct-connect;
264                duplicate-protection {
265                    include {
266                        relay-session-id;
267                    }
268                }
269                max-sessions 500;
270                max-sessions-vsa-ignore;
271                service-name-table SNT;
272                short-cycle-protection lockout-time-min 1 lockout-time-max 60 filter [ aci ];
273            }
274        }
275        unit 200 {
276            family pppoe {
277                max-sessions 10;
278                duplicate-protection;
279                short-cycle-protection;
280            }
281        }
282        unit 300 {
283            description plain;
284        }
285    }
286}
287"#,
288        ));
289        let pp0 = &ifaces[0].units;
290        let pppoe = pp0[0].pppoe_options.as_ref().unwrap();
291        assert_eq!(pppoe.access_concentrator.as_deref(), Some("AC1"));
292        assert_eq!(pppoe.auto_reconnect.as_deref(), Some("10"));
293        assert_eq!(pppoe.role.as_deref(), Some("client"));
294        assert!(pppoe.ignore_eol_tag);
295        assert_eq!(pppoe.service_name.as_deref(), Some("isp"));
296        assert_eq!(pppoe.underlying_interface.as_deref(), Some("ge-0/0/0.0"));
297        assert_eq!(pppoe.ppp_max_payload.as_deref(), Some("1500"));
298        let ppp = pp0[0].ppp_options.as_ref().unwrap();
299        assert!(ppp.peer_ip_address_optional);
300        assert_eq!(ppp.initiate_ncp.as_deref(), Some("ipv6"));
301        assert_eq!(
302            ppp.chap.as_ref().unwrap().challenge_length_maximum.as_deref(),
303            Some("16")
304        );
305        assert_eq!(pp0[1].pppoe_options.as_ref().unwrap().role.as_deref(), Some("server"));
306        assert!(pp0[1].ppp_options.is_none());
307
308        let units = &ifaces[1].units;
309        let underlying = units[0].pppoe_underlying_options.as_ref().unwrap();
310        assert_eq!(underlying.access_concentrator.as_deref(), Some("AC2"));
311        assert_eq!(underlying.dynamic_profile.as_deref(), Some("pppoe-prof"));
312        assert!(underlying.direct_connect && underlying.max_sessions_vsa_ignore);
313        assert!(
314            underlying
315                .duplicate_protection
316                .as_ref()
317                .unwrap()
318                .include_relay_session_id
319        );
320        assert_eq!(underlying.max_sessions.as_deref(), Some("500"));
321        assert_eq!(underlying.service_name_table.as_deref(), Some("SNT"));
322        let cycle = underlying.short_cycle_protection.as_ref().unwrap();
323        assert_eq!(cycle.lockout_time_min.as_deref(), Some("1"));
324        assert_eq!(cycle.lockout_time_max.as_deref(), Some("60"));
325        assert_eq!(cycle.filter, ["aci"]);
326
327        let family = units[1].family_settings[0].pppoe.as_ref().unwrap();
328        assert_eq!(family.max_sessions.as_deref(), Some("10"));
329        assert!(!family.duplicate_protection.as_ref().unwrap().include_relay_session_id);
330        let bare = family.short_cycle_protection.as_ref().unwrap();
331        assert!(bare.lockout_time_min.is_none() && bare.filter.is_empty());
332
333        assert!(units[2].pppoe_underlying_options.is_none() && units[2].pppoe_options.is_none());
334        assert!(units[2].ppp_options.is_none());
335    }
336
337    #[test]
338    fn extracts_unit_vlan_id_forms_and_encapsulation() {
339        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
340        assert_eq!(units[0].vlan_id, Some(100));
341        assert!(!units[0].vlan_id_unmatched);
342        assert_eq!(units[0].encapsulation.as_deref(), Some("vlan-bridge"));
343
344        assert_eq!(units[2].vlan_id, None);
345        assert!(units[2].vlan_id_unmatched);
346
347        assert_eq!(units[3].vlan_id_range.as_deref(), Some("400-499"));
348        assert_eq!(units[1].encapsulation, None);
349    }
350
351    #[test]
352    fn extracts_vlan_tags() {
353        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
354        let tags = units[1].vlan_tags.as_ref().unwrap();
355        assert_eq!(tags.outer.as_deref(), Some("0x8100.200"));
356        assert_eq!(tags.inner.as_deref(), Some("0x8100.300"));
357        assert_eq!(tags.inner_range, None);
358
359        let ranged = units[3].vlan_tags.as_ref().unwrap();
360        assert_eq!(ranged.outer.as_deref(), Some("500"));
361        assert_eq!(ranged.inner, None);
362        assert_eq!(ranged.inner_range.as_deref(), Some("1-4094"));
363
364        assert!(units[0].vlan_tags.is_none());
365    }
366
367    #[test]
368    fn extracts_vlan_maps_in_block_and_one_line_form() {
369        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
370
371        let input = units[0].input_vlan_map.as_ref().unwrap();
372        assert_eq!(input.operation.as_deref(), Some("swap"));
373        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x8100"));
374        assert_eq!(input.vlan_id.as_deref(), Some("200"));
375        assert_eq!(input.inner_vlan_id, None);
376        assert_eq!(
377            units[0].output_vlan_map.as_ref().unwrap().operation.as_deref(),
378            Some("swap")
379        );
380
381        let input = units[1].input_vlan_map.as_ref().unwrap();
382        assert_eq!(input.operation.as_deref(), Some("push-push"));
383        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x9100"));
384        assert_eq!(input.vlan_id.as_deref(), Some("10"));
385        assert_eq!(input.inner_tag_protocol_id.as_deref(), Some("0x8100"));
386        assert_eq!(input.inner_vlan_id.as_deref(), Some("20"));
387        assert_eq!(
388            units[1].output_vlan_map.as_ref().unwrap().operation.as_deref(),
389            Some("pop-pop")
390        );
391
392        assert!(units[2].input_vlan_map.is_none());
393        assert!(units[2].output_vlan_map.is_none());
394    }
395
396    #[test]
397    fn extracts_bridge_family_vlan_ids() {
398        let ifaces = interfaces(&juniper::parse_braces(
399            "interfaces {\n    ge-0/0/1 {\n        unit 0 {\n            family bridge {\n                vlan-id-list [ 100-200 300 ];\n                vlan-id 50;\n            }\n        }\n    }\n}\n",
400        ));
401        let bridge = &ifaces[0].units[0].family_settings[0];
402        assert_eq!(bridge.name, "bridge");
403        assert_eq!(bridge.vlan_id_list, ["100-200", "300"]);
404        assert_eq!(bridge.vlan_id.as_deref(), Some("50"));
405    }
406
407    // Adapted from the EVPN-VXLAN User Guide, the IGMP snooping example: the `irb` units of the first leaf device; see
408    // the README "Sources".
409    #[test]
410    fn reads_the_guide_virtual_gateway_address() {
411        let ifaces = interfaces(&juniper::parse_braces(
412            "interfaces {\n    irb {\n        unit 1 {\n            family inet {\n                address 10.1.1.1/24 {\n                    virtual-gateway-address 10.1.1.10;\n                }\n            }\n        }\n        unit 2 {\n            family inet {\n                address 10.2.1.1/24 {\n                    virtual-gateway-address 10.2.1.10;\n                }\n            }\n        }\n    }\n}\n",
413        ));
414        let units = &ifaces[0].units;
415        let address = &units[0].family_settings[0].addresses[0];
416        assert_eq!(address.address, "10.1.1.1/24");
417        assert_eq!(address.virtual_gateway_address.as_deref(), Some("10.1.1.10"));
418        assert_eq!(
419            units[1].family_settings[0].addresses[0]
420                .virtual_gateway_address
421                .as_deref(),
422            Some("10.2.1.10")
423        );
424    }
425
426    // Not from documentation: written from the CLI Reference pages; `virtual-gateway-esi` follows the `set` commands of
427    // the EVPN-VXLAN WAN example, in brace form. See the README "Sources".
428    #[test]
429    fn reads_the_irb_unit_statements() {
430        let ifaces = interfaces(&juniper::parse_braces(
431            r#"
432interfaces {
433    irb {
434        unit 1 {
435            proxy-macip-advertisement;
436            enhanced-convergence;
437            virtual-gateway-v4-mac 00:00:5e:00:01:01;
438            virtual-gateway-v6-mac 00:00:5e:00:02:01;
439            virtual-gateway-esi {
440                00:11:aa:aa:aa:aa:aa:aa:aa:aa;
441                all-active;
442            }
443            proxy-arp restricted;
444            ip-address-owner SERVICE;
445            family inet6 {
446                nd6-max-cache 1000;
447                nd6-stale-time 1200;
448                nd6-new-hold-limit 5;
449            }
450        }
451        unit 2 {
452            proxy-arp;
453            family inet;
454        }
455    }
456    ge-0/0/0 {
457        media-type fiber;
458        ether-options {
459            mdi-mode auto;
460            no-auto-mdix;
461        }
462    }
463}
464"#,
465        ));
466        let unit = &ifaces[0].units[0];
467        assert!(unit.proxy_macip_advertisement && unit.enhanced_convergence);
468        assert_eq!(unit.virtual_gateway_v4_mac.as_deref(), Some("00:00:5e:00:01:01"));
469        assert_eq!(unit.virtual_gateway_v6_mac.as_deref(), Some("00:00:5e:00:02:01"));
470        let esi = unit.virtual_gateway_esi.as_ref().unwrap();
471        assert_eq!(esi.identifier.as_deref(), Some("00:11:aa:aa:aa:aa:aa:aa:aa:aa"));
472        assert!(esi.all_active);
473        assert!(unit.proxy_arp);
474        assert_eq!(unit.proxy_arp_mode.as_deref(), Some("restricted"));
475        assert_eq!(unit.ip_address_owner.as_deref(), Some("SERVICE"));
476        let inet6 = &unit.family_settings[0];
477        assert_eq!(inet6.nd6_max_cache.as_deref(), Some("1000"));
478        assert_eq!(inet6.nd6_stale_time.as_deref(), Some("1200"));
479        assert_eq!(inet6.nd6_new_hold_limit.as_deref(), Some("5"));
480
481        let bare = &ifaces[0].units[1];
482        assert!(bare.proxy_arp);
483        assert_eq!(bare.proxy_arp_mode, None);
484        assert!(!bare.proxy_macip_advertisement && bare.virtual_gateway_esi.is_none());
485
486        let port = &ifaces[1];
487        assert_eq!(port.media_type.as_deref(), Some("fiber"));
488        let ether = port.ether_options.as_ref().unwrap();
489        assert_eq!(ether.mdi_mode.as_deref(), Some("auto"));
490        assert!(ether.no_auto_mdix);
491    }
492}