PcapPlusPlus  Next
PfRingDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 // GCOVR_EXCL_START
4 
5 #include "Device.h"
6 #include "MacAddress.h"
7 #include "SystemUtils.h"
8 #include "Packet.h"
9 #include <thread>
10 #include <mutex>
11 #include <condition_variable>
12 
14 
15 // forward declaration of PF_RING structs
16 struct __pfring;
17 typedef struct __pfring pfring;
18 
21 namespace pcpp
22 {
23 
24  class PfRingDevice;
25 
26  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
27  PfRingDevice* device, void* userCookie);
28 
31  class PfRingDevice : public IDevice, public IFilterableDevice
32  {
33  friend class PfRingDeviceList;
34 
35  private:
36  struct CoreConfiguration
37  {
38  std::thread RxThread;
39  pfring* Channel;
40  bool IsInUse;
41  bool IsAffinitySet;
42 
43  CoreConfiguration();
44  void clear();
45  };
46 
47  struct StartupBlock
48  {
49  std::mutex Mutex;
50  std::condition_variable Cond;
51  int State = 0;
52  };
53 
54  pfring** m_PfRingDescriptors;
55  uint8_t m_NumOfOpenedRxChannels;
56  std::string m_DeviceName;
57  int m_InterfaceIndex;
58  MacAddress m_MacAddress;
59  int m_DeviceMTU;
60  CoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
61  bool m_StopThread;
62  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
63  void* m_OnPacketsArriveUserCookie;
64  bool m_ReentrantMode;
65  bool m_HwClockEnabled;
66  bool m_IsFilterCurrentlySet;
67 
68  PfRingDevice(const char* deviceName);
69 
70  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
71  void captureThreadMain(std::shared_ptr<StartupBlock> startupBlock);
72 
73  int openSingleRxChannel(const char* deviceName, pfring** ring);
74 
75  bool getIsHwClockEnable()
76  {
77  setPfRingDeviceAttributes();
78  return m_HwClockEnabled;
79  }
80  bool setPfRingDeviceClock(pfring* ring);
81 
82  void clearCoreConfiguration();
83  int getCoresInUseCount() const;
84 
85  void setPfRingDeviceAttributes();
86 
87  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
88 
89  public:
92  {
96  PerFlow
97  };
98 
101  struct PfRingStats
102  {
104  uint64_t recv;
106  uint64_t drop;
107  };
108 
111 
115  {
116  setPfRingDeviceAttributes();
117  return m_MacAddress;
118  }
119 
123  {
124  setPfRingDeviceAttributes();
125  return m_InterfaceIndex;
126  }
127 
130  int getMtu()
131  {
132  setPfRingDeviceAttributes();
133  return m_DeviceMTU;
134  }
135 
140  {
141  setPfRingDeviceAttributes();
142  return m_HwClockEnabled;
143  }
144 
147  std::string getDeviceName() const
148  {
149  return m_DeviceName;
150  }
151 
157  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
158 
167  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
168  CoreMask coreMask);
169 
171  void stopCapture();
172 
178  bool openSingleRxChannel(uint8_t channelId);
179 
186  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
187 
197  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
198 
204  uint8_t getNumOfOpenedRxChannels() const
205  {
206  return m_NumOfOpenedRxChannels;
207  }
208 
211  uint8_t getTotalNumOfRxChannels() const;
212 
216 
221  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
222 
227 
232  void getStatistics(PfRingStats& stats) const;
233 
236  bool isFilterCurrentlySet() const;
237 
245  bool sendPacket(const RawPacket& rawPacket);
246 
255  bool sendPacket(const uint8_t* packetData, int packetDataLength);
256 
264  bool sendPacket(const Packet& packet);
265 
273  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
274 
282  int sendPackets(const Packet** packetsArr, int arrLength);
283 
290  int sendPackets(const RawPacketVector& rawPackets);
291 
292  // implement abstract methods
293 
297  bool open();
298 
300  void close();
301 
303 
306  bool setFilter(std::string filterAsString);
307 
310  bool clearFilter();
311  };
312 
313 } // namespace pcpp
314 
315 // GCOVR_EXCL_STOP
Definition: Device.h:20
Definition: Device.h:51
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:63
Definition: MacAddress.h:21
Definition: Packet.h:27
Definition: PfRingDevice.h:32
bool sendPacket(const RawPacket &rawPacket)
bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
ChannelDistribution
An enum representing the type of packet distribution between different RX channels.
Definition: PfRingDevice.h:92
@ RoundRobin
Packets are distributed between channels in a round-robin manner.
Definition: PfRingDevice.h:94
@ PerFlow
Packets are distributed between channels per flow (each flow goes for different channel)
Definition: PfRingDevice.h:96
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
void stopCapture()
Stops capturing packets (works will all type of startCapture*)
int getMtu()
Definition: PfRingDevice.h:130
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:139
bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist)
bool openSingleRxChannel(uint8_t channelId)
uint8_t getTotalNumOfRxChannels() const
void getThreadStatistics(SystemCore core, PfRingStats &stats) const
~PfRingDevice()
A destructor for PfRingDevice class.
bool sendPacket(const Packet &packet)
int sendPackets(const Packet **packetsArr, int arrLength)
bool openMultiRxChannels(const uint8_t *channelIds, int numOfChannelIds)
int sendPackets(const RawPacketVector &rawPackets)
uint8_t getNumOfOpenedRxChannels() const
Definition: PfRingDevice.h:204
std::string getDeviceName() const
Definition: PfRingDevice.h:147
int getInterfaceIndex()
Definition: PfRingDevice.h:122
bool isFilterCurrentlySet() const
SystemCore getCurrentCoreId() const
void getCurrentThreadStatistics(PfRingStats &stats) const
MacAddress getMacAddress()
Definition: PfRingDevice.h:114
bool sendPacket(const uint8_t *packetData, int packetDataLength)
void close()
Closes all RX channels currently opened in device.
bool setFilter(std::string filterAsString)
void getStatistics(PfRingStats &stats) const
int sendPackets(const RawPacket *rawPacketsArr, int arrLength)
Definition: PfRingDeviceList.h:17
Definition: PointerVector.h:50
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
Definition: PfRingDevice.h:102
uint64_t drop
Number of packets dropped.
Definition: PfRingDevice.h:106
uint64_t recv
Number of packets received.
Definition: PfRingDevice.h:104
Definition: SystemUtils.h:23