Skip to main content

asterust_junos/class_of_service/
traffic_control_profile.rs

1//! `traffic-control-profiles`
2//! `class-of-service { traffic-control-profiles { <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [traffic-control-profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traffic-control-profiles-edit-cos.html)
7//! - [guaranteed-rate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/guaranteed-rate-edit-cos.html)
8//! - [delay-buffer-rate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delay-buffer-rate-edit-cos.html)
9//! - [overhead-accounting](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/overhead-accounting-edit-cos.html)
10//! - [Example: Configuring a Four-Level Scheduler Hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-hierarchical-scheduler-security-configuring.html)
11//! - [Example: Controlling Remaining Traffic](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-control-remaining-traffic-security-configuring.html)
12
13use super::scheduler::{parse_rate, rate_of, SchedulerRate};
14use super::words;
15use crate::{active_children, arg_of, child_block, has_leaf, statement_words, value_after};
16use asterust::config::ConfigLine;
17
18/// An `overhead-accounting <mode> [bytes <n>]` statement.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct OverheadAccounting {
22    /// `frame-mode`, `cell-mode`, `frame-mode-bytes` or `cell-mode-bytes`.
23    pub mode: String,
24    pub bytes: Option<String>,
25}
26
27/// A per-priority `shaping-rate-*` or `excess-rate-*` statement of a traffic-control profile.
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct ProfileRate {
31    /// The statement, such as `shaping-rate-priority-high` or `excess-rate-low`.
32    pub statement: String,
33    pub rate: SchedulerRate,
34}
35
36/// A `traffic-control-profiles` entry.
37#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct TrafficControlProfile {
40    pub name: String,
41    pub shaping_rate: Option<SchedulerRate>,
42    pub guaranteed_rate: Option<SchedulerRate>,
43    pub delay_buffer_rate: Option<SchedulerRate>,
44    pub excess_rate: Option<SchedulerRate>,
45    /// The `shaping-rate-excess-*`, `shaping-rate-priority-*` and `excess-rate-*` statements, in the order written.
46    pub priority_rates: Vec<ProfileRate>,
47    pub scheduler_map: Option<String>,
48    pub overhead_accounting: Option<OverheadAccounting>,
49    pub adjust_minimum: Option<String>,
50    pub atm_service: Option<String>,
51    pub max_burst_size: Option<String>,
52    pub peak_rate: Option<String>,
53    pub sustained_rate: Option<String>,
54    pub granularity_mode_low: bool,
55    pub queue_mode_4q: bool,
56    pub strict_priority_scheduler: bool,
57}
58
59fn parse_profile(profile: &ConfigLine) -> Option<TrafficControlProfile> {
60    let name = profile.text.split_whitespace().next()?;
61    let field = |keyword: &str| arg_of(profile, keyword).map(str::to_string);
62    Some(TrafficControlProfile {
63        name: name.to_string(),
64        shaping_rate: rate_of(profile, "shaping-rate"),
65        guaranteed_rate: rate_of(profile, "guaranteed-rate"),
66        delay_buffer_rate: rate_of(profile, "delay-buffer-rate"),
67        excess_rate: rate_of(profile, "excess-rate"),
68        priority_rates: active_children(profile)
69            .filter_map(|child| {
70                let statement = child.text.split_whitespace().next()?;
71                (statement.starts_with("shaping-rate-") || statement.starts_with("excess-rate-")).then(|| ProfileRate {
72                    statement: statement.to_string(),
73                    rate: parse_rate(&words(child, 1)),
74                })
75            })
76            .collect(),
77        scheduler_map: field("scheduler-map"),
78        overhead_accounting: statement_words(profile, "overhead-accounting").and_then(|words| {
79            Some(OverheadAccounting {
80                mode: words.first()?.to_string(),
81                bytes: value_after(&words, "bytes"),
82            })
83        }),
84        adjust_minimum: field("adjust-minimum"),
85        atm_service: field("atm-service"),
86        max_burst_size: field("max-burst-size"),
87        peak_rate: field("peak-rate"),
88        sustained_rate: field("sustained-rate"),
89        granularity_mode_low: has_leaf(profile, "granularity-mode-low"),
90        queue_mode_4q: has_leaf(profile, "queue-mode-4q"),
91        strict_priority_scheduler: has_leaf(profile, "strict-priority-scheduler"),
92    })
93}
94
95pub(super) fn parse_traffic_control_profiles(cos: &ConfigLine) -> Vec<TrafficControlProfile> {
96    let Some(block) = child_block(cos, "traffic-control-profiles") else {
97        return Vec::new();
98    };
99    active_children(block).filter_map(parse_profile).collect()
100}
101
102#[cfg(test)]
103mod tests {
104    use crate::class_of_service::class_of_service;
105    use asterust::config::juniper;
106
107    // The `traffic-control-profiles` of Example: Configuring a Four-Level Scheduler Hierarchy and Example: Controlling
108    // Remaining Traffic (Class of Service User Guide for Security Devices), wrapped in `class-of-service` and
109    // shortened; see the README "Sources".
110    const DOC_PROFILES: &str = r#"
111class-of-service {
112    traffic-control-profiles {
113        tcp-500m-shaping-rate {
114            shaping-rate 500m;
115        }
116        tcp-svlan0 {
117            shaping-rate 200m;
118            guaranteed-rate 100m;
119            delay-buffer-rate 300m;
120        }
121        tcp-cvlan0 {
122            shaping-rate 100m;
123            guaranteed-rate 60m;
124            scheduler-map tcp-map-cvlan0;
125        }
126        tcp-svlan1-remaining {
127            shaping-rate 300m;
128            guaranteed-rate 200m;
129            scheduler-map smap-remainder;
130        }
131        tcp-ifl1 {
132            scheduler-map smap-ifl1;
133        }
134    }
135}
136"#;
137
138    // Not from documentation: written from the CLI Reference. See the README "Sources".
139    const EXTRA: &str = r#"
140class-of-service {
141    traffic-control-profiles {
142        full {
143            shaping-rate percent 80 burst-size 64k;
144            guaranteed-rate 10m burst-size 32k;
145            excess-rate proportion 20;
146            excess-rate-high percent 60;
147            excess-rate-low proportion 5;
148            shaping-rate-priority-high 50m;
149            shaping-rate-excess-low percent 10 burst-size 8k;
150            overhead-accounting frame-mode-bytes bytes 20;
151            adjust-minimum 1m;
152            atm-service cbr;
153            max-burst-size 64;
154            peak-rate 100m;
155            sustained-rate 50m;
156            granularity-mode-low;
157            queue-mode-4q;
158            strict-priority-scheduler;
159        }
160        plain {
161            overhead-accounting cell-mode;
162        }
163    }
164}
165"#;
166
167    fn profiles(text: &str) -> Vec<super::TrafficControlProfile> {
168        class_of_service(&juniper::parse_braces(text))
169            .unwrap()
170            .traffic_control_profiles
171    }
172
173    #[test]
174    fn reads_the_guide_profiles() {
175        let profiles = profiles(DOC_PROFILES);
176        assert_eq!(profiles.len(), 5);
177        assert_eq!(profiles[0].name, "tcp-500m-shaping-rate");
178        assert_eq!(
179            profiles[0].shaping_rate.as_ref().unwrap().value.as_deref(),
180            Some("500m")
181        );
182        assert_eq!(profiles[0].guaranteed_rate, None);
183        let svlan = &profiles[1];
184        assert_eq!(svlan.shaping_rate.as_ref().unwrap().value.as_deref(), Some("200m"));
185        assert_eq!(svlan.guaranteed_rate.as_ref().unwrap().value.as_deref(), Some("100m"));
186        assert_eq!(svlan.delay_buffer_rate.as_ref().unwrap().value.as_deref(), Some("300m"));
187        assert_eq!(svlan.scheduler_map, None);
188        assert_eq!(profiles[2].scheduler_map.as_deref(), Some("tcp-map-cvlan0"));
189        assert_eq!(profiles[3].name, "tcp-svlan1-remaining");
190        assert_eq!(profiles[4].name, "tcp-ifl1");
191        assert_eq!(profiles[4].scheduler_map.as_deref(), Some("smap-ifl1"));
192        assert_eq!(profiles[4].shaping_rate, None);
193    }
194
195    #[test]
196    fn reads_the_rates() {
197        let profile = &profiles(EXTRA)[0];
198        let shaping = profile.shaping_rate.as_ref().unwrap();
199        assert_eq!(shaping.mode.as_deref(), Some("percent"));
200        assert_eq!(shaping.value.as_deref(), Some("80"));
201        assert_eq!(shaping.burst_size.as_deref(), Some("64k"));
202        let guaranteed = profile.guaranteed_rate.as_ref().unwrap();
203        assert_eq!(guaranteed.value.as_deref(), Some("10m"));
204        assert_eq!(guaranteed.burst_size.as_deref(), Some("32k"));
205        let excess = profile.excess_rate.as_ref().unwrap();
206        assert_eq!(excess.mode.as_deref(), Some("proportion"));
207        assert_eq!(excess.value.as_deref(), Some("20"));
208    }
209
210    #[test]
211    fn reads_the_priority_rates_in_order() {
212        let rates = &profiles(EXTRA)[0].priority_rates;
213        assert_eq!(rates.len(), 4);
214        assert_eq!(rates[0].statement, "excess-rate-high");
215        assert_eq!(rates[0].rate.mode.as_deref(), Some("percent"));
216        assert_eq!(rates[1].statement, "excess-rate-low");
217        assert_eq!(rates[2].statement, "shaping-rate-priority-high");
218        assert_eq!(rates[2].rate.value.as_deref(), Some("50m"));
219        assert_eq!(rates[3].statement, "shaping-rate-excess-low");
220        assert_eq!(rates[3].rate.burst_size.as_deref(), Some("8k"));
221    }
222
223    #[test]
224    fn reads_the_other_options() {
225        let profiles = profiles(EXTRA);
226        let profile = &profiles[0];
227        let overhead = profile.overhead_accounting.as_ref().unwrap();
228        assert_eq!(overhead.mode, "frame-mode-bytes");
229        assert_eq!(overhead.bytes.as_deref(), Some("20"));
230        assert_eq!(profile.adjust_minimum.as_deref(), Some("1m"));
231        assert_eq!(profile.atm_service.as_deref(), Some("cbr"));
232        assert_eq!(profile.max_burst_size.as_deref(), Some("64"));
233        assert_eq!(profile.peak_rate.as_deref(), Some("100m"));
234        assert_eq!(profile.sustained_rate.as_deref(), Some("50m"));
235        assert!(profile.granularity_mode_low && profile.queue_mode_4q && profile.strict_priority_scheduler);
236
237        let plain = &profiles[1];
238        assert_eq!(plain.overhead_accounting.as_ref().unwrap().mode, "cell-mode");
239        assert_eq!(plain.overhead_accounting.as_ref().unwrap().bytes, None);
240        assert!(plain.priority_rates.is_empty());
241        assert!(!plain.queue_mode_4q);
242    }
243}