1mod as_list;
9mod as_path;
10mod as_path_group;
11mod community;
12mod condition;
13mod damping;
14mod defaults;
15mod filter_list;
16mod lists;
17mod policy_statement;
18mod prefix_list;
19mod resolution_map;
20
21pub use as_list::AsList;
22pub use as_path::AsPath;
23pub use as_path_group::AsPathGroup;
24pub use community::Community;
25pub use condition::{IfRouteExists, IfRouteExistsFamily, PolicyCondition};
26pub use damping::Damping;
27pub use defaults::PolicyDefaults;
28pub use filter_list::RouteFilterList;
29pub use lists::{MacList, RibList};
30pub use policy_statement::{
31 CommunityAction, CommunityOperation, PolicyAction, PolicyActions, PolicyConditions, PolicyPrefixSegment,
32 PolicySrv6Action, PolicyStatement, PolicyTerm, RouteFilter, ValidationDatabaseInstance, ValidationDatabaseMatch,
33};
34pub use prefix_list::PrefixList;
35pub use resolution_map::ResolutionMap;
36
37use crate::{active_children, named_children, top_level};
38use as_list::parse_as_list;
39use as_path::parse_as_path;
40use as_path_group::parse_as_path_group;
41use asterust::config::Config;
42use community::parse_community;
43use condition::parse_condition;
44use damping::parse_damping;
45use defaults::parse_defaults;
46use filter_list::parse_filter_list;
47use lists::{parse_mac_list, parse_rib_list};
48use policy_statement::parse_policy;
49use prefix_list::parse_prefix_list;
50use resolution_map::parse_resolution_map;
51
52#[derive(Clone, Debug, PartialEq, Eq)]
54#[non_exhaustive]
55pub struct PolicyOptions {
56 pub policy_statements: Vec<PolicyStatement>,
57 pub communities: Vec<Community>,
58 pub prefix_lists: Vec<PrefixList>,
59 pub as_paths: Vec<AsPath>,
60 pub as_path_groups: Vec<AsPathGroup>,
61 pub resolution_maps: Vec<ResolutionMap>,
62 pub dampings: Vec<Damping>,
63 pub conditions: Vec<PolicyCondition>,
64 pub route_filter_lists: Vec<RouteFilterList>,
65 pub source_address_filter_lists: Vec<RouteFilterList>,
66 pub as_lists: Vec<AsList>,
67 pub mac_lists: Vec<MacList>,
68 pub rib_lists: Vec<RibList>,
69 pub defaults: Option<PolicyDefaults>,
70}
71
72pub fn policy_options(config: &Config) -> Option<PolicyOptions> {
86 let root = top_level(config, "policy-options")?;
87 Some(PolicyOptions {
88 policy_statements: named_children(root, "policy-statement")
89 .map(|(name, line)| parse_policy(name, line))
90 .collect(),
91 communities: active_children(root).filter_map(parse_community).collect(),
92 prefix_lists: named_children(root, "prefix-list")
93 .map(|(name, line)| parse_prefix_list(name, line))
94 .collect(),
95 as_paths: active_children(root).filter_map(parse_as_path).collect(),
96 as_path_groups: named_children(root, "as-path-group")
97 .map(|(name, line)| parse_as_path_group(name, line))
98 .collect(),
99 resolution_maps: named_children(root, "resolution-map")
100 .map(|(name, line)| parse_resolution_map(name, line))
101 .collect(),
102 dampings: active_children(root).filter_map(parse_damping).collect(),
103 conditions: named_children(root, "condition")
104 .map(|(name, line)| parse_condition(name, line))
105 .collect(),
106 route_filter_lists: named_children(root, "route-filter-list")
107 .map(|(name, line)| parse_filter_list(name, line))
108 .collect(),
109 source_address_filter_lists: named_children(root, "source-address-filter-list")
110 .map(|(name, line)| parse_filter_list(name, line))
111 .collect(),
112 as_lists: active_children(root).filter_map(parse_as_list).collect(),
113 mac_lists: named_children(root, "mac-list")
114 .map(|(name, line)| parse_mac_list(name, line))
115 .collect(),
116 rib_lists: named_children(root, "rib-list")
117 .map(|(name, line)| parse_rib_list(name, line))
118 .collect(),
119 defaults: parse_defaults(root),
120 })
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126 use asterust::config::juniper;
127
128 fn parse(text: &str) -> PolicyOptions {
129 policy_options(&juniper::parse_braces(text)).expect("fixture has a policy-options block")
130 }
131
132 const DOC_COMMUNITY_TAG: &str = r#"
134policy-options {
135 policy-statement Edu-to-isis {
136 term 1 {
137 from {
138 protocol bgp;
139 community Edu;
140 }
141 then {
142 metric 63;
143 accept;
144 }
145 }
146 }
147 policy-statement send-isis-and-direct {
148 term 1 {
149 from {
150 protocol [ isis direct ];
151 route-filter 10.0.0.0/16 orlonger;
152 route-filter 192.168.0.0/16 orlonger;
153 }
154 then accept;
155 }
156 }
157 community Edu members 2:5;
158}
159"#;
160
161 #[test]
162 fn doc_term_with_from_block_and_metric() {
163 let options = parse(DOC_COMMUNITY_TAG);
164 assert_eq!(options.policy_statements.len(), 2);
165
166 let policy = &options.policy_statements[0];
167 assert_eq!(policy.name, "Edu-to-isis");
168 let term = &policy.terms[0];
169 assert_eq!(term.name.as_deref(), Some("1"));
170 assert_eq!(term.from.protocols, ["bgp"]);
171 assert_eq!(term.from.communities, ["Edu"]);
172 assert_eq!(term.then.metric.as_deref(), Some("63"));
173 assert_eq!(term.then.action, Some(PolicyAction::Accept));
174 }
175
176 #[test]
177 fn doc_protocol_list_route_filters_and_leaf_then() {
178 let options = parse(DOC_COMMUNITY_TAG);
179 let term = &options.policy_statements[1].terms[0];
180 assert_eq!(term.from.protocols, ["isis", "direct"]);
181 assert_eq!(term.from.route_filters.len(), 2);
182 assert_eq!(term.from.route_filters[0].prefix, "10.0.0.0/16");
183 assert_eq!(term.from.route_filters[0].match_type.as_deref(), Some("orlonger"));
184 assert_eq!(term.then.action, Some(PolicyAction::Accept));
185 }
186
187 #[test]
188 fn doc_one_line_community() {
189 let options = parse(DOC_COMMUNITY_TAG);
190 assert_eq!(options.communities.len(), 1);
191 assert_eq!(options.communities[0].name, "Edu");
192 assert_eq!(options.communities[0].members, ["2:5"]);
193 assert!(!options.communities[0].invert_match);
194 }
195
196 const DOC_COMMUNITY_ACTIONS: &str = r#"
199policy-options {
200 policy-statement statics {
201 from protocol static;
202 then {
203 community add 1;
204 accept;
205 }
206 }
207 policy-statement remove-communities {
208 term 1 {
209 from protocol bgp;
210 then {
211 community delete wild;
212 accept;
213 }
214 }
215 term 2 {
216 then reject;
217 }
218 }
219 community wild members *:*;
220 community 1 members [ 2:1 2:2 2:3 ];
221}
222"#;
223
224 #[test]
225 fn doc_policy_without_terms_has_one_unnamed_term() {
226 let options = parse(DOC_COMMUNITY_ACTIONS);
227 let statics = &options.policy_statements[0];
228 assert_eq!(statics.terms.len(), 1);
229 let term = &statics.terms[0];
230 assert_eq!(term.name, None);
231 assert_eq!(term.from.protocols, ["static"]);
232 assert_eq!(
233 term.then.communities,
234 [CommunityAction {
235 operation: CommunityOperation::Add,
236 community: "1".to_string()
237 }]
238 );
239 assert_eq!(term.then.action, Some(PolicyAction::Accept));
240 }
241
242 #[test]
243 fn doc_community_delete_and_final_reject_term() {
244 let options = parse(DOC_COMMUNITY_ACTIONS);
245 let remove = &options.policy_statements[1];
246 assert_eq!(remove.terms.len(), 2);
247 assert_eq!(
248 remove.terms[0].then.communities[0].operation,
249 CommunityOperation::Delete
250 );
251 assert_eq!(remove.terms[0].then.communities[0].community, "wild");
252 assert_eq!(remove.terms[1].name.as_deref(), Some("2"));
253 assert_eq!(remove.terms[1].then.action, Some(PolicyAction::Reject));
254 }
255
256 #[test]
257 fn doc_community_members_list() {
258 let options = parse(DOC_COMMUNITY_ACTIONS);
259 assert_eq!(options.communities[0].members, ["*:*"]);
260 assert_eq!(options.communities[1].name, "1");
261 assert_eq!(options.communities[1].members, ["2:1", "2:2", "2:3"]);
262 }
263
264 const CONFIG: &str = r#"
266policy-options {
267 prefix-list CUSTOMERS {
268 192.0.2.0/24;
269 2001:db8::/32;
270 }
271 prefix-list DYNAMIC {
272 apply-path "protocols bgp group <*> neighbor <*>";
273 }
274 as-path FROM-AS65001 ".* 65001";
275 as-path-group TRANSIT {
276 as-path TIER1 "65100 .*";
277 as-path TIER2 "65200 .*";
278 }
279 community BLOCK {
280 invert-match;
281 members [ no-export 65000:666 ];
282 }
283 community WILD members "* : *";
284 policy-statement IMPORT {
285 term FROM-CUSTOMER {
286 from {
287 family inet;
288 neighbor 192.0.2.1;
289 prefix-list CUSTOMERS;
290 prefix-list-filter CUSTOMERS orlonger;
291 as-path FROM-AS65001;
292 route-filter 10.0.0.0/8 upto /24;
293 policy SUBROUTINE;
294 }
295 then {
296 local-preference 200;
297 next-hop self;
298 as-path-prepend "65000 65000";
299 next policy;
300 }
301 }
302 inactive: term DISABLED {
303 then reject;
304 }
305 term SKIP {
306 from protocol ospf;
307 then next term;
308 }
309 then reject;
310 }
311}
312"#;
313
314 #[test]
315 fn extracts_prefix_lists_and_as_paths() {
316 let options = parse(CONFIG);
317 assert_eq!(options.prefix_lists[0].name, "CUSTOMERS");
318 assert_eq!(options.prefix_lists[0].prefixes, ["192.0.2.0/24", "2001:db8::/32"]);
319 assert_eq!(options.prefix_lists[0].apply_path, None);
320 assert!(options.prefix_lists[1].prefixes.is_empty());
321 assert_eq!(
322 options.prefix_lists[1].apply_path.as_deref(),
323 Some("protocols bgp group <*> neighbor <*>")
324 );
325
326 assert_eq!(options.as_paths.len(), 1);
327 assert_eq!(options.as_paths[0].name, "FROM-AS65001");
328 assert_eq!(options.as_paths[0].regex, ".* 65001");
329 let group = &options.as_path_groups[0];
330 assert_eq!(group.name, "TRANSIT");
331 let names: Vec<_> = group.as_paths.iter().map(|p| p.name.as_str()).collect();
332 assert_eq!(names, ["TIER1", "TIER2"]);
333 assert_eq!(group.as_paths[1].regex, "65200 .*");
334 }
335
336 #[test]
337 fn extracts_block_and_quoted_communities() {
338 let options = parse(CONFIG);
339 assert_eq!(options.communities.len(), 2);
340 assert_eq!(options.communities[0].name, "BLOCK");
341 assert_eq!(options.communities[0].members, ["no-export", "65000:666"]);
342 assert!(options.communities[0].invert_match);
343 assert_eq!(options.communities[1].members, ["* : *"]);
344 }
345
346 #[test]
347 fn extracts_conditions_and_manipulation_actions() {
348 let options = parse(CONFIG);
349 let term = &options.policy_statements[0].terms[0];
350 assert_eq!(term.name.as_deref(), Some("FROM-CUSTOMER"));
351 assert_eq!(term.from.families, ["inet"]);
352 assert_eq!(term.from.neighbors, ["192.0.2.1"]);
353 assert_eq!(term.from.prefix_lists, ["CUSTOMERS"]);
354 assert_eq!(term.from.prefix_list_filters, ["CUSTOMERS orlonger"]);
355 assert_eq!(term.from.as_paths, ["FROM-AS65001"]);
356 assert_eq!(term.from.route_filters[0].match_type.as_deref(), Some("upto /24"));
357 assert_eq!(term.from.policies, ["SUBROUTINE"]);
358 assert_eq!(term.then.local_preference.as_deref(), Some("200"));
359 assert_eq!(term.then.next_hop.as_deref(), Some("self"));
360 assert_eq!(term.then.as_path_prepend.as_deref(), Some("65000 65000"));
361 assert_eq!(term.then.action, Some(PolicyAction::NextPolicy));
362 }
363
364 #[test]
365 fn skips_inactive_terms_and_keeps_unnamed_final_term_last() {
366 let options = parse(CONFIG);
367 let terms = &options.policy_statements[0].terms;
368 let names: Vec<_> = terms.iter().map(|t| t.name.as_deref()).collect();
369 assert_eq!(names, [Some("FROM-CUSTOMER"), Some("SKIP"), None]);
370 assert_eq!(terms[1].then.action, Some(PolicyAction::NextTerm));
371 assert_eq!(terms[2].then.action, Some(PolicyAction::Reject));
372 }
373
374 #[test]
375 fn no_policy_options_is_none() {
376 assert!(policy_options(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
377 }
378
379 const DOC_SRV6_TE: &str = r#"
382policy-options {
383 policy-statement LBPP {
384 term 1 {
385 then {
386 load-balance per-packet;
387 }
388 }
389 }
390 policy-statement mpath-resolve {
391 then multipath-resolve;
392 }
393 policy-statement v4vpn1_res_map1 {
394 term 1 {
395 from protocol bgp;
396 then {
397 accept;
398 resolution-map map1;
399 }
400 }
401 }
402 policy-statement v6vpn1_res_map1 {
403 term 1 {
404 from {
405 family inet6-vpn;
406 protocol bgp;
407 }
408 then {
409 accept;
410 resolution-map map1;
411 }
412 }
413 }
414 community to_CE0_community members target:65500:1;
415 resolution-map map1 {
416 mode ip-color;
417 }
418}
419"#;
420
421 const SRV6_ACTION: &str = r#"
424policy-options {
425 policy-statement LL-PREFIXES {
426 term VOICE {
427 from community VOICE;
428 then {
429 srv6 locator LOC128 micro-dt46-sid;
430 accept;
431 }
432 }
433 term STATIC {
434 then srv6 locator LOC1 end-dt4-sid 3001::4;
435 }
436 term PLAIN {
437 then srv6 locator LOC2;
438 }
439 }
440 resolution-map map2 {
441 mode [ color-only ip-color ];
442 }
443}
444"#;
445
446 #[test]
447 fn doc_load_balance_and_multipath_resolve() {
448 let options = parse(DOC_SRV6_TE);
449 let balance = &options.policy_statements[0].terms[0].then;
450 assert_eq!(balance.load_balance.as_deref(), Some("per-packet"));
451 assert!(!balance.multipath_resolve);
452
453 let resolve = &options.policy_statements[1].terms[0].then;
454 assert!(resolve.multipath_resolve);
455 assert_eq!(resolve.load_balance, None);
456 }
457
458 #[test]
459 fn doc_resolution_map_action_and_definition() {
460 let options = parse(DOC_SRV6_TE);
461 let term = &options.policy_statements[3].terms[0];
462 assert_eq!(term.from.families, ["inet6-vpn"]);
463 assert_eq!(term.then.resolution_map.as_deref(), Some("map1"));
464 assert_eq!(term.then.action, Some(PolicyAction::Accept));
465
466 assert_eq!(options.resolution_maps.len(), 1);
467 assert_eq!(options.resolution_maps[0].name, "map1");
468 assert_eq!(options.resolution_maps[0].modes, ["ip-color"]);
469 }
470
471 #[test]
472 fn srv6_actions() {
473 let options = parse(SRV6_ACTION);
474 let terms = &options.policy_statements[0].terms;
475
476 let dynamic = terms[0].then.srv6.as_ref().unwrap();
477 assert_eq!(dynamic.locator, "LOC128");
478 assert_eq!(dynamic.sid_type.as_deref(), Some("micro-dt46-sid"));
479 assert_eq!(dynamic.sid_value, None);
480 assert_eq!(terms[0].then.action, Some(PolicyAction::Accept));
481
482 let fixed = terms[1].then.srv6.as_ref().unwrap();
483 assert_eq!(fixed.locator, "LOC1");
484 assert_eq!(fixed.sid_type.as_deref(), Some("end-dt4-sid"));
485 assert_eq!(fixed.sid_value.as_deref(), Some("3001::4"));
486
487 let bare = terms[2].then.srv6.as_ref().unwrap();
488 assert_eq!(bare.locator, "LOC2");
489 assert_eq!(bare.sid_type, None);
490
491 assert_eq!(options.resolution_maps[0].modes, ["color-only", "ip-color"]);
492 }
493
494 #[test]
497 fn reads_the_guide_dynamic_db() {
498 let options = parse(
499 "policy-options {\n policy-statement dyn_policy1 {\n dynamic-db;\n }\n policy-statement plain {\n then accept;\n }\n prefix-list pl2 {\n dynamic-db;\n }\n}\n",
500 );
501 assert!(options.policy_statements[0].dynamic_db);
502 assert!(options.policy_statements[0].terms.is_empty());
503 assert!(!options.policy_statements[1].dynamic_db);
504 assert!(options.prefix_lists[0].dynamic_db);
505 }
506
507 #[test]
509 fn reads_dynamic_db_on_paths_and_communities() {
510 let options = parse(
511 "policy-options {\n as-path P {\n dynamic-db;\n }\n as-path-group G {\n dynamic-db;\n }\n community C {\n dynamic-db;\n }\n}\n",
512 );
513 assert!(options.as_paths[0].dynamic_db);
514 assert!(options.as_path_groups[0].dynamic_db);
515 assert!(options.communities[0].dynamic_db);
516 }
517}