PcapPlusPlus  21.05
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 */
The main namespace for the PcapPlusPlus lib.
std::string getDeviceName() const
Definition: PfRingDevice.h:141
ChannelDistribution
Definition: PfRingDevice.h:83
uint64_t drop
Definition: PfRingDevice.h:104
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:32
SystemCore getCurrentCoreId() const
Definition: Device.h:60
uint64_t recv
Definition: PfRingDevice.h:102
Definition: PfRingDevice.h:92
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
Definition: PfRingDevice.h:99
void getCurrentThreadStatistics(PfRingStats &stats) const
int getMtu()
Definition: PfRingDevice.h:128
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:208
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
bool isFilterCurrentlySet() const
MacAddress getMacAddress()
Definition: PfRingDevice.h:116
int getInterfaceIndex()
Definition: PfRingDevice.h:122
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
uint8_t getTotalNumOfRxChannels() const
bool sendPacket(const RawPacket &rawPacket)
Definition: PfRingDevice.h:88
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:76
Definition: MacAddress.h:27
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:135
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)