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  std::vector<pfring*> m_PfRingDescriptors;
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);
82  void closeAllRxChannels();
83 
84  bool getIsHwClockEnable()
85  {
86  setPfRingDeviceAttributes();
87  return m_HwClockEnabled;
88  }
89  bool setPfRingDeviceClock(pfring* ring);
90 
91  void clearCoreConfiguration();
92  size_t getCoresInUseCount() const;
93 
94  void setPfRingDeviceAttributes();
95 
96  bool sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues);
97 
98  public:
101  {
105  PerFlow
106  };
107 
110  struct PfRingStats
111  {
113  uint64_t recv;
115  uint64_t drop;
116  };
117 
120 
124  {
125  setPfRingDeviceAttributes();
126  return m_MacAddress;
127  }
128 
132  {
133  setPfRingDeviceAttributes();
134  return m_InterfaceIndex;
135  }
136 
139  int getMtu()
140  {
141  setPfRingDeviceAttributes();
142  return m_DeviceMTU;
143  }
144 
149  {
150  setPfRingDeviceAttributes();
151  return m_HwClockEnabled;
152  }
153 
156  std::string getDeviceName() const
157  {
158  return m_DeviceName;
159  }
160 
166  bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
167 
176  bool startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
177  CoreMask coreMask);
178 
180  void stopCapture();
181 
187  bool openSingleRxChannel(uint8_t channelId);
188 
195  bool openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds);
196 
206  bool openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist);
207 
213  uint8_t getNumOfOpenedRxChannels() const
214  {
215  return static_cast<uint8_t>(m_PfRingDescriptors.size());
216  }
217 
220  uint8_t getTotalNumOfRxChannels() const;
221 
225 
230  void getThreadStatistics(SystemCore core, PfRingStats& stats) const;
231 
236 
241  void getStatistics(PfRingStats& stats) const;
242 
245  bool isFilterCurrentlySet() const;
246 
254  bool sendPacket(const RawPacket& rawPacket);
255 
264  bool sendPacket(const uint8_t* packetData, int packetDataLength);
265 
273  bool sendPacket(const Packet& packet);
274 
282  int sendPackets(const RawPacket* rawPacketsArr, int arrLength);
283 
291  int sendPackets(const Packet** packetsArr, int arrLength);
292 
299  int sendPackets(const RawPacketVector& rawPackets);
300 
301  // implement abstract methods
302 
306  bool open();
307 
309  void close();
310 
312 
315  bool setFilter(std::string filterAsString);
316 
319  bool clearFilter();
320  };
321 
322 } // namespace pcpp
323 
324 // 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: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:101
@ RoundRobin
Packets are distributed between channels in a round-robin manner.
Definition: PfRingDevice.h:103
@ PerFlow
Packets are distributed between channels per flow (each flow goes for different channel)
Definition: PfRingDevice.h:105
bool startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
void stopCapture()
Stops capturing packets (works will all type of startCapture*)
int getMtu()
Definition: PfRingDevice.h:139
bool isHwClockEnabledForDevice()
Definition: PfRingDevice.h:148
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:213
std::string getDeviceName() const
Definition: PfRingDevice.h:156
int getInterfaceIndex()
Definition: PfRingDevice.h:131
bool isFilterCurrentlySet() const
SystemCore getCurrentCoreId() const
void getCurrentThreadStatistics(PfRingStats &stats) const
MacAddress getMacAddress()
Definition: PfRingDevice.h:123
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:111
uint64_t drop
Number of packets dropped.
Definition: PfRingDevice.h:115
uint64_t recv
Number of packets received.
Definition: PfRingDevice.h:113
Definition: SystemUtils.h:29