1use super::scheduler::{rate_of, SchedulerRate};
16use super::words;
17use crate::{active_children, arg_of, child_block, has_leaf, named_children, value_after};
18use asterust::config::ConfigLine;
19
20#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct AppliedMapping {
25 pub kind: String,
27 pub name: String,
29 pub family: Option<String>,
31 pub protocol: Option<String>,
32 pub vlan_tag: Option<String>,
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct AppliedProfile {
39 pub name: String,
40 pub shared_instance: Option<String>,
41}
42
43#[derive(Clone, Debug, Default, PartialEq, Eq)]
45#[non_exhaustive]
46pub struct CosSettings {
47 pub classifiers: Vec<AppliedMapping>,
48 pub rewrite_rules: Vec<AppliedMapping>,
49 pub forwarding_class: Option<String>,
50 pub fragmentation_map: Option<String>,
51 pub scheduler_map: Option<String>,
52 pub scheduler_map_chassis: Option<String>,
53 pub input_scheduler_map: Option<String>,
54 pub shaping_rate: Option<SchedulerRate>,
55 pub input_shaping_rate: Option<SchedulerRate>,
56 pub input_traffic_control_profile: Option<AppliedProfile>,
57 pub input_traffic_control_profile_remaining: Option<AppliedProfile>,
58 pub output_traffic_control_profile: Option<AppliedProfile>,
59 pub output_traffic_control_profile_remaining: Option<AppliedProfile>,
60 pub per_session_scheduler: bool,
61 pub policy_map: Option<String>,
62 pub policy_map_marking: bool,
63 pub hierarchical_scheduler: bool,
64 pub member_link_scheduler: Option<String>,
66 pub congestion_notification_profile: Option<String>,
67 pub dedicated_buffer_profile: Option<String>,
68}
69
70#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct CosUnit {
74 pub number: String,
75 pub settings: CosSettings,
76}
77
78#[derive(Clone, Debug, PartialEq, Eq)]
80#[non_exhaustive]
81pub struct CosBinding {
82 pub name: String,
83 pub output_traffic_control_profile: Option<AppliedProfile>,
84}
85
86#[derive(Clone, Debug, PartialEq, Eq)]
88#[non_exhaustive]
89pub struct CosInterface {
90 pub name: String,
91 pub settings: CosSettings,
92 pub forwarding_class_sets: Vec<CosBinding>,
93 pub slices: Vec<CosBinding>,
94 pub units: Vec<CosUnit>,
95}
96
97#[derive(Clone, Debug, PartialEq, Eq)]
99#[non_exhaustive]
100pub struct CosInterfaceSet {
101 pub name: String,
102 pub excess_bandwidth_share: Option<String>,
104 pub internal_node: bool,
105 pub output_traffic_control_profile: Option<AppliedProfile>,
106 pub output_traffic_control_profile_remaining: Option<AppliedProfile>,
107}
108
109fn parse_mappings(line: &ConfigLine, block: &str) -> Vec<AppliedMapping> {
110 let Some(block) = child_block(line, block) else {
111 return Vec::new();
112 };
113 active_children(block)
114 .filter_map(|statement| {
115 let words = words(statement, 0);
116 let (kind, name) = (words.first()?, words.get(1)?);
117 Some(AppliedMapping {
118 kind: kind.to_string(),
119 name: name.to_string(),
120 family: value_after(&words, "family"),
121 protocol: value_after(&words, "protocol"),
122 vlan_tag: value_after(&words, "vlan-tag"),
123 })
124 })
125 .collect()
126}
127
128fn parse_profile(line: &ConfigLine, keyword: &str) -> Option<AppliedProfile> {
129 let words: Vec<&str> = arg_of(line, keyword)?.split_whitespace().collect();
130 Some(AppliedProfile {
131 name: words.first()?.to_string(),
132 shared_instance: value_after(&words, "shared-instance"),
133 })
134}
135
136fn parse_settings(line: &ConfigLine) -> CosSettings {
137 let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
138 CosSettings {
139 classifiers: parse_mappings(line, "classifiers"),
140 rewrite_rules: parse_mappings(line, "rewrite-rules"),
141 forwarding_class: field("forwarding-class"),
142 fragmentation_map: field("fragmentation-map"),
143 scheduler_map: field("scheduler-map"),
144 scheduler_map_chassis: field("scheduler-map-chassis"),
145 input_scheduler_map: field("input-scheduler-map"),
146 shaping_rate: rate_of(line, "shaping-rate"),
147 input_shaping_rate: rate_of(line, "input-shaping-rate"),
148 input_traffic_control_profile: parse_profile(line, "input-traffic-control-profile"),
149 input_traffic_control_profile_remaining: parse_profile(line, "input-traffic-control-profile-remaining"),
150 output_traffic_control_profile: parse_profile(line, "output-traffic-control-profile"),
151 output_traffic_control_profile_remaining: parse_profile(line, "output-traffic-control-profile-remaining"),
152 per_session_scheduler: has_leaf(line, "per-session-scheduler"),
153 policy_map: field("policy-map"),
154 policy_map_marking: has_leaf(line, "policy-map-marking"),
155 hierarchical_scheduler: has_leaf(line, "hierarchical-scheduler"),
156 member_link_scheduler: field("member-link-scheduler"),
157 congestion_notification_profile: field("congestion-notification-profile"),
158 dedicated_buffer_profile: field("dedicated-buffer-profile"),
159 }
160}
161
162fn parse_bindings(interface: &ConfigLine, keyword: &str) -> Vec<CosBinding> {
163 named_children(interface, keyword)
164 .map(|(name, binding)| CosBinding {
165 name: name.to_string(),
166 output_traffic_control_profile: parse_profile(binding, "output-traffic-control-profile"),
167 })
168 .collect()
169}
170
171fn parse_interface(name: &str, interface: &ConfigLine) -> CosInterface {
172 CosInterface {
173 name: name.to_string(),
174 settings: parse_settings(interface),
175 forwarding_class_sets: parse_bindings(interface, "forwarding-class-set"),
176 slices: parse_bindings(interface, "slice"),
177 units: named_children(interface, "unit")
178 .map(|(number, unit)| CosUnit {
179 number: number.to_string(),
180 settings: parse_settings(unit),
181 })
182 .collect(),
183 }
184}
185
186fn parse_interface_set(name: &str, set: &ConfigLine) -> CosInterfaceSet {
187 let share = crate::statement_words(set, "excess-bandwidth-share").map(|words| words.join(" "));
188 CosInterfaceSet {
189 name: name.to_string(),
190 excess_bandwidth_share: share,
191 internal_node: has_leaf(set, "internal-node"),
192 output_traffic_control_profile: parse_profile(set, "output-traffic-control-profile"),
193 output_traffic_control_profile_remaining: parse_profile(set, "output-traffic-control-profile-remaining"),
194 }
195}
196
197pub(super) fn parse_interfaces(cos: &ConfigLine) -> (Vec<CosInterface>, Vec<CosInterfaceSet>) {
198 let Some(block) = child_block(cos, "interfaces") else {
199 return (Vec::new(), Vec::new());
200 };
201 let mut interfaces = Vec::new();
202 let mut sets = Vec::new();
203 for child in active_children(block) {
204 if let Some(name) = child.arg("interface-set") {
205 sets.push(parse_interface_set(
206 name.split_whitespace().next().unwrap_or(name),
207 child,
208 ));
209 } else if let Some(name) = child.text.split_whitespace().next() {
210 interfaces.push(parse_interface(name, child));
211 }
212 }
213 (interfaces, sets)
214}
215
216#[cfg(test)]
217mod tests {
218 use crate::class_of_service::class_of_service;
219 use asterust::config::juniper;
220
221 const DOC_UNITS: &str = r#"
225class-of-service {
226 interfaces {
227 ge-0/0/0 {
228 unit 0 {
229 classifiers {
230 dscp ba-classifier;
231 }
232 rewrite-rules {
233 dscp-ipv6 rewrite-ipv6-dscps;
234 }
235 scheduler-map diffserv-cos-map;
236 }
237 }
238 ge-0/0/1 {
239 scheduler-map be-map;
240 }
241 }
242}
243"#;
244
245 const DOC_HIERARCHY: &str = r#"
246class-of-service {
247 interfaces {
248 ge-1/0/0 {
249 output-traffic-control-profile tcp-500m-shaping-rate;
250 unit 0 {
251 output-traffic-control-profile tcp-cvlan0;
252 }
253 unit 1 {
254 output-traffic-control-profile tcp-cvlan1;
255 }
256 }
257 interface-set svlan0 {
258 output-traffic-control-profile tcp-svlan0;
259 }
260 interface-set svlan1 {
261 output-traffic-control-profile tcp-svlan1;
262 output-traffic-control-profile-remaining tcp-svlan1-remaining;
263 }
264 }
265}
266"#;
267
268 const EXTRA: &str = r#"
270class-of-service {
271 interfaces {
272 xe-0/0/0 {
273 hierarchical-scheduler;
274 shaping-rate 5g burst-size 128k;
275 input-shaping-rate percent 50;
276 input-scheduler-map in-map;
277 forwarding-class-set VOICE {
278 output-traffic-control-profile tcp-voice;
279 }
280 slice A {
281 output-traffic-control-profile tcp-a shared-instance si1;
282 }
283 member-link-scheduler scale;
284 congestion-notification-profile cnp1;
285 dedicated-buffer-profile dbp1;
286 classifiers {
287 ieee-802.1 dot1p vlan-tag outer;
288 }
289 rewrite-rules {
290 exp mpls-out protocol mpls-any;
291 exp-push-push-push default;
292 }
293 unit 100 {
294 forwarding-class be;
295 fragmentation-map fm1;
296 input-traffic-control-profile tcp-in shared-instance in1;
297 per-session-scheduler;
298 policy-map pm1;
299 policy-map-marking;
300 classifiers {
301 dscp default family mpls;
302 }
303 }
304 }
305 interface-set EDGE {
306 excess-bandwidth-share proportional 100;
307 internal-node;
308 }
309 interface-set SPLIT {
310 excess-bandwidth-share equal;
311 }
312 }
313}
314"#;
315
316 fn cos(text: &str) -> crate::class_of_service::ClassOfService {
317 class_of_service(&juniper::parse_braces(text)).unwrap()
318 }
319
320 #[test]
321 fn reads_the_guide_units() {
322 let cos = cos(DOC_UNITS);
323 assert_eq!(cos.interfaces.len(), 2);
324 let first = &cos.interfaces[0];
325 assert_eq!(first.name, "ge-0/0/0");
326 assert_eq!(first.units.len(), 1);
327 let unit = &first.units[0];
328 assert_eq!(unit.number, "0");
329 assert_eq!(unit.settings.classifiers[0].kind, "dscp");
330 assert_eq!(unit.settings.classifiers[0].name, "ba-classifier");
331 assert_eq!(unit.settings.rewrite_rules[0].kind, "dscp-ipv6");
332 assert_eq!(unit.settings.rewrite_rules[0].name, "rewrite-ipv6-dscps");
333 assert_eq!(unit.settings.scheduler_map.as_deref(), Some("diffserv-cos-map"));
334 assert_eq!(cos.interfaces[1].settings.scheduler_map.as_deref(), Some("be-map"));
335 assert!(cos.interfaces[1].units.is_empty());
336 }
337
338 #[test]
339 fn reads_the_guide_hierarchy() {
340 let cos = cos(DOC_HIERARCHY);
341 assert_eq!(cos.interfaces.len(), 1);
342 let port = &cos.interfaces[0];
343 let profile = port.settings.output_traffic_control_profile.as_ref().unwrap();
344 assert_eq!(profile.name, "tcp-500m-shaping-rate");
345 assert_eq!(profile.shared_instance, None);
346 assert_eq!(port.units.len(), 2);
347 assert_eq!(
348 port.units[1]
349 .settings
350 .output_traffic_control_profile
351 .as_ref()
352 .unwrap()
353 .name,
354 "tcp-cvlan1"
355 );
356 assert_eq!(cos.interface_sets.len(), 2);
357 assert_eq!(cos.interface_sets[0].name, "svlan0");
358 assert_eq!(
359 cos.interface_sets[0]
360 .output_traffic_control_profile
361 .as_ref()
362 .unwrap()
363 .name,
364 "tcp-svlan0"
365 );
366 assert_eq!(cos.interface_sets[0].output_traffic_control_profile_remaining, None);
367 assert_eq!(
368 cos.interface_sets[1]
369 .output_traffic_control_profile_remaining
370 .as_ref()
371 .unwrap()
372 .name,
373 "tcp-svlan1-remaining"
374 );
375 }
376
377 #[test]
378 fn reads_the_interface_options() {
379 let interface = &cos(EXTRA).interfaces[0];
380 let settings = &interface.settings;
381 assert!(settings.hierarchical_scheduler);
382 let shaping = settings.shaping_rate.as_ref().unwrap();
383 assert_eq!(shaping.value.as_deref(), Some("5g"));
384 assert_eq!(shaping.burst_size.as_deref(), Some("128k"));
385 let input = settings.input_shaping_rate.as_ref().unwrap();
386 assert_eq!(input.mode.as_deref(), Some("percent"));
387 assert_eq!(settings.input_scheduler_map.as_deref(), Some("in-map"));
388 assert_eq!(settings.member_link_scheduler.as_deref(), Some("scale"));
389 assert_eq!(settings.congestion_notification_profile.as_deref(), Some("cnp1"));
390 assert_eq!(settings.dedicated_buffer_profile.as_deref(), Some("dbp1"));
391 assert_eq!(interface.forwarding_class_sets[0].name, "VOICE");
392 assert_eq!(
393 interface.forwarding_class_sets[0]
394 .output_traffic_control_profile
395 .as_ref()
396 .unwrap()
397 .name,
398 "tcp-voice"
399 );
400 let slice = interface.slices[0].output_traffic_control_profile.as_ref().unwrap();
401 assert_eq!(slice.name, "tcp-a");
402 assert_eq!(slice.shared_instance.as_deref(), Some("si1"));
403 }
404
405 #[test]
406 fn reads_vlan_tags_protocols_and_families() {
407 let interface = &cos(EXTRA).interfaces[0];
408 let classifier = &interface.settings.classifiers[0];
409 assert_eq!(classifier.kind, "ieee-802.1");
410 assert_eq!(classifier.vlan_tag.as_deref(), Some("outer"));
411 assert_eq!(
412 interface.settings.rewrite_rules[0].protocol.as_deref(),
413 Some("mpls-any")
414 );
415 assert_eq!(interface.settings.rewrite_rules[1].kind, "exp-push-push-push");
416 assert_eq!(interface.settings.rewrite_rules[1].name, "default");
417 let unit = &interface.units[0];
418 assert_eq!(unit.settings.classifiers[0].name, "default");
419 assert_eq!(unit.settings.classifiers[0].family.as_deref(), Some("mpls"));
420 }
421
422 #[test]
423 fn reads_the_unit_options() {
424 let unit = &cos(EXTRA).interfaces[0].units[0];
425 assert_eq!(unit.number, "100");
426 assert_eq!(unit.settings.forwarding_class.as_deref(), Some("be"));
427 assert_eq!(unit.settings.fragmentation_map.as_deref(), Some("fm1"));
428 let profile = unit.settings.input_traffic_control_profile.as_ref().unwrap();
429 assert_eq!(profile.name, "tcp-in");
430 assert_eq!(profile.shared_instance.as_deref(), Some("in1"));
431 assert!(unit.settings.per_session_scheduler);
432 assert_eq!(unit.settings.policy_map.as_deref(), Some("pm1"));
433 assert!(unit.settings.policy_map_marking);
434 assert!(!unit.settings.hierarchical_scheduler);
435 }
436
437 #[test]
438 fn reads_the_interface_set_options() {
439 let sets = cos(EXTRA).interface_sets;
440 assert_eq!(sets.len(), 2);
441 assert_eq!(sets[0].name, "EDGE");
442 assert_eq!(sets[0].excess_bandwidth_share.as_deref(), Some("proportional 100"));
443 assert!(sets[0].internal_node);
444 assert_eq!(sets[1].excess_bandwidth_share.as_deref(), Some("equal"));
445 assert!(!sets[1].internal_node);
446 }
447}