PcapPlusPlus  24.09
PfRingDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 // GCOVR_EXCL_START
4 
5 #include "Device.h"
6 #include "MacAddress.h"
7 #include "SystemUtils.h"
8 #include "Packet.h"
9 #include <thread>
10 #include <condition_variable>
11 
13 
14 // forward declaration of PF_RING structs
15 struct __pfring;
16 typedef struct __pfring pfring;
17 
22 namespace pcpp
23 {
24 
25  class PfRingDevice;
26 
27  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
28  PfRingDevice* device, void* userCookie);
29 
34  class PfRingDevice : public IDevice, public IFilterableDevice
35  {
36  friend class PfRingDeviceList;
37 
38  private:
39  struct CoreConfiguration
40  {
41  std::thread RxThread;
42  pfring* Channel;
43  bool IsInUse;
44  bool IsAffinitySet;
45 
46  CoreConfiguration();
47  void clear();
48  };
49 
50  pfring** m_PfRingDescriptors;
51  uint8_t m_NumOfOpenedRxChannels;
52  std::string m_DeviceName;
53  int m_InterfaceIndex;
54  MacAddress m_MacAddress;
55  int m_DeviceMTU;
56  CoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
57  bool m_StopThread;
58  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
59  void* m_OnPacketsArriveUserCookie;
60  bool m_ReentrantMode;
61  bool m_HwClockEnabled;
62  bool m_IsFilterCurrentlySet;
63 
64  PfRingDevice(const char* deviceName);
65 
66  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
67  void captureThreadMain(std::condition_variable* startCond, std::mutex* startMutex, const int* startState);
68 
69  int openSingleRxChannel(const char* deviceName, pfring** ring);
70 
71  bool getIsHwClockEnable()
72  {
73  setPfRingDeviceAttributes();
74  return m_HwClockEnabled;
75  }
76  bool setPfRingDeviceClock(pfring* ring);
77 
78  void clearCoreConfiguration();
79  int getCoresInUseCount() const;
80 
81  void setPfRingDeviceAttributes();
82 
83  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
84 
85  public:
90  {
98  PerFlow
99  };
100 
105  struct PfRingStats
106  {
108  uint64_t recv;
110  uint64_t drop;
111  };
112 
117 
123  {
124  setPfRingDeviceAttributes();
125  return m_MacAddress;
126  }
127 
133  {
134  setPfRingDeviceAttributes();
135  return m_InterfaceIndex;
136  }
137 
142  int getMtu()
143  {
144  setPfRingDeviceAttributes();
145  return m_DeviceMTU;
146  }
147 
154  {
155  setPfRingDeviceAttributes();
156  return m_HwClockEnabled;
157  }
158 
163  std::string getDeviceName() const
164  {
165  return m_DeviceName;
166  }
167 
175  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
176 
187  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
188  CoreMask coreMask);
189 
193  void stopCapture();
194 
202  bool openSingleRxChannel(uint8_t channelId);
203 
212  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
213 
225  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
226 
234  uint8_t getNumOfOpenedRxChannels() const
235  {
236  return m_NumOfOpenedRxChannels;
237  }
238 
243  uint8_t getTotalNumOfRxChannels() const;
244 
250 
257  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
258 
265 
272  void getStatistics(PfRingStats& stats) const;
273 
278  bool isFilterCurrentlySet() const;
279 
289  bool sendPacket(const RawPacket& rawPacket);
290 
302  bool sendPacket(const uint8_t* packetData, int packetDataLength);
303 
313  bool sendPacket(const Packet& packet);
314 
324  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
325 
335  int sendPackets(const Packet** packetsArr, int arrLength);
336 
345  int sendPackets(const RawPacketVector& rawPackets);
346 
347  // implement abstract methods
348 
354  bool open();
355 
359  void close();
360 
362 
367  bool setFilter(std::string filterAsString);
368 
373  bool clearFilter();
374  };
375 
376 } // namespace pcpp
377 
378 // GCOVR_EXCL_STOP
Definition: Device.h:24
Definition: Device.h:63
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:79
Definition: MacAddress.h:25
Definition: Packet.h:27
Definition: PfRingDevice.h:35
bool sendPacket(const RawPacket &rawPacket)
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
ChannelDistribution
Definition: PfRingDevice.h:90
@ RoundRobin
Definition: PfRingDevice.h:94
@ PerFlow
Definition: PfRingDevice.h:98
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
int getMtu()
Definition: PfRingDevice.h:142
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:153
bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist)
bool openSingleRxChannel(uint8_t channelId)
uint8_t getTotalNumOfRxChannels() const
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
bool sendPacket(const Packet &packet)
int sendPackets(const Packet **packetsArr, int arrLength)
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)
int sendPackets(const RawPacketVector &rawPackets)
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:234
std::string getDeviceName() const
Definition: PfRingDevice.h:163
int getInterfaceIndex()
Definition: PfRingDevice.h:132
bool isFilterCurrentlySet() const
SystemCore getCurrentCoreId() const
void getCurrentThreadStatistics(PfRingStats &stats) const
MacAddress getMacAddress()
Definition: PfRingDevice.h:122
bool sendPacket(const uint8_t *packetData, int packetDataLength)
bool setFilter(std::string filterAsString)
void getStatistics(PfRingStats &stats) const
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
Definition: PfRingDeviceList.h:23
Definition: PointerVector.h:29
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
Definition: PfRingDevice.h:106
uint64_t drop
Definition: PfRingDevice.h:110
uint64_t recv
Definition: PfRingDevice.h:108
Definition: SystemUtils.h:27