Skip to main content

asterust_junos/class_of_service/
scheduler.rs

1//! `schedulers`
2//! `class-of-service { schedulers { <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [schedulers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/schedulers-edit-cos.html)
7//! - [transmit-rate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/transmit-rate-edit-cos.html)
8//! - [buffer-size](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/buffer-size-edit-cos.html)
9//! - [shaping-rate (schedulers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/shaping-rate-edit-cos-schedulers.html)
10//! - [excess-rate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/excess-rate-edit-cos.html)
11//! - [excess-priority](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/excess-priority-edit-cos.html)
12//! - [priority (schedulers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/priority-edit-cos-schedulers.html)
13//! - [drop-profile-map](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/drop-profile-map-edit-cos.html)
14//! - [Example: Configuring Class-of-Service Schedulers on a Security Device](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-scheduler-security-configuring.html)
15//! - [Example: Configuring Priority Scheduling](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-priority-scheduling-security-configuring.html)
16
17use super::words;
18use crate::{active_children, arg_of, args_of, child_block, has_leaf, statement_words, value_after};
19use asterust::config::ConfigLine;
20
21/// A rate or size of a scheduler, such as `transmit-rate percent 45`, `shaping-rate 500m` or
22/// `buffer-size temporal 100k`.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct SchedulerRate {
26    /// `percent`, `remainder`, `proportion`, `temporal` or `shared`; `None` for an absolute value.
27    pub mode: Option<String>,
28    /// The value after the mode, or the absolute value.
29    pub value: Option<String>,
30    pub exact: bool,
31    pub rate_limit: bool,
32    pub burst_size: Option<String>,
33}
34
35/// A `drop-profile-map loss-priority <level> protocol <protocol> drop-profile <name>` entry.
36#[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/// A `schedulers` entry.
45#[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    /// The `threshold` of `latency-ecn`.
61    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
67fn 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
85fn parse_drop_profile_map(arg: &str) -> DropProfileMap {
86    let words: Vec<&str> = arg.split_whitespace().collect();
87    DropProfileMap {
88        loss_priority: value_after(&words, "loss-priority"),
89        protocol: value_after(&words, "protocol"),
90        drop_profile: value_after(&words, "drop-profile"),
91    }
92}
93
94fn parse_scheduler(scheduler: &ConfigLine) -> Option<Scheduler> {
95    let name = scheduler.text.split_whitespace().next()?;
96    let rate = |keyword: &str| statement_words(scheduler, keyword).map(|words| parse_rate(&words));
97    let field = |keyword: &str| arg_of(scheduler, keyword).map(str::to_string);
98    Some(Scheduler {
99        name: name.to_string(),
100        transmit_rate: rate("transmit-rate"),
101        shaping_rate: rate("shaping-rate"),
102        buffer_size: rate("buffer-size"),
103        buffer_rate: rate("buffer-rate"),
104        excess_rate: rate("excess-rate"),
105        priority: field("priority"),
106        excess_priority: active_children(scheduler)
107            .find(|child| child.text == "excess-priority" || child.arg("excess-priority").is_some())
108            .map(|child| words(child, 1).into_iter().map(str::to_string).collect())
109            .unwrap_or_default(),
110        adjust_minimum: field("adjust-minimum"),
111        adjust_percent: field("adjust-percent"),
112        buffer_dynamic_threshold: field("buffer-dynamic-threshold"),
113        explicit_congestion_notification: has_leaf(scheduler, "explicit-congestion-notification"),
114        latency_ecn_threshold: statement_words(scheduler, "latency-ecn")
115            .and_then(|words| value_after(&words, "threshold")),
116        drop_profile_maps: args_of(scheduler, "drop-profile-map")
117            .into_iter()
118            .map(parse_drop_profile_map)
119            .collect(),
120    })
121}
122
123pub(super) fn parse_schedulers(cos: &ConfigLine) -> Vec<Scheduler> {
124    let Some(block) = child_block(cos, "schedulers") else {
125        return Vec::new();
126    };
127    active_children(block).filter_map(parse_scheduler).collect()
128}
129
130#[cfg(test)]
131mod tests {
132    use crate::class_of_service::class_of_service;
133    use asterust::config::juniper;
134
135    // The `schedulers` of Example: Configuring Class-of-Service Schedulers on a Security Device, Example: Configuring
136    // Priority Scheduling and Example: Configuring a Four-Level Scheduler Hierarchy (Class of Service User Guide for
137    // Security Devices), wrapped in `class-of-service` and shortened; see the README "Sources".
138    const DOC_SCHEDULERS: &str = r#"
139class-of-service {
140    schedulers {
141        be-scheduler {
142            transmit-rate remainder 40;
143            buffer-size percent 40;
144            priority low;
145        }
146        af-scheduler {
147            transmit-rate percent 45;
148            buffer-size percent 45;
149            priority high;
150            drop-profile-map loss-priority low protocol any drop-profile af-normal;
151            drop-profile-map loss-priority high protocol any drop-profile af-with-PLP;
152        }
153        be-sched {
154            priority medium-low;
155        }
156        sched-cvlan0-qx {
157            transmit-rate 20m;
158            buffer-size temporal 100k;
159            priority low;
160            drop-profile-map loss-priority low protocol any drop-profile dp-low;
161        }
162    }
163}
164"#;
165
166    // Not from documentation: written from the CLI Reference. See the README "Sources".
167    const EXTRA: &str = r#"
168class-of-service {
169    schedulers {
170        voice {
171            transmit-rate percent 10 exact;
172            shaping-rate percent 20 burst-size 64k;
173            buffer-rate percent 5;
174            excess-rate proportion 100;
175            excess-priority [ low medium-low ];
176            adjust-minimum 1m;
177            adjust-percent 90;
178            buffer-dynamic-threshold 7;
179            explicit-congestion-notification;
180            latency-ecn threshold 5;
181        }
182        ctl {
183            transmit-rate 2m rate-limit;
184            excess-priority none;
185            drop-profile-map loss-priority any protocol tcp drop-profile red;
186        }
187        bulk {
188            transmit-rate remainder;
189            buffer-size shared;
190        }
191    }
192}
193"#;
194
195    fn schedulers(text: &str) -> Vec<super::Scheduler> {
196        class_of_service(&juniper::parse_braces(text)).unwrap().schedulers
197    }
198
199    #[test]
200    fn reads_the_guide_schedulers() {
201        let schedulers = schedulers(DOC_SCHEDULERS);
202        assert_eq!(schedulers.len(), 4);
203
204        let be = &schedulers[0];
205        assert_eq!(be.name, "be-scheduler");
206        let transmit = be.transmit_rate.as_ref().unwrap();
207        assert_eq!(transmit.mode.as_deref(), Some("remainder"));
208        assert_eq!(transmit.value.as_deref(), Some("40"));
209        assert_eq!(be.buffer_size.as_ref().unwrap().mode.as_deref(), Some("percent"));
210        assert_eq!(be.priority.as_deref(), Some("low"));
211        assert!(be.drop_profile_maps.is_empty());
212
213        let af = &schedulers[1];
214        assert_eq!(af.transmit_rate.as_ref().unwrap().value.as_deref(), Some("45"));
215        assert_eq!(af.priority.as_deref(), Some("high"));
216        assert_eq!(af.drop_profile_maps.len(), 2);
217        assert_eq!(af.drop_profile_maps[1].loss_priority.as_deref(), Some("high"));
218        assert_eq!(af.drop_profile_maps[1].protocol.as_deref(), Some("any"));
219        assert_eq!(af.drop_profile_maps[1].drop_profile.as_deref(), Some("af-with-PLP"));
220
221        assert_eq!(schedulers[2].priority.as_deref(), Some("medium-low"));
222        assert_eq!(schedulers[2].transmit_rate, None);
223
224        let cvlan = &schedulers[3];
225        let absolute = cvlan.transmit_rate.as_ref().unwrap();
226        assert_eq!(absolute.mode, None);
227        assert_eq!(absolute.value.as_deref(), Some("20m"));
228        let buffer = cvlan.buffer_size.as_ref().unwrap();
229        assert_eq!(buffer.mode.as_deref(), Some("temporal"));
230        assert_eq!(buffer.value.as_deref(), Some("100k"));
231    }
232
233    #[test]
234    fn reads_the_scheduler_options() {
235        let schedulers = schedulers(EXTRA);
236        let voice = &schedulers[0];
237        let transmit = voice.transmit_rate.as_ref().unwrap();
238        assert!(transmit.exact && !transmit.rate_limit);
239        assert_eq!(transmit.value.as_deref(), Some("10"));
240        let shaping = voice.shaping_rate.as_ref().unwrap();
241        assert_eq!(shaping.mode.as_deref(), Some("percent"));
242        assert_eq!(shaping.burst_size.as_deref(), Some("64k"));
243        assert_eq!(voice.buffer_rate.as_ref().unwrap().value.as_deref(), Some("5"));
244        let excess = voice.excess_rate.as_ref().unwrap();
245        assert_eq!(excess.mode.as_deref(), Some("proportion"));
246        assert_eq!(excess.value.as_deref(), Some("100"));
247        assert_eq!(voice.excess_priority, ["low", "medium-low"]);
248        assert_eq!(voice.adjust_minimum.as_deref(), Some("1m"));
249        assert_eq!(voice.adjust_percent.as_deref(), Some("90"));
250        assert_eq!(voice.buffer_dynamic_threshold.as_deref(), Some("7"));
251        assert!(voice.explicit_congestion_notification);
252        assert_eq!(voice.latency_ecn_threshold.as_deref(), Some("5"));
253    }
254
255    #[test]
256    fn reads_rate_limits_bare_modes_and_single_priorities() {
257        let schedulers = schedulers(EXTRA);
258        let ctl = &schedulers[1];
259        let transmit = ctl.transmit_rate.as_ref().unwrap();
260        assert!(transmit.rate_limit && !transmit.exact);
261        assert_eq!(transmit.mode, None);
262        assert_eq!(transmit.value.as_deref(), Some("2m"));
263        assert_eq!(ctl.excess_priority, ["none"]);
264        assert_eq!(ctl.drop_profile_maps[0].protocol.as_deref(), Some("tcp"));
265
266        let bulk = &schedulers[2];
267        let remainder = bulk.transmit_rate.as_ref().unwrap();
268        assert_eq!(remainder.mode.as_deref(), Some("remainder"));
269        assert_eq!(remainder.value, None);
270        assert_eq!(bulk.buffer_size.as_ref().unwrap().mode.as_deref(), Some("shared"));
271        assert!(!bulk.explicit_congestion_notification);
272        assert!(bulk.excess_priority.is_empty());
273    }
274}