chiark / gitweb /
make success_report_interval configuraable
[hippotat.git] / src / config.rs
index e6d7e5920590b70838c64c3cc27790ed0818782b..a36ec584f987864a27bb2803ca553dce9d574dd8 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright 2021 Ian Jackson and contributors to Hippotat
-// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-License-Identifier: GPL-3.0-or-later
 // There is NO WARRANTY.
 
 use crate::prelude::*;
@@ -10,9 +10,9 @@ use configparser::ini::Ini;
 #[derive(Debug,Clone)]
 pub struct InstanceConfig {
   // Exceptional settings
-  #[special(special_link, SKL::ServerName)]   pub link:   LinkName,
+  #[special(special_link, SKL::ServerName)] pub   link:   LinkName,
   pub                                             secret: Secret,
-  #[special(special_ipif, SKL::Ordinary)]     pub ipif:   String,
+  #[special(special_ipif, SKL::Ordinary)] pub     ipif:   String,
 
   // Capped settings:
   #[limited]    pub max_batch_down:               u32,
@@ -38,8 +38,12 @@ pub struct InstanceConfig {
   #[client]  pub max_requests_outstanding:     u32,
   #[client]  pub max_batch_up:                 u32,
   #[client]  pub http_retry:                   Duration,
+  #[client]  pub success_report_interval:      Duration,
   #[client]  pub url:                          Uri,
   #[client]  pub vroutes:                      Vec<IpNet>,
+
+  // Computed, rather than looked up.  Client only:
+  #[computed]  pub effective_http_timeout:     Duration,
 }
 
 static DEFAULT_CONFIG: &str = r#"
@@ -54,11 +58,12 @@ max_batch_up = 4000
 http_retry = 5
 port = 80
 vroutes = ''
-ifname_client = hippo%%d
-ifname_server = shippo%%d
+ifname_client = hippo%d
+ifname_server = shippo%d
 max_clock_skew = 300
+success_report_interval = 3600
 
-ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip,%(ifname)s '%(rnets)s'
+ipif = userv root ipif %{local},%{peer},%{mtu},slip,%{ifname} '%{rnets}'
 
 mtu = 1500
 
@@ -86,6 +91,11 @@ pub struct Opts {
   pub extra_config: Vec<PathBuf>,
 }
 
+#[ext(pub)]
+impl u32 {
+  fn sat(self) -> usize { self.try_into().unwrap_or(usize::MAX) }
+}
+
 #[ext]
 impl<'s> Option<&'s str> {
   #[throws(AE)]
@@ -623,6 +633,13 @@ impl<'c> ResolveContext<'c> {
     }
   }
 
+  #[throws(AE)]
+  pub fn computed<T>(&self, _key: &'static str) -> T
+  where T: Default
+  {
+    default()
+  }
+
   #[throws(AE)]
   pub fn special_ipif(&self, key: &'static str) -> String {
     match self.end {
@@ -658,6 +675,18 @@ impl InstanceConfig {
       )?;
     }
 
+    let check_batch = {
+      let mtu = self.mtu;
+      move |max_batch, key| {
+        if max_batch/2 < mtu {
+          throw!(anyhow!("max batch {:?} ({}) must be >= 2 x mtu ({}) \
+                          (to allow for SLIP ESC-encoding)",
+                         key, max_batch, mtu))
+        }
+        Ok::<_,AE>(())
+      }
+    };
+
     match end {
       LinkEnd::Client => {
         if &self.url == &default::<Uri>() {
@@ -676,13 +705,26 @@ impl InstanceConfig {
             })
             .parse().unwrap()
         }
+
+        self.effective_http_timeout = {
+          let a = self.http_timeout;
+          let b = self.http_timeout_grace;
+          a.checked_add(b).ok_or_else(
+            || anyhow!("calculate effective http timeout ({:?} + {:?})", a, b)
+          )?
+        };
+
+        check_batch(self.max_batch_up, "max_batch_up")?;
       },
 
       LinkEnd::Server => {
         if self.addrs.is_empty() {
           throw!(anyhow!("missing 'addrs' setting"))
         }
+        check_batch(self.max_batch_down, "max_batch_down")?;
       },
+
+      // xxx check target vs max req outstanding
     }
 
     #[throws(AE)]
@@ -694,11 +736,11 @@ impl InstanceConfig {
         .collect::<HashMap<String, String>>();
       let bad = parking_lot::Mutex::new(vec![]);
       *var = regex_replace_all!(
-        r#"%(?:%|\((\w+)\)s|.)"#,
+        r#"%(?:%|\((\w+)\)s|\{(\w+)\}|.)"#,
         &var,
-        |whole, k| (|| Ok::<_,String>({
+        |whole, k1, k2| (|| Ok::<_,String>({
           if whole == "%%" { "%" }
-          else if k != "" {
+          else if let Some(&k) = [k1,k2].iter().find(|&&s| s != "") {
             substs.get(k).ok_or_else(
               || format!("unknown key %({})s", k)
             )?
@@ -713,42 +755,39 @@ impl InstanceConfig {
       }
     }
 
-    let ifname = match match end {
-      LinkEnd::Client => (&mut self.ifname_client, "ifname_client"),
-      LinkEnd::Server => (&mut self.ifname_server, "ifname_server"),
-    } { (var,name) => {
-      subst(var, &mut iter::empty()).context(name).context("interface name")?;
-      var
-    } };
-
     {
       use LinkEnd::*;
       type DD<'d> = &'d dyn Display;
       fn dv<T:Display>(v: &[T]) -> String {
         format!("{}", v.iter().format(" "))
       }
-      let vnetwork = dv(&self.vnetwork);
-      let vroutes  = dv(&self.vroutes);
-      
-      let keys = &["local",               "peer",       "rnets",    "ifname"];
+      let mut ipif = mem::take(&mut self.ipif); // lets us borrow all of self
+      let s = &self; // just for abbreviation, below
+      let vnetwork = dv(&s.vnetwork);
+      let vroutes  = dv(&s.vroutes);
+
+      let keys = &["local",       "peer",    "rnets",   "ifname"];
       let values = match end {
       Server => [&self.vaddr as DD      , &self.vrelay, &vnetwork, ifname],
       Client => [&self.link.client as DD, &self.vaddr,  &vroutes,  ifname],
Server => [&s.vaddr as DD      , &s.vrelay, &vnetwork, &s.ifname_server],
Client => [&s.link.client as DD, &s.vaddr,  &vroutes,  &s.ifname_client],
       };
+      let always = [
+        ( "mtu",     &s.mtu as DD ),
+      ];
+
       subst(
-        &mut self.ipif,
+        &mut ipif,
         &mut keys.iter().cloned()
           .zip_eq(values)
-          .chain([( "mtu", &self.mtu as DD )].iter().cloned()),
+          .chain(always.iter().cloned()),
       ).context("ipif")?;
+      self.ipif = ipif;
     }
   }
 }
 
 #[throws(AE)]
-pub fn read(end: LinkEnd) -> Vec<InstanceConfig> {
-  let opts = config::Opts::from_args();
-
+pub fn read(opts: &Opts, end: LinkEnd) -> Vec<InstanceConfig> {
   let agg = (||{
     let mut agg = Aggregate::default();
     agg.keys_allowed.extend(