Skip to main content

asterust_junos/interfaces/unit/family/
ethernet_switching.rs

1//! `ethernet-switching`
2//! `interfaces <name> { unit <n> { family ethernet-switching { ... } } }`.
3//!
4//! The Layer 2 switching port settings of EX and QFX Series switches.
5//!
6//! # See also
7//!
8//! - [family ethernet-switching (EX Series and QFX Series)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-ethernet-switching-edit-interfaces-qfx-series-els.html)
9
10use crate::{arg_of, child_block, has_leaf, list_values, statements};
11use asterust::config::ConfigLine;
12
13/// The settings of a `family ethernet-switching`. The firewall filters are in [`UnitFamily::filter`].
14///
15/// [`UnitFamily::filter`]: super::UnitFamily::filter
16#[derive(Clone, Debug, Default, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct EthernetSwitching {
19    /// `"access"` or `"trunk"`.
20    pub interface_mode: Option<String>,
21    /// The older `port-mode`, e.g. `"trunk"` or `"tagged-access"`.
22    pub port_mode: Option<String>,
23    pub native_vlan_id: Option<String>,
24    /// The `vlan members`: names, IDs and ranges, or `all`.
25    pub vlan_members: Vec<String>,
26    /// The `storm-control` profile.
27    pub storm_control: Option<String>,
28    pub recovery_timeout: Option<String>,
29    pub reflective_relay: bool,
30    pub block_non_ip_all: bool,
31    pub bpdu_vlan_flooding: bool,
32    pub bypass_non_ip_unicast: bool,
33    pub no_packet_flooding: bool,
34    /// `no-trace-route` inside `no-packet-flooding`.
35    pub no_trace_route: bool,
36}
37
38pub(super) fn parse_ethernet_switching(family: &ConfigLine) -> EthernetSwitching {
39    let field = |keyword: &str| arg_of(family, keyword).map(str::to_string);
40    EthernetSwitching {
41        interface_mode: field("interface-mode"),
42        port_mode: field("port-mode"),
43        native_vlan_id: field("native-vlan-id"),
44        vlan_members: statements(family, "vlan")
45            .into_iter()
46            .filter_map(|s| s.strip_prefix("members "))
47            .flat_map(list_values)
48            .collect(),
49        storm_control: field("storm-control"),
50        recovery_timeout: field("recovery-timeout"),
51        reflective_relay: has_leaf(family, "reflective-relay"),
52        block_non_ip_all: has_leaf(family, "block-non-ip-all"),
53        bpdu_vlan_flooding: has_leaf(family, "bpdu-vlan-flooding"),
54        bypass_non_ip_unicast: has_leaf(family, "bypass-non-ip-unicast"),
55        no_packet_flooding: has_leaf(family, "no-packet-flooding")
56            || child_block(family, "no-packet-flooding").is_some(),
57        no_trace_route: child_block(family, "no-packet-flooding").is_some_and(|b| has_leaf(b, "no-trace-route")),
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use crate::interfaces::interfaces;
64    use asterust::config::juniper;
65
66    // Adapted from the QFX Series guides: the `show configuration interfaces` output of Example: Configuring CoS PFC
67    // across Layer 3 Interfaces, the FCoE MC-LAG example and the automatically generated ESI page (unit 0 of ae0); see
68    // the README "Sources".
69    const DOC_SWITCHING: &str = r#"
70interfaces {
71    xe-0/0/20 {
72        unit 0 {
73            family ethernet-switching {
74                interface-mode trunk;
75                vlan {
76                    members vlan105;
77                }
78            }
79        }
80    }
81    ae0 {
82        mtu 2180;
83        unit 0 {
84            family ethernet-switching {
85                port-mode trunk;
86                vlan {
87                    members fcoe_vlan;
88                }
89            }
90        }
91    }
92    xe-0/0/30 {
93        mtu 2180;
94        unit 0 {
95            family ethernet-switching {
96                port-mode tagged-access;
97                vlan {
98                    members fcoe_vlan;
99                }
100            }
101        }
102    }
103    ae5 {
104        unit 0 {
105            family ethernet-switching {
106                interface-mode trunk;
107                vlan {
108                    members [ v100 v200 ];
109                }
110            }
111        }
112    }
113}
114"#;
115
116    fn switching(ifaces: &[crate::interfaces::Interface], index: usize) -> &super::EthernetSwitching {
117        ifaces[index].units[0].family_settings[0]
118            .ethernet_switching
119            .as_ref()
120            .unwrap()
121    }
122
123    #[test]
124    fn reads_the_guide_ports() {
125        let ifaces = interfaces(&juniper::parse_braces(DOC_SWITCHING));
126        let trunk = switching(&ifaces, 0);
127        assert_eq!(trunk.interface_mode.as_deref(), Some("trunk"));
128        assert_eq!(trunk.port_mode, None);
129        assert_eq!(trunk.vlan_members, ["vlan105"]);
130
131        let legacy = switching(&ifaces, 1);
132        assert_eq!(legacy.port_mode.as_deref(), Some("trunk"));
133        assert_eq!(legacy.interface_mode, None);
134        assert_eq!(legacy.vlan_members, ["fcoe_vlan"]);
135        assert_eq!(switching(&ifaces, 2).port_mode.as_deref(), Some("tagged-access"));
136        assert_eq!(switching(&ifaces, 3).vlan_members, ["v100", "v200"]);
137        assert_eq!(ifaces[0].units[0].family_settings[0].name, "ethernet-switching");
138    }
139
140    // Not from documentation: written from the family ethernet-switching CLI Reference pages. See the README
141    // "Sources".
142    #[test]
143    fn reads_the_other_statements() {
144        let ifaces = interfaces(&juniper::parse_braces(
145            r#"
146interfaces {
147    xe-0/0/1 {
148        unit 0 {
149            family ethernet-switching {
150                filter {
151                    input FILTER-IN;
152                    output-list [ A B ];
153                }
154                interface-mode access;
155                native-vlan-id 10;
156                recovery-timeout 600;
157                storm-control STORM;
158                reflective-relay;
159                block-non-ip-all;
160                bpdu-vlan-flooding;
161                bypass-non-ip-unicast;
162                no-packet-flooding {
163                    no-trace-route;
164                }
165                vlan {
166                    members all;
167                }
168            }
169            family inet;
170        }
171    }
172    xe-0/0/2 {
173        unit 0 {
174            family ethernet-switching {
175                filter input OLD-IN;
176                filter output OLD-OUT;
177                no-packet-flooding;
178            }
179        }
180    }
181}
182"#,
183        ));
184        let family = &ifaces[0].units[0].family_settings[0];
185        let switching = family.ethernet_switching.as_ref().unwrap();
186        assert_eq!(switching.interface_mode.as_deref(), Some("access"));
187        assert_eq!(switching.native_vlan_id.as_deref(), Some("10"));
188        assert_eq!(switching.recovery_timeout.as_deref(), Some("600"));
189        assert_eq!(switching.storm_control.as_deref(), Some("STORM"));
190        assert!(switching.reflective_relay && switching.block_non_ip_all);
191        assert!(switching.bpdu_vlan_flooding && switching.bypass_non_ip_unicast);
192        assert!(switching.no_packet_flooding && switching.no_trace_route);
193        assert_eq!(switching.vlan_members, ["all"]);
194        let filter = family.filter.as_ref().unwrap();
195        assert_eq!(filter.input.as_deref(), Some("FILTER-IN"));
196        assert_eq!(filter.output_list, ["A", "B"]);
197        assert!(ifaces[0].units[0].family_settings[1].ethernet_switching.is_none());
198
199        let old = &ifaces[1].units[0].family_settings[0];
200        let filter = old.filter.as_ref().unwrap();
201        assert_eq!(filter.input.as_deref(), Some("OLD-IN"));
202        assert_eq!(filter.output.as_deref(), Some("OLD-OUT"));
203        let switching = old.ethernet_switching.as_ref().unwrap();
204        assert!(switching.no_packet_flooding && !switching.no_trace_route);
205    }
206}