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