asterust_junos/protocols/bgp/peer_options/
mod.rs1mod bfd_liveness_detection;
35mod graceful_restart;
36mod local_as;
37mod multihop;
38mod multipath;
39mod remove_private;
40
41pub use bfd_liveness_detection::BgpBfd;
42pub use graceful_restart::BgpGracefulRestart;
43pub use local_as::LocalAs;
44pub use multihop::Multihop;
45pub use multipath::BgpMultipath;
46pub use remove_private::RemovePrivate;
47
48use super::family::{parse_families, BgpFamily};
49use crate::{arg_of, has_leaf, statement_words, toggle};
50use asterust::config::ConfigLine;
51use bfd_liveness_detection::parse_bfd;
52use graceful_restart::parse_graceful_restart;
53use local_as::parse_local_as;
54use multihop::parse_multihop;
55use multipath::parse_multipath;
56use remove_private::parse_remove_private;
57
58#[derive(Clone, Debug, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct BgpPeerOptions {
63 pub local_as_options: Option<LocalAs>,
64 pub multihop: Option<Multihop>,
65 pub remove_private: Option<RemovePrivate>,
66 pub bfd_liveness_detection: Option<BgpBfd>,
67 pub graceful_restart: Option<BgpGracefulRestart>,
68 pub multipath: Option<BgpMultipath>,
69 pub address_families: Vec<BgpFamily>,
71 pub authentication_algorithm: Option<String>,
73 pub has_authentication_key: bool,
75 pub authentication_key_chain: Option<String>,
76 pub hold_time: Option<String>,
78 pub out_delay: Option<String>,
80 pub tcp_mss: Option<String>,
81 pub keep: Option<String>,
83 pub local_preference: Option<String>,
84 pub preference: Option<String>,
85 pub bgp_identifier: Option<String>,
86 pub ipsec_sa: Option<String>,
87 pub local_interface: Option<String>,
89 pub metric_out: Option<String>,
91 pub advertise_peer_as: Option<bool>,
93 pub advertise_external: bool,
94 pub advertise_external_conditional: bool,
96 pub passive: bool,
97 pub mtu_discovery: bool,
98 pub advertise_inactive: bool,
99 pub as_override: bool,
100 pub damping: bool,
101 pub no_aggregator_id: bool,
102 pub no_client_reflect: bool,
103 pub log_updown: bool,
104 pub include_mp_next_hop: bool,
105 pub accept_remote_nexthop: bool,
106 pub vpn_apply_export: bool,
107}
108
109pub(super) fn parse_peer_options(line: &ConfigLine) -> BgpPeerOptions {
111 let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
112 let external = statement_words(line, "advertise-external");
113 BgpPeerOptions {
114 local_as_options: parse_local_as(line),
115 multihop: parse_multihop(line),
116 remove_private: parse_remove_private(line),
117 bfd_liveness_detection: parse_bfd(line),
118 graceful_restart: parse_graceful_restart(line),
119 multipath: parse_multipath(line),
120 address_families: parse_families(line),
121 authentication_algorithm: field("authentication-algorithm"),
122 has_authentication_key: arg_of(line, "authentication-key").is_some(),
123 authentication_key_chain: field("authentication-key-chain"),
124 hold_time: field("hold-time"),
125 out_delay: field("out-delay"),
126 tcp_mss: field("tcp-mss"),
127 keep: field("keep"),
128 local_preference: field("local-preference"),
129 preference: field("preference"),
130 bgp_identifier: field("bgp-identifier"),
131 ipsec_sa: field("ipsec-sa"),
132 local_interface: field("local-interface"),
133 metric_out: statement_words(line, "metric-out").map(|words| words.join(" ")),
134 advertise_peer_as: toggle(line, "advertise-peer-as"),
135 advertise_external: external.is_some(),
136 advertise_external_conditional: external.is_some_and(|words| words.contains(&"conditional")),
137 passive: has_leaf(line, "passive"),
138 mtu_discovery: has_leaf(line, "mtu-discovery"),
139 advertise_inactive: has_leaf(line, "advertise-inactive"),
140 as_override: has_leaf(line, "as-override"),
141 damping: has_leaf(line, "damping"),
142 no_aggregator_id: has_leaf(line, "no-aggregator-id"),
143 no_client_reflect: has_leaf(line, "no-client-reflect"),
144 log_updown: has_leaf(line, "log-updown"),
145 include_mp_next_hop: has_leaf(line, "include-mp-next-hop"),
146 accept_remote_nexthop: has_leaf(line, "accept-remote-nexthop"),
147 vpn_apply_export: has_leaf(line, "vpn-apply-export"),
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use crate::protocols::bgp::bgp;
154 use asterust::config::juniper;
155
156 const CONFIG: &str = r#"
158protocols {
159 bgp {
160 hold-time 30;
161 precision-timers;
162 group EBGP {
163 type external;
164 peer-as 65001;
165 local-as 65000 loops 2 private no-prepend-global-as;
166 multihop {
167 ttl 5;
168 no-nexthop-change;
169 }
170 authentication-algorithm hmac-sha-1-96;
171 authentication-key "$9$secret"; ## SECRET-DATA
172 hold-time 90;
173 out-delay 10;
174 tcp-mss 1400;
175 keep all;
176 local-preference 200;
177 preference 100;
178 metric-out minimum-igp 10;
179 remove-private all replace;
180 advertise-peer-as;
181 advertise-external conditional;
182 passive;
183 mtu-discovery;
184 as-override;
185 damping;
186 log-updown;
187 include-mp-next-hop;
188 neighbor 192.0.2.2 {
189 multihop ttl 3;
190 authentication-key-chain BGP-KEYS;
191 no-advertise-peer-as;
192 local-interface ge-0/0/0.0;
193 remove-private;
194 }
195 }
196 }
197}
198"#;
199
200 #[test]
201 fn extracts_group_session_options_without_the_key() {
202 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
203 let options = &bgp.groups[0].options;
204
205 let local_as = options.local_as_options.as_ref().unwrap();
206 assert_eq!(local_as.autonomous_system.as_deref(), Some("65000"));
207 assert_eq!(local_as.loops.as_deref(), Some("2"));
208 assert!(local_as.private);
209 assert!(!local_as.alias);
210 assert!(local_as.no_prepend_global_as);
211
212 let multihop = options.multihop.as_ref().unwrap();
213 assert_eq!(multihop.ttl.as_deref(), Some("5"));
214 assert!(multihop.no_nexthop_change);
215
216 assert_eq!(options.authentication_algorithm.as_deref(), Some("hmac-sha-1-96"));
217 assert!(options.has_authentication_key);
218 assert_eq!(options.hold_time.as_deref(), Some("90"));
219 assert_eq!(options.out_delay.as_deref(), Some("10"));
220 assert_eq!(options.tcp_mss.as_deref(), Some("1400"));
221 assert_eq!(options.keep.as_deref(), Some("all"));
222 assert_eq!(options.local_preference.as_deref(), Some("200"));
223 assert_eq!(options.preference.as_deref(), Some("100"));
224 assert_eq!(options.metric_out.as_deref(), Some("minimum-igp 10"));
225 assert_eq!(options.advertise_peer_as, Some(true));
226 assert!(options.advertise_external);
227 assert!(options.advertise_external_conditional);
228 assert!(options.passive);
229 assert!(options.mtu_discovery);
230 assert!(options.as_override);
231 assert!(options.damping);
232 assert!(options.log_updown);
233 assert!(options.include_mp_next_hop);
234 assert!(!options.no_aggregator_id);
235
236 let remove_private = options.remove_private.as_ref().unwrap();
237 assert!(remove_private.all);
238 assert!(remove_private.replace);
239 assert!(!remove_private.nearest);
240 }
241
242 #[test]
243 fn extracts_neighbor_options_and_older_multihop_form() {
244 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
245 let options = &bgp.groups[0].neighbors[0].options;
246 assert_eq!(options.multihop.as_ref().unwrap().ttl.as_deref(), Some("3"));
247 assert_eq!(options.authentication_key_chain.as_deref(), Some("BGP-KEYS"));
248 assert!(!options.has_authentication_key);
249 assert_eq!(options.advertise_peer_as, Some(false));
250 assert_eq!(options.local_interface.as_deref(), Some("ge-0/0/0.0"));
251 assert!(!options.remove_private.as_ref().unwrap().all);
252 assert_eq!(options.hold_time, None);
253 }
254
255 #[test]
256 fn extracts_global_options_and_flags() {
257 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
258 assert_eq!(bgp.options.hold_time.as_deref(), Some("30"));
259 assert!(bgp.precision_timers);
260 assert!(!bgp.disable);
261 }
262
263 #[test]
264 fn peer_options_default_to_unset() {
265 let bgp = bgp(&juniper::parse_braces(
266 "protocols {\n bgp {\n group G {\n type internal;\n }\n }\n}\n",
267 ))
268 .unwrap();
269 let options = &bgp.groups[0].options;
270 assert!(options.local_as_options.is_none());
271 assert!(options.multihop.is_none());
272 assert!(options.remove_private.is_none());
273 assert_eq!(options.advertise_peer_as, None);
274 assert!(!options.advertise_external);
275 assert!(!options.passive);
276 }
277}