91 const uint8_t *
data =
nullptr;
96 PcapReplaySource(std::string name, std::string root, uint64_t begin_ns, uint64_t end_ns,
97 std::vector<PcapLine> lines,
size_t max_queued_batches = 64)
98 : name_(std::move(name))
99 , root_(std::move(root))
100 , begin_ns_(begin_ns)
102 , lines_(std::move(lines))
103 , batches_(static_cast<uint32_t>(std::bit_ceil(std::max<size_t>(max_queued_batches, 1))))
105 if (end_ns_ < begin_ns_) {
106 throw std::runtime_error(std::format(
"replay {}: end is before begin", name_));
118 if (thread_.joinable()) {
121 thread_ = std::thread([
this] { run(); });
125 stopping_.store(
true, std::memory_order_relaxed);
126 if (thread_.joinable()) {
133 [[nodiscard]]
bool positioned()
const {
return positioned_.load(std::memory_order_acquire); }
137 [[nodiscard]]
bool reader_done()
const {
return done_.load(std::memory_order_acquire); }
142 [[nodiscard]] uint64_t
truncated_frames()
const {
return truncated_frames_.load(std::memory_order_relaxed); }
145 [[nodiscard]] std::string
error()
const {
146 return failed_.load(std::memory_order_acquire) ? error_ : std::string();
150 [[nodiscard]] uint64_t
read_ts_ns()
const {
return read_ts_ns_.load(std::memory_order_relaxed); }
155 if (cursor_ >= current_.entries.size() && !fetch()) {
158 const Entry &entry = current_.entries[cursor_];
159 item.
ts_ns = entry.ts_ns;
160 item.
line = entry.line;
161 item.
data = current_.bytes.data() + entry.offset;
162 item.
size = entry.size;
168 if (cursor_ < current_.entries.size()) {
176 if (cursor_ < current_.entries.size()) {
180 return done_.load(std::memory_order_acquire) && !fetch();
191 std::vector<uint8_t> bytes;
192 std::vector<Entry> entries;
194 static constexpr size_t BATCH_PACKETS = 256;
195 static constexpr size_t BATCH_BYTES = 1u << 20;
201 std::vector<std::string> files;
202 size_t next_file = 0;
203 std::unique_ptr<PcapReader> reader;
204 bool has_head =
false;
205 bool exhausted =
false;
206 uint64_t head_ts = 0;
207 std::vector<uint8_t> head;
208 uint64_t last_ts = 0;
209 uint64_t truncated_counted = 0;
212 uint64_t overlap_until = 0;
217 Batch *next = batches_.read();
222 std::swap(current_, *next);
225 return !current_.entries.empty();
236 void trim_files(Stream &s) {
237 if (s.files.size() < 2) {
240 std::vector<std::optional<uint64_t>> starts;
241 for (
const auto &file : s.files) {
243 PcapReader reader(file);
245 starts.push_back(reader.next(packet) ? std::optional<uint64_t>(packet.ts_ns) : std::nullopt);
246 }
catch (
const std::exception &) {
247 starts.push_back(std::nullopt);
250 std::vector<std::string> kept;
251 for (
size_t i = 0; i < s.files.size(); ++i) {
252 const bool covered_by_next = i + 1 < s.files.size() && starts[i + 1] && *starts[i + 1] <= begin_ns_;
253 const bool after_window = starts[i] && *starts[i] > end_ns_;
254 if (!covered_by_next && !after_window) {
255 kept.push_back(std::move(s.files[i]));
260 kept.push_back(s.files.back());
262 s.files = std::move(kept);
266 void advance(Stream &s) {
271 if (stopping_.load(std::memory_order_relaxed)) {
277 if (s.next_file >= s.files.size()) {
281 s.reader = std::make_unique<PcapReader>(s.files[s.next_file++]);
282 s.truncated_counted = 0;
283 s.overlap_until = s.last_ts;
285 const bool got = s.reader->next(packet);
286 if (
const uint64_t truncated = s.reader->truncated(); truncated != s.truncated_counted) {
287 truncated_frames_.fetch_add(truncated - s.truncated_counted, std::memory_order_relaxed);
288 s.truncated_counted = truncated;
291 if (!s.reader->error().empty()) {
292 throw std::runtime_error(std::format(
"{}: {}", s.reader->path(), s.reader->error()));
297 if (packet.dst_addr != s.addr || packet.dst_port != s.config.port) {
300 if (packet.ts_ns < begin_ns_ || packet.ts_ns <= s.overlap_until) {
304 if (packet.ts_ns > end_ns_) {
309 s.head.assign(packet.data, packet.data + packet.size);
310 s.head_ts = s.last_ts = packet.ts_ns;
318 Batch *acquire_batch(uint64_t &index) {
319 while ((index = batches_.try_reserve()) == slick::spsc_queue<Batch>::invalid_index) {
320 if (stopping_.load(std::memory_order_relaxed)) {
323 std::this_thread::sleep_for(std::chrono::microseconds(200));
325 Batch *batch = batches_[index];
326 batch->entries.clear();
327 batch->bytes.clear();
333 std::vector<Stream> streams(lines_.size());
334 for (
size_t i = 0; i < lines_.size(); ++i) {
335 streams[i].config = lines_[i];
336 streams[i].addr =
parse_ipv4(lines_[i].address);
337 streams[i].files = lines_[i].paths.empty()
340 if (streams[i].files.empty()) {
341 throw std::runtime_error(std::format(
"replay {}: no capture files for {}:{} in the window",
342 name_, lines_[i].address, lines_[i].port));
347 std::vector<std::exception_ptr> failures(streams.size());
348 std::vector<std::thread> positioning;
349 for (
size_t i = 0; i < streams.size(); ++i) {
350 positioning.emplace_back([&, i] {
352 trim_files(streams[i]);
355 failures[i] = std::current_exception();
359 for (
auto &t : positioning) {
362 for (
auto &failure : failures) {
364 std::rethrow_exception(failure);
367 positioned_.store(
true, std::memory_order_release);
371 auto later = [&](
size_t a,
size_t b) {
372 return streams[a].head_ts != streams[b].head_ts ? streams[a].head_ts > streams[b].head_ts : a > b;
374 std::priority_queue<size_t, std::vector<size_t>,
decltype(later)> heads(later);
375 for (
size_t i = 0; i < streams.size(); ++i) {
376 if (streams[i].has_head) {
380 Batch *batch =
nullptr;
382 while (!heads.empty()) {
383 if (stopping_.load(std::memory_order_relaxed)) {
386 if (!batch && !(batch = acquire_batch(slot))) {
389 const size_t i = heads.top();
391 Stream &s = streams[i];
392 batch->entries.push_back({s.head_ts,
static_cast<uint32_t
>(batch->bytes.size()),
393 static_cast<uint32_t
>(s.head.size()), s.config.line});
394 batch->bytes.insert(batch->bytes.end(), s.head.begin(), s.head.end());
395 read_ts_ns_.store(s.head_ts, std::memory_order_relaxed);
400 if (batch->entries.size() >= BATCH_PACKETS || batch->bytes.size() >= BATCH_BYTES) {
401 batches_.publish(slot);
405 if (batch && !batch->entries.empty() && !stopping_.load(std::memory_order_relaxed)) {
406 batches_.publish(slot);
408 }
catch (
const std::exception &e) {
410 failed_.store(
true, std::memory_order_release);
413 positioned_.store(
true, std::memory_order_release);
414 done_.store(
true, std::memory_order_release);
421 std::vector<PcapLine> lines_;
425 slick::spsc_queue<Batch> batches_;
427 std::atomic<bool> failed_{
false};
428 std::atomic<uint64_t> truncated_frames_{0};
429 std::atomic<bool> stopping_{
false};
430 std::atomic<bool> positioned_{
false};
431 std::atomic<bool> done_{
false};
432 std::atomic<uint64_t> read_ts_ns_{0};