asterust_junos/class_of_service/
scheduler.rs1use super::words;
18use crate::{active_children, arg_of, args_of, child_block, has_leaf, statement_words, value_after};
19use asterust::config::ConfigLine;
20
21#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct SchedulerRate {
26 pub mode: Option<String>,
28 pub value: Option<String>,
30 pub exact: bool,
31 pub rate_limit: bool,
32 pub burst_size: Option<String>,
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct DropProfileMap {
39 pub loss_priority: Option<String>,
40 pub protocol: Option<String>,
41 pub drop_profile: Option<String>,
42}
43
44#[derive(Clone, Debug, PartialEq, Eq)]
46#[non_exhaustive]
47pub struct Scheduler {
48 pub name: String,
49 pub transmit_rate: Option<SchedulerRate>,
50 pub shaping_rate: Option<SchedulerRate>,
51 pub buffer_size: Option<SchedulerRate>,
52 pub buffer_rate: Option<SchedulerRate>,
53 pub excess_rate: Option<SchedulerRate>,
54 pub priority: Option<String>,
55 pub excess_priority: Vec<String>,
56 pub adjust_minimum: Option<String>,
57 pub adjust_percent: Option<String>,
58 pub buffer_dynamic_threshold: Option<String>,
59 pub explicit_congestion_notification: bool,
60 pub latency_ecn_threshold: Option<String>,
62 pub drop_profile_maps: Vec<DropProfileMap>,
63}
64
65const MODES: [&str; 5] = ["percent", "remainder", "proportion", "temporal", "shared"];
66
67pub(super) fn parse_rate(words: &[&str]) -> SchedulerRate {
68 let mode = words
69 .first()
70 .filter(|word| MODES.contains(*word))
71 .map(|word| word.to_string());
72 let value = words
73 .get(usize::from(mode.is_some()))
74 .filter(|word| !matches!(**word, "exact" | "rate-limit" | "burst-size"))
75 .map(|word| word.to_string());
76 SchedulerRate {
77 mode,
78 value,
79 exact: words.contains(&"exact"),
80 rate_limit: words.contains(&"rate-limit"),
81 burst_size: value_after(words, "burst-size"),
82 }
83}
84
85pub(super) fn rate_of(line: &ConfigLine, keyword: &str) -> Option<SchedulerRate> {
87 statement_words(line, keyword).map(|words| parse_rate(&words))
88}
89
90fn parse_drop_profile_map(arg: &str) -> DropProfileMap {
91 let words: Vec<&str> = arg.split_whitespace().collect();
92 DropProfileMap {
93 loss_priority: value_after(&words, "loss-priority"),
94 protocol: value_after(&words, "protocol"),
95 drop_profile: value_after(&words, "drop-profile"),
96 }
97}
98
99fn parse_scheduler(scheduler: &ConfigLine) -> Option<Scheduler> {
100 let name = scheduler.text.split_whitespace().next()?;
101 let rate = |keyword: &str| rate_of(scheduler, keyword);
102 let field = |keyword: &str| arg_of(scheduler, keyword).map(str::to_string);
103 Some(Scheduler {
104 name: name.to_string(),
105 transmit_rate: rate("transmit-rate"),
106 shaping_rate: rate("shaping-rate"),
107 buffer_size: rate("buffer-size"),
108 buffer_rate: rate("buffer-rate"),
109 excess_rate: rate("excess-rate"),
110 priority: field("priority"),
111 excess_priority: active_children(scheduler)
112 .find(|child| child.text == "excess-priority" || child.arg("excess-priority").is_some())
113 .map(|child| words(child, 1).into_iter().map(str::to_string).collect())
114 .unwrap_or_default(),
115 adjust_minimum: field("adjust-minimum"),
116 adjust_percent: field("adjust-percent"),
117 buffer_dynamic_threshold: field("buffer-dynamic-threshold"),
118 explicit_congestion_notification: has_leaf(scheduler, "explicit-congestion-notification"),
119 latency_ecn_threshold: statement_words(scheduler, "latency-ecn")
120 .and_then(|words| value_after(&words, "threshold")),
121 drop_profile_maps: args_of(scheduler, "drop-profile-map")
122 .into_iter()
123 .map(parse_drop_profile_map)
124 .collect(),
125 })
126}
127
128pub(super) fn parse_schedulers(cos: &ConfigLine) -> Vec<Scheduler> {
129 let Some(block) = child_block(cos, "schedulers") else {
130 return Vec::new();
131 };
132 active_children(block).filter_map(parse_scheduler).collect()
133}
134
135#[cfg(test)]
136mod tests {
137 use crate::class_of_service::class_of_service;
138 use asterust::config::juniper;
139
140 const DOC_SCHEDULERS: &str = r#"
144class-of-service {
145 schedulers {
146 be-scheduler {
147 transmit-rate remainder 40;
148 buffer-size percent 40;
149 priority low;
150 }
151 af-scheduler {
152 transmit-rate percent 45;
153 buffer-size percent 45;
154 priority high;
155 drop-profile-map loss-priority low protocol any drop-profile af-normal;
156 drop-profile-map loss-priority high protocol any drop-profile af-with-PLP;
157 }
158 be-sched {
159 priority medium-low;
160 }
161 sched-cvlan0-qx {
162 transmit-rate 20m;
163 buffer-size temporal 100k;
164 priority low;
165 drop-profile-map loss-priority low protocol any drop-profile dp-low;
166 }
167 }
168}
169"#;
170
171 const EXTRA: &str = r#"
173class-of-service {
174 schedulers {
175 voice {
176 transmit-rate percent 10 exact;
177 shaping-rate percent 20 burst-size 64k;
178 buffer-rate percent 5;
179 excess-rate proportion 100;
180 excess-priority [ low medium-low ];
181 adjust-minimum 1m;
182 adjust-percent 90;
183 buffer-dynamic-threshold 7;
184 explicit-congestion-notification;
185 latency-ecn threshold 5;
186 }
187 ctl {
188 transmit-rate 2m rate-limit;
189 excess-priority none;
190 drop-profile-map loss-priority any protocol tcp drop-profile red;
191 }
192 bulk {
193 transmit-rate remainder;
194 buffer-size shared;
195 }
196 }
197}
198"#;
199
200 fn schedulers(text: &str) -> Vec<super::Scheduler> {
201 class_of_service(&juniper::parse_braces(text)).unwrap().schedulers
202 }
203
204 #[test]
205 fn reads_the_guide_schedulers() {
206 let schedulers = schedulers(DOC_SCHEDULERS);
207 assert_eq!(schedulers.len(), 4);
208
209 let be = &schedulers[0];
210 assert_eq!(be.name, "be-scheduler");
211 let transmit = be.transmit_rate.as_ref().unwrap();
212 assert_eq!(transmit.mode.as_deref(), Some("remainder"));
213 assert_eq!(transmit.value.as_deref(), Some("40"));
214 assert_eq!(be.buffer_size.as_ref().unwrap().mode.as_deref(), Some("percent"));
215 assert_eq!(be.priority.as_deref(), Some("low"));
216 assert!(be.drop_profile_maps.is_empty());
217
218 let af = &schedulers[1];
219 assert_eq!(af.transmit_rate.as_ref().unwrap().value.as_deref(), Some("45"));
220 assert_eq!(af.priority.as_deref(), Some("high"));
221 assert_eq!(af.drop_profile_maps.len(), 2);
222 assert_eq!(af.drop_profile_maps[1].loss_priority.as_deref(), Some("high"));
223 assert_eq!(af.drop_profile_maps[1].protocol.as_deref(), Some("any"));
224 assert_eq!(af.drop_profile_maps[1].drop_profile.as_deref(), Some("af-with-PLP"));
225
226 assert_eq!(schedulers[2].priority.as_deref(), Some("medium-low"));
227 assert_eq!(schedulers[2].transmit_rate, None);
228
229 let cvlan = &schedulers[3];
230 let absolute = cvlan.transmit_rate.as_ref().unwrap();
231 assert_eq!(absolute.mode, None);
232 assert_eq!(absolute.value.as_deref(), Some("20m"));
233 let buffer = cvlan.buffer_size.as_ref().unwrap();
234 assert_eq!(buffer.mode.as_deref(), Some("temporal"));
235 assert_eq!(buffer.value.as_deref(), Some("100k"));
236 }
237
238 #[test]
239 fn reads_the_scheduler_options() {
240 let schedulers = schedulers(EXTRA);
241 let voice = &schedulers[0];
242 let transmit = voice.transmit_rate.as_ref().unwrap();
243 assert!(transmit.exact && !transmit.rate_limit);
244 assert_eq!(transmit.value.as_deref(), Some("10"));
245 let shaping = voice.shaping_rate.as_ref().unwrap();
246 assert_eq!(shaping.mode.as_deref(), Some("percent"));
247 assert_eq!(shaping.burst_size.as_deref(), Some("64k"));
248 assert_eq!(voice.buffer_rate.as_ref().unwrap().value.as_deref(), Some("5"));
249 let excess = voice.excess_rate.as_ref().unwrap();
250 assert_eq!(excess.mode.as_deref(), Some("proportion"));
251 assert_eq!(excess.value.as_deref(), Some("100"));
252 assert_eq!(voice.excess_priority, ["low", "medium-low"]);
253 assert_eq!(voice.adjust_minimum.as_deref(), Some("1m"));
254 assert_eq!(voice.adjust_percent.as_deref(), Some("90"));
255 assert_eq!(voice.buffer_dynamic_threshold.as_deref(), Some("7"));
256 assert!(voice.explicit_congestion_notification);
257 assert_eq!(voice.latency_ecn_threshold.as_deref(), Some("5"));
258 }
259
260 #[test]
261 fn reads_rate_limits_bare_modes_and_single_priorities() {
262 let schedulers = schedulers(EXTRA);
263 let ctl = &schedulers[1];
264 let transmit = ctl.transmit_rate.as_ref().unwrap();
265 assert!(transmit.rate_limit && !transmit.exact);
266 assert_eq!(transmit.mode, None);
267 assert_eq!(transmit.value.as_deref(), Some("2m"));
268 assert_eq!(ctl.excess_priority, ["none"]);
269 assert_eq!(ctl.drop_profile_maps[0].protocol.as_deref(), Some("tcp"));
270
271 let bulk = &schedulers[2];
272 let remainder = bulk.transmit_rate.as_ref().unwrap();
273 assert_eq!(remainder.mode.as_deref(), Some("remainder"));
274 assert_eq!(remainder.value, None);
275 assert_eq!(bulk.buffer_size.as_ref().unwrap().mode.as_deref(), Some("shared"));
276 assert!(!bulk.explicit_congestion_notification);
277 assert!(bulk.excess_priority.is_empty());
278 }
279
280 const DOC_LATENCY_ECN: &str = r#"
283class-of-service {
284 forwarding-classes {
285 class fc1 queue-num 0;
286 }
287 schedulers {
288 sched1 {
289 latency-ecn threshold 50000;
290 }
291 }
292 scheduler-maps {
293 smap1 {
294 forwarding-class fc1 scheduler sched1;
295 }
296 }
297}
298"#;
299
300 #[test]
301 fn reads_the_guide_latency_ecn() {
302 let cos = class_of_service(&juniper::parse_braces(DOC_LATENCY_ECN)).unwrap();
303 assert_eq!(cos.forwarding_classes[0].name, "fc1");
304 assert_eq!(cos.forwarding_classes[0].queue.as_deref(), Some("0"));
305 assert_eq!(cos.schedulers[0].latency_ecn_threshold.as_deref(), Some("50000"));
306 assert_eq!(cos.scheduler_maps[0].entries[0].scheduler.as_deref(), Some("sched1"));
307 }
308}