Skip to content

Fix Mbps pacing overflow on long replays#989

Open
AB-Coder96 wants to merge 1 commit intoappneta:masterfrom
AB-Coder96:issue-974-fix-only
Open

Fix Mbps pacing overflow on long replays#989
AB-Coder96 wants to merge 1 commit intoappneta:masterfrom
AB-Coder96:issue-974-fix-only

Conversation

@AB-Coder96
Copy link
Copy Markdown

@AB-Coder96 AB-Coder96 commented Apr 27, 2026

Standards checklist:

  • The PR title is descriptive.
  • The PR doesn't replicate another PR which is already open.
  • The code is mine or it's from somewhere with an MIT-compatible license.
  • The code is efficient, to the best of my ability, and does not waste computer resources.
  • The code is stable and I have tested it myself, to the best of my abilities.
  • The code passes sudo make test

Note: I noticed #982 after opening this PR. #982 appears to address the same overflow class and is broader because it touches both speed_mbpsrate and speed_packetrate.

This PR is a smaller, fix-only alternative focused on the speed_mbpsrate path, which I independently reproduced and validated. One difference is that this PR uses quotient/remainder arithmetic, which avoids multiplying the full bits_sent value by 1e9 while preserving the fractional remainder after division.

Changes:

Fixes #974.

This fixes an overflow in the speed_mbpsrate timing calculation for long low-rate replays.

Previously, next_tx_ns could be calculated as:

(bits_sent * 1000000000) / bps

For large captures, bits_sent * 1000000000 can overflow COUNTER before the division. When that happens, next_tx_ns can wrap to a much smaller value, which can make skip_length become very large. The send loop then skips sleep recalculation and can send the remaining packets too quickly near EOF.

This patch computes the same value using quotient/remainder arithmetic, avoiding multiplication of the full bits_sent value by 1e9.

Other comments:

Validated with a synthetic 22M-packet UDP pcap at -M 10.

Before fix:

  • -T nano: final average around 10.11 Mbps, with a near-EOF final interval burst
  • -T gtod: final average around 10.09 Mbps, with a near-EOF final interval burst

After fix:

  • -T nano: expected 1865.60s, observed 1865.60s, final average 10.00 Mbps
  • -T gtod: expected 1865.60s, observed 1865.62s, final average 9.99 Mbps

Test environment:

  • Ubuntu 24.04.4 LTS under WSL2
  • Kernel 6.6.87.2-microsoft-standard-WSL2
  • dummy replay0 interface
  • PF_PACKET send()

Also ran sudo make test successfully.

@AB-Coder96
Copy link
Copy Markdown
Author

AB-Coder96 commented Apr 27, 2026

I noticed #982 after opening this PR. It appears to address the same overflow class and is broader because it touches both speed_mbpsrate and speed_packetrate.

One difference: this PR uses quotient/remainder arithmetic for the speed_mbpsrate calculation, so it avoids multiplying the full bits_sent value by 1e9 while preserving the remainder:

next_tx_ns = (bits_sent / bps) * 1000000000;
next_tx_ns += ((bits_sent % bps) * 1000000000) / bps;

#982 instead converts to nanoseconds after an earlier division, which also avoids the reproduced overflow but may lose some sub-millisecond precision due to integer truncation.

I’m happy to close this if maintainers prefer #982, or adjust this PR if the quotient/remainder approach is useful.

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.

[Bug] Tcpreplay ceases to sleep between packets when executed with -M 10

1 participant