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 15 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 82.76%. Comparing base (5bc893d) to head (1d2905e).

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   82.81%   82.76%   -0.06%     
==========================================
  Files         291      291              
  Lines       51596    51640      +44     
  Branches    11448    11251     -197     
==========================================
+ Hits        42730    42738       +8     
- Misses       7708     8071     +363     
+ Partials     1158      831     -327     
Flag Coverage Δ
alpine320 74.51% <50.92%> (-0.07%) ⬇️
fedora42 74.63% <48.62%> (-0.08%) ⬇️
macos-13 80.99% <63.50%> (-0.04%) ⬇️
macos-14 80.99% <63.50%> (-0.04%) ⬇️
macos-15 80.99% <63.50%> (-0.04%) ⬇️
mingw32 69.72% <43.92%> (-0.16%) ⬇️
mingw64 69.73% <43.92%> (-0.14%) ⬇️
npcap 84.62% <63.11%> (-0.20%) ⬇️
rhel94 74.39% <50.92%> (-0.07%) ⬇️
ubuntu2004 58.72% <54.08%> (-0.01%) ⬇️
ubuntu2004-zstd 58.84% <54.08%> (-0.02%) ⬇️
ubuntu2204 74.29% <50.92%> (-0.07%) ⬇️
ubuntu2204-icpx 60.63% <48.90%> (-0.12%) ⬇️
ubuntu2404 74.54% <50.92%> (-0.08%) ⬇️
ubuntu2404-arm64 74.53% <50.92%> (-0.06%) ⬇️
unittest 82.76% <66.86%> (-0.06%) ⬇️
windows-2022 84.59% <60.52%> (-0.21%) ⬇️
windows-2025 84.72% <63.11%> (-0.13%) ⬇️
winpcap 84.82% <60.52%> (-0.13%) ⬇️
xdp 51.24% <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