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 <array>
10 #include <thread>
11 #include <mutex>
12 #include <atomic>
13 #include <condition_variable>
14 
16 
17 // forward declaration of PF_RING structs
18 struct __pfring;
19 typedef struct __pfring pfring;
20 
23 namespace pcpp
24 {
25 
26  class PfRingDevice;
27 
28  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
29  PfRingDevice* device, void* userCookie);
30 
33  class PfRingDevice : public IDevice, public IFilterableDevice
34  {
35  friend class PfRingDeviceList;
36 
37  private:
38  struct CoreConfiguration
39  {
40  std::thread RxThread;
41  pfring* Channel;
42  bool IsInUse;
43  bool IsAffinitySet;
44 
45  CoreConfiguration();
46  void clear();
47  };
48 
49  class StartupBlock
50  {
51  public:
52  void notifyStartup();
53  void waitForStartup();
54 
55  private:
56  std::mutex m_Mutex;
57  std::condition_variable m_Cv;
58  bool m_Ready = false;
59  };
60 
61  pfring** m_PfRingDescriptors;
62  uint8_t m_NumOfOpenedRxChannels;
63  std::string m_DeviceName;
64  int m_InterfaceIndex;
65  MacAddress m_MacAddress;
66  int m_DeviceMTU;
67  std::array<CoreConfiguration, MAX_NUM_OF_CORES> m_CoreConfiguration;
68  std::atomic<bool> m_StopThread;
69  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
70  void* m_OnPacketsArriveUserCookie;
71  bool m_ReentrantMode;
72  bool m_HwClockEnabled;
73  bool m_IsFilterCurrentlySet;
74 
75  PfRingDevice(const char* deviceName);
76 
77  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
78  void captureThreadMain(std::shared_ptr<StartupBlock> startupBlock);
79 
80  int openSingleRxChannel(const char* deviceName, pfring** ring);
81 
82  bool getIsHwClockEnable()
83  {
84  setPfRingDeviceAttributes();
85  return m_HwClockEnabled;
86  }
87  bool setPfRingDeviceClock(pfring* ring);
88 
89  void clearCoreConfiguration();
90  int getCoresInUseCount() const;
91 
92  void setPfRingDeviceAttributes();
93 
94  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
95 
96  public:
99  {
103  PerFlow
104  };
105 
108  struct PfRingStats
109  {
111  uint64_t recv;
113  uint64_t drop;
114  };
115 
118 
122  {
123  setPfRingDeviceAttributes();
124  return m_MacAddress;
125  }
126 
130  {
131  setPfRingDeviceAttributes();
132  return m_InterfaceIndex;
133  }
134 
137  int getMtu()
138  {
139  setPfRingDeviceAttributes();
140  return m_DeviceMTU;
141  }
142 
147  {
148  setPfRingDeviceAttributes();
149  return m_HwClockEnabled;
150  }
151 
154  std::string getDeviceName() const
155  {
156  return m_DeviceName;
157  }
158 
164  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
165 
174  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
175  CoreMask coreMask);
176 
178  void stopCapture();
179 
185  bool openSingleRxChannel(uint8_t channelId);
186 
193  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
194 
204  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
205 
211  uint8_t getNumOfOpenedRxChannels() const
212  {
213  return m_NumOfOpenedRxChannels;
214  }
215 
218  uint8_t getTotalNumOfRxChannels() const;
219 
223 
228  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
229 
234 
239  void getStatistics(PfRingStats& stats) const;
240 
243  bool isFilterCurrentlySet() const;
244 
252  bool sendPacket(const RawPacket& rawPacket);
253 
262  bool sendPacket(const uint8_t* packetData, int packetDataLength);
263 
271  bool sendPacket(const Packet& packet);
272 
280  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
281 
289  int sendPackets(const Packet** packetsArr, int arrLength);
290 
297  int sendPackets(const RawPacketVector& rawPackets);
298 
299  // implement abstract methods
300 
304  bool open();
305 
307  void close();
308 
310 
313  bool setFilter(std::string filterAsString);
314 
317  bool clearFilter();
318  };
319 
320 } // namespace pcpp
321 
322 // 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:22
Definition: PfRingDevice.h:34
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:99
@ RoundRobin
Packets are distributed between channels in a round-robin manner.
Definition: PfRingDevice.h:101
@ PerFlow
Packets are distributed between channels per flow (each flow goes for different channel)
Definition: PfRingDevice.h:103
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
void stopCapture()
Stops capturing packets (works will all type of startCapture*)
int getMtu()
Definition: PfRingDevice.h:137
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:146
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:211
std::string getDeviceName() const
Definition: PfRingDevice.h:154
int getInterfaceIndex()
Definition: PfRingDevice.h:129
bool isFilterCurrentlySet() const
SystemCore getCurrentCoreId() const
void getCurrentThreadStatistics(PfRingStats &stats) const
MacAddress getMacAddress()
Definition: PfRingDevice.h:121
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:259
The main namespace for the PcapPlusPlus lib.
Definition: PfRingDevice.h:109
uint64_t drop
Number of packets dropped.
Definition: PfRingDevice.h:113
uint64_t recv
Number of packets received.
Definition: PfRingDevice.h:111
Definition: SystemUtils.h:29