PcapPlusPlus  20.08
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 <pthread.h>
9 
11 
12 // forward declaration of PF_RING structs
13 struct __pfring;
14 typedef struct __pfring pfring;
15 
20 namespace pcpp
21 {
22 
23  class PfRingDevice;
24 
25  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, PfRingDevice* device, void* userCookie);
26 
27 
32  class PfRingDevice : public IDevice, public IFilterableDevice
33  {
34  friend class PfRingDeviceList;
35  private:
36 
37  struct CoreConfiguration
38  {
39  pthread_t RxThread;
40  pfring* Channel;
41  bool IsInUse;
42  bool IsAffinitySet;
43 
44  CoreConfiguration();
45  void clear();
46  };
47 
48  pfring** m_PfRingDescriptors;
49  uint8_t m_NumOfOpenedRxChannels;
50  char m_DeviceName[30];
51  int m_InterfaceIndex;
52  MacAddress m_MacAddress;
53  int m_DeviceMTU;
54  CoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
55  bool m_StopThread;
56  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
57  void* m_OnPacketsArriveUserCookie;
58  bool m_ReentrantMode;
59  bool m_HwClockEnabled;
60  bool m_IsFilterCurrentlySet;
61 
62  PfRingDevice(const char* deviceName);
63 
64  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
65  static void* captureThreadMain(void *ptr);
66 
67  int openSingleRxChannel(const char* deviceName, pfring** ring);
68 
69  bool getIsHwClockEnable() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
70  bool setPfRingDeviceClock(pfring* ring);
71 
72  void clearCoreConfiguration();
73  int getCoresInUseCount() const;
74 
75  void setPfRingDeviceAttributes();
76 
77  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
78  public:
79 
84  {
93  };
94 
99  struct PfRingStats
100  {
102  uint64_t recv;
104  uint64_t drop;
105  };
106 
110  ~PfRingDevice();
111 
116  MacAddress getMacAddress() { setPfRingDeviceAttributes(); return m_MacAddress; }
117 
122  int getInterfaceIndex() { setPfRingDeviceAttributes(); return m_InterfaceIndex; }
123 
128  int getMtu() { setPfRingDeviceAttributes(); return m_DeviceMTU; }
129 
135  bool isHwClockEnabledForDevice() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
136 
141  std::string getDeviceName() const { return std::string(m_DeviceName); }
142 
143 
150  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
151 
161  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
162 
166  void stopCapture();
167 
168 
176  bool openSingleRxChannel(uint8_t channelId);
177 
186  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
187 
199  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
200 
208  uint8_t getNumOfOpenedRxChannels() const { return m_NumOfOpenedRxChannels; }
209 
214  uint8_t getTotalNumOfRxChannels() const;
215 
221 
227  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
228 
233  void getCurrentThreadStatistics(PfRingStats& stats) const;
234 
240  void getStatistics(PfRingStats& stats) const;
241 
246  bool isFilterCurrentlySet() const;
247 
257  bool sendPacket(const RawPacket& rawPacket);
258 
270  bool sendPacket(const uint8_t* packetData, int packetDataLength);
271 
281  bool sendPacket(const Packet& packet);
282 
292  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
293 
303  int sendPackets(const Packet** packetsArr, int arrLength);
304 
313  int sendPackets(const RawPacketVector& rawPackets);
314 
315 
316  // implement abstract methods
317 
318 
324  bool open();
325 
329  void close();
330 
332 
337  bool setFilter(std::string filterAsString);
338 
343  bool clearFilter();
344  };
345 
346 } // namespace pcpp
347 
348 #endif /* PCAPPP_PF_RING_DEVICE */
pcpp::PfRingDevice::PfRingStats::drop
uint64_t drop
Definition: PfRingDevice.h:104
pcpp::SystemCore
Definition: SystemUtils.h:32
pcpp::PfRingDevice::setFilter
bool setFilter(std::string filterAsString)
pcpp::Packet
Definition: Packet.h:26
pcpp::PfRingDevice::PfRingStats
Definition: PfRingDevice.h:99
pcpp::PfRingDevice::stopCapture
void stopCapture()
pcpp::PfRingDevice::isHwClockEnabledForDevice
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:135
pcpp::PfRingDevice::clearFilter
bool clearFilter()
pcpp::PfRingDevice::ChannelDistribution
ChannelDistribution
Definition: PfRingDevice.h:83
SystemUtils.h
MacAddress.h
pcpp::PfRingDevice::getStatistics
void getStatistics(PfRingStats &stats) const
pcpp::IFilterableDevice::setFilter
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:76
pcpp::PfRingDevice::getCurrentCoreId
SystemCore getCurrentCoreId() const
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::PfRingDevice::PerFlow
@ PerFlow
Definition: PfRingDevice.h:92
pcpp::PfRingDevice::startCaptureMultiThread
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
pcpp::IFilterableDevice
Definition: Device.h:60
pcpp::PfRingDevice::getThreadStatistics
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
Device.h
pcpp::PfRingDevice::getInterfaceIndex
int getInterfaceIndex()
Definition: PfRingDevice.h:122
pcpp::PfRingDevice::sendPackets
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::PfRingDevice::open
bool open()
pcpp::PfRingDevice::getMtu
int getMtu()
Definition: PfRingDevice.h:128
pcpp::PfRingDevice::getCurrentThreadStatistics
void getCurrentThreadStatistics(PfRingStats &stats) const
pcpp::PfRingDevice::getNumOfOpenedRxChannels
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:208
pcpp::PfRingDevice::getTotalNumOfRxChannels
uint8_t getTotalNumOfRxChannels() const
pcpp::PfRingDevice::startCaptureSingleThread
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
pcpp::PfRingDevice::PfRingStats::recv
uint64_t recv
Definition: PfRingDevice.h:102
pcpp::PfRingDevice
Definition: PfRingDevice.h:32
pcpp::PfRingDevice::close
void close()
Packet.h
pcpp::PfRingDevice::openMultiRxChannels
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)
pcpp::PfRingDevice::~PfRingDevice
~PfRingDevice()
pcpp::PfRingDevice::isFilterCurrentlySet
bool isFilterCurrentlySet() const
pcpp::PointerVector
Definition: PointerVector.h:24
pcpp::PfRingDevice::getDeviceName
std::string getDeviceName() const
Definition: PfRingDevice.h:141
pcpp::IDevice
Definition: Device.h:24
pcpp::PfRingDevice::getMacAddress
MacAddress getMacAddress()
Definition: PfRingDevice.h:116
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::PfRingDevice::RoundRobin
@ RoundRobin
Definition: PfRingDevice.h:88
pcpp::PfRingDevice::sendPacket
bool sendPacket(const RawPacket &rawPacket)
pcpp::PfRingDeviceList
Definition: PfRingDeviceList.h:20