PcapPlusPlus  22.11
PfRingDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_PF_RING_DEVICE
2 #define PCAPPP_PF_RING_DEVICE
3 
4 #include "Device.h"
5 #include "MacAddress.h"
6 #include "SystemUtils.h"
7 #include "Packet.h"
8 #include <thread>
9 #include <condition_variable>
10 
12 
13 // forward declaration of PF_RING structs
14 struct __pfring;
15 typedef struct __pfring pfring;
16 
21 namespace pcpp
22 {
23 
24  class PfRingDevice;
25 
26  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, PfRingDevice* device, void* userCookie);
27 
28 
33  class PfRingDevice : public IDevice, public IFilterableDevice
34  {
35  friend class PfRingDeviceList;
36  private:
37 
38  struct CoreConfiguration
39  {
40  std::thread RxThread;
41  pfring* Channel;
42  bool IsInUse;
43  bool IsAffinitySet;
44 
45  CoreConfiguration();
46  void clear();
47  };
48 
49  pfring** m_PfRingDescriptors;
50  uint8_t m_NumOfOpenedRxChannels;
51  std::string m_DeviceName;
52  int m_InterfaceIndex;
53  MacAddress m_MacAddress;
54  int m_DeviceMTU;
55  CoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
56  bool m_StopThread;
57  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
58  void* m_OnPacketsArriveUserCookie;
59  bool m_ReentrantMode;
60  bool m_HwClockEnabled;
61  bool m_IsFilterCurrentlySet;
62 
63  PfRingDevice(const char* deviceName);
64 
65  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
66  void captureThreadMain(std::condition_variable* startCond, std::mutex* startMutex, const int* startState);
67 
68  int openSingleRxChannel(const char* deviceName, pfring** ring);
69 
70  bool getIsHwClockEnable() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
71  bool setPfRingDeviceClock(pfring* ring);
72 
73  void clearCoreConfiguration();
74  int getCoresInUseCount() const;
75 
76  void setPfRingDeviceAttributes();
77 
78  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
79  public:
80 
85  {
94  };
95 
100  struct PfRingStats
101  {
103  uint64_t recv;
105  uint64_t drop;
106  };
107 
111  ~PfRingDevice();
112 
117  MacAddress getMacAddress() { setPfRingDeviceAttributes(); return m_MacAddress; }
118 
123  int getInterfaceIndex() { setPfRingDeviceAttributes(); return m_InterfaceIndex; }
124 
129  int getMtu() { setPfRingDeviceAttributes(); return m_DeviceMTU; }
130 
136  bool isHwClockEnabledForDevice() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
137 
142  std::string getDeviceName() const { return m_DeviceName; }
143 
144 
151  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
152 
162  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
163 
167  void stopCapture();
168 
169 
177  bool openSingleRxChannel(uint8_t channelId);
178 
187  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
188 
200  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
201 
209  uint8_t getNumOfOpenedRxChannels() const { return m_NumOfOpenedRxChannels; }
210 
215  uint8_t getTotalNumOfRxChannels() const;
216 
222 
228  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
229 
234  void getCurrentThreadStatistics(PfRingStats& stats) const;
235 
241  void getStatistics(PfRingStats& stats) const;
242 
247  bool isFilterCurrentlySet() const;
248 
258  bool sendPacket(const RawPacket& rawPacket);
259 
271  bool sendPacket(const uint8_t* packetData, int packetDataLength);
272 
282  bool sendPacket(const Packet& packet);
283 
293  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
294 
304  int sendPackets(const Packet** packetsArr, int arrLength);
305 
314  int sendPackets(const RawPacketVector& rawPackets);
315 
316 
317  // implement abstract methods
318 
319 
325  bool open();
326 
330  void close();
331 
333 
338  bool setFilter(std::string filterAsString);
339 
344  bool clearFilter();
345  };
346 
347 } // namespace pcpp
348 
349 #endif /* PCAPPP_PF_RING_DEVICE */
The main namespace for the PcapPlusPlus lib.
std::string getDeviceName() const
Definition: PfRingDevice.h:142
ChannelDistribution
Definition: PfRingDevice.h:84
uint64_t drop
Definition: PfRingDevice.h:105
Definition: SystemUtils.h:27
bool setFilter(std::string filterAsString)
Definition: Packet.h:26
Definition: PointerVector.h:24
Definition: RawPacket.h:252
Definition: Device.h:24
Definition: PfRingDeviceList.h:20
void getStatistics(PfRingStats &stats) const
Definition: PfRingDevice.h:33
SystemCore getCurrentCoreId() const
Definition: Device.h:61
uint64_t recv
Definition: PfRingDevice.h:103
Definition: PfRingDevice.h:93
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
Definition: PfRingDevice.h:100
void getCurrentThreadStatistics(PfRingStats &stats) const
int getMtu()
Definition: PfRingDevice.h:129
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:209
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
bool isFilterCurrentlySet() const
MacAddress getMacAddress()
Definition: PfRingDevice.h:117
int getInterfaceIndex()
Definition: PfRingDevice.h:123
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
uint8_t getTotalNumOfRxChannels() const
bool sendPacket(const RawPacket &rawPacket)
Definition: PfRingDevice.h:89
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:77
Definition: MacAddress.h:28
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:136
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)