PcapPlusPlus  23.09
PfRingDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_PF_RING_DEVICE
2 #define PCAPPP_PF_RING_DEVICE
3 
4 // GCOVR_EXCL_START
5 
6 #include "Device.h"
7 #include "MacAddress.h"
8 #include "SystemUtils.h"
9 #include "Packet.h"
10 #include <thread>
11 #include <condition_variable>
12 
14 
15 // forward declaration of PF_RING structs
16 struct __pfring;
17 typedef struct __pfring pfring;
18 
23 namespace pcpp
24 {
25 
26  class PfRingDevice;
27 
28  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, PfRingDevice* device, void* userCookie);
29 
30 
35  class PfRingDevice : public IDevice, public IFilterableDevice
36  {
37  friend class PfRingDeviceList;
38  private:
39 
40  struct CoreConfiguration
41  {
42  std::thread RxThread;
43  pfring* Channel;
44  bool IsInUse;
45  bool IsAffinitySet;
46 
47  CoreConfiguration();
48  void clear();
49  };
50 
51  pfring** m_PfRingDescriptors;
52  uint8_t m_NumOfOpenedRxChannels;
53  std::string m_DeviceName;
54  int m_InterfaceIndex;
55  MacAddress m_MacAddress;
56  int m_DeviceMTU;
57  CoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
58  bool m_StopThread;
59  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
60  void* m_OnPacketsArriveUserCookie;
61  bool m_ReentrantMode;
62  bool m_HwClockEnabled;
63  bool m_IsFilterCurrentlySet;
64 
65  PfRingDevice(const char* deviceName);
66 
67  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
68  void captureThreadMain(std::condition_variable* startCond, std::mutex* startMutex, const int* startState);
69 
70  int openSingleRxChannel(const char* deviceName, pfring** ring);
71 
72  bool getIsHwClockEnable() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
73  bool setPfRingDeviceClock(pfring* ring);
74 
75  void clearCoreConfiguration();
76  int getCoresInUseCount() const;
77 
78  void setPfRingDeviceAttributes();
79 
80  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
81  public:
82 
87  {
96  };
97 
102  struct PfRingStats
103  {
105  uint64_t recv;
107  uint64_t drop;
108  };
109 
113  ~PfRingDevice();
114 
119  MacAddress getMacAddress() { setPfRingDeviceAttributes(); return m_MacAddress; }
120 
125  int getInterfaceIndex() { setPfRingDeviceAttributes(); return m_InterfaceIndex; }
126 
131  int getMtu() { setPfRingDeviceAttributes(); return m_DeviceMTU; }
132 
138  bool isHwClockEnabledForDevice() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
139 
144  std::string getDeviceName() const { return m_DeviceName; }
145 
146 
153  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
154 
164  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
165 
169  void stopCapture();
170 
171 
179  bool openSingleRxChannel(uint8_t channelId);
180 
189  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
190 
202  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
203 
211  uint8_t getNumOfOpenedRxChannels() const { return m_NumOfOpenedRxChannels; }
212 
217  uint8_t getTotalNumOfRxChannels() const;
218 
224 
230  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
231 
236  void getCurrentThreadStatistics(PfRingStats& stats) const;
237 
243  void getStatistics(PfRingStats& stats) const;
244 
249  bool isFilterCurrentlySet() const;
250 
260  bool sendPacket(const RawPacket& rawPacket);
261 
273  bool sendPacket(const uint8_t* packetData, int packetDataLength);
274 
284  bool sendPacket(const Packet& packet);
285 
295  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
296 
306  int sendPackets(const Packet** packetsArr, int arrLength);
307 
316  int sendPackets(const RawPacketVector& rawPackets);
317 
318 
319  // implement abstract methods
320 
321 
327  bool open();
328 
332  void close();
333 
335 
340  bool setFilter(std::string filterAsString);
341 
346  bool clearFilter();
347  };
348 
349 } // namespace pcpp
350 
351 // GCOVR_EXCL_STOP
352 
353 #endif /* PCAPPP_PF_RING_DEVICE */
The main namespace for the PcapPlusPlus lib.
std::string getDeviceName() const
Definition: PfRingDevice.h:144
ChannelDistribution
Definition: PfRingDevice.h:86
uint64_t drop
Definition: PfRingDevice.h:107
Definition: SystemUtils.h:27
bool setFilter(std::string filterAsString)
Definition: Packet.h:26
Definition: PointerVector.h:24
Definition: RawPacket.h:254
Definition: Device.h:24
Definition: PfRingDeviceList.h:22
void getStatistics(PfRingStats &stats) const
Definition: PfRingDevice.h:35
SystemCore getCurrentCoreId() const
Definition: Device.h:60
uint64_t recv
Definition: PfRingDevice.h:105
Definition: PfRingDevice.h:95
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
Definition: PfRingDevice.h:102
void getCurrentThreadStatistics(PfRingStats &stats) const
int getMtu()
Definition: PfRingDevice.h:131
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:211
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
bool isFilterCurrentlySet() const
MacAddress getMacAddress()
Definition: PfRingDevice.h:119
int getInterfaceIndex()
Definition: PfRingDevice.h:125
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
uint8_t getTotalNumOfRxChannels() const
bool sendPacket(const RawPacket &rawPacket)
Definition: PfRingDevice.h:91
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:76
Definition: MacAddress.h:28
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:138
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)