Skip to main content

asterust_junos/class_of_service/
buffer.rs

1//! `shared-buffer`, `dedicated-buffer`, `dedicated-buffer-profile` and `dynamic-threshold-profile`
2//! `class-of-service { shared-buffer { ingress { ... } egress { ... } } dynamic-threshold-profile <name> { ... } }`.
3//!
4//! # See also
5//!
6//! - [shared-buffer](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/shared-buffer-edit-class-of-service-qfx-series.html)
7//! - [buffer-partition](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/buffer-partition-edit-class-of-service-egress-qfx-series.html)
8//! - [Configuring Ingress and Egress Dedicated Buffers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/traffic-mgmt-qfx/topics/topic-map/cos-dedicated-buffer-configuring.html)
9//! - [Configuring Global Ingress and Egress Shared Buffers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/traffic-mgmt-qfx/topics/task/cos-shared-buffer-global-qfx-series-cli.html)
10//! - [Dynamic Threshold Profiles for Shared Buffer Pools](https://networkingdocs.hpe.com/documentation/us/en/software/junos/traffic-mgmt-qfx/topics/topic-map/dynamic-threshold-profiles-for-shared-buffer-pools.html)
11//! - [dynamic-threshold-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-threshold-profile-edit-class-of-service.html)
12
13use super::words;
14use crate::{active_children, arg_of, child_block, named_children, value_after};
15use asterust::config::ConfigLine;
16
17/// A `buffer-partition <kind>` entry of a shared buffer.
18#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct BufferPartition {
21    /// `lossless`, `lossless-headroom`, `lossy` or `multicast`.
22    pub kind: String,
23    pub percent: Option<String>,
24    pub dynamic_threshold: Option<String>,
25}
26
27/// The `ingress` or `egress` block of `shared-buffer`.
28#[derive(Clone, Debug, Default, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct SharedBufferDirection {
31    pub percent: Option<String>,
32    pub partitions: Vec<BufferPartition>,
33}
34
35/// The `shared-buffer` block.
36#[derive(Clone, Debug, Default, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct SharedBuffer {
39    pub ingress: Option<SharedBufferDirection>,
40    pub egress: Option<SharedBufferDirection>,
41}
42
43/// The `dedicated-buffer` block: the share of the buffer that is dedicated rather than shared.
44#[derive(Clone, Debug, Default, PartialEq, Eq)]
45#[non_exhaustive]
46pub struct DedicatedBuffer {
47    pub ingress_percent: Option<String>,
48    pub egress_percent: Option<String>,
49}
50
51/// A `dedicated-buffer-profile` entry.
52#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct DedicatedBufferProfile {
55    pub name: String,
56    /// `none` or a size in cells.
57    pub ingress_buffer_size: Option<String>,
58    pub egress_buffer_size: Option<String>,
59}
60
61/// A `priority-group` or `priority-group-all` entry of a dynamic threshold profile.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct PriorityGroupThreshold {
65    /// `None` for `priority-group-all`.
66    pub priority_group: Option<String>,
67    pub dynamic_threshold: Option<String>,
68}
69
70/// A `dynamic-threshold-profile` entry.
71#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct DynamicThresholdProfile {
74    pub name: String,
75    pub ingress: Vec<PriorityGroupThreshold>,
76}
77
78fn parse_direction(block: &ConfigLine) -> SharedBufferDirection {
79    SharedBufferDirection {
80        percent: arg_of(block, "percent").map(str::to_string),
81        partitions: named_children(block, "buffer-partition")
82            .map(|(kind, partition)| BufferPartition {
83                kind: kind.to_string(),
84                percent: arg_of(partition, "percent").map(str::to_string),
85                dynamic_threshold: arg_of(partition, "dynamic-threshold").map(str::to_string),
86            })
87            .collect(),
88    }
89}
90
91pub(super) fn parse_shared_buffer(cos: &ConfigLine) -> Option<SharedBuffer> {
92    let buffer = child_block(cos, "shared-buffer")?;
93    Some(SharedBuffer {
94        ingress: child_block(buffer, "ingress").map(parse_direction),
95        egress: child_block(buffer, "egress").map(parse_direction),
96    })
97}
98
99pub(super) fn parse_dedicated_buffer(cos: &ConfigLine) -> Option<DedicatedBuffer> {
100    let buffer = child_block(cos, "dedicated-buffer")?;
101    let percent = |direction: &str| {
102        child_block(buffer, direction)
103            .and_then(|block| arg_of(block, "percent"))
104            .map(str::to_string)
105    };
106    Some(DedicatedBuffer {
107        ingress_percent: percent("ingress"),
108        egress_percent: percent("egress"),
109    })
110}
111
112pub(super) fn parse_dedicated_buffer_profiles(cos: &ConfigLine) -> Vec<DedicatedBufferProfile> {
113    named_children(cos, "dedicated-buffer-profile")
114        .map(|(name, profile)| {
115            let size = |direction: &str| {
116                child_block(profile, direction)
117                    .and_then(|block| arg_of(block, "buffer-size"))
118                    .map(str::to_string)
119            };
120            DedicatedBufferProfile {
121                name: name.to_string(),
122                ingress_buffer_size: size("ingress"),
123                egress_buffer_size: size("egress"),
124            }
125        })
126        .collect()
127}
128
129pub(super) fn parse_dynamic_threshold_profiles(cos: &ConfigLine) -> Vec<DynamicThresholdProfile> {
130    named_children(cos, "dynamic-threshold-profile")
131        .map(|(name, profile)| DynamicThresholdProfile {
132            name: name.to_string(),
133            ingress: child_block(profile, "ingress")
134                .map(|ingress| {
135                    active_children(ingress)
136                        .filter_map(|group| {
137                            let words = words(group, 0);
138                            let (keyword, rest) = (words.first()?, &words[1..]);
139                            let priority_group = match *keyword {
140                                "priority-group" => Some(rest.first()?.to_string()),
141                                "priority-group-all" => None,
142                                _ => return None,
143                            };
144                            Some(PriorityGroupThreshold {
145                                priority_group,
146                                dynamic_threshold: value_after(rest, "dynamic-threshold"),
147                            })
148                        })
149                        .collect()
150                })
151                .unwrap_or_default(),
152        })
153        .collect()
154}
155
156#[cfg(test)]
157mod tests {
158    use crate::class_of_service::class_of_service;
159    use asterust::config::juniper;
160
161    // Not from documentation: written from the CLI Reference. See the README "Sources".
162    const EXTRA: &str = r#"
163class-of-service {
164    shared-buffer {
165        ingress {
166            percent 100;
167            buffer-partition lossless {
168                percent 50;
169                dynamic-threshold 7;
170            }
171            buffer-partition lossless-headroom {
172                percent 10;
173            }
174        }
175        egress {
176            percent 90;
177            buffer-partition multicast {
178                percent 20;
179            }
180        }
181    }
182    dynamic-threshold-profile DT1 {
183        ingress {
184            priority-group 3 dynamic-threshold 5;
185            priority-group-all dynamic-threshold 7;
186        }
187    }
188    dynamic-threshold-profile DT2;
189}
190"#;
191
192    #[test]
193    fn reads_the_shared_buffer() {
194        let buffer = class_of_service(&juniper::parse_braces(EXTRA))
195            .unwrap()
196            .shared_buffer
197            .unwrap();
198        let ingress = buffer.ingress.unwrap();
199        assert_eq!(ingress.percent.as_deref(), Some("100"));
200        assert_eq!(ingress.partitions.len(), 2);
201        assert_eq!(ingress.partitions[0].kind, "lossless");
202        assert_eq!(ingress.partitions[0].percent.as_deref(), Some("50"));
203        assert_eq!(ingress.partitions[0].dynamic_threshold.as_deref(), Some("7"));
204        assert_eq!(ingress.partitions[1].dynamic_threshold, None);
205        let egress = buffer.egress.unwrap();
206        assert_eq!(egress.percent.as_deref(), Some("90"));
207        assert_eq!(egress.partitions[0].kind, "multicast");
208    }
209
210    #[test]
211    fn reads_the_dynamic_threshold_profiles() {
212        let profiles = class_of_service(&juniper::parse_braces(EXTRA))
213            .unwrap()
214            .dynamic_threshold_profiles;
215        assert_eq!(profiles.len(), 2);
216        assert_eq!(profiles[0].name, "DT1");
217        assert_eq!(profiles[0].ingress[0].priority_group.as_deref(), Some("3"));
218        assert_eq!(profiles[0].ingress[0].dynamic_threshold.as_deref(), Some("5"));
219        assert_eq!(profiles[0].ingress[1].priority_group, None);
220        assert_eq!(profiles[0].ingress[1].dynamic_threshold.as_deref(), Some("7"));
221        assert!(profiles[1].ingress.is_empty());
222    }
223
224    // The `class-of-service` statements of Configuring Ingress and Egress Dedicated Buffers, Dynamic Threshold Profiles
225    // for Shared Buffer Pools and Example: Recommended Configuration of the Shared Buffer Pool for Networks with Mostly
226    // Lossless Traffic (Traffic Management User Guide for QFX Series Switches and EX4600 Switches), the last two
227    // written in brace form; see the README "Sources".
228    const DOC_DEDICATED: &str = r#"
229class-of-service {
230    dedicated-buffer {
231        ingress {
232            percent 25;
233        }
234        egress {
235            percent 20;
236        }
237    }
238}
239"#;
240
241    const DOC_DYNAMIC_THRESHOLD: &str = r#"
242class-of-service {
243    dynamic-threshold-profile DT1 {
244        ingress {
245            priority-group 0 dynamic-threshold 5;
246            priority-group 1 dynamic-threshold 6;
247            priority-group 5 dynamic-threshold 10;
248            priority-group 7 dynamic-threshold 10;
249        }
250    }
251    interfaces {
252        et-0/0/0 {
253            dynamic-threshold-profile DT1;
254        }
255    }
256}
257"#;
258
259    const DOC_LOSSLESS_BUFFER: &str = r#"
260class-of-service {
261    shared-buffer {
262        ingress {
263            percent 100;
264            buffer-partition lossless {
265                percent 15;
266            }
267            buffer-partition lossless-headroom {
268                percent 80;
269            }
270            buffer-partition lossy {
271                percent 5;
272            }
273        }
274        egress {
275            percent 100;
276            buffer-partition lossless {
277                percent 90;
278            }
279            buffer-partition lossy {
280                percent 5;
281            }
282            buffer-partition multicast {
283                percent 5;
284            }
285        }
286    }
287}
288"#;
289
290    #[test]
291    fn reads_the_guide_dedicated_buffer() {
292        let buffer = class_of_service(&juniper::parse_braces(DOC_DEDICATED))
293            .unwrap()
294            .dedicated_buffer
295            .unwrap();
296        assert_eq!(buffer.ingress_percent.as_deref(), Some("25"));
297        assert_eq!(buffer.egress_percent.as_deref(), Some("20"));
298    }
299
300    #[test]
301    fn reads_the_guide_dynamic_threshold_profile() {
302        let cos = class_of_service(&juniper::parse_braces(DOC_DYNAMIC_THRESHOLD)).unwrap();
303        let profile = &cos.dynamic_threshold_profiles[0];
304        assert_eq!(profile.name, "DT1");
305        assert_eq!(profile.ingress.len(), 4);
306        assert_eq!(profile.ingress[2].priority_group.as_deref(), Some("5"));
307        assert_eq!(profile.ingress[2].dynamic_threshold.as_deref(), Some("10"));
308        assert_eq!(
309            cos.interfaces[0].settings.dynamic_threshold_profile.as_deref(),
310            Some("DT1")
311        );
312    }
313
314    #[test]
315    fn reads_the_guide_lossless_shared_buffer() {
316        let buffer = class_of_service(&juniper::parse_braces(DOC_LOSSLESS_BUFFER))
317            .unwrap()
318            .shared_buffer
319            .unwrap();
320        let ingress = buffer.ingress.unwrap();
321        assert_eq!(ingress.percent.as_deref(), Some("100"));
322        assert_eq!(ingress.partitions.len(), 3);
323        assert_eq!(ingress.partitions[1].kind, "lossless-headroom");
324        assert_eq!(ingress.partitions[1].percent.as_deref(), Some("80"));
325        let egress = buffer.egress.unwrap();
326        assert_eq!(egress.partitions[0].percent.as_deref(), Some("90"));
327        assert_eq!(egress.partitions[2].kind, "multicast");
328    }
329
330    // Not from documentation: written from the CLI Reference and the `set` statements of the guide. See the README
331    // "Sources".
332    #[test]
333    fn reads_the_dedicated_buffer_profiles() {
334        let cos = class_of_service(&juniper::parse_braces(
335            "class-of-service {\n    dedicated-buffer-profile DBP1 {\n        ingress {\n            buffer-size 1000;\n        }\n        egress {\n            buffer-size none;\n        }\n    }\n    dedicated-buffer-profile DBP2;\n    interfaces {\n        xe-0/0/1 {\n            dedicated-buffer-profile DBP1;\n        }\n    }\n}\n",
336        ))
337        .unwrap();
338        assert_eq!(cos.dedicated_buffer_profiles.len(), 2);
339        assert_eq!(
340            cos.dedicated_buffer_profiles[0].ingress_buffer_size.as_deref(),
341            Some("1000")
342        );
343        assert_eq!(
344            cos.dedicated_buffer_profiles[0].egress_buffer_size.as_deref(),
345            Some("none")
346        );
347        assert_eq!(cos.dedicated_buffer_profiles[1].ingress_buffer_size, None);
348        assert_eq!(
349            cos.interfaces[0].settings.dedicated_buffer_profile.as_deref(),
350            Some("DBP1")
351        );
352    }
353}