iec61850-kit 0.8.1

IEC 61850 in Rust: GOOSE and Sampled Values on capture files and live interfaces, an MMS client and server over the full OSI stack, and SCL loading and editing. Panic-free, sans-IO, no_std-capable.
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
419
420
421
422
//! The steady state allocates nothing, and this counts the allocations to prove it.
//!
//! "Zero allocations per frame" is the sort of claim that is true when it is written and
//! quietly false three commits later — a `to_vec()` in an error path, a `format!` in a
//! trace, a `Vec` that is rebuilt instead of cleared. A counting allocator turns it into a
//! number a test can assert on, with no dependency and nothing to run by hand.
//!
//! Only the hot paths are covered: a GOOSE publisher retransmitting, a sampled-value
//! publisher patching its template, and a sampled-value subscriber receiving. The GOOSE
//! *subscriber* deliberately allocates on a state change — it hands the application owned
//! values — so it is measured rather than required to be free.
//!
//! One claim here is about **bytes** rather than calls: a file service that serves a record
//! in chunks must cost the chunk, not the record. A count of allocations cannot tell a
//! four-megabyte one from a one-kilobyte one, so the allocator totals the sizes as well.

// The one place in this repository that opts into `unsafe`, and only to implement
// `GlobalAlloc` by delegating every call to the system allocator. The library keeps
// `#![forbid(unsafe_code)]`.
#![allow(unsafe_code)]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]

use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;

use iec61850_kit::common::{Instant, Quality, TimeQuality, UtcTime};
use iec61850_kit::proto::data::Value;
use iec61850_kit::proto::ethernet::{ETHERTYPE_GOOSE, ETHERTYPE_SV, FrameHeader, MacAddr, VlanTag};
use iec61850_kit::proto::goose::{
    Publisher as GoosePublisher, PublisherConfig as GooseConfig, Retransmission, Subscriber as GooseSubscriber, SubscriberConfig, SubscriptionKey,
};
use iec61850_kit::proto::sv::le::PhsMeas1;
use iec61850_kit::proto::sv::{
    ChannelType, Publisher as SvPublisher, PublisherConfig as SvConfig, SampleLayout, SmpSynch, StreamConfig, StreamKey, Subscriber as SvSubscriber, SvProfile,
};

/// Delegates to the system allocator and counts what the *measuring thread* asks for.
///
/// Everything is thread-local and const-initialised, so the test harness's own threads — and
/// the lazy initialisation of the cells themselves — cannot land in the count. Thread-local
/// *counters*, not just a thread-local flag: with a process-wide total, two `#[test]`s in
/// this file measure each other, which is a constraint that quietly turns into a wrong number
/// the day somebody adds the second test.
struct Counting;

thread_local! {
    static MEASURING: Cell<bool> = const { Cell::new(false) };
    static ALLOCATIONS: Cell<u64> = const { Cell::new(0) };
    /// Octets requested, not just requests. Some claims are about how *much* was allocated.
    static BYTES: Cell<u64> = const { Cell::new(0) };
}

unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        count(layout.size());
        // SAFETY: `layout` is forwarded unchanged to the system allocator.
        unsafe { System.alloc(layout) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        // SAFETY: `ptr` and `layout` are forwarded unchanged to the system allocator.
        unsafe { System.dealloc(ptr, layout) }
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        count(new_size.saturating_sub(layout.size()));
        // SAFETY: all three arguments are forwarded unchanged to the system allocator.
        unsafe { System.realloc(ptr, layout, new_size) }
    }
}

fn count(bytes: usize) {
    // `try_with` rather than `with`: during thread teardown the local is gone, and a panic
    // inside the allocator would be considerably worse than a missed count. `Cell::set`
    // allocates nothing, so this cannot recurse.
    let _ = MEASURING.try_with(|m| {
        if m.get() {
            let _ = ALLOCATIONS.try_with(|c| c.set(c.get().saturating_add(1)));
            let _ = BYTES.try_with(|c| c.set(c.get().saturating_add(bytes as u64)));
        }
    });
}

#[global_allocator]
static ALLOC: Counting = Counting;

/// Run `f` with the allocation counter on, and return how many allocations it made.
fn allocations(f: impl FnOnce()) -> u64 {
    measure(f).0
}

/// Run `f` with the counter on, and return `(allocations, octets)`.
fn measure(f: impl FnOnce()) -> (u64, u64) {
    ALLOCATIONS.with(|c| c.set(0));
    BYTES.with(|c| c.set(0));
    MEASURING.with(|m| m.set(true));
    f();
    MEASURING.with(|m| m.set(false));
    (ALLOCATIONS.with(Cell::get), BYTES.with(Cell::get))
}

fn goose_header() -> FrameHeader {
    FrameHeader {
        dst: MacAddr::GOOSE_BASE,
        src: MacAddr([2, 0, 0, 0, 0, 1]),
        vlan: Some(VlanTag::DEFAULT),
        ethertype: ETHERTYPE_GOOSE,
        appid: 1,
        reserved1: 0,
        reserved2: 0,
    }
}

fn sv_header() -> FrameHeader {
    FrameHeader {
        dst: MacAddr::SV_BASE,
        src: MacAddr([2, 0, 0, 0, 0, 2]),
        vlan: Some(VlanTag::DEFAULT),
        ethertype: ETHERTYPE_SV,
        appid: 0x4000,
        reserved1: 0,
        reserved2: 0,
    }
}

#[test]
// The counters are thread-local, so tests in this file may run in parallel without measuring
// each other. This one stays a single function because its sections share warmed-up state.
#[allow(clippy::too_many_lines)]
fn the_steady_state_allocates_nothing() {
    // --- GOOSE publisher, retransmitting -------------------------------------------------
    let values = [Value::Boolean(true), Value::quality(Quality::GOOD), Value::Integer(-7), Value::Float32(1.5)];
    let cfg = GooseConfig {
        header: goose_header(),
        gocb_ref: "IED1LD0/LLN0$GO$gcbTrip".into(),
        dat_set: "IED1LD0/LLN0$dsTrip".into(),
        go_id: Some("IED1".into()),
        conf_rev: 1,
        retransmission: Retransmission::DEFAULT,
        simulation: false,
        nds_com: false,
    };
    let mut pubr = GoosePublisher::new(cfg, &values, UtcTime::default()).unwrap();
    // Warm the buffers: the first frame is allowed to allocate, every one after it is not.
    let mut now = Instant::ZERO;
    for _ in 0..4 {
        pubr.on_timeout(now).unwrap();
        assert!(pubr.poll_transmit().is_some());
        now = pubr.next_timeout().unwrap();
    }
    let n = allocations(|| {
        for _ in 0..1000 {
            pubr.on_timeout(now).unwrap();
            let frame = pubr.poll_transmit().expect("a frame is due");
            std::hint::black_box(frame.len());
            now = pubr.next_timeout().unwrap();
        }
    });
    assert_eq!(n, 0, "a GOOSE publisher retransmitting must not allocate");

    // --- GOOSE publisher, changing state -------------------------------------------------
    // A state change re-encodes the data set. It must reuse the buffer it already has: a
    // fault is the worst possible moment to visit the allocator.
    let changed = [Value::Boolean(false), Value::quality(Quality::GOOD), Value::Integer(9), Value::Float32(-1.5)];
    let n = allocations(|| {
        for i in 0..1000 {
            let v = if i % 2 == 0 { &changed } else { &values };
            pubr.publish(now, v, UtcTime::default()).unwrap();
            std::hint::black_box(pubr.poll_transmit().expect("a state change always produces a frame").len());
            now = now.plus_millis(1);
        }
    });
    assert_eq!(n, 0, "a GOOSE state change must not allocate either");

    // --- GOOSE publisher, deciding whether the state changed at all ----------------------
    // `publish_if_changed` encodes into a second kept buffer and compares. An application
    // that offers its whole data set every scan cycle is the normal case, so this path has
    // to be free too.
    let n = allocations(|| {
        for i in 0..1000 {
            let v = if i % 100 == 0 { &changed } else { &values };
            std::hint::black_box(pubr.publish_if_changed(now, v, UtcTime::default()).unwrap());
            pubr.poll_transmit();
            now = now.plus_millis(1);
        }
    });
    assert_eq!(n, 0, "comparing the data set against the one on the wire must not allocate");

    // --- Sampled-value publisher, patching its template ----------------------------------
    let mut mu = SvPublisher::new(SvConfig::new(sv_header(), "MU01", SvProfile::F4800S2I4U4).with_time_fields(true, true)).unwrap();
    mu.set_smp_synch(SmpSynch::Global).unwrap();
    let sample = PhsMeas1 { currents: [1, 2, 3, 4], current_quality: [Quality::GOOD; 4], voltages: [5, 6, 7, 8], voltage_quality: [Quality::GOOD; 4] }.encode();
    mu.publish_repeating(Instant::ZERO, &sample).unwrap();
    mu.poll_transmit();
    // The convenience path that repeats one block per ASDU patches the template directly;
    // building a list of identical slices would allocate 2400 times a second.
    let n = allocations(|| {
        for i in 0..2400u64 {
            mu.publish_repeating(Instant(i), &sample).unwrap();
            std::hint::black_box(mu.poll_transmit().expect("a frame is pending").len());
        }
    });
    assert_eq!(n, 0, "publishing one repeated block per ASDU must not allocate");
    let n = allocations(|| {
        let blocks: [&[u8]; 2] = [&sample, &sample];
        for i in 0..2400u64 {
            mu.set_refr_tm(UtcTime::from_unix(1_700_000_000, (i * 416_666) as u32, TimeQuality::SYNCHRONIZED));
            mu.publish(Instant(i), &blocks).unwrap();
            std::hint::black_box(mu.poll_transmit().expect("a frame is pending").len());
        }
    });
    assert_eq!(n, 0, "one second of IEC 61869-9 publishing must not allocate");

    // --- Sampled-value subscriber, receiving ---------------------------------------------
    let frames: Vec<Vec<u8>> = (0..64u64)
        .map(|i| {
            mu.publish(Instant(i), &[&sample, &sample]).unwrap();
            mu.poll_transmit().unwrap().to_vec()
        })
        .collect();
    // With a layout, so that the dataset-driven decoding path is measured too: reading
    // channels out of the frame's own octets must not allocate either.
    let mut channels = Vec::new();
    for name in ["Ia", "Ib", "Ic", "In", "Ua", "Ub", "Uc", "Un"] {
        channels.push((format!("{name}.instMag.i"), ChannelType::Int(4)));
        channels.push((format!("{name}.q"), ChannelType::Quality));
    }
    let mut sub = SvSubscriber::new(vec![
        StreamConfig::new(StreamKey { dst: MacAddr::SV_BASE, appid: 0x4000, sv_id: "MU01".into() })
            .with_samples_per_second(4800)
            .with_layout(SampleLayout::new(channels)),
    ]);
    sub.on_frame(Instant::ZERO, &frames[0], |_| {});
    while sub.poll_event().is_some() {}
    let mut seen = 0u64;
    let n = allocations(|| {
        for (i, f) in frames.iter().enumerate().skip(1) {
            sub.on_frame(Instant(i as u64), f, |s| {
                seen += u64::from(s.asdu.smp_cnt);
                for (_, v) in s.channels() {
                    seen += v.as_i64().unwrap_or(0).unsigned_abs();
                }
            });
        }
    });
    std::hint::black_box(seen);
    assert_eq!(n, 0, "the sampled-value receive path must not allocate");
    assert_eq!(sub.state(0).unwrap().gaps, 0, "the run must have been a clean stream, or it measured nothing");

    // --- GOOSE subscriber: bounded, not free ---------------------------------------------
    // This one *does* allocate, by design: it hands the application owned values. What
    // matters is that a retransmission — the common case by far — does not.
    let mut gsub =
        GooseSubscriber::new(SubscriberConfig::new(SubscriptionKey { dst: MacAddr::GOOSE_BASE, appid: 1, gocb_ref: "IED1LD0/LLN0$GO$gcbTrip".into() }));
    pubr.publish(now, &values, UtcTime::default()).unwrap();
    let first = pubr.poll_transmit().unwrap().to_vec();
    let repeats: Vec<Vec<u8>> = (0..100)
        .map(|_| {
            now = pubr.next_timeout().unwrap();
            pubr.on_timeout(now).unwrap();
            pubr.poll_transmit().unwrap().to_vec()
        })
        .collect();
    // Delivered on their own timeline, a millisecond apart, so nothing expires: the point
    // here is what a retransmission costs, not what a restart costs.
    gsub.on_frame(Instant::ZERO, &first);
    while gsub.poll_event().is_some() {}
    let n = allocations(|| {
        for (i, f) in repeats.iter().enumerate() {
            gsub.on_frame(Instant::ZERO.plus_millis(i as u64 + 1), f);
            while gsub.poll_event().is_some() {}
        }
    });
    let stats = gsub.stats();
    assert_eq!((stats.state_changes, stats.retransmissions, stats.expiries), (1, 100, 0), "{stats:?}");
    assert_eq!(n, 0, "a GOOSE retransmission must not allocate; only a state change may");
}

/// A file service costs the **chunk**, not the record.
///
/// The store reads ranges, so an open costs a path and a read costs one chunk whatever the
/// file's size — a client opening a 200 MB COMTRADE record on many associations cannot make
/// the server allocate the record each time.
///
/// This is a claim about octets rather than calls: a count of allocations cannot tell a
/// four-megabyte one from a one-kilobyte one, which is exactly the difference at stake.
#[test]
#[cfg(all(feature = "server", feature = "std"))]
fn opening_a_large_file_does_not_allocate_it() {
    use iec61850_kit::common::Instant;
    use iec61850_kit::proto::mms::ConfirmedRequest;
    use iec61850_kit::proto::mms::file::FileNameBuf;
    use iec61850_kit::server::{Acsi, Answer, DirectoryStore, Ied};

    const RECORD: usize = 4 << 20; // 4 MiB — far above any buffer here.

    let dir = std::env::temp_dir().join(format!("iec61850-bigfile-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("rec.dat"), vec![0x5Au8; RECORD]).unwrap();

    let mut acsi = Acsi::new(Ied::from_scl(MODEL, Some("IED1")).unwrap());
    acsi.set_file_store(Box::new(DirectoryStore::new(&dir)));

    // Warm every lazily-initialised path (the store's canonicalisation, the answer buffers)
    // so the measurement is the file service and not the first call into it.
    let path = FileNameBuf::from_path("rec.dat").unwrap();
    let name = path.as_name();
    let warm = acsi.request(1, Instant::ZERO, &ConfirmedRequest::FileOpen { name, position: 0 });
    let Answer::FileOpen { frsm_id, size, .. } = warm else { panic!("not an open: {warm:?}") };
    assert_eq!(size as usize, RECORD);
    acsi.request(1, Instant::ZERO, &ConfirmedRequest::FileClose(frsm_id));

    let chunk = acsi.config().file_chunk;
    let mut frsm = 0;
    let (_, open_bytes) = measure(|| {
        let a = acsi.request(1, Instant::ZERO, &ConfirmedRequest::FileOpen { name, position: 0 });
        if let Answer::FileOpen { frsm_id, .. } = a {
            frsm = frsm_id;
        }
    });
    let (_, read_bytes) = measure(|| {
        acsi.request(1, Instant::ZERO, &ConfirmedRequest::FileRead(frsm));
    });

    // Generous ceilings — the point is the order of magnitude, not a golden number. Under
    // the old behaviour `open_bytes` alone was the whole 4 MiB.
    assert!(open_bytes < 64 * 1024, "an open must not allocate the file: {open_bytes} octets for a {RECORD}-octet record");
    assert!(read_bytes < (chunk as u64) * 8, "a read must cost a chunk ({chunk}), not a file: {read_bytes} octets");

    // …and it still delivers the record, in order and whole.
    let mut total = 0usize;
    loop {
        match acsi.request(1, Instant::ZERO, &ConfirmedRequest::FileRead(frsm)) {
            Answer::FileRead { data, more } => {
                assert!(data.iter().all(|b| *b == 0x5A), "the octets are the file's");
                total += data.len();
                if !more {
                    break;
                }
            }
            other => panic!("not a read: {other:?}"),
        }
        assert!(total <= RECORD, "the reads must terminate");
    }
    assert_eq!(total, RECORD - chunk, "the warm read above already took the first chunk");

    let _ = std::fs::remove_dir_all(&dir);
}

/// The smallest model that has a logical device, for the file-service test above.
#[cfg(all(feature = "server", feature = "std"))]
const MODEL: &str = r#"<?xml version="1.0"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
  <Header id="f"/>
  <IED name="IED1"><AccessPoint name="P1"><Server><LDevice inst="LD0">
    <LN0 lnClass="LLN0" inst="" lnType="LLN0_T"/>
  </LDevice></Server></AccessPoint></IED>
  <DataTypeTemplates>
    <LNodeType id="LLN0_T" lnClass="LLN0"><DO name="Mod" type="INC_T"/></LNodeType>
    <DOType id="INC_T" cdc="INC"><DA name="stVal" fc="ST" bType="INT32"/></DOType>
  </DataTypeTemplates>
</SCL>"#;

/// Receiving a file costs one chunk at a time, not the file: what the server holds per
/// `FileRead` answer is bounded by the chunk, however large the file is.
#[test]
#[cfg(all(feature = "server", feature = "std"))]
fn receiving_a_file_costs_a_chunk_not_the_file() {
    use iec61850_kit::common::Instant;
    use iec61850_kit::proto::mms::file::{FileAttributes, FileNameBuf};
    use iec61850_kit::proto::mms::{ConfirmedRequest, ConfirmedResponse, Mms};
    use iec61850_kit::server::{Acsi, AcsiConfig, Answer, DirectoryStore, FileWritePolicy, Ied};

    const CHUNK: usize = 4096;
    const CHUNKS: u32 = 64;
    let dir = std::env::temp_dir().join(format!("iec61850-receive-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let cfg = AcsiConfig { file_write: FileWritePolicy::Anyone, ..AcsiConfig::default() };
    let mut acsi = Acsi::with_config(Ied::from_scl(MODEL, Some("IED1")).unwrap(), cfg);
    acsi.set_file_store(Box::new(DirectoryStore::new(&dir).writable()));
    let source = FileNameBuf::from_path("big.bin").unwrap();
    let dest = FileNameBuf::from_path("big.bin").unwrap();
    let answer = acsi.request(1, Instant::ZERO, &ConfirmedRequest::ObtainFile { source_server: None, source: source.as_name(), destination: dest.as_name() });
    assert!(matches!(answer, Answer::Deferred), "{answer:?}");
    acsi.obtain_bind(1, 1);

    let size = CHUNKS * u32::try_from(CHUNK).unwrap();
    let open =
        Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileOpen { frsm_id: 3, attributes: FileAttributes { size, last_modified: None } } }
            .to_vec()
            .unwrap();
    let data = vec![0xA5u8; CHUNK];
    let answer_once = |acsi: &mut Acsi, invoke: i64, pdu: &[u8]| {
        assert!(acsi.obtain_call(1).is_some(), "a call is due");
        acsi.obtain_issued(1, invoke);
        let mms = Mms::parse(pdu, &iec61850_kit::common::Limits::DEFAULT).unwrap();
        acsi.obtain_response(1, invoke, &mms)
    };
    let mut invoke = 10;
    assert!(answer_once(&mut acsi, invoke, &open).is_none());
    let mut worst = 0u64;
    for n in 0..CHUNKS {
        let read =
            Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileRead { data: &data, more_follows: n + 1 < CHUNKS } }.to_vec().unwrap();
        let mms = Mms::parse(&read, &iec61850_kit::common::Limits::DEFAULT).unwrap();
        assert!(acsi.obtain_call(1).is_some());
        invoke += 1;
        acsi.obtain_issued(1, invoke);
        let (_, bytes) = measure(|| {
            let _ = acsi.obtain_response(1, invoke, &mms);
        });
        worst = worst.max(bytes);
    }
    assert!(worst < (CHUNK as u64) * 4, "a chunk received costs {worst} octets, more than a few chunks of {CHUNK}");
    let close = Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileClose }.to_vec().unwrap();
    let done = answer_once(&mut acsi, invoke + 1, &close).expect("the ObtainFile is answered");
    assert!(matches!(Mms::parse(&done, &iec61850_kit::common::Limits::DEFAULT), Ok(Mms::ConfirmedResponse { service: ConfirmedResponse::ObtainFile, .. })));
    assert_eq!(std::fs::metadata(dir.join("big.bin")).unwrap().len(), u64::from(size));
    let _ = std::fs::remove_dir_all(&dir);
}