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 
11 namespace pcpp
12 {
19  class XdpDevice : public IDevice
20  {
21  public:
28  typedef void (*OnPacketsArrive)(RawPacket packets[], uint32_t packetCount, XdpDevice* device, void* userCookie);
29 
33  {
37  {
40  SkbMode = 1,
44  AutoMode = 3
45  };
46 
49 
52  uint16_t umemNumFrames;
53 
57  uint16_t umemFrameSize;
58 
61  uint32_t fillRingSize;
62 
66 
69  uint32_t rxSize;
70 
73  uint32_t txSize;
74 
76  uint16_t rxTxBatchSize;
77 
90  uint16_t umemFrameSize = 0, uint32_t fillRingSize = 0,
91  uint32_t completionRingSize = 0, uint32_t rxSize = 0, uint32_t txSize = 0,
92  uint16_t rxTxBatchSize = 0)
93  {
94  this->attachMode = attachMode;
95  this->umemNumFrames = umemNumFrames;
96  this->umemFrameSize = umemFrameSize;
97  this->fillRingSize = fillRingSize;
98  this->completionRingSize = completionRingSize;
99  this->rxSize = rxSize;
100  this->txSize = txSize;
101  this->rxTxBatchSize = rxTxBatchSize;
102  }
103  };
104 
108  {
110  timespec timestamp;
112  uint64_t rxPackets;
114  uint64_t rxPacketsPerSec;
116  uint64_t rxBytes;
118  uint64_t rxBytesPerSec;
128  uint64_t rxPollTimeout;
130  uint64_t txSentPackets;
134  uint64_t txSentBytes;
144  uint64_t rxRingId;
146  uint64_t txRingId;
148  uint64_t fqRingId;
150  uint64_t cqRingId;
154  uint64_t umemFreeFrames;
155  };
156 
160  explicit XdpDevice(std::string interfaceName);
161 
163  ~XdpDevice() override;
164 
170  bool open() override;
171 
177  bool open(const XdpDeviceConfiguration& config);
178 
180  void close() override;
181 
182  bool isOpened() const override
183  {
184  return m_DeviceOpened;
185  }
186 
199  bool receivePackets(OnPacketsArrive onPacketsArrive, void* onPacketsArriveUserCookie, int timeoutMS = 5000);
200 
204 
214  bool sendPackets(const RawPacketVector& packets, bool waitForTxCompletion = false,
215  int waitForTxCompletionTimeoutMS = 5000);
216 
227  bool sendPackets(RawPacket packets[], size_t packetCount, bool waitForTxCompletion = false,
228  int waitForTxCompletionTimeoutMS = 5000);
229 
232  {
233  return m_Config;
234  }
235 
238 
239  private:
240  class XdpUmem
241  {
242  public:
243  explicit XdpUmem(uint16_t numFrames, uint16_t frameSize, uint32_t fillRingSize,
244  uint32_t completionRingSize);
245 
246  virtual ~XdpUmem();
247 
248  inline uint16_t getFrameSize() const
249  {
250  return m_FrameSize;
251  }
252  inline uint16_t getFrameCount() const
253  {
254  return m_FrameCount;
255  }
256 
257  std::pair<bool, std::vector<uint64_t>> allocateFrames(uint32_t count);
258 
259  void freeFrame(uint64_t addr);
260 
261  const uint8_t* getDataPtr(uint64_t addr) const;
262 
263  void setData(uint64_t addr, const uint8_t* data, size_t dataLen);
264 
265  inline size_t getFreeFrameCount()
266  {
267  return m_FreeFrames.size();
268  }
269 
270  inline void* getInfo()
271  {
272  return m_UmemInfo;
273  }
274 
275  private:
276  void* m_UmemInfo;
277  void* m_Buffer;
278  uint16_t m_FrameSize;
279  uint16_t m_FrameCount;
280  std::vector<uint64_t> m_FreeFrames;
281  };
282 
283  struct XdpPrevDeviceStats
284  {
285  timespec timestamp;
286  uint64_t rxPackets;
287  uint64_t rxBytes;
288  uint64_t txSentPackets;
289  uint64_t txSentBytes;
290  uint64_t txCompletedPackets;
291  };
292 
293  bool m_DeviceOpened = false;
294 
295  std::string m_InterfaceName;
296  XdpDeviceConfiguration* m_Config;
297  bool m_ReceivingPackets;
298  XdpUmem* m_Umem;
299  void* m_SocketInfo;
300  XdpDeviceStats m_Stats;
301  XdpPrevDeviceStats m_PrevStats;
302 
303  bool sendPackets(const std::function<RawPacket(uint32_t)>& getPacketAt,
304  const std::function<uint32_t()>& getPacketCount, bool waitForTxCompletion = false,
305  int waitForTxCompletionTimeoutMS = 5000);
306  bool populateFillRing(uint32_t count, uint32_t rxId = 0);
307  bool populateFillRing(const std::vector<uint64_t>& addresses, uint32_t rxId);
308  uint32_t checkCompletionRing();
309  bool configureSocket();
310  bool initUmem();
311  bool initConfig();
312  bool getSocketStats();
313  };
314 } // namespace pcpp
Definition: Device.h:20
Definition: PointerVector.h:50
Definition: RawPacket.h:259
Definition: XdpDevice.h:20
bool receivePackets(OnPacketsArrive onPacketsArrive, void *onPacketsArriveUserCookie, int timeoutMS=5000)
XdpDeviceStats getStatistics()
bool open(const XdpDeviceConfiguration &config)
bool open() override
void close() override
Close the device. This method closes the AF_XDP socket and frees the UMEM that was allocated for it.
bool isOpened() const override
Definition: XdpDevice.h:182
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:28
XdpDevice(std::string interfaceName)
XdpDeviceConfiguration * getConfig() const
Definition: XdpDevice.h:231
~XdpDevice() override
A d'tor for this class. It closes the device if it's open.
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
Definition: XdpDevice.h:33
uint16_t umemNumFrames
Definition: XdpDevice.h:52
uint32_t fillRingSize
Definition: XdpDevice.h:61
uint16_t umemFrameSize
Definition: XdpDevice.h:57
AttachMode
Definition: XdpDevice.h:37
@ DriverMode
Use this mode if the network driver has support for XDP.
Definition: XdpDevice.h:42
@ SkbMode
Definition: XdpDevice.h:40
@ AutoMode
Automatically detect whether driver mode is supported, otherwise fallback to SKB mode.
Definition: XdpDevice.h:44
uint32_t txSize
Definition: XdpDevice.h:73
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:89
uint32_t completionRingSize
Definition: XdpDevice.h:65
uint16_t rxTxBatchSize
The max number of packets to be received or sent in one batch.
Definition: XdpDevice.h:76
uint32_t rxSize
Definition: XdpDevice.h:69
AttachMode attachMode
AF_XDP operation mode.
Definition: XdpDevice.h:48
Definition: XdpDevice.h:108
uint64_t rxDroppedInvalidPackets
RX packets dropped due to invalid descriptor.
Definition: XdpDevice.h:122
timespec timestamp
The timestamp when the stats were collected.
Definition: XdpDevice.h:110
uint64_t rxPackets
Number of packets received.
Definition: XdpDevice.h:112
uint64_t txSentBytes
Number of bytes sent from the application.
Definition: XdpDevice.h:134
uint64_t txRingId
Current TX ring ID.
Definition: XdpDevice.h:146
uint64_t rxRingId
Current RX ring ID.
Definition: XdpDevice.h:144
uint64_t umemAllocatedFrames
Number of UMEM frames that are currently in-use (allocated)
Definition: XdpDevice.h:152
uint64_t txSentBytesPerSec
Bytes per second sent from the app. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:136
uint64_t txDroppedInvalidPackets
TX packets dropped due to invalid descriptor.
Definition: XdpDevice.h:142
uint64_t fqRingId
Current fill ring ID.
Definition: XdpDevice.h:148
uint64_t rxDroppedTotalPackets
Total number of dropped RX packets.
Definition: XdpDevice.h:120
uint64_t rxPacketsPerSec
Packets received per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:114
uint64_t rxDroppedRxRingFullPackets
RX packets dropped due to RX ring being full.
Definition: XdpDevice.h:124
uint64_t txCompletedPackets
Number of completed sent packets, meaning packets that were confirmed as sent by the kernel.
Definition: XdpDevice.h:138
uint64_t rxDroppedFillRingPackets
Failed RX packets to retrieve item from fill ring.
Definition: XdpDevice.h:126
uint64_t txCompletedPacketsPerSec
Completed sent packets per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:140
uint64_t rxBytesPerSec
Bytes per second received. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:118
uint64_t cqRingId
Current completion ring ID.
Definition: XdpDevice.h:150
uint64_t umemFreeFrames
Number of UMEM frames that are currently free (not allocated)
Definition: XdpDevice.h:154
uint64_t rxBytes
Number of bytes received.
Definition: XdpDevice.h:116
uint64_t txSentPackets
Number of packets sent from the application.
Definition: XdpDevice.h:130
uint64_t rxPollTimeout
Number of poll() timeouts.
Definition: XdpDevice.h:128
uint64_t txSentPacketsPerSec
Packets sent from the app per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:132