asterust_junos/interfaces/ether_options/
mod.rs1mod ethernet_switch_profile;
32mod ieee_802_3ad;
33
34pub(in crate::interfaces) use ethernet_switch_profile::parse_ethernet_switch_profile;
35pub use ethernet_switch_profile::{EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile};
36pub use ieee_802_3ad::Ieee8023ad;
37
38use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, toggle, value_after};
39use asterust::config::ConfigLine;
40use ieee_802_3ad::parse_ieee_802_3ad;
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct EtherOptions {
46 pub auto_negotiation: Option<bool>,
48 pub remote_fault: Option<String>,
50 pub flow_control: Option<bool>,
52 pub loopback: Option<bool>,
54 pub source_filtering: Option<bool>,
56 pub speed: Option<String>,
57 pub link_mode: Option<String>,
59 pub fec: Option<String>,
61 pub ieee_802_3ad: Option<Ieee8023ad>,
63 pub mdi_mode: Option<String>,
64 pub no_auto_mdix: bool,
65 pub autostate_exclude: bool,
66 pub ieee_802_3az_eee: bool,
67 pub configured_flow_control_rx_buffers: Option<bool>,
69 pub configured_flow_control_tx_buffers: Option<bool>,
71 pub ethernet_switch_profile: Option<EthernetSwitchProfile>,
72 pub redundant_parent: Option<String>,
74 pub mru: Option<String>,
75 pub loopback_remote: bool,
76 pub ignore_l3_incompletes: bool,
77 pub asynchronous_notification: Option<bool>,
79 pub ingress_rate_limit: Option<String>,
81 pub source_address_filter: Vec<String>,
83}
84
85pub(super) fn source_address_filter(options: &ConfigLine) -> Vec<String> {
87 let mut found: Vec<String> = args_of(options, "source-address-filter")
88 .into_iter()
89 .flat_map(list_values)
90 .collect();
91 if let Some(filter) = child_block(options, "source-address-filter") {
92 found.extend(crate::active_children(filter).map(|c| c.text.clone()));
93 }
94 found
95}
96
97pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
98 let options = ["ether-options", "gigether-options", "fastether-options"]
99 .iter()
100 .find_map(|name| child_block(interface, name))?;
101 let negotiation =
102 statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
103 let flow_control = child_block(options, "configured-flow-control");
104 Some(EtherOptions {
105 auto_negotiation: toggle(options, "auto-negotiation"),
106 remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
107 flow_control: toggle(options, "flow-control"),
108 loopback: toggle(options, "loopback"),
109 source_filtering: toggle(options, "source-filtering"),
110 speed: arg_of(options, "speed").map(str::to_string),
111 link_mode: arg_of(options, "link-mode").map(str::to_string),
112 fec: arg_of(options, "fec").map(str::to_string),
113 ieee_802_3ad: parse_ieee_802_3ad(options),
114 mdi_mode: arg_of(options, "mdi-mode").map(str::to_string),
115 no_auto_mdix: has_leaf(options, "no-auto-mdix"),
116 autostate_exclude: has_leaf(options, "autostate-exclude"),
117 ieee_802_3az_eee: has_leaf(options, "ieee-802-3az-eee"),
118 configured_flow_control_rx_buffers: flow_control.and_then(|f| arg_of(f, "rx-buffers")).map(|v| v == "on"),
119 configured_flow_control_tx_buffers: flow_control.and_then(|f| arg_of(f, "tx-buffers")).map(|v| v == "on"),
120 ethernet_switch_profile: child_block(options, "ethernet-switch-profile").map(parse_ethernet_switch_profile),
121 redundant_parent: arg_of(options, "redundant-parent").map(str::to_string),
122 mru: arg_of(options, "mru").map(str::to_string),
123 loopback_remote: has_leaf(options, "loopback-remote"),
124 ignore_l3_incompletes: has_leaf(options, "ignore-l3-incompletes"),
125 asynchronous_notification: toggle(options, "asynchronous-notification"),
126 ingress_rate_limit: arg_of(options, "ingress-rate-limit").map(str::to_string),
127 source_address_filter: source_address_filter(options),
128 })
129}
130
131#[cfg(test)]
132mod tests {
133 use crate::interfaces::interfaces;
134 use asterust::config::juniper;
135
136 const CONFIG: &str = r#"
138interfaces {
139 ge-0/0/0 {
140 gigether-options {
141 802.3ad ae0;
142 no-auto-negotiation;
143 no-flow-control;
144 loopback;
145 source-filtering;
146 speed 1g;
147 }
148 }
149 ge-0/0/1 {
150 gigether-options {
151 802.3ad ae1 {
152 primary;
153 link-index 2;
154 distribution-list DL1;
155 lacp {
156 port-priority 64;
157 }
158 }
159 auto-negotiation remote-fault local-interface-offline;
160 }
161 }
162 xe-0/0/0 {
163 ether-options {
164 802.3ad {
165 ae2;
166 backup;
167 }
168 link-mode full-duplex;
169 fec fec91;
170 no-loopback;
171 no-source-filtering;
172 flow-control;
173 }
174 }
175}
176"#;
177
178 #[test]
179 fn extracts_ether_options_and_bundle_membership() {
180 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
181 let options = ifaces[0].ether_options.as_ref().unwrap();
182 assert_eq!(options.auto_negotiation, Some(false));
183 assert_eq!(options.remote_fault, None);
184 assert_eq!(options.flow_control, Some(false));
185 assert_eq!(options.loopback, Some(true));
186 assert_eq!(options.source_filtering, Some(true));
187 assert_eq!(options.speed.as_deref(), Some("1g"));
188 assert_eq!(options.link_mode, None);
189
190 let bundle = options.ieee_802_3ad.as_ref().unwrap();
191 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
192 assert_eq!(bundle.role, None);
193 }
194
195 #[test]
196 fn extracts_802_3ad_block_and_remote_fault() {
197 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
198 let options = ifaces[1].ether_options.as_ref().unwrap();
199 assert_eq!(options.auto_negotiation, Some(true));
200 assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));
201
202 let bundle = options.ieee_802_3ad.as_ref().unwrap();
203 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
204 assert_eq!(bundle.role.as_deref(), Some("primary"));
205 assert_eq!(bundle.link_index.as_deref(), Some("2"));
206 assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
207 assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
208 }
209
210 #[test]
211 fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
212 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
213 let options = ifaces[2].ether_options.as_ref().unwrap();
214 assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
215 assert_eq!(options.fec.as_deref(), Some("fec91"));
216 assert_eq!(options.loopback, Some(false));
217 assert_eq!(options.source_filtering, Some(false));
218 assert_eq!(options.flow_control, Some(true));
219 assert_eq!(options.auto_negotiation, None);
220
221 let bundle = options.ieee_802_3ad.as_ref().unwrap();
222 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
223 assert_eq!(bundle.role.as_deref(), Some("backup"));
224 }
225
226 #[test]
227 fn extracts_the_other_ethernet_options() {
228 let ifaces = interfaces(&juniper::parse_braces(
229 r#"
230interfaces {
231 ge-0/0/5 {
232 gigether-options {
233 redundant-parent reth0;
234 mru 9216;
235 loopback-remote;
236 no-asynchronous-notification;
237 ignore-l3-incompletes;
238 source-address-filter {
239 00:11:22:33:44:55;
240 00:11:22:33:44:66;
241 }
242 }
243 }
244 fe-0/0/6 {
245 fastether-options {
246 ingress-rate-limit 100;
247 redundant-parent reth1;
248 }
249 }
250 xe-0/0/7 {
251 ether-options {
252 ieee-802-3az-eee;
253 autostate-exclude;
254 configured-flow-control {
255 rx-buffers on;
256 tx-buffers off;
257 }
258 802.3ad {
259 ae3;
260 link-protection-sub-group grp1;
261 }
262 }
263 }
264}
265"#,
266 ));
267 let gig = ifaces[0].ether_options.as_ref().unwrap();
268 assert_eq!(gig.redundant_parent.as_deref(), Some("reth0"));
269 assert_eq!(gig.mru.as_deref(), Some("9216"));
270 assert!(gig.loopback_remote);
271 assert_eq!(gig.asynchronous_notification, Some(false));
272 assert!(gig.ignore_l3_incompletes);
273 assert_eq!(gig.source_address_filter, ["00:11:22:33:44:55", "00:11:22:33:44:66"]);
274 let fast = ifaces[1].ether_options.as_ref().unwrap();
275 assert_eq!(fast.ingress_rate_limit.as_deref(), Some("100"));
276 assert_eq!(fast.redundant_parent.as_deref(), Some("reth1"));
277 let xe = ifaces[2].ether_options.as_ref().unwrap();
278 assert!(xe.ieee_802_3az_eee);
279 assert!(xe.autostate_exclude);
280 assert_eq!(xe.configured_flow_control_rx_buffers, Some(true));
281 assert_eq!(xe.configured_flow_control_tx_buffers, Some(false));
282 assert_eq!(
283 xe.ieee_802_3ad.as_ref().unwrap().link_protection_sub_group.as_deref(),
284 Some("grp1")
285 );
286 assert!(!gig.autostate_exclude);
287 assert_eq!(gig.configured_flow_control_rx_buffers, None);
288 assert!(gig.ethernet_switch_profile.is_none());
289 }
290
291 #[test]
292 fn ether_options_are_optional() {
293 let ifaces = interfaces(&juniper::parse_braces(
294 "interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n",
295 ));
296 assert!(ifaces[0].ether_options.is_none());
297 }
298}