Files
dingoandClaude Opus 5.5 61d899de3c
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 12m33s
Foundation / checks (network) (push) Canceled after 10m23s
Foundation / checks (client) (push) Canceled after 12m33s
Add M10.6c probe-marker prediction traces and close M10.6
evo-motion-trace drives the engineering client's own session code through
the owned impairment relay against a real-time 50 ms loopback region:
exact without loss, 94-99 % exact at 60-200 ms RTT with 2-5 % loss, every
profile converging after stopping, restart resync about 2-3 s
(docs/networking/M10_PREDICTION_TRACES.md). The CTest motion-trace-smoke
checks convergence, bounds and a majority of exact predictions.

The relay lets a new sender take over after the current client has been
silent for 1 s (reconnects); the client counts refusal receipts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:03:45 -04:00

271 lines
8.9 KiB
C++

#include "udp_relay.hpp"
#include <array>
#include <atomic>
#include <mutex>
#include <optional>
#include <queue>
#include <thread>
#include <vector>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
// After <winsock2.h>: timeBeginPeriod for 1 ms waits (default timer tick is ~15.6 ms).
#include <timeapi.h>
#else
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
namespace evo::network::impair {
namespace {
#ifdef _WIN32
using Native = SOCKET;
constexpr Native invalid = INVALID_SOCKET;
void close_native(Native s) {
closesocket(s);
}
bool startup() {
static std::once_flag once;
static bool ok = false;
std::call_once(once, [] {
WSADATA data{};
ok = WSAStartup(MAKEWORD(2, 2), &data) == 0;
});
return ok;
}
int wait_readable(std::array<pollfd, 2>& fds, int ms) {
return WSAPoll(fds.data(), static_cast<ULONG>(fds.size()), ms);
}
using Length = int;
#else
using Native = int;
constexpr Native invalid = -1;
void close_native(Native s) {
::close(s);
}
bool startup() {
return true;
}
int wait_readable(std::array<pollfd, 2>& fds, int ms) {
return ::poll(fds.data(), fds.size(), ms);
}
using Length = std::size_t;
#endif
using Clock = std::chrono::steady_clock;
struct SplitMix64 {
std::uint64_t state;
std::uint64_t next() {
std::uint64_t z = (state += 0x9E3779B97F4A7C15ULL);
z = (z ^ (z >> 30U)) * 0xBF58476D1CE4E5B9ULL;
z = (z ^ (z >> 27U)) * 0x94D049BB133111EBULL;
return z ^ (z >> 31U);
}
double unit() {
return static_cast<double>(next() >> 11U) * 0x1.0p-53;
}
};
Native bound_udp(std::uint16_t port) {
const Native s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == invalid)
return invalid;
sockaddr_in local{};
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (::bind(s, reinterpret_cast<const sockaddr*>(&local), sizeof local) != 0) {
close_native(s);
return invalid;
}
return s;
}
} // namespace
struct UdpRelay::Impl {
Native client_side{invalid}, server_side{invalid};
std::uint16_t port{};
sockaddr_in target{};
std::optional<sockaddr_in> client;
Clock::time_point client_seen{};
mutable std::mutex mutex;
Impairment up, down;
RelayCounters counters;
SplitMix64 rng{0};
std::atomic<bool> stop{false};
std::thread thread;
struct Pending {
Clock::time_point due;
std::uint64_t order;
bool upstream;
std::vector<std::uint8_t> bytes;
bool operator>(const Pending& o) const {
return due != o.due ? due > o.due : order > o.order;
}
};
std::priority_queue<Pending, std::vector<Pending>, std::greater<>> queue;
std::uint64_t order{};
// Under `mutex`: applies one direction's impairment to a received packet.
void admit(bool upstream, std::vector<std::uint8_t> bytes, Clock::time_point now) {
const auto& rule = upstream ? up : down;
if (bytes.size() > rule.max_payload) {
++counters.oversized;
return;
}
if (rng.unit() < rule.loss) {
++counters.dropped;
return;
}
if (!bytes.empty() && rng.unit() < rule.corrupt) {
bytes[rng.next() % bytes.size()] ^= static_cast<std::uint8_t>(1U + (rng.next() % 255U));
++counters.corrupted;
}
const auto jitter =
rule.jitter.count() > 0
? std::chrono::milliseconds{static_cast<std::int64_t>(
rng.next() % static_cast<std::uint64_t>(rule.jitter.count() + 1))}
: std::chrono::milliseconds{0};
auto due = now + rule.delay + jitter;
if (rng.unit() < rule.reorder) {
due += rule.reorder_delay;
++counters.reordered;
}
const int copies = rng.unit() < rule.duplicate ? 2 : 1;
if (copies == 2)
++counters.duplicated;
for (int i = 0; i < copies; ++i)
queue.push({due, order++, upstream, bytes});
}
void run() {
std::array<std::uint8_t, 65536> buffer{};
while (!stop.load()) {
std::array<pollfd, 2> fds{};
fds[0].fd = client_side;
fds[0].events = POLLIN;
fds[1].fd = server_side;
fds[1].events = POLLIN;
(void)wait_readable(fds, 1);
const auto now = Clock::now();
for (int side = 0; side < 2; ++side) {
if ((fds[static_cast<std::size_t>(side)].revents & POLLIN) == 0)
continue;
sockaddr_in from{};
socklen_t from_size = sizeof from;
const auto n = ::recvfrom(side == 0 ? client_side : server_side,
reinterpret_cast<char*>(buffer.data()),
static_cast<Length>(buffer.size()), 0,
reinterpret_cast<sockaddr*>(&from), &from_size);
if (n <= 0)
continue;
const std::lock_guard lock{mutex};
// One client at a time: the first owns the relay; another sender takes over
// only after the owner has been silent for a second (a reconnect, M10.6c).
const bool owner = client && from.sin_port == client->sin_port &&
from.sin_addr.s_addr == client->sin_addr.s_addr;
if (side == 0 && !owner) {
if (client && now - client_seen < std::chrono::seconds{1})
continue;
client = from;
}
if (side == 0)
client_seen = now;
admit(side == 0, {buffer.data(), buffer.data() + n}, now);
}
std::vector<Pending> ready;
{
const std::lock_guard lock{mutex};
while (!queue.empty() && queue.top().due <= Clock::now()) {
ready.push_back(queue.top());
queue.pop();
}
}
for (const auto& p : ready) {
const Native s = p.upstream ? server_side : client_side;
const sockaddr_in* to = p.upstream ? &target : (client ? &*client : nullptr);
if (to == nullptr)
continue;
(void)::sendto(s, reinterpret_cast<const char*>(p.bytes.data()),
static_cast<Length>(p.bytes.size()), 0,
reinterpret_cast<const sockaddr*>(to), sizeof *to);
const std::lock_guard lock{mutex};
++counters.forwarded;
}
}
}
};
UdpRelay::UdpRelay(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
UdpRelay::~UdpRelay() {
impl_->stop.store(true);
#ifdef _WIN32
timeEndPeriod(1);
#endif
if (impl_->thread.joinable())
impl_->thread.join();
if (impl_->client_side != invalid)
close_native(impl_->client_side);
if (impl_->server_side != invalid)
close_native(impl_->server_side);
}
std::unique_ptr<UdpRelay> UdpRelay::start(std::uint16_t target_port, Impairment upstream,
Impairment downstream, std::uint64_t seed) {
if (!startup() || target_port == 0)
return nullptr;
auto impl = std::make_unique<Impl>();
impl->client_side = bound_udp(0);
impl->server_side = bound_udp(0);
if (impl->client_side == invalid || impl->server_side == invalid)
return nullptr;
sockaddr_in local{};
socklen_t size = sizeof local;
if (::getsockname(impl->client_side, reinterpret_cast<sockaddr*>(&local), &size) != 0)
return nullptr;
impl->port = ntohs(local.sin_port);
impl->target.sin_family = AF_INET;
impl->target.sin_port = htons(target_port);
impl->target.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
impl->up = upstream;
impl->down = downstream;
impl->rng.state = seed;
#ifdef _WIN32
// Delays are emulated with 1 ms waits; Windows' default timer tick (~15.6 ms) would
// add its own latency to every forwarded packet. Balanced by the destructor.
timeBeginPeriod(1);
#endif
std::unique_ptr<UdpRelay> relay{new UdpRelay{std::move(impl)}};
auto* raw = relay->impl_.get();
raw->thread = std::thread{[raw] { raw->run(); }};
return relay;
}
std::uint16_t UdpRelay::port() const noexcept {
return impl_->port;
}
void UdpRelay::set(Impairment upstream, Impairment downstream) {
const std::lock_guard lock{impl_->mutex};
impl_->up = upstream;
impl_->down = downstream;
}
RelayCounters UdpRelay::counters() const {
const std::lock_guard lock{impl_->mutex};
return impl_->counters;
}
} // namespace evo::network::impair