Skip to content

Cleanup of PcapLiveDevice capture machinery. #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from

Conversation

Dimi1010
Copy link
Collaborator

@Dimi1010 Dimi1010 commented May 31, 2025

This PR moves the implementation of the capture procedures to internally linked functions in PcapLiveDevice.cpp and aims to reduce the the need for extensive bookkeeping of capture thread only variables inside the device object's fields.

The static member function callback routines for pcap_dispatch have been replaced with internal linkage free functions to improve readability and encapsulation. The user token is now expected to be a pointer to a specific capture context object instead of the device instance.

Replacement list:

  • onPacketArrivesCallback replaces onPacketArrives. Expects user token of type CaptureContext
  • onPacketArrivesAccumulator replaces onPacketArrivesNoCallback. Expects user token of type AccumulatorCaptureContext
  • onPacketArrivesCallbackWithCancellation replaces onPacketArrivesBlockingMode. Expects user token of type CaptureContextST
  • Added new callback handler onPacketArrivesNoop to be used when no handling is required.

Replaced captureThreadMain static private function with internal linkage free functions. The free functions take their inputs as parameters instead of relying on data from the implicit this pointer, simplifying data ownership and reducing risk of data races.

  • captureThreadMain - used for callback or noop capture.
  • captureThreadMainAccumulator - used for accumulating the packets into a RawPacketVector.

Changed blocking mode capture to utilize onPacketArrivesCallbackWithCancellation. The new infrastructure provides a layer of separation between the device threading machinery and the cancellation requests by relying on requestStop flag to record a cancellation request that is then forwarded to m_StopThread after dispatch finishes instead of directly writing to m_StopThread.

Removed PcapLiveDevice member fields previously only used to store data for the capture thread as that data is now stored in context objects created on demand during capture startup.

Copy link

codecov bot commented May 31, 2025

Codecov Report

❌ Patch coverage is 66.86747% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.42%. Comparing base (b25fb59) to head (0505cd8).

Files with missing lines Patch % Lines
Pcap++/src/PcapLiveDevice.cpp 66.86% 36 Missing and 19 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1838      +/-   ##
==========================================
- Coverage   83.47%   83.42%   -0.05%     
==========================================
  Files         298      298              
  Lines       53950    53994      +44     
  Branches    12164    11702     -462     
==========================================
+ Hits        45034    45045      +11     
- Misses       7712     7734      +22     
- Partials     1204     1215      +11     
Flag Coverage Δ
alpine320 75.37% <50.92%> (-0.07%) ⬇️
fedora42 75.50% <51.37%> (-0.06%) ⬇️
macos-13 81.63% <63.50%> (-0.04%) ⬇️
macos-14 81.62% <61.31%> (-0.05%) ⬇️
macos-15 81.63% <63.50%> (-0.04%) ⬇️
mingw32 70.18% <43.92%> (-0.10%) ⬇️
mingw64 70.18% <43.92%> (-0.12%) ⬇️
rhel94 75.21% <50.92%> (-0.07%) ⬇️
ubuntu2004 59.18% <54.08%> (-0.01%) ⬇️
ubuntu2004-zstd 59.29% <54.08%> (-0.01%) ⬇️
ubuntu2204 75.14% <50.92%> (-0.07%) ⬇️
ubuntu2204-icpx 60.79% <48.90%> (-0.12%) ⬇️
ubuntu2404 75.40% <50.92%> (-0.06%) ⬇️
ubuntu2404-arm64 75.37% <50.92%> (-0.09%) ⬇️
unittest 83.42% <66.86%> (-0.05%) ⬇️
windows-2022 85.37% <62.28%> (-0.12%) ⬇️
windows-2025 85.38% <63.11%> (-0.12%) ⬇️
winpcap 85.39% <64.75%> (-0.11%) ⬇️
xdp 52.92% <0.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Dimi1010 Dimi1010 marked this pull request as ready for review May 31, 2025 19:58
@Dimi1010 Dimi1010 requested a review from seladb as a code owner May 31, 2025 19:58
@Dimi1010
Copy link
Collaborator Author

Can we review this? It has been sitting here for a while.

@tigercosmos
Copy link
Collaborator

@Dimi1010 It's a big one... That's why... I will look into this recently.

@tigercosmos tigercosmos requested a review from Copilot July 21, 2025 11:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the PcapLiveDevice capture machinery to improve encapsulation and thread safety by replacing static member function callbacks with internal linkage free functions. The changes move capture-specific state from device member variables into local context objects, reducing the risk of data races and simplifying the threading model.

  • Replaces static member callback functions with internal linkage free functions that use specific context objects
  • Eliminates thread-specific member variables from the device class in favor of local context objects
  • Consolidates the StatisticsUpdateWorker class into a simpler thread function approach

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
PcapLiveDevice.cpp Implements new internal capture context structures and free functions, removes StatisticsUpdateWorker class
PcapLiveDevice.h Removes StatisticsUpdateWorker class and capture-related member variables, adds stats thread member

{
PCPP_LOG_ERROR("Unable to extract PcapLiveDevice instance");
return;
OnStatsUpdateCallback cbOnStatsUpdate;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the convention is for this, no m_ prefix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, the m_ prefix is only for private / protected fields?
Other structures with public fields also don't use it.

@Dimi1010
Copy link
Collaborator Author

@seladb can you look through this one when you have time? It has been sitting for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants