asterust_junos/class_of_service/
platform.rs1use super::interface::{parse_mappings, AppliedMapping};
13use super::scheduler::{parse_rate, SchedulerRate};
14use super::words;
15use crate::{active_children, arg_of, args_of, child_block, has_leaf, statement_words, value_after};
16use asterust::config::ConfigLine;
17
18#[derive(Clone, Debug, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct CosOptions {
22 pub hierarchical_scheduler_disable: bool,
23 pub latency_ecn_threshold: Option<String>,
25 pub latency_tracking: bool,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct FabricScheduler {
32 pub priority: String,
33 pub scheduler: Option<String>,
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct AdaptiveShaperTrigger {
40 pub kind: String,
42 pub shaping_rate: Option<SchedulerRate>,
43}
44
45#[derive(Clone, Debug, PartialEq, Eq)]
47#[non_exhaustive]
48pub struct AdaptiveShaper {
49 pub name: String,
50 pub triggers: Vec<AdaptiveShaperTrigger>,
51}
52
53#[derive(Clone, Debug, PartialEq, Eq)]
55#[non_exhaustive]
56pub struct AdjustmentApplication {
57 pub kind: String,
58 pub priority: Option<String>,
59}
60
61#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct AdjustmentControlProfile {
65 pub name: String,
66 pub applications: Vec<AdjustmentApplication>,
67 pub algorithm: Option<String>,
68}
69
70pub(super) fn parse_options(cos: &ConfigLine) -> Option<CosOptions> {
71 let options = child_block(cos, "options")?;
72 Some(CosOptions {
73 hierarchical_scheduler_disable: has_leaf(options, "hierarchical-scheduler-disable"),
74 latency_ecn_threshold: statement_words(options, "latency-ecn")
75 .and_then(|words| value_after(&words, "threshold")),
76 latency_tracking: has_leaf(options, "latency-tracking"),
77 })
78}
79
80pub(super) fn parse_multi_destination(line: &ConfigLine) -> Vec<AppliedMapping> {
82 child_block(line, "multi-destination")
83 .map(|block| parse_mappings(block, "classifiers"))
84 .unwrap_or_default()
85}
86
87pub(super) fn parse_fabric(cos: &ConfigLine) -> Vec<FabricScheduler> {
88 let Some(map) = child_block(cos, "fabric").and_then(|fabric| child_block(fabric, "scheduler-map")) else {
89 return Vec::new();
90 };
91 args_of(map, "priority")
92 .into_iter()
93 .filter_map(|arg| {
94 let words: Vec<&str> = arg.split_whitespace().collect();
95 Some(FabricScheduler {
96 priority: words.first()?.to_string(),
97 scheduler: value_after(&words, "scheduler"),
98 })
99 })
100 .collect()
101}
102
103pub(super) fn parse_adaptive_shapers(cos: &ConfigLine) -> Vec<AdaptiveShaper> {
104 let Some(block) = child_block(cos, "adaptive-shapers") else {
105 return Vec::new();
106 };
107 active_children(block)
108 .filter_map(|shaper| {
109 Some(AdaptiveShaper {
110 name: shaper.text.split_whitespace().next()?.to_string(),
111 triggers: active_children(shaper)
112 .filter(|child| child.arg("trigger").is_some())
113 .filter_map(|trigger| {
114 let words = words(trigger, 1);
115 let rate = words
116 .iter()
117 .position(|word| *word == "shaping-rate")
118 .map(|at| &words[at + 1..]);
119 Some(AdaptiveShaperTrigger {
120 kind: words.first()?.to_string(),
121 shaping_rate: rate.map(parse_rate),
122 })
123 })
124 .collect(),
125 })
126 })
127 .collect()
128}
129
130pub(super) fn parse_adjustment_control_profiles(cos: &ConfigLine) -> Vec<AdjustmentControlProfile> {
131 let Some(block) = child_block(cos, "adjustment-control-profiles") else {
132 return Vec::new();
133 };
134 active_children(block)
135 .filter_map(|profile| {
136 let application = child_block(profile, "application");
137 Some(AdjustmentControlProfile {
138 name: profile.text.split_whitespace().next()?.to_string(),
139 applications: application
140 .map(|block| {
141 active_children(block)
142 .filter(|child| child.text != "algorithm" && child.arg("algorithm").is_none())
143 .filter_map(|child| {
144 let words: Vec<&str> = child.text.split_whitespace().collect();
145 Some(AdjustmentApplication {
146 kind: words.first()?.to_string(),
147 priority: value_after(&words, "priority"),
148 })
149 })
150 .collect()
151 })
152 .unwrap_or_default(),
153 algorithm: application
154 .and_then(|block| arg_of(block, "algorithm"))
155 .map(str::to_string),
156 })
157 })
158 .collect()
159}
160
161#[cfg(test)]
162mod tests {
163 use crate::class_of_service::class_of_service;
164 use asterust::config::juniper;
165
166 const EXTRA: &str = r#"
168class-of-service {
169 options {
170 hierarchical-scheduler-disable;
171 latency-ecn threshold 20;
172 latency-tracking;
173 }
174 multi-destination {
175 classifiers {
176 dscp mc-dscp;
177 ieee-802.1 mc-dot1p;
178 }
179 }
180 interfaces {
181 xe-0/0/1 {
182 multi-destination {
183 classifiers {
184 inet-precedence mc-prec;
185 }
186 }
187 }
188 }
189 fabric {
190 scheduler-map {
191 priority high scheduler fabric-high;
192 priority low scheduler fabric-low;
193 }
194 }
195 adaptive-shapers {
196 FR {
197 trigger becn shaping-rate 64k;
198 trigger other shaping-rate percent 50;
199 }
200 EMPTY;
201 }
202 adjustment-control-profiles {
203 ACP1 {
204 application {
205 ancp priority 1;
206 pppoe-tags priority 2;
207 algorithm highest-rate;
208 }
209 }
210 ACP2;
211 }
212}
213"#;
214
215 #[test]
216 fn reads_the_options() {
217 let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
218 let options = cos.options.unwrap();
219 assert!(options.hierarchical_scheduler_disable && options.latency_tracking);
220 assert_eq!(options.latency_ecn_threshold.as_deref(), Some("20"));
221 }
222
223 #[test]
224 fn reads_multi_destination_classifiers() {
225 let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
226 assert_eq!(cos.multi_destination_classifiers.len(), 2);
227 assert_eq!(cos.multi_destination_classifiers[1].kind, "ieee-802.1");
228 assert_eq!(cos.multi_destination_classifiers[1].name, "mc-dot1p");
229 let interface = &cos.interfaces[0].settings.multi_destination_classifiers;
230 assert_eq!(interface.len(), 1);
231 assert_eq!(interface[0].name, "mc-prec");
232 }
233
234 #[test]
235 fn reads_the_fabric_scheduler_map() {
236 let fabric = class_of_service(&juniper::parse_braces(EXTRA)).unwrap().fabric;
237 assert_eq!(fabric.len(), 2);
238 assert_eq!(fabric[0].priority, "high");
239 assert_eq!(fabric[0].scheduler.as_deref(), Some("fabric-high"));
240 assert_eq!(fabric[1].priority, "low");
241 }
242
243 #[test]
244 fn reads_the_adaptive_shapers() {
245 let shapers = class_of_service(&juniper::parse_braces(EXTRA))
246 .unwrap()
247 .adaptive_shapers;
248 assert_eq!(shapers.len(), 2);
249 assert_eq!(shapers[0].name, "FR");
250 assert_eq!(shapers[0].triggers.len(), 2);
251 assert_eq!(shapers[0].triggers[0].kind, "becn");
252 assert_eq!(
253 shapers[0].triggers[0].shaping_rate.as_ref().unwrap().value.as_deref(),
254 Some("64k")
255 );
256 let percent = shapers[0].triggers[1].shaping_rate.as_ref().unwrap();
257 assert_eq!(percent.mode.as_deref(), Some("percent"));
258 assert!(shapers[1].triggers.is_empty());
259 }
260
261 #[test]
262 fn reads_the_adjustment_control_profiles() {
263 let profiles = class_of_service(&juniper::parse_braces(EXTRA))
264 .unwrap()
265 .adjustment_control_profiles;
266 assert_eq!(profiles.len(), 2);
267 assert_eq!(profiles[0].name, "ACP1");
268 assert_eq!(profiles[0].applications.len(), 2);
269 assert_eq!(profiles[0].applications[1].kind, "pppoe-tags");
270 assert_eq!(profiles[0].applications[1].priority.as_deref(), Some("2"));
271 assert_eq!(profiles[0].algorithm.as_deref(), Some("highest-rate"));
272 assert!(profiles[1].applications.is_empty() && profiles[1].algorithm.is_none());
273 }
274}