PcapPlusPlus
PfRingDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_PF_RING_DEVICE
2 #define PCAPPP_PF_RING_DEVICE
3 
4 #include "PcapDevice.h"
5 #include "PcapFilter.h"
6 #include "MacAddress.h"
7 #include "SystemUtils.h"
8 #include "RawPacket.h"
9 #include "Packet.h"
10 #include <pthread.h>
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, PfRingDevice* device, void* userCookie);
28 
29 
34  class PfRingDevice : public IPcapDevice
35  {
36  friend class PfRingDeviceList;
37  private:
38 
39  struct CoreConfiguration
40  {
41  pthread_t 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  char m_DeviceName[30];
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  static void* captureThreadMain(void *ptr);
68 
69  int openSingleRxChannel(const char* deviceName, pfring** ring);
70 
71  inline bool getIsHwClockEnable() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
72  bool setPfRingDeviceClock(pfring* ring);
73 
74  void clearCoreConfiguration();
75  int getCoresInUseCount();
76 
77  void setPfRingDeviceAttributes();
78 
79  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
80  public:
81 
86  {
95  };
96 
100  ~PfRingDevice();
101 
106  MacAddress getMacAddress() { setPfRingDeviceAttributes(); return m_MacAddress; }
107 
112  int getInterfaceIndex() { setPfRingDeviceAttributes(); return m_InterfaceIndex; }
113 
118  int getMtu() { setPfRingDeviceAttributes(); return m_DeviceMTU; }
119 
125  bool isHwClockEnabledForDevice() { setPfRingDeviceAttributes(); return m_HwClockEnabled; }
126 
131  inline std::string getDeviceName() { return std::string(m_DeviceName); }
132 
133 
140  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
141 
151  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
152 
156  void stopCapture();
157 
158 
166  bool openSingleRxChannel(uint8_t channelId);
167 
176  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
177 
189  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
190 
198  inline uint8_t getNumOfOpenedRxChannels() { return m_NumOfOpenedRxChannels; }
199 
204  uint8_t getTotalNumOfRxChannels();
205 
211 
217  void getThreadStatistics(SystemCore core, pcap_stat& stats);
218 
223  void getCurrentThreadStatistics(pcap_stat& stats);
224 
225 
226 
227  // implement abstract methods
228 
229 
235  bool open();
236 
240  void close();
241 
247  void getStatistics(pcap_stat& stats);
248 
249 
254  bool setFilter(GeneralFilter& filter);
255 
260  bool setFilter(std::string filterAsString);
261 
266  bool removeFilter();
267 
272  bool isFilterCurrentlySet();
273 
274 
284  bool sendPacket(const RawPacket& rawPacket);
285 
297  bool sendPacket(const uint8_t* packetData, int packetDataLength);
298 
308  bool sendPacket(const Packet& packet);
309 
319  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
320 
330  int sendPackets(const Packet** packetsArr, int arrLength);
331 
340  int sendPackets(const RawPacketVector& rawPackets);
341  };
342 
343 } // namespace pcpp
344 
345 #endif /* PCAPPP_PF_RING_DEVICE */
MacAddress getMacAddress()
Definition: PfRingDevice.h:106
bool setFilter(GeneralFilter &filter)
void getCurrentThreadStatistics(pcap_stat &stats)
ChannelDistribution
Definition: PfRingDevice.h:85
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
std::string getDeviceName()
Definition: PfRingDevice.h:131
uint8_t getNumOfOpenedRxChannels()
Definition: PfRingDevice.h:198
Definition: RawPacket.h:220
Definition: PcapDevice.h:38
Definition: Packet.h:26
The main namespace for the PcapPlusPlus lib.
void getThreadStatistics(SystemCore core, pcap_stat &stats)
Definition: PcapFilter.h:72
Definition: SystemUtils.h:32
void getStatistics(pcap_stat &stats)
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
Definition: PointerVector.h:24
Definition: PfRingDevice.h:34
SystemCore getCurrentCoreId()
Definition: PfRingDevice.h:90
uint8_t getTotalNumOfRxChannels()
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:125
Definition: PfRingDeviceList.h:20
Definition: MacAddress.h:21
int getMtu()
Definition: PfRingDevice.h:118
int getInterfaceIndex()
Definition: PfRingDevice.h:112
bool isFilterCurrentlySet()
Definition: PfRingDevice.h:94
bool sendPacket(const RawPacket &rawPacket)
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)