Skip to main content

asterust_junos/interfaces/
esi.rs

1//! `esi`
2//! `interfaces <name> { esi [<identifier>] { ... } }` and `interfaces <name> { unit <n> { esi { ... } } }`.
3//!
4//! The Ethernet segment of an EVPN multihomed interface.
5//!
6//! # See also
7//!
8//! - [esi (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/esi-edit-interfaces.html)
9//! - [Understanding Automatically Generated ESIs in EVPN Networks](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/concept/evpn-auto-esi.html)
10
11use crate::{active_children, arg_of, child_block, has_leaf};
12use asterust::config::ConfigLine;
13
14/// The `type-3-system-mac` of `auto-derive`.
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct EsiSystemMac {
18    pub mac: Option<String>,
19    pub local_discriminator: Option<String>,
20}
21
22/// The `auto-derive` of an ESI.
23#[derive(Clone, Debug, Default, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct EsiAutoDerive {
26    pub lacp: bool,
27    pub type_1_lacp: bool,
28    pub type_3_system_mac: Option<EsiSystemMac>,
29}
30
31/// The `df-election-granularity` of an ESI.
32#[derive(Clone, Debug, Default, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct EsiDfGranularity {
35    pub per_esi: bool,
36    pub lacp_oos_on_ndf: bool,
37    pub per_esi_vlan: bool,
38    pub per_evi: bool,
39}
40
41/// The `preference` of `df-election-type`.
42#[derive(Clone, Debug, Default, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct EsiPreference {
45    pub least: bool,
46    pub non_revertive: bool,
47    pub value: Option<String>,
48}
49
50/// The `df-election-type` of an ESI.
51#[derive(Clone, Debug, Default, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct EsiDfElectionType {
54    /// `mod`: the default modulo election.
55    pub mod_based: bool,
56    pub preference: Option<EsiPreference>,
57}
58
59/// An `esi`, written `esi <identifier> { ... }` or, when auto-derived, `esi { ... }`.
60#[derive(Clone, Debug, Default, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct Esi {
63    pub identifier: Option<String>,
64    pub all_active: bool,
65    pub single_active: bool,
66    pub auto_derive: Option<EsiAutoDerive>,
67    pub df_election_granularity: Option<EsiDfGranularity>,
68    pub df_election_type: Option<EsiDfElectionType>,
69    pub source_bmac: Option<String>,
70}
71
72/// The `esi` of an interface or unit, or `None` if it has none.
73pub(super) fn parse_esi(parent: &ConfigLine) -> Option<Esi> {
74    let line = active_children(parent).find(|c| c.text == "esi" || c.arg("esi").is_some())?;
75    Some(Esi {
76        identifier: line.arg("esi").map(str::to_string),
77        all_active: has_leaf(line, "all-active"),
78        single_active: has_leaf(line, "single-active"),
79        auto_derive: child_block(line, "auto-derive").map(|derive| EsiAutoDerive {
80            lacp: has_leaf(derive, "lacp"),
81            type_1_lacp: has_leaf(derive, "type-1-lacp"),
82            type_3_system_mac: child_block(derive, "type-3-system-mac").map(|mac| EsiSystemMac {
83                mac: arg_of(mac, "mac").map(str::to_string),
84                local_discriminator: arg_of(mac, "local-discriminator").map(str::to_string),
85            }),
86        }),
87        df_election_granularity: child_block(line, "df-election-granularity").map(|granularity| {
88            let per_esi = child_block(granularity, "per-esi");
89            EsiDfGranularity {
90                per_esi: per_esi.is_some() || has_leaf(granularity, "per-esi"),
91                lacp_oos_on_ndf: per_esi.is_some_and(|block| has_leaf(block, "lacp-oos-on-ndf")),
92                per_esi_vlan: has_leaf(granularity, "per-esi-vlan"),
93                per_evi: has_leaf(granularity, "per-evi"),
94            }
95        }),
96        df_election_type: child_block(line, "df-election-type").map(|election| EsiDfElectionType {
97            mod_based: has_leaf(election, "mod"),
98            preference: child_block(election, "preference").map(|preference| EsiPreference {
99                least: has_leaf(preference, "least"),
100                non_revertive: has_leaf(preference, "non-revertive"),
101                value: arg_of(preference, "value").map(str::to_string),
102            }),
103        }),
104        source_bmac: arg_of(line, "source-bmac").map(str::to_string),
105    })
106}
107
108#[cfg(test)]
109mod tests {
110    use crate::interfaces::interfaces;
111    use asterust::config::juniper;
112
113    // Adapted from the EVPN User Guide, Understanding Automatically Generated ESIs in EVPN Networks: the `show
114    // interfaces ae0` output of the LACP and the system MAC examples (the `###` comments are removed); see the
115    // README "Sources".
116    const DOC_AUTO_ESI: &str = r#"
117interfaces {
118    ae0 {
119        esi {
120            auto-derive {
121                type-1-lacp;
122            }
123            all-active;
124        }
125        aggregated-ether-options {
126            minimum-links 1;
127            lacp {
128                active;
129                system-id 00:11:22:33:44:55;
130                admin-key 1234;
131            }
132        }
133    }
134    ae1 {
135        esi {
136            auto-derive {
137                type-3-system-mac {
138                    mac 00:aa:bb:cc:dd:ee;
139                    local-discriminator 1000;
140                }
141            }
142            all-active;
143        }
144    }
145}
146"#;
147
148    #[test]
149    fn reads_the_guide_auto_derived_esis() {
150        let ifaces = interfaces(&juniper::parse_braces(DOC_AUTO_ESI));
151        let lacp = ifaces[0].esi.as_ref().unwrap();
152        assert_eq!(lacp.identifier, None);
153        assert!(lacp.all_active && !lacp.single_active);
154        let derive = lacp.auto_derive.as_ref().unwrap();
155        assert!(derive.type_1_lacp && !derive.lacp);
156        assert!(derive.type_3_system_mac.is_none());
157
158        let mac = ifaces[1]
159            .esi
160            .as_ref()
161            .unwrap()
162            .auto_derive
163            .as_ref()
164            .unwrap()
165            .type_3_system_mac
166            .as_ref()
167            .unwrap();
168        assert_eq!(mac.mac.as_deref(), Some("00:aa:bb:cc:dd:ee"));
169        assert_eq!(mac.local_discriminator.as_deref(), Some("1000"));
170    }
171
172    // Not from documentation: written from the esi CLI Reference pages. See the README "Sources".
173    #[test]
174    fn reads_the_other_statements_on_interfaces_and_units() {
175        let ifaces = interfaces(&juniper::parse_braces(
176            r#"
177interfaces {
178    ae2 {
179        esi 00:11:22:33:44:55:66:77:88:99 {
180            single-active;
181            auto-derive {
182                lacp;
183            }
184            df-election-granularity {
185                per-esi {
186                    lacp-oos-on-ndf;
187                }
188            }
189            df-election-type {
190                preference {
191                    least;
192                    non-revertive;
193                    value 100;
194                }
195            }
196            source-bmac 00:00:5e:00:53:01;
197        }
198        unit 10 {
199            esi 00:11:22:33:44:55:66:77:88:aa {
200                all-active;
201                df-election-granularity {
202                    per-evi;
203                }
204                df-election-type {
205                    mod;
206                }
207            }
208        }
209        unit 11;
210    }
211}
212"#,
213        ));
214        let esi = ifaces[0].esi.as_ref().unwrap();
215        assert_eq!(esi.identifier.as_deref(), Some("00:11:22:33:44:55:66:77:88:99"));
216        assert!(esi.single_active && !esi.all_active);
217        assert!(esi.auto_derive.as_ref().unwrap().lacp);
218        let granularity = esi.df_election_granularity.as_ref().unwrap();
219        assert!(granularity.per_esi && granularity.lacp_oos_on_ndf && !granularity.per_evi);
220        let preference = esi.df_election_type.as_ref().unwrap().preference.as_ref().unwrap();
221        assert!(preference.least && preference.non_revertive);
222        assert_eq!(preference.value.as_deref(), Some("100"));
223        assert_eq!(esi.source_bmac.as_deref(), Some("00:00:5e:00:53:01"));
224
225        let unit = ifaces[0].units[0].esi.as_ref().unwrap();
226        assert_eq!(unit.identifier.as_deref(), Some("00:11:22:33:44:55:66:77:88:aa"));
227        assert!(unit.all_active);
228        assert!(unit.df_election_granularity.as_ref().unwrap().per_evi);
229        assert!(unit.df_election_type.as_ref().unwrap().mod_based);
230        assert!(ifaces[0].units[1].esi.is_none());
231    }
232}