Skip to main content

asterust_junos/interfaces/
services_options.rs

1//! `services-options`
2//! `interfaces { <name> { services-options { ... } } }`.
3//!
4//! # See also
5//!
6//! - [services-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-options-edit-interfaces.html)
7//! - [services-options (Next Gen Services Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-options-edit-interfaces-usf.html)
8//! - [ignore-errors](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ignore-errors-edit-interfaces.html)
9//! - [trio-flow-offload](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/trio-flow-offload-edit-services.html)
10//! - [utc-timestamp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/utc-timestamp-edit-services.html)
11//! - [enable-subscriber-analysis](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enable-subscriber-edit-interface-service-options-usf.html)
12//! - [message-rate-limit (jflow-log)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/message-rate-limit-edit-interfaces-services-options.html)
13
14use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
15use crate::{arg_of, child_block, has_leaf, named_children, statement_words, value_after};
16use asterust::config::ConfigLine;
17
18/// The `session-limit` of the services options.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct SessionLimit {
22    pub maximum: Option<String>,
23    /// New sessions per second.
24    pub rate: Option<String>,
25    /// Percentage.
26    pub cpu_load_threshold: Option<String>,
27}
28
29/// A `host` of the services `syslog`.
30#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct ServicesSyslogHost {
33    pub name: String,
34    pub facility_override: Option<String>,
35    pub log_prefix: Option<String>,
36    pub port: Option<String>,
37    pub services: Option<String>,
38}
39
40/// The `syslog` block of the services options.
41#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub struct ServicesSyslog {
44    pub message_rate_limit: Option<String>,
45    pub hosts: Vec<ServicesSyslogHost>,
46}
47
48/// The `services-options` of a services interface.
49#[derive(Clone, Debug, PartialEq, Eq)]
50#[non_exhaustive]
51pub struct ServicesOptions {
52    pub cgn_pic: bool,
53    pub close_timeout: Option<String>,
54    pub fragment_limit: Option<String>,
55    pub disable_global_timeout_override: bool,
56    pub enable_subscriber_analysis: bool,
57    pub utc_timestamp: bool,
58    /// The `<alg> <tcp>` words of `ignore-errors`, or none for a bare statement.
59    pub ignore_errors: Option<Vec<String>>,
60    pub inactivity_non_tcp_timeout: Option<String>,
61    pub inactivity_tcp_timeout: Option<String>,
62    pub inactivity_timeout: Option<String>,
63    pub open_timeout: Option<String>,
64    pub pba_interim_logging_interval: Option<String>,
65    pub reassembly_timeout: Option<String>,
66    pub session_timeout: Option<String>,
67    pub tcp_tickles: Option<String>,
68    /// From `trio-flow-offload minimum-bytes`.
69    pub trio_flow_offload_minimum_bytes: Option<String>,
70    pub session_limit: Option<SessionLimit>,
71    /// From `jflow-log { message-rate-limit }`.
72    pub jflow_log_message_rate_limit: Option<String>,
73    pub syslog: Option<ServicesSyslog>,
74    /// From `flow { traceoptions { ... } }`; its `packet-filter` blocks are not read.
75    pub flow_traceoptions: Option<Traceoptions>,
76    /// `brief`, `detail` or `error`.
77    pub flow_trace_level: Option<String>,
78    pub flow_trace_rate_limit: Option<String>,
79}
80
81pub(super) fn parse_services_options(options: &ConfigLine) -> ServicesOptions {
82    let value = |keyword| arg_of(options, keyword).map(str::to_string);
83    let flow = child_block(options, "flow");
84    let flow_trace = flow.and_then(|f| child_block(f, "traceoptions"));
85    ServicesOptions {
86        cgn_pic: has_leaf(options, "cgn-pic"),
87        close_timeout: value("close-timeout"),
88        fragment_limit: value("fragment-limit"),
89        disable_global_timeout_override: has_leaf(options, "disable-global-timeout-override"),
90        enable_subscriber_analysis: has_leaf(options, "enable-subscriber-analysis"),
91        utc_timestamp: has_leaf(options, "utc-timestamp"),
92        ignore_errors: statement_words(options, "ignore-errors")
93            .map(|words| words.into_iter().map(str::to_string).collect()),
94        inactivity_non_tcp_timeout: value("inactivity-non-tcp-timeout"),
95        inactivity_tcp_timeout: value("inactivity-tcp-timeout"),
96        inactivity_timeout: value("inactivity-timeout"),
97        open_timeout: value("open-timeout"),
98        pba_interim_logging_interval: value("pba-interim-logging-interval"),
99        reassembly_timeout: value("reassembly-timeout"),
100        session_timeout: value("session-timeout"),
101        tcp_tickles: value("tcp-tickles"),
102        trio_flow_offload_minimum_bytes: statement_words(options, "trio-flow-offload")
103            .and_then(|words| value_after(&words, "minimum-bytes")),
104        session_limit: child_block(options, "session-limit").map(|limit| SessionLimit {
105            maximum: arg_of(limit, "maximum").map(str::to_string),
106            rate: arg_of(limit, "rate").map(str::to_string),
107            cpu_load_threshold: arg_of(limit, "cpu-load-threshold").map(str::to_string),
108        }),
109        jflow_log_message_rate_limit: child_block(options, "jflow-log")
110            .and_then(|log| arg_of(log, "message-rate-limit"))
111            .map(str::to_string),
112        syslog: child_block(options, "syslog").map(|syslog| ServicesSyslog {
113            message_rate_limit: arg_of(syslog, "message-rate-limit").map(str::to_string),
114            hosts: named_children(syslog, "host")
115                .map(|(name, host)| ServicesSyslogHost {
116                    name: name.to_string(),
117                    facility_override: arg_of(host, "facility-override").map(str::to_string),
118                    log_prefix: arg_of(host, "log-prefix").map(str::to_string),
119                    port: arg_of(host, "port").map(str::to_string),
120                    services: arg_of(host, "services").map(str::to_string),
121                })
122                .collect(),
123        }),
124        flow_traceoptions: flow.and_then(parse_traceoptions),
125        flow_trace_level: flow_trace.and_then(|t| arg_of(t, "trace-level")).map(str::to_string),
126        flow_trace_rate_limit: flow_trace.and_then(|t| arg_of(t, "rate-limit")).map(str::to_string),
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use crate::interfaces::interfaces;
133    use asterust::config::juniper;
134
135    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
136    #[test]
137    fn extracts_services_options() {
138        let ifaces = interfaces(&juniper::parse_braces(
139            r#"
140interfaces {
141    ms-1/0/0 {
142        services-options {
143            cgn-pic;
144            close-timeout 30;
145            fragment-limit 5;
146            disable-global-timeout-override;
147            enable-subscriber-analysis;
148            utc-timestamp;
149            ignore-errors alg tcp;
150            inactivity-non-tcp-timeout 60;
151            inactivity-tcp-timeout 1800;
152            inactivity-timeout 90;
153            open-timeout 20;
154            pba-interim-logging-interval 600;
155            reassembly-timeout 5;
156            session-timeout 300;
157            tcp-tickles 4;
158            trio-flow-offload minimum-bytes 1000;
159            session-limit {
160                maximum 100000;
161                rate 500;
162                cpu-load-threshold 80;
163            }
164            jflow-log {
165                message-rate-limit 1000;
166            }
167            syslog {
168                host 10.1.1.1 {
169                    facility-override local3;
170                    log-prefix nat;
171                    port 514;
172                    services info;
173                }
174                message-rate-limit 200;
175            }
176            flow {
177                traceoptions {
178                    file flow.log size 1m;
179                    flag all;
180                    trace-level detail;
181                    rate-limit 100;
182                }
183            }
184        }
185    }
186    ms-1/0/1 {
187        services-options {
188            ignore-errors;
189        }
190    }
191    ms-1/0/2 {
192        disable;
193    }
194}
195"#,
196        ));
197        let options = ifaces[0].services_options.as_ref().unwrap();
198        assert!(options.cgn_pic && options.disable_global_timeout_override);
199        assert!(options.enable_subscriber_analysis && options.utc_timestamp);
200        assert_eq!(options.close_timeout.as_deref(), Some("30"));
201        assert_eq!(options.fragment_limit.as_deref(), Some("5"));
202        assert_eq!(
203            options.ignore_errors.as_deref(),
204            Some(&["alg".to_string(), "tcp".to_string()][..])
205        );
206        assert_eq!(options.inactivity_non_tcp_timeout.as_deref(), Some("60"));
207        assert_eq!(options.inactivity_tcp_timeout.as_deref(), Some("1800"));
208        assert_eq!(options.inactivity_timeout.as_deref(), Some("90"));
209        assert_eq!(options.open_timeout.as_deref(), Some("20"));
210        assert_eq!(options.pba_interim_logging_interval.as_deref(), Some("600"));
211        assert_eq!(options.reassembly_timeout.as_deref(), Some("5"));
212        assert_eq!(options.session_timeout.as_deref(), Some("300"));
213        assert_eq!(options.tcp_tickles.as_deref(), Some("4"));
214        assert_eq!(options.trio_flow_offload_minimum_bytes.as_deref(), Some("1000"));
215        let limit = options.session_limit.as_ref().unwrap();
216        assert_eq!(limit.maximum.as_deref(), Some("100000"));
217        assert_eq!(limit.rate.as_deref(), Some("500"));
218        assert_eq!(limit.cpu_load_threshold.as_deref(), Some("80"));
219        assert_eq!(options.jflow_log_message_rate_limit.as_deref(), Some("1000"));
220        let syslog = options.syslog.as_ref().unwrap();
221        assert_eq!(syslog.message_rate_limit.as_deref(), Some("200"));
222        assert_eq!(syslog.hosts[0].name, "10.1.1.1");
223        assert_eq!(syslog.hosts[0].facility_override.as_deref(), Some("local3"));
224        assert_eq!(syslog.hosts[0].log_prefix.as_deref(), Some("nat"));
225        assert_eq!(syslog.hosts[0].port.as_deref(), Some("514"));
226        assert_eq!(syslog.hosts[0].services.as_deref(), Some("info"));
227        let trace = options.flow_traceoptions.as_ref().unwrap();
228        assert_eq!(trace.file.as_deref(), Some("flow.log"));
229        assert_eq!(trace.flags[0].name, "all");
230        assert_eq!(options.flow_trace_level.as_deref(), Some("detail"));
231        assert_eq!(options.flow_trace_rate_limit.as_deref(), Some("100"));
232
233        let bare = ifaces[1].services_options.as_ref().unwrap();
234        assert_eq!(bare.ignore_errors, Some(Vec::new()));
235        assert!(!bare.cgn_pic && bare.session_limit.is_none() && bare.syslog.is_none());
236        assert!(bare.flow_traceoptions.is_none());
237        assert!(ifaces[2].services_options.is_none());
238    }
239}