Skip to main content

asterust_junos/protocols/bgp/
output_queue_priority.rs

1//! `output-queue-priority`
2//! `protocols { bgp { output-queue-priority { ... } } }`.
3//!
4//! # See also
5//!
6//! - [BGP Route Prioritization](https://networkingdocs.hpe.com/documentation/us/en/software/junos/bgp/topics/topic-map/bgp-route-prioritization.html)
7
8use crate::{active_children, child_block, value_after};
9use asterust::config::ConfigLine;
10
11/// The BGP output scheduler: the work tokens of each output queue and the queues behind the `low`, `medium` and
12/// `high` keywords. The per-family `output-queue-priority` is on [`BgpFamily`](crate::protocols::bgp::BgpFamily).
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct OutputQueuePriority {
16    /// `update-tokens` of the `expedited` queue.
17    pub expedited_tokens: Option<String>,
18    /// The queue number and `update-tokens` of each `priority <n>` queue.
19    pub priority_tokens: Vec<(String, String)>,
20    /// A queue as written, e.g. `priority 10` or `expedited`.
21    pub default_low: Option<String>,
22    pub default_medium: Option<String>,
23    pub default_high: Option<String>,
24}
25
26pub(super) fn parse_output_queue_priority(line: &ConfigLine) -> Option<OutputQueuePriority> {
27    let scheduler = child_block(line, "output-queue-priority")?;
28    let mut result = OutputQueuePriority::default();
29    for child in active_children(scheduler) {
30        let words: Vec<&str> = child
31            .text
32            .split_whitespace()
33            .chain(active_children(child).flat_map(|grandchild| grandchild.text.split_whitespace()))
34            .collect();
35        match words.first().copied() {
36            Some("expedited") => result.expedited_tokens = value_after(&words, "update-tokens"),
37            Some("priority") => {
38                if let (Some(queue), Some(tokens)) = (words.get(1), value_after(&words, "update-tokens")) {
39                    result.priority_tokens.push((queue.to_string(), tokens));
40                }
41            }
42            Some("defaults") => {
43                result.default_low = result.default_low.or_else(|| default_queue(&words, "low"));
44                result.default_medium = result.default_medium.or_else(|| default_queue(&words, "medium"));
45                result.default_high = result.default_high.or_else(|| default_queue(&words, "high"));
46            }
47            _ => {}
48        }
49    }
50    Some(result)
51}
52
53/// The words after `class` up to the next class, e.g. `priority 10`.
54fn default_queue(words: &[&str], class: &str) -> Option<String> {
55    const CLASSES: [&str; 3] = ["low", "medium", "high"];
56    let start = words.iter().position(|word| *word == class)? + 1;
57    let end = words[start..]
58        .iter()
59        .position(|word| CLASSES.contains(word))
60        .map_or(words.len(), |offset| start + offset);
61    Some(words[start..end].join(" "))
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::protocols::bgp::bgp;
67    use asterust::config::juniper;
68
69    // Not from documentation: written from the BGP Route Prioritization guide. See the README "Sources".
70    const CONFIG: &str = r#"
71protocols {
72    bgp {
73        output-queue-priority {
74            expedited update-tokens 100;
75            priority 1 update-tokens 1;
76            priority 2 {
77                update-tokens 10;
78            }
79            defaults {
80                low priority 1;
81                medium priority 10;
82                high expedited;
83            }
84        }
85    }
86}
87"#;
88
89    #[test]
90    fn reads_the_tokens_of_each_queue() {
91        let scheduler = bgp(&juniper::parse_braces(CONFIG))
92            .unwrap()
93            .output_queue_priority
94            .unwrap();
95        assert_eq!(scheduler.expedited_tokens.as_deref(), Some("100"));
96        assert_eq!(
97            scheduler.priority_tokens,
98            [("1".to_string(), "1".to_string()), ("2".to_string(), "10".to_string())]
99        );
100    }
101
102    #[test]
103    fn reads_the_default_queues() {
104        let scheduler = bgp(&juniper::parse_braces(CONFIG))
105            .unwrap()
106            .output_queue_priority
107            .unwrap();
108        assert_eq!(scheduler.default_low.as_deref(), Some("priority 1"));
109        assert_eq!(scheduler.default_medium.as_deref(), Some("priority 10"));
110        assert_eq!(scheduler.default_high.as_deref(), Some("expedited"));
111    }
112
113    // Not from documentation: written from the CLI Reference and the BGP Route Prioritization guide.
114    const FAMILY: &str = r#"
115protocols {
116    bgp {
117        group SR {
118            family inet {
119                unicast {
120                    extended-nexthop-color;
121                    extended-nexthop-tunnel;
122                    output-queue-priority priority 7;
123                    route-refresh-priority priority 8;
124                    withdraw-priority expedited;
125                }
126                labeled-unicast;
127            }
128        }
129    }
130}
131"#;
132
133    #[test]
134    fn reads_the_per_family_priorities_and_extended_nexthop_options() {
135        let bgp = bgp(&juniper::parse_braces(FAMILY)).unwrap();
136        let families = &bgp.groups[0].options.address_families;
137        let unicast = &families[0];
138        assert!(unicast.extended_nexthop_color);
139        assert!(unicast.extended_nexthop_tunnel);
140        assert_eq!(unicast.output_queue_priority.as_deref(), Some("priority 7"));
141        assert_eq!(unicast.route_refresh_priority.as_deref(), Some("priority 8"));
142        assert_eq!(unicast.withdraw_priority.as_deref(), Some("expedited"));
143
144        let labeled = &families[1];
145        assert!(!labeled.extended_nexthop_color);
146        assert_eq!(labeled.output_queue_priority, None);
147    }
148
149    #[test]
150    fn is_none_when_absent() {
151        let config = juniper::parse_braces(
152            "protocols {\n    bgp {\n        group G {\n            type external;\n        }\n    }\n}\n",
153        );
154        assert_eq!(bgp(&config).unwrap().output_queue_priority, None);
155    }
156}