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 
194  bool receivePackets(OnPacketsArrive onPacketsArrive, void* onPacketsArriveUserCookie, int timeoutMS = 5000);
195 
199 
209  bool sendPackets(const RawPacketVector& packets, bool waitForTxCompletion = false,
210  int waitForTxCompletionTimeoutMS = 5000);
211 
222  bool sendPackets(RawPacket packets[], size_t packetCount, bool waitForTxCompletion = false,
223  int waitForTxCompletionTimeoutMS = 5000);
224 
227  {
228  return m_Config;
229  }
230 
233 
234  private:
235  class XdpUmem
236  {
237  public:
238  explicit XdpUmem(uint16_t numFrames, uint16_t frameSize, uint32_t fillRingSize,
239  uint32_t completionRingSize);
240 
241  virtual ~XdpUmem();
242 
243  inline uint16_t getFrameSize() const
244  {
245  return m_FrameSize;
246  }
247  inline uint16_t getFrameCount() const
248  {
249  return m_FrameCount;
250  }
251 
252  std::pair<bool, std::vector<uint64_t>> allocateFrames(uint32_t count);
253 
254  void freeFrame(uint64_t addr);
255 
256  const uint8_t* getDataPtr(uint64_t addr) const;
257 
258  void setData(uint64_t addr, const uint8_t* data, size_t dataLen);
259 
260  inline size_t getFreeFrameCount()
261  {
262  return m_FreeFrames.size();
263  }
264 
265  inline void* getInfo()
266  {
267  return m_UmemInfo;
268  }
269 
270  private:
271  void* m_UmemInfo;
272  void* m_Buffer;
273  uint16_t m_FrameSize;
274  uint16_t m_FrameCount;
275  std::vector<uint64_t> m_FreeFrames;
276  };
277 
278  struct XdpPrevDeviceStats
279  {
280  timespec timestamp;
281  uint64_t rxPackets;
282  uint64_t rxBytes;
283  uint64_t txSentPackets;
284  uint64_t txSentBytes;
285  uint64_t txCompletedPackets;
286  };
287 
288  std::string m_InterfaceName;
289  XdpDeviceConfiguration* m_Config;
290  bool m_ReceivingPackets;
291  XdpUmem* m_Umem;
292  void* m_SocketInfo;
293  XdpDeviceStats m_Stats;
294  XdpPrevDeviceStats m_PrevStats;
295 
296  bool sendPackets(const std::function<RawPacket(uint32_t)>& getPacketAt,
297  const std::function<uint32_t()>& getPacketCount, bool waitForTxCompletion = false,
298  int waitForTxCompletionTimeoutMS = 5000);
299  bool populateFillRing(uint32_t count, uint32_t rxId = 0);
300  bool populateFillRing(const std::vector<uint64_t>& addresses, uint32_t rxId);
301  uint32_t checkCompletionRing();
302  bool configureSocket();
303  bool initUmem();
304  bool initConfig();
305  bool getSocketStats();
306  };
307 } // namespace pcpp
Definition: Device.h:20
Definition: PointerVector.h:50
Definition: RawPacket.h:269
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 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:226
~XdpDevice() override
A d'tor for this class. It closes the device if it's open.
The main namespace for the PcapPlusPlus lib.
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