From 5e7a3d4d45830e538a8ce136682a1de05c12df07 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 23 Mar 2025 08:02:19 +0200 Subject: [PATCH 1/3] Renamed private overload of `openSingleRxChannel` to `openSingleRxChannelImpl` to remove ambiguity between public and private calls. --- Pcap++/header/PfRingDevice.h | 3 ++- Pcap++/src/PfRingDevice.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Pcap++/header/PfRingDevice.h b/Pcap++/header/PfRingDevice.h index 6ebdb47ed..37991a905 100644 --- a/Pcap++/header/PfRingDevice.h +++ b/Pcap++/header/PfRingDevice.h @@ -70,7 +70,8 @@ namespace pcpp bool initCoreConfigurationByCoreMask(CoreMask coreMask); void captureThreadMain(std::shared_ptr startupBlock); - int openSingleRxChannel(const char* deviceName, pfring** ring); + // Opens a single RxChannel for the device. Does not use PF_RING_REENTRANT flag. + int openSingleRxChannelImpl(const char* deviceName, pfring** ring); bool getIsHwClockEnable() { diff --git a/Pcap++/src/PfRingDevice.cpp b/Pcap++/src/PfRingDevice.cpp index 29e57cbc3..8f8ed52bf 100644 --- a/Pcap++/src/PfRingDevice.cpp +++ b/Pcap++/src/PfRingDevice.cpp @@ -70,7 +70,7 @@ namespace pcpp m_NumOfOpenedRxChannels = 0; PCPP_LOG_DEBUG("Trying to open device [" << m_DeviceName << "]"); - int res = openSingleRxChannel(m_DeviceName.c_str(), &m_PfRingDescriptors[0]); + int res = openSingleRxChannelImpl(m_DeviceName.c_str(), &m_PfRingDescriptors[0]); if (res == 0) { PCPP_LOG_DEBUG("Succeeded opening device [" << m_DeviceName << "]"); @@ -92,7 +92,7 @@ namespace pcpp return openMultiRxChannels(channelIds, 1); } - int PfRingDevice::openSingleRxChannel(const char* deviceName, pfring** ring) + int PfRingDevice::openSingleRxChannelImpl(const char* deviceName, pfring** ring) { if (m_DeviceOpened) { @@ -176,7 +176,7 @@ namespace pcpp std::string ringName = ringNameStream.str(); PCPP_LOG_DEBUG("Trying to open device [" << m_DeviceName << "] on channel [" << channelId << "]. Channel name [" << ringName << "]"); - int res = openSingleRxChannel(ringName.c_str(), &m_PfRingDescriptors[i]); + int res = openSingleRxChannelImpl(ringName.c_str(), &m_PfRingDescriptors[i]); if (res == 0) { PCPP_LOG_DEBUG("Succeeded opening device [" << m_DeviceName << "] on channel [" << channelId @@ -198,7 +198,7 @@ namespace pcpp { // if an error occurred, close all rings from index=0 to index=m_NumOfOpenedRxChannels-1 // there's no need to close m_PfRingDescriptors[m_NumOfOpenedRxChannels] because it has already been - // closed by openSingleRxChannel + // closed by openSingleRxChannelImpl for (int i = 0; i < m_NumOfOpenedRxChannels - 1; i++) { pfring_close(m_PfRingDescriptors[i]); From 1678405f664be6e4aaac61fd159f99ef62996b55 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 23 Mar 2025 08:16:06 +0200 Subject: [PATCH 2/3] Changed `m_ReentrantMode` to be set when opening the channels instead of when starting capture as it is determined by the PF_RING_REENTRANT flag. - Added ability to choose if `openSingleChannelImpl` will open w/reenterant flag. --- Pcap++/header/PfRingDevice.h | 3 +-- Pcap++/src/PfRingDevice.cpp | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Pcap++/header/PfRingDevice.h b/Pcap++/header/PfRingDevice.h index 37991a905..583c56cdd 100644 --- a/Pcap++/header/PfRingDevice.h +++ b/Pcap++/header/PfRingDevice.h @@ -70,8 +70,7 @@ namespace pcpp bool initCoreConfigurationByCoreMask(CoreMask coreMask); void captureThreadMain(std::shared_ptr startupBlock); - // Opens a single RxChannel for the device. Does not use PF_RING_REENTRANT flag. - int openSingleRxChannelImpl(const char* deviceName, pfring** ring); + int openSingleRxChannelImpl(const char* deviceName, pfring** ring, bool useReenterant = false); bool getIsHwClockEnable() { diff --git a/Pcap++/src/PfRingDevice.cpp b/Pcap++/src/PfRingDevice.cpp index 8f8ed52bf..cd797065a 100644 --- a/Pcap++/src/PfRingDevice.cpp +++ b/Pcap++/src/PfRingDevice.cpp @@ -75,6 +75,8 @@ namespace pcpp { PCPP_LOG_DEBUG("Succeeded opening device [" << m_DeviceName << "]"); m_NumOfOpenedRxChannels = 1; + // Set reentrant mode to false as the channel is opened without the PF_RING_REENTRANT flag. + m_ReentrantMode = false; m_DeviceOpened = true; return true; } @@ -92,7 +94,7 @@ namespace pcpp return openMultiRxChannels(channelIds, 1); } - int PfRingDevice::openSingleRxChannelImpl(const char* deviceName, pfring** ring) + int PfRingDevice::openSingleRxChannelImpl(const char* deviceName, pfring** ring, bool useReenterant) { if (m_DeviceOpened) { @@ -101,6 +103,11 @@ namespace pcpp } uint32_t flags = PF_RING_PROMISC | PF_RING_HW_TIMESTAMP | PF_RING_DNA_SYMMETRIC_RSS; + if (useReenterant) + { + flags |= PF_RING_REENTRANT; + } + *ring = pfring_open(deviceName, DEFAULT_PF_RING_SNAPLEN, flags); if (*ring == nullptr) @@ -176,6 +183,8 @@ namespace pcpp std::string ringName = ringNameStream.str(); PCPP_LOG_DEBUG("Trying to open device [" << m_DeviceName << "] on channel [" << channelId << "]. Channel name [" << ringName << "]"); + // todo: Shouldn't we use the reentrant mode here? We are opening multiple channels? + // todo: Potentially only open in reenterant mode if creating N > 1 channels? int res = openSingleRxChannelImpl(ringName.c_str(), &m_PfRingDescriptors[i]); if (res == 0) { @@ -208,6 +217,8 @@ namespace pcpp return false; } + // Set reentrant mode to false as the channels are opened without the PF_RING_REENTRANT flag. + m_ReentrantMode = false; m_DeviceOpened = true; return true; @@ -329,7 +340,8 @@ namespace pcpp } m_NumOfOpenedRxChannels = ringsOpen; - + // Set reentrant mode to true as the channels are opened with the PF_RING_REENTRANT flag. + m_ReentrantMode = true; m_DeviceOpened = true; return true; } @@ -478,8 +490,6 @@ namespace pcpp if (!m_CoreConfiguration[coreId].IsInUse) continue; - m_ReentrantMode = true; - m_OnPacketsArriveCallback = onPacketsArrive; m_OnPacketsArriveUserCookie = onPacketsArriveUserCookie; @@ -538,8 +548,6 @@ namespace pcpp m_StopThread = false; - m_ReentrantMode = false; - std::shared_ptr startupBlock = std::make_shared(); m_CoreConfiguration[0].IsInUse = true; From 3123107139a705f19dc0ecec9e44e4345071e2c1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 2 Apr 2025 23:28:57 +0300 Subject: [PATCH 3/3] Lint --- Pcap++/src/PfRingDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PfRingDevice.cpp b/Pcap++/src/PfRingDevice.cpp index cd797065a..a9c2a7781 100644 --- a/Pcap++/src/PfRingDevice.cpp +++ b/Pcap++/src/PfRingDevice.cpp @@ -218,7 +218,7 @@ namespace pcpp } // Set reentrant mode to false as the channels are opened without the PF_RING_REENTRANT flag. - m_ReentrantMode = false; + m_ReentrantMode = false; m_DeviceOpened = true; return true;