Skip to content

feat(rwnd): add rwnd trace and its unit test#26

Open
zhang-hc22 wants to merge 1 commit into
mainfrom
rwnd_trace
Open

feat(rwnd): add rwnd trace and its unit test#26
zhang-hc22 wants to merge 1 commit into
mainfrom
rwnd_trace

Conversation

@zhang-hc22

Copy link
Copy Markdown

This PR solves issue #25

Description

This PR adds receive window (rwnd) trace support to netem-trace, enabling simulation and replay of TCP receiver-side behaviour over time.

Each step in a rwnd trace is described by four fields:

  • duration — how long this configuration lasts before the next step applies
  • set_rcv_buf (optional) — resize the socket's receive buffer to this value at this step
  • app_read_bytes (optional) — simulate the application reading this many bytes from the receive buffer (drives the receiver model)
  • rwnd_remaining (optional) — directly assert the observed remaining receive window after consumption (useful for replaying captured traces)

Exactly one of app_read_bytes / rwnd_remaining must be set per step, never both and never neither. This invariant is enforced at two levels: deserialization rejects invalid inputs with a clear error message, and StaticRwndConfig::build() panics for programmatically-constructed configs that omit both.

New public surface in src/lib.rs:

  • Rwnd — type alias (u64) for all byte-count fields
  • RwndAction — enum encoding the mutually-exclusive action per step (AppRead { bytes } / Remaining { rwnd })
  • RwndDecision — a single step's payload (set_rcv_buf + RwndAction)
  • RwndTrace — trait mirroring BwTrace/DelayTrace/LossTrace, returning Option<(RwndDecision, Duration)>

New model module src/model/rwnd.rs (enabled via rwnd-model / model feature):

  • StaticRwnd / StaticRwndConfig — single-step model with builder API
  • RepeatedRwndPattern / RepeatedRwndPatternConfig — repeating pattern over a sequence of steps, with infinite-loop (count = 0) support
  • RwndTraceConfig — config-to-model conversion trait, typetag-registered for serde polymorphism
  • Custom Serialize/Deserialize for StaticRwndConfig that flattens app_read_bytes/rwnd_remaining to the top level of the JSON object (consistent with the flat style of other models)

The rwnd-model feature is added to the model umbrella feature and follows the same dependency pattern as loss-model and duplicate-model (dep:dyn-clone).

How Has This Been Tested

  • Unit tests (src/model/rwnd.rs #[cfg(test)] block):
    • test_static_rwnd_model_app_read — verifies AppRead step fields and exhaustion
    • test_static_rwnd_model_remaining — verifies Remaining step fields and exhaustion
    • test_repeated_rwnd_pattern — verifies a 2-step pattern repeated twice, then None
    • test_build_panics_without_action — confirms build() panics without an action set
    • test_serde_roundtrip_app_read — serialize → deserialize → assert field equality (#[cfg(feature = "serde")])
    • test_serde_roundtrip_remaining — same for Remaining variant
    • test_serde_rejects_both — confirms deserialization error when both fields are present
    • test_serde_rejects_neither — confirms deserialization error when neither field is present
  • Doc tests on all public types and the module-level examples
  • Tests run against --features "model,serde" (without human) and --features "model,serde,human" to confirm both duration-format paths work correctly. (The rejection tests were specifically written to avoid the "1s" human-format string to ensure they test the right thing regardless of features.)

cargo test --features "model,serde" # 22 unit + 47 doc — all pass
cargo test --features "model,serde,human" # 23 unit + 47 doc — all pass

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • Code follows the code style of this project.
  • Changes follow the CONTRIBUTING guidelines.
  • Update necessary documentation accordingly.
  • Lint and tests pass locally with the changes.
  • Check issues and pull requests first. You don't want to duplicate effort.

Other information

The RwndAction / RwndDecision types are placed in lib.rs alongside BwTrace, LossTrace, etc., matching twnd-model feature follows the same opt-in pattern as other model features and is included in the modelumbrella and full feature set.

@zhang-hc22 zhang-hc22 requested a review from Centaurus99 July 1, 2026 03:07
@zhang-hc22 zhang-hc22 linked an issue Jul 1, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[enhancement] Support Rwnd trace

1 participant