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 */
void getThreadStatistics(SystemCore core, pcap_stat &stats)
Definition: PcapFilter.h:72
ChannelDistribution
Definition: PfRingDevice.h:85
bool setFilter(GeneralFilter &filter)
bool isFilterCurrentlySet()
Definition: RawPacket.h:220
Definition: PfRingDeviceList.h:20
uint8_t getTotalNumOfRxChannels()
void getCurrentThreadStatistics(pcap_stat &stats)
int getInterfaceIndex()
Definition: PfRingDevice.h:112
Definition: PcapDevice.h:26
Definition: SystemUtils.h:32
Definition: MacAddress.h:21
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
MacAddress getMacAddress()
Definition: PfRingDevice.h:106
Definition: Packet.h:26
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: PfRingDevice.h:90
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:125
Definition: PfRingDevice.h:94
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
int getMtu()
Definition: PfRingDevice.h:118
SystemCore getCurrentCoreId()
uint8_t getNumOfOpenedRxChannels()
Definition: PfRingDevice.h:198
void getStatistics(pcap_stat &stats)
bool sendPacket(const RawPacket &rawPacket)
std::string getDeviceName()
Definition: PfRingDevice.h:131
The main namespace for the PcapPlusPlus lib.
Definition: PointerVector.h:24
Definition: PfRingDevice.h:34
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)