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 <vector>
11 #include <thread>
12 #include <mutex>
13 #include <atomic>
14 #include <condition_variable>
15 
17 
18 // forward declaration of PF_RING structs
19 struct __pfring;
20 typedef struct __pfring pfring;
21 
24 namespace pcpp
25 {
26 
27  class PfRingDevice;
28 
29  typedef void (*OnPfRingPacketsArriveCallback)(RawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
30  PfRingDevice* device, void* userCookie);
31 
34  class PfRingDevice : public IDevice, public IFilterableDevice
35  {
36  friend class PfRingDeviceList;
37 
38  private:
39  struct CoreConfiguration
40  {
41  std::thread RxThread;
42  pfring* Channel;
43  bool IsInUse;
44  bool IsAffinitySet;
45 
46  CoreConfiguration();
47  void clear();
48  };
49 
50  class StartupBlock
51  {
52  public:
53  void notifyStartup();
54  void waitForStartup();
55 
56  private:
57  std::mutex m_Mutex;
58  std::condition_variable m_Cv;
59  bool m_Ready = false;
60  };
61 
62  bool m_DeviceOpened = false;
63 
64  std::vector<pfring*> m_PfRingDescriptors;
65  std::string m_DeviceName;
66  int m_InterfaceIndex;
67  MacAddress m_MacAddress;
68  int m_DeviceMTU;
69  std::array<CoreConfiguration, MAX_NUM_OF_CORES> m_CoreConfiguration;
70  std::atomic<bool> m_StopThread;
71  OnPfRingPacketsArriveCallback m_OnPacketsArriveCallback;
72  void* m_OnPacketsArriveUserCookie;
73  bool m_ReentrantMode;
74  bool m_HwClockEnabled;
75  bool m_IsFilterCurrentlySet;
76 
77  PfRingDevice(const char* deviceName);
78 
79  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
80  void captureThreadMain(std::shared_ptr<StartupBlock> startupBlock);
81 
82  int openSingleRxChannel(const char* deviceName, pfring*& ring);
84  void closeAllRxChannels();
85 
86  bool getIsHwClockEnable()
87  {
88  setPfRingDeviceAttributes();
89  return m_HwClockEnabled;
90  }
91  bool setPfRingDeviceClock(pfring* ring);
92 
93  void clearCoreConfiguration();
94  size_t getCoresInUseCount() const;
95 
96  void setPfRingDeviceAttributes();
97 
98  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
99 
100  public:
103  {
107  PerFlow
108  };
109 
112  struct PfRingStats
113  {
115  uint64_t recv;
117  uint64_t drop;
118  };
119 
122 
126  {
127  setPfRingDeviceAttributes();
128  return m_MacAddress;
129  }
130 
134  {
135  setPfRingDeviceAttributes();
136  return m_InterfaceIndex;
137  }
138 
141  int getMtu()
142  {
143  setPfRingDeviceAttributes();
144  return m_DeviceMTU;
145  }
146 
151  {
152  setPfRingDeviceAttributes();
153  return m_HwClockEnabled;
154  }
155 
158  std::string getDeviceName() const
159  {
160  return m_DeviceName;
161  }
162 
168  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
169 
178  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
179  CoreMask coreMask);
180 
182  void stopCapture();
183 
189  bool openSingleRxChannel(uint8_t channelId);
190 
197  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
198 
208  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
209 
215  uint8_t getNumOfOpenedRxChannels() const
216  {
217  return static_cast<uint8_t>(m_PfRingDescriptors.size());
218  }
219 
222  uint8_t getTotalNumOfRxChannels() const;
223 
227 
232  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
233 
238 
243  void getStatistics(PfRingStats& stats) const;
244 
247  bool isFilterCurrentlySet() const;
248 
256  bool sendPacket(const RawPacket& rawPacket);
257 
266  bool sendPacket(const uint8_t* packetData, int packetDataLength);
267 
275  bool sendPacket(const Packet& packet);
276 
284  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
285 
293  int sendPackets(const Packet** packetsArr, int arrLength);
294 
301  int sendPackets(const RawPacketVector& rawPackets);
302 
303  // implement abstract methods
304 
308  bool open();
309 
311  void close();
312 
313  bool isOpened() const override
314  {
315  return m_DeviceOpened;
316  }
317 
319 
322  bool setFilter(std::string filterAsString);
323 
326  bool clearFilter();
327  };
328 
329 } // namespace pcpp
330 
331 // GCOVR_EXCL_STOP
Definition: Device.h:20
Definition: Device.h:44
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:56
Definition: MacAddress.h:24
Definition: Packet.h:22
Definition: PfRingDevice.h:35
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:103
@ RoundRobin
Packets are distributed between channels in a round-robin manner.
Definition: PfRingDevice.h:105
@ PerFlow
Packets are distributed between channels per flow (each flow goes for different channel)
Definition: PfRingDevice.h:107
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
void stopCapture()
Stops capturing packets (works will all type of startCapture*)
int getMtu()
Definition: PfRingDevice.h:141
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:150
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:215
std::string getDeviceName() const
Definition: PfRingDevice.h:158
int getInterfaceIndex()
Definition: PfRingDevice.h:133
bool isFilterCurrentlySet() const
SystemCore getCurrentCoreId() const
void getCurrentThreadStatistics(PfRingStats &stats) const
MacAddress getMacAddress()
Definition: PfRingDevice.h:125
bool sendPacket(const uint8_t *packetData, int packetDataLength)
bool isOpened() const override
Definition: PfRingDevice.h:313
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:18
Definition: PointerVector.h:50
Definition: RawPacket.h:259
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
Definition: PfRingDevice.h:113
uint64_t drop
Number of packets dropped.
Definition: PfRingDevice.h:117
uint64_t recv
Number of packets received.
Definition: PfRingDevice.h:115
Definition: SystemUtils.h:30