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//! - [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)
9
10mod family;
11mod tunnel;
12mod vlan_map;
13mod vlan_tags;
14
15pub use family::{
16    AddressArp, BandwidthThreshold, EthernetSwitching, FamilyFilter, FamilyPolicer, FamilySampling, RpfCheck,
17    TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, VrrpGroup, VrrpInheritFrom, VrrpTrack,
18};
19pub use tunnel::UnitTunnel;
20pub use vlan_map::VlanMap;
21pub use vlan_tags::VlanTags;
22
23use super::esi::{parse_esi, Esi};
24use crate::{active_children, arg_of, args_of, has_leaf, named_children};
25use asterust::config::ConfigLine;
26use family::parse_family;
27use tunnel::parse_tunnel;
28use vlan_map::parse_vlan_map;
29use vlan_tags::parse_vlan_tags;
30
31/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g.
32/// `"10.0.0.1/24"`; IS-IS NETs for `family iso`).
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct InterfaceUnit {
36    pub number: String,
37    pub description: Option<String>,
38    pub vlan_id: Option<u32>,
39    /// `vlan-id unmatched`.
40    pub vlan_id_unmatched: bool,
41    /// e.g. `"100-200"`.
42    pub vlan_id_range: Option<String>,
43    pub vlan_tags: Option<VlanTags>,
44    pub tunnel: Option<UnitTunnel>,
45    pub esi: Option<Esi>,
46    /// `proxy-macip-advertisement` on an IRB unit.
47    pub proxy_macip_advertisement: bool,
48    /// `enhanced-convergence` on an IRB unit.
49    pub enhanced_convergence: bool,
50    pub virtual_gateway_v4_mac: Option<String>,
51    pub virtual_gateway_v6_mac: Option<String>,
52    /// The `virtual-gateway-esi` of an IRB unit.
53    pub virtual_gateway_esi: Option<VirtualGatewayEsi>,
54    /// `proxy-arp` is configured; `proxy_arp_mode` is its `restricted` or `unrestricted` argument.
55    pub proxy_arp: bool,
56    pub proxy_arp_mode: Option<String>,
57    pub ip_address_owner: Option<String>,
58    pub input_vlan_map: Option<VlanMap>,
59    pub output_vlan_map: Option<VlanMap>,
60    /// Logical encapsulation, e.g. `"vlan-bridge"` or `"vlan-ccc"`.
61    pub encapsulation: Option<String>,
62    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
63    pub families: Vec<String>,
64    pub inet_addresses: Vec<String>,
65    pub inet6_addresses: Vec<String>,
66    pub iso_addresses: Vec<String>,
67    /// The settings of each `family`, in the order written.
68    pub family_settings: Vec<UnitFamily>,
69}
70
71/// The `virtual-gateway-esi` of an IRB unit: an ESI identifier and `all-active`.
72#[derive(Clone, Debug, Default, PartialEq, Eq)]
73#[non_exhaustive]
74pub struct VirtualGatewayEsi {
75    pub identifier: Option<String>,
76    pub all_active: bool,
77}
78
79fn parse_virtual_gateway_esi(unit: &ConfigLine) -> Option<VirtualGatewayEsi> {
80    let line =
81        active_children(unit).find(|c| c.text == "virtual-gateway-esi" || c.arg("virtual-gateway-esi").is_some())?;
82    let inline = line.arg("virtual-gateway-esi");
83    Some(VirtualGatewayEsi {
84        identifier: inline.filter(|v| *v != "all-active").map(str::to_string).or_else(|| {
85            active_children(line)
86                .find(|c| !c.text.contains(' ') && c.text != "all-active")
87                .map(|c| c.text.clone())
88        }),
89        all_active: inline == Some("all-active") || has_leaf(line, "all-active"),
90    })
91}
92
93pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
94    let families: Vec<_> = named_children(unit, "family").collect();
95    let addresses = |family: &str| -> Vec<String> {
96        families
97            .iter()
98            .filter(|(name, _)| *name == family)
99            .flat_map(|(_, f)| args_of(f, "address"))
100            .map(str::to_string)
101            .collect()
102    };
103    InterfaceUnit {
104        number: number.to_string(),
105        description: arg_of(unit, "description").map(str::to_string),
106        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
107        vlan_id_unmatched: arg_of(unit, "vlan-id") == Some("unmatched"),
108        vlan_id_range: arg_of(unit, "vlan-id-range").map(str::to_string),
109        vlan_tags: parse_vlan_tags(unit),
110        tunnel: parse_tunnel(unit),
111        esi: parse_esi(unit),
112        proxy_macip_advertisement: has_leaf(unit, "proxy-macip-advertisement"),
113        enhanced_convergence: has_leaf(unit, "enhanced-convergence"),
114        virtual_gateway_v4_mac: arg_of(unit, "virtual-gateway-v4-mac").map(str::to_string),
115        virtual_gateway_v6_mac: arg_of(unit, "virtual-gateway-v6-mac").map(str::to_string),
116        virtual_gateway_esi: parse_virtual_gateway_esi(unit),
117        proxy_arp: has_leaf(unit, "proxy-arp") || arg_of(unit, "proxy-arp").is_some(),
118        proxy_arp_mode: arg_of(unit, "proxy-arp").map(str::to_string),
119        ip_address_owner: arg_of(unit, "ip-address-owner").map(str::to_string),
120        input_vlan_map: parse_vlan_map(unit, "input-vlan-map"),
121        output_vlan_map: parse_vlan_map(unit, "output-vlan-map"),
122        encapsulation: arg_of(unit, "encapsulation").map(str::to_string),
123        inet_addresses: addresses("inet"),
124        inet6_addresses: addresses("inet6"),
125        iso_addresses: addresses("iso"),
126        families: families.iter().map(|(name, _)| name.to_string()).collect(),
127        family_settings: families
128            .iter()
129            .map(|(name, family)| parse_family(name, family))
130            .collect(),
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use crate::interfaces::interfaces;
137    use asterust::config::juniper;
138
139    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
140    const CONFIG: &str = r#"
141interfaces {
142    ge-0/0/0 {
143        flexible-vlan-tagging;
144        encapsulation flexible-ethernet-services;
145        unit 100 {
146            encapsulation vlan-bridge;
147            vlan-id 100;
148            input-vlan-map {
149                swap;
150                tag-protocol-id 0x8100;
151                vlan-id 200;
152            }
153            output-vlan-map swap;
154        }
155        unit 200 {
156            vlan-tags outer 0x8100.200 inner 0x8100.300;
157            input-vlan-map {
158                push-push;
159                tag-protocol-id 0x9100;
160                vlan-id 10;
161                inner-tag-protocol-id 0x8100;
162                inner-vlan-id 20;
163            }
164            output-vlan-map {
165                pop-pop;
166            }
167        }
168        unit 300 {
169            vlan-id unmatched;
170        }
171        unit 400 {
172            vlan-id-range 400-499;
173            vlan-tags outer 500 inner-range 1-4094;
174        }
175    }
176}
177"#;
178
179    #[test]
180    fn extracts_unit_vlan_id_forms_and_encapsulation() {
181        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
182        assert_eq!(units[0].vlan_id, Some(100));
183        assert!(!units[0].vlan_id_unmatched);
184        assert_eq!(units[0].encapsulation.as_deref(), Some("vlan-bridge"));
185
186        assert_eq!(units[2].vlan_id, None);
187        assert!(units[2].vlan_id_unmatched);
188
189        assert_eq!(units[3].vlan_id_range.as_deref(), Some("400-499"));
190        assert_eq!(units[1].encapsulation, None);
191    }
192
193    #[test]
194    fn extracts_vlan_tags() {
195        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
196        let tags = units[1].vlan_tags.as_ref().unwrap();
197        assert_eq!(tags.outer.as_deref(), Some("0x8100.200"));
198        assert_eq!(tags.inner.as_deref(), Some("0x8100.300"));
199        assert_eq!(tags.inner_range, None);
200
201        let ranged = units[3].vlan_tags.as_ref().unwrap();
202        assert_eq!(ranged.outer.as_deref(), Some("500"));
203        assert_eq!(ranged.inner, None);
204        assert_eq!(ranged.inner_range.as_deref(), Some("1-4094"));
205
206        assert!(units[0].vlan_tags.is_none());
207    }
208
209    #[test]
210    fn extracts_vlan_maps_in_block_and_one_line_form() {
211        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
212
213        let input = units[0].input_vlan_map.as_ref().unwrap();
214        assert_eq!(input.operation.as_deref(), Some("swap"));
215        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x8100"));
216        assert_eq!(input.vlan_id.as_deref(), Some("200"));
217        assert_eq!(input.inner_vlan_id, None);
218        assert_eq!(
219            units[0].output_vlan_map.as_ref().unwrap().operation.as_deref(),
220            Some("swap")
221        );
222
223        let input = units[1].input_vlan_map.as_ref().unwrap();
224        assert_eq!(input.operation.as_deref(), Some("push-push"));
225        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x9100"));
226        assert_eq!(input.vlan_id.as_deref(), Some("10"));
227        assert_eq!(input.inner_tag_protocol_id.as_deref(), Some("0x8100"));
228        assert_eq!(input.inner_vlan_id.as_deref(), Some("20"));
229        assert_eq!(
230            units[1].output_vlan_map.as_ref().unwrap().operation.as_deref(),
231            Some("pop-pop")
232        );
233
234        assert!(units[2].input_vlan_map.is_none());
235        assert!(units[2].output_vlan_map.is_none());
236    }
237
238    #[test]
239    fn extracts_bridge_family_vlan_ids() {
240        let ifaces = interfaces(&juniper::parse_braces(
241            "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",
242        ));
243        let bridge = &ifaces[0].units[0].family_settings[0];
244        assert_eq!(bridge.name, "bridge");
245        assert_eq!(bridge.vlan_id_list, ["100-200", "300"]);
246        assert_eq!(bridge.vlan_id.as_deref(), Some("50"));
247    }
248
249    // Adapted from the EVPN-VXLAN User Guide, the IGMP snooping example: the `irb` units of the first leaf device; see
250    // the README "Sources".
251    #[test]
252    fn reads_the_guide_virtual_gateway_address() {
253        let ifaces = interfaces(&juniper::parse_braces(
254            "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",
255        ));
256        let units = &ifaces[0].units;
257        let address = &units[0].family_settings[0].addresses[0];
258        assert_eq!(address.address, "10.1.1.1/24");
259        assert_eq!(address.virtual_gateway_address.as_deref(), Some("10.1.1.10"));
260        assert_eq!(
261            units[1].family_settings[0].addresses[0]
262                .virtual_gateway_address
263                .as_deref(),
264            Some("10.2.1.10")
265        );
266    }
267
268    // Not from documentation: written from the CLI Reference pages; `virtual-gateway-esi` follows the `set` commands of
269    // the EVPN-VXLAN WAN example, in brace form. See the README "Sources".
270    #[test]
271    fn reads_the_irb_unit_statements() {
272        let ifaces = interfaces(&juniper::parse_braces(
273            r#"
274interfaces {
275    irb {
276        unit 1 {
277            proxy-macip-advertisement;
278            enhanced-convergence;
279            virtual-gateway-v4-mac 00:00:5e:00:01:01;
280            virtual-gateway-v6-mac 00:00:5e:00:02:01;
281            virtual-gateway-esi {
282                00:11:aa:aa:aa:aa:aa:aa:aa:aa;
283                all-active;
284            }
285            proxy-arp restricted;
286            ip-address-owner SERVICE;
287            family inet6 {
288                nd6-max-cache 1000;
289                nd6-stale-time 1200;
290                nd6-new-hold-limit 5;
291            }
292        }
293        unit 2 {
294            proxy-arp;
295            family inet;
296        }
297    }
298    ge-0/0/0 {
299        media-type fiber;
300        ether-options {
301            mdi-mode auto;
302            no-auto-mdix;
303        }
304    }
305}
306"#,
307        ));
308        let unit = &ifaces[0].units[0];
309        assert!(unit.proxy_macip_advertisement && unit.enhanced_convergence);
310        assert_eq!(unit.virtual_gateway_v4_mac.as_deref(), Some("00:00:5e:00:01:01"));
311        assert_eq!(unit.virtual_gateway_v6_mac.as_deref(), Some("00:00:5e:00:02:01"));
312        let esi = unit.virtual_gateway_esi.as_ref().unwrap();
313        assert_eq!(esi.identifier.as_deref(), Some("00:11:aa:aa:aa:aa:aa:aa:aa:aa"));
314        assert!(esi.all_active);
315        assert!(unit.proxy_arp);
316        assert_eq!(unit.proxy_arp_mode.as_deref(), Some("restricted"));
317        assert_eq!(unit.ip_address_owner.as_deref(), Some("SERVICE"));
318        let inet6 = &unit.family_settings[0];
319        assert_eq!(inet6.nd6_max_cache.as_deref(), Some("1000"));
320        assert_eq!(inet6.nd6_stale_time.as_deref(), Some("1200"));
321        assert_eq!(inet6.nd6_new_hold_limit.as_deref(), Some("5"));
322
323        let bare = &ifaces[0].units[1];
324        assert!(bare.proxy_arp);
325        assert_eq!(bare.proxy_arp_mode, None);
326        assert!(!bare.proxy_macip_advertisement && bare.virtual_gateway_esi.is_none());
327
328        let port = &ifaces[1];
329        assert_eq!(port.media_type.as_deref(), Some("fiber"));
330        let ether = port.ether_options.as_ref().unwrap();
331        assert_eq!(ether.mdi_mode.as_deref(), Some("auto"));
332        assert!(ether.no_auto_mdix);
333    }
334}