dslite-b4 0.1.2

DS-Lite B4 tunnel management daemon
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Versioned operational status stored in a file on each supported platform.

use std::{
    net::Ipv6Addr,
    path::{Path, PathBuf},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use anyhow::Context;
use serde::{Deserialize, Deserializer, Serialize};

use crate::atomic_file::atomic_replace;

/// Current JSON status schema version.
pub const SCHEMA_VERSION: u32 = 1;
/// Filename used for the status snapshot inside the runtime directory.
pub const STATUS_FILENAME: &str = "status.json";

/// Versioned snapshot of the daemon's last reconciliation result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StatusSnapshot {
    #[serde(deserialize_with = "deserialize_schema_version")]
    /// Status schema version.
    pub schema_version: u32,
    /// Time at which the snapshot was generated.
    pub generated_at: jiff::Timestamp,
    /// Daemon process identifier.
    pub pid: u32,
    /// Daemon package version.
    pub version: String,
    /// Managed tunnel interface name.
    pub tunnel_name: String,
    /// Availability of desired tunnel state.
    pub desired: StatusDesired,
    /// Source that supplied the current AFTR.
    pub aftr_source: AftrSource,
    #[serde(deserialize_with = "deserialize_required_option")]
    /// Configured or discovered AFTR name or literal.
    pub aftr: Option<String>,
    #[serde(deserialize_with = "deserialize_required_option")]
    /// Selected local IPv6 tunnel endpoint.
    pub local_ipv6: Option<Ipv6Addr>,
    #[serde(deserialize_with = "deserialize_required_option")]
    /// Resolved remote IPv6 tunnel endpoint.
    pub remote_ipv6: Option<Ipv6Addr>,
    /// Action taken by the last reconciliation pass.
    pub last_action: StatusAction,
    /// Scheduled time of the next reconciliation pass.
    pub next_reconcile_at: jiff::Timestamp,
    /// Constraint that selected the next reconciliation time.
    pub next_reconcile_reason: ReconcileReason,
}

/// Whether complete desired tunnel state was available.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StatusDesired {
    /// Complete tunnel state was resolved.
    Resolved,
    /// Configuration requests no tunnel.
    Absent,
    /// Desired state could not be resolved temporarily.
    Unavailable,
}

/// Source of the AFTR used to compute desired state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AftrSource {
    /// Static configuration.
    Config,
    /// Runtime override provided by the operator.
    Provided,
    /// HB46PP automatic discovery.
    Hb46pp,
    /// No AFTR source is active.
    None,
}

/// Reconciliation action reported in a status snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StatusAction {
    /// A tunnel was created.
    Create,
    /// Mutable tunnel properties were updated.
    Update,
    /// A tunnel was replaced.
    Rebuild,
    /// A tunnel was removed.
    Teardown,
    /// Existing state was retained because desired state was unavailable.
    Keep,
    /// No change was required.
    Noop,
}

/// Reason that determines the next reconciliation deadline.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconcileReason {
    /// Periodic health interval.
    Health,
    /// Discovery refresh directed by the protocol.
    Discovery,
    /// Retry backoff after a transient failure.
    Retry,
}

impl StatusSnapshot {
    /// Reads and validates a snapshot from `state_dir`.
    pub fn read(state_dir: &Path) -> anyhow::Result<Self> {
        let path = state_dir.join(STATUS_FILENAME);
        let bytes = std::fs::read(&path)
            .with_context(|| format!("reading status snapshot {}", path.display()))?;
        serde_json::from_slice(&bytes)
            .with_context(|| format!("parsing status snapshot {}", path.display()))
    }

    /// Atomically writes the snapshot with mode `0644`.
    pub fn write_atomic(&self, state_dir: &Path) -> anyhow::Result<()> {
        let path = state_dir.join(STATUS_FILENAME);
        let mut contents =
            serde_json::to_vec_pretty(self).context("serializing status snapshot")?;
        contents.push(b'\n');
        atomic_replace(&path, Some(0o644), &contents)
    }

    /// Serializes the snapshot as indented JSON.
    pub fn pretty_json(&self) -> anyhow::Result<String> {
        serde_json::to_string_pretty(self).context("serializing status snapshot")
    }

    /// Formats the snapshot for interactive status output relative to `now`.
    pub fn human(&self, now: SystemTime) -> String {
        let generated: SystemTime = self.generated_at.into();
        let next: SystemTime = self.next_reconcile_at.into();
        let age = relative_duration(now, generated, "old", "in the future");
        let next = {
            let at = next;
            if at <= now {
                format!(
                    "overdue by {}",
                    duration_text(now.duration_since(at).unwrap_or_default())
                )
            } else {
                format!(
                    "in {}",
                    duration_text(at.duration_since(now).unwrap_or_default())
                )
            }
        };
        let endpoint = |value: Option<Ipv6Addr>| {
            value.map_or_else(|| "unavailable".to_owned(), |addr| addr.to_string())
        };

        format!(
            "Snapshot: {} ({age})\nDesired: {}\nAFTR: {} ({})\nLocal IPv6: {}\nRemote IPv6: {}\nLast action: {}\nNext reconciliation: {} ({next})",
            self.generated_at,
            display_json_name(&self.desired),
            self.aftr.as_deref().unwrap_or("unavailable"),
            display_json_name(&self.aftr_source),
            endpoint(self.local_ipv6),
            endpoint(self.remote_ipv6),
            display_json_name(&self.last_action),
            display_json_name(&self.next_reconcile_reason),
        )
    }

    /// Formats the concise status sent to the service manager.
    pub fn supervisor_summary(&self) -> String {
        format!(
            "desired={}, action={}, next={} at {}",
            display_json_name(&self.desired),
            display_json_name(&self.last_action),
            display_json_name(&self.next_reconcile_reason),
            self.next_reconcile_at
        )
    }
}

/// Returns the current time as a status timestamp.
pub fn timestamp_now() -> jiff::Timestamp {
    jiff::Timestamp::now()
}

/// Returns a status timestamp `duration` after the current time.
pub fn timestamp_after(duration: Duration) -> anyhow::Result<jiff::Timestamp> {
    let value = SystemTime::now()
        .checked_add(duration)
        .context("next reconciliation time exceeds system clock range")?;
    let since_epoch = value
        .duration_since(UNIX_EPOCH)
        .context("system clock is before Unix epoch")?;
    let timestamp = jiff::Timestamp::new(
        i64::try_from(since_epoch.as_secs()).context("timestamp exceeds supported range")?,
        since_epoch.subsec_nanos() as i32,
    )?;
    Ok(timestamp)
}

/// Removes a stale snapshot, treating an absent file as success.
pub fn remove(state_dir: &Path) -> anyhow::Result<()> {
    let path = state_dir.join(STATUS_FILENAME);
    match std::fs::remove_file(&path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => {
            Err(error).with_context(|| format!("removing status file {}", path.display()))
        }
    }
}

/// Returns the platform default runtime state directory.
pub fn default_state_dir() -> PathBuf {
    #[cfg(target_os = "linux")]
    return PathBuf::from("/run/dslite-b4");
    #[cfg(target_os = "illumos")]
    return PathBuf::from("/var/run/dslite-b4");
}

fn deserialize_schema_version<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: Deserializer<'de>,
{
    let version = u32::deserialize(deserializer)?;
    if version != SCHEMA_VERSION {
        return Err(serde::de::Error::custom(format!(
            "unsupported schema version {version}; expected {SCHEMA_VERSION}"
        )));
    }
    Ok(version)
}

fn deserialize_required_option<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de>,
{
    // Using `deserialize_with` without `default` makes the field required while
    // still allowing an explicit JSON null to deserialize as `None`.
    Option::<T>::deserialize(deserializer)
}

fn display_json_name<T: Serialize>(value: &T) -> String {
    serde_json::to_string(value)
        .unwrap_or_else(|_| "unknown".to_owned())
        .trim_matches('"')
        .to_owned()
}

fn relative_duration(now: SystemTime, at: SystemTime, past: &str, future: &str) -> String {
    match now.duration_since(at) {
        Ok(duration) => format!("{} {past}", duration_text(duration)),
        Err(error) => format!("{} {future}", duration_text(error.duration())),
    }
}

fn duration_text(duration: Duration) -> String {
    let seconds = duration.as_secs();
    if seconds >= 86_400 {
        format!("{}d", seconds / 86_400)
    } else if seconds >= 3_600 {
        format!("{}h", seconds / 3_600)
    } else if seconds >= 60 {
        format!("{}m", seconds / 60)
    } else {
        format!("{seconds}s")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::PermissionsExt;

    fn snapshot() -> StatusSnapshot {
        StatusSnapshot {
            schema_version: SCHEMA_VERSION,
            generated_at: "2026-08-02T12:34:56Z".parse().unwrap(),
            pid: 1234,
            version: "0.1.0".to_owned(),
            tunnel_name: "dslite0".to_owned(),
            desired: StatusDesired::Resolved,
            aftr_source: AftrSource::Hb46pp,
            aftr: Some("dslite.example.net".to_owned()),
            local_ipv6: Some("2001:db8::1".parse().unwrap()),
            remote_ipv6: Some("2001:db8::2".parse().unwrap()),
            last_action: StatusAction::Noop,
            next_reconcile_at: "2026-08-02T12:35:26Z".parse().unwrap(),
            next_reconcile_reason: ReconcileReason::Health,
        }
    }

    #[test]
    fn rejects_unsupported_schema_version() {
        let text = serde_json::to_string(&snapshot())
            .unwrap()
            .replace("\"schema_version\":1", "\"schema_version\":2");
        assert!(serde_json::from_str::<StatusSnapshot>(&text).is_err());

        let invalid_time = serde_json::to_string(&snapshot())
            .unwrap()
            .replace("2026-08-02T12:34:56Z", "not-a-timestamp");
        assert!(serde_json::from_str::<StatusSnapshot>(&invalid_time).is_err());
    }

    #[test]
    fn nullable_fields_are_always_present() {
        let mut value = snapshot();
        value.aftr = None;
        value.local_ipv6 = None;
        value.remote_ipv6 = None;
        let json = serde_json::to_value(value).unwrap();
        assert!(json.get("aftr").unwrap().is_null());
        assert!(json.get("local_ipv6").unwrap().is_null());
        assert!(json.get("remote_ipv6").unwrap().is_null());

        let mut object = json.as_object().unwrap().clone();
        object.remove("aftr");
        assert!(serde_json::from_value::<StatusSnapshot>(object.into()).is_err());
    }

    #[test]
    fn serializes_all_stable_enum_values() {
        assert_eq!(
            [
                StatusDesired::Resolved,
                StatusDesired::Absent,
                StatusDesired::Unavailable
            ]
            .map(|value| serde_json::to_value(value).unwrap()),
            ["resolved", "absent", "unavailable"]
                .map(|value| serde_json::Value::String(value.to_owned()))
        );
        assert_eq!(
            [
                AftrSource::Config,
                AftrSource::Provided,
                AftrSource::Hb46pp,
                AftrSource::None
            ]
            .map(|value| serde_json::to_value(value).unwrap()),
            ["config", "provided", "hb46pp", "none"]
                .map(|value| serde_json::Value::String(value.to_owned()))
        );
        assert_eq!(
            [
                StatusAction::Create,
                StatusAction::Update,
                StatusAction::Rebuild,
                StatusAction::Teardown,
                StatusAction::Keep,
                StatusAction::Noop,
            ]
            .map(|value| serde_json::to_value(value).unwrap()),
            ["create", "update", "rebuild", "teardown", "keep", "noop"]
                .map(|value| serde_json::Value::String(value.to_owned()))
        );
        assert_eq!(
            [
                ReconcileReason::Health,
                ReconcileReason::Discovery,
                ReconcileReason::Retry
            ]
            .map(|value| serde_json::to_value(value).unwrap()),
            ["health", "discovery", "retry"]
                .map(|value| serde_json::Value::String(value.to_owned()))
        );
    }

    #[test]
    fn writes_and_reads_mode_0644() {
        let directory = tempfile_dir();
        std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700)).unwrap();
        snapshot().write_atomic(&directory).unwrap();
        assert_eq!(StatusSnapshot::read(&directory).unwrap(), snapshot());
        let mode = std::fs::metadata(directory.join(STATUS_FILENAME))
            .unwrap()
            .permissions()
            .mode();
        assert_eq!(mode & 0o777, 0o644);
        let directory_mode = std::fs::metadata(&directory).unwrap().permissions().mode();
        assert_eq!(directory_mode & 0o777, 0o700);
        std::fs::remove_dir_all(directory).unwrap();
    }

    #[test]
    fn repeated_readers_only_see_complete_replacements() {
        let directory = tempfile_dir();
        snapshot().write_atomic(&directory).unwrap();
        let reader_directory = directory.clone();
        let reader = std::thread::spawn(move || {
            for _ in 0..500 {
                let value = StatusSnapshot::read(&reader_directory).unwrap();
                assert!(value.pid == 1234 || value.pid == 5678);
            }
        });
        for index in 0..40 {
            let mut value = snapshot();
            value.pid = if index % 2 == 0 { 1234 } else { 5678 };
            value.write_atomic(&directory).unwrap();
        }
        reader.join().unwrap();
        std::fs::remove_dir_all(directory).unwrap();
    }

    fn tempfile_dir() -> PathBuf {
        let path = std::env::temp_dir().join(format!(
            "dslite-b4-status-test-{}-{}",
            std::process::id(),
            timestamp_now()
        ));
        std::fs::create_dir(&path).unwrap();
        path
    }
}