PcapPlusPlus  Next
XdpDevice.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "Device.h"
6 #include <utility>
7 #include <functional>
8 
13 namespace pcpp
14 {
23  class XdpDevice : public IDevice
24  {
25  public:
34  typedef void (*OnPacketsArrive)(RawPacket packets[], uint32_t packetCount, XdpDevice* device, void* userCookie);
35 
41  {
47  {
50  SkbMode = 1,
54  AutoMode = 3
55  };
56 
59 
64  uint16_t umemNumFrames;
65 
71  uint16_t umemFrameSize;
72 
77  uint32_t fillRingSize;
78 
84 
89  uint32_t rxSize;
90 
95  uint32_t txSize;
96 
100  uint16_t rxTxBatchSize;
101 
116  uint16_t umemFrameSize = 0, uint32_t fillRingSize = 0,
117  uint32_t completionRingSize = 0, uint32_t rxSize = 0, uint32_t txSize = 0,
118  uint16_t rxTxBatchSize = 0)
119  {
120  this->attachMode = attachMode;
121  this->umemNumFrames = umemNumFrames;
122  this->umemFrameSize = umemFrameSize;
123  this->fillRingSize = fillRingSize;
124  this->completionRingSize = completionRingSize;
125  this->rxSize = rxSize;
126  this->txSize = txSize;
127  this->rxTxBatchSize = rxTxBatchSize;
128  }
129  };
130 
136  {
138  timespec timestamp;
140  uint64_t rxPackets;
142  uint64_t rxPacketsPerSec;
144  uint64_t rxBytes;
146  uint64_t rxBytesPerSec;
156  uint64_t rxPollTimeout;
158  uint64_t txSentPackets;
162  uint64_t txSentBytes;
172  uint64_t rxRingId;
174  uint64_t txRingId;
176  uint64_t fqRingId;
178  uint64_t cqRingId;
182  uint64_t umemFreeFrames;
183  };
184 
190  explicit XdpDevice(std::string interfaceName);
191 
195  ~XdpDevice() override;
196 
204  bool open() override;
205 
213  bool open(const XdpDeviceConfiguration& config);
214 
218  void close() override;
219 
233  bool receivePackets(OnPacketsArrive onPacketsArrive, void* onPacketsArriveUserCookie, int timeoutMS = 5000);
234 
240 
252  bool sendPackets(const RawPacketVector& packets, bool waitForTxCompletion = false,
253  int waitForTxCompletionTimeoutMS = 5000);
254 
267  bool sendPackets(RawPacket packets[], size_t packetCount, bool waitForTxCompletion = false,
268  int waitForTxCompletionTimeoutMS = 5000);
269 
274  {
275  return m_Config;
276  }
277 
282 
283  private:
284  class XdpUmem
285  {
286  public:
287  explicit XdpUmem(uint16_t numFrames, uint16_t frameSize, uint32_t fillRingSize,
288  uint32_t completionRingSize);
289 
290  virtual ~XdpUmem();
291 
292  inline uint16_t getFrameSize() const
293  {
294  return m_FrameSize;
295  }
296  inline uint16_t getFrameCount() const
297  {
298  return m_FrameCount;
299  }
300 
301  std::pair<bool, std::vector<uint64_t>> allocateFrames(uint32_t count);
302 
303  void freeFrame(uint64_t addr);
304 
305  const uint8_t* getDataPtr(uint64_t addr) const;
306 
307  void setData(uint64_t addr, const uint8_t* data, size_t dataLen);
308 
309  inline size_t getFreeFrameCount()
310  {
311  return m_FreeFrames.size();
312  }
313 
314  inline void* getInfo()
315  {
316  return m_UmemInfo;
317  }
318 
319  private:
320  void* m_UmemInfo;
321  void* m_Buffer;
322  uint16_t m_FrameSize;
323  uint16_t m_FrameCount;
324  std::vector<uint64_t> m_FreeFrames;
325  };
326 
327  struct XdpPrevDeviceStats
328  {
329  timespec timestamp;
330  uint64_t rxPackets;
331  uint64_t rxBytes;
332  uint64_t txSentPackets;
333  uint64_t txSentBytes;
334  uint64_t txCompletedPackets;
335  };
336 
337  std::string m_InterfaceName;
338  XdpDeviceConfiguration* m_Config;
339  bool m_ReceivingPackets;
340  XdpUmem* m_Umem;
341  void* m_SocketInfo;
342  XdpDeviceStats m_Stats;
343  XdpPrevDeviceStats m_PrevStats;
344 
345  bool sendPackets(const std::function<RawPacket(uint32_t)>& getPacketAt,
346  const std::function<uint32_t()>& getPacketCount, bool waitForTxCompletion = false,
347  int waitForTxCompletionTimeoutMS = 5000);
348  bool populateFillRing(uint32_t count, uint32_t rxId = 0);
349  bool populateFillRing(const std::vector<uint64_t>& addresses, uint32_t rxId);
350  uint32_t checkCompletionRing();
351  bool configureSocket();
352  bool initUmem();
353  bool initConfig();
354  bool getSocketStats();
355  };
356 } // namespace pcpp
Definition: Device.h:24
Definition: PointerVector.h:58
Definition: RawPacket.h:269
Definition: XdpDevice.h:24
bool receivePackets(OnPacketsArrive onPacketsArrive, void *onPacketsArriveUserCookie, int timeoutMS=5000)
XdpDeviceStats getStatistics()
bool open(const XdpDeviceConfiguration &config)
bool open() override
void close() override
bool sendPackets(const RawPacketVector &packets, bool waitForTxCompletion=false, int waitForTxCompletionTimeoutMS=5000)
void stopReceivePackets()
bool sendPackets(RawPacket packets[], size_t packetCount, bool waitForTxCompletion=false, int waitForTxCompletionTimeoutMS=5000)
void(* OnPacketsArrive)(RawPacket packets[], uint32_t packetCount, XdpDevice *device, void *userCookie)
Definition: XdpDevice.h:34
XdpDevice(std::string interfaceName)
XdpDeviceConfiguration * getConfig() const
Definition: XdpDevice.h:273
~XdpDevice() override
The main namespace for the PcapPlusPlus lib.
Definition: XdpDevice.h:41
uint16_t umemNumFrames
Definition: XdpDevice.h:64
uint32_t fillRingSize
Definition: XdpDevice.h:77
uint16_t umemFrameSize
Definition: XdpDevice.h:71
AttachMode
Definition: XdpDevice.h:47
@ DriverMode
Definition: XdpDevice.h:52
@ SkbMode
Definition: XdpDevice.h:50
@ AutoMode
Definition: XdpDevice.h:54
uint32_t txSize
Definition: XdpDevice.h:95
XdpDeviceConfiguration(AttachMode attachMode=AutoMode, uint16_t umemNumFrames=0, uint16_t umemFrameSize=0, uint32_t fillRingSize=0, uint32_t completionRingSize=0, uint32_t rxSize=0, uint32_t txSize=0, uint16_t rxTxBatchSize=0)
Definition: XdpDevice.h:115
uint32_t completionRingSize
Definition: XdpDevice.h:83
uint16_t rxTxBatchSize
Definition: XdpDevice.h:100
uint32_t rxSize
Definition: XdpDevice.h:89
AttachMode attachMode
Definition: XdpDevice.h:58
Definition: XdpDevice.h:136
uint64_t rxDroppedInvalidPackets
Definition: XdpDevice.h:150
timespec timestamp
Definition: XdpDevice.h:138
uint64_t rxPackets
Definition: XdpDevice.h:140
uint64_t txSentBytes
Definition: XdpDevice.h:162
uint64_t txRingId
Definition: XdpDevice.h:174
uint64_t rxRingId
Definition: XdpDevice.h:172
uint64_t umemAllocatedFrames
Definition: XdpDevice.h:180
uint64_t txSentBytesPerSec
Definition: XdpDevice.h:164
uint64_t txDroppedInvalidPackets
Definition: XdpDevice.h:170
uint64_t fqRingId
Definition: XdpDevice.h:176
uint64_t rxDroppedTotalPackets
Definition: XdpDevice.h:148
uint64_t rxPacketsPerSec
Definition: XdpDevice.h:142
uint64_t rxDroppedRxRingFullPackets
Definition: XdpDevice.h:152
uint64_t txCompletedPackets
Definition: XdpDevice.h:166
uint64_t rxDroppedFillRingPackets
Definition: XdpDevice.h:154
uint64_t txCompletedPacketsPerSec
Definition: XdpDevice.h:168
uint64_t rxBytesPerSec
Definition: XdpDevice.h:146
uint64_t cqRingId
Definition: XdpDevice.h:178
uint64_t umemFreeFrames
Definition: XdpDevice.h:182
uint64_t rxBytes
Definition: XdpDevice.h:144
uint64_t txSentPackets
Definition: XdpDevice.h:158
uint64_t rxPollTimeout
Definition: XdpDevice.h:156
uint64_t txSentPacketsPerSec
Definition: XdpDevice.h:160