PcapPlusPlus  Next
XdpDevice.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "Device.h"
6 #include <memory>
7 #include <utility>
8 #include <functional>
9 
12 namespace pcpp
13 {
20  class XdpDevice : public IDevice
21  {
22  public:
29  typedef void (*OnPacketsArrive)(RawPacket packets[], uint32_t packetCount, XdpDevice* device, void* userCookie);
30 
34  {
38  {
41  SkbMode = 1,
45  AutoMode = 3
46  };
47 
50 
53  uint16_t umemNumFrames;
54 
58  uint16_t umemFrameSize;
59 
62  uint32_t fillRingSize;
63 
67 
70  uint32_t rxSize;
71 
74  uint32_t txSize;
75 
77  uint16_t rxTxBatchSize;
78 
91  uint16_t umemFrameSize = 0, uint32_t fillRingSize = 0,
92  uint32_t completionRingSize = 0, uint32_t rxSize = 0, uint32_t txSize = 0,
93  uint16_t rxTxBatchSize = 0)
94  {
95  this->attachMode = attachMode;
96  this->umemNumFrames = umemNumFrames;
97  this->umemFrameSize = umemFrameSize;
98  this->fillRingSize = fillRingSize;
99  this->completionRingSize = completionRingSize;
100  this->rxSize = rxSize;
101  this->txSize = txSize;
102  this->rxTxBatchSize = rxTxBatchSize;
103  }
104  };
105 
109  {
111  timespec timestamp;
113  uint64_t rxPackets;
115  uint64_t rxPacketsPerSec;
117  uint64_t rxBytes;
119  uint64_t rxBytesPerSec;
129  uint64_t rxPollTimeout;
131  uint64_t txSentPackets;
135  uint64_t txSentBytes;
145  uint64_t rxRingId;
147  uint64_t txRingId;
149  uint64_t fqRingId;
151  uint64_t cqRingId;
155  uint64_t umemFreeFrames;
156  };
157 
161  explicit XdpDevice(std::string interfaceName);
162 
164  ~XdpDevice() override;
165 
171  bool open() override;
172 
178  bool open(const XdpDeviceConfiguration& config);
179 
181  void close() override;
182 
183  bool isOpened() const override
184  {
185  return m_DeviceOpened;
186  }
187 
200  bool receivePackets(OnPacketsArrive onPacketsArrive, void* onPacketsArriveUserCookie, int timeoutMS = 5000);
201 
205 
215  bool sendPackets(const RawPacketVector& packets, bool waitForTxCompletion = false,
216  int waitForTxCompletionTimeoutMS = 5000);
217 
228  bool sendPackets(RawPacket packets[], size_t packetCount, bool waitForTxCompletion = false,
229  int waitForTxCompletionTimeoutMS = 5000);
230 
233  {
234  // TODO: Return a copy or const ref to avoid user modifying config?
235  return m_Config.get();
236  }
237 
240 
241  private:
242  class XdpUmem
243  {
244  public:
245  explicit XdpUmem(uint16_t numFrames, uint16_t frameSize, uint32_t fillRingSize,
246  uint32_t completionRingSize);
247 
248  virtual ~XdpUmem();
249 
250  inline uint16_t getFrameSize() const
251  {
252  return m_FrameSize;
253  }
254  inline uint16_t getFrameCount() const
255  {
256  return m_FrameCount;
257  }
258 
259  std::pair<bool, std::vector<uint64_t>> allocateFrames(uint32_t count);
260 
261  void freeFrame(uint64_t addr);
262 
263  const uint8_t* getDataPtr(uint64_t addr) const;
264 
265  void setData(uint64_t addr, const uint8_t* data, size_t dataLen);
266 
267  inline size_t getFreeFrameCount()
268  {
269  return m_FreeFrames.size();
270  }
271 
272  inline void* getInfo()
273  {
274  return m_UmemInfo;
275  }
276 
277  private:
278  void* m_UmemInfo;
279  void* m_Buffer;
280  uint16_t m_FrameSize;
281  uint16_t m_FrameCount;
282  std::vector<uint64_t> m_FreeFrames;
283  };
284 
285  struct XdpPrevDeviceStats
286  {
287  timespec timestamp;
288  uint64_t rxPackets;
289  uint64_t rxBytes;
290  uint64_t txSentPackets;
291  uint64_t txSentBytes;
292  uint64_t txCompletedPackets;
293  };
294 
295  bool m_DeviceOpened = false;
296 
297  std::string m_InterfaceName;
298  std::unique_ptr<XdpDeviceConfiguration> m_Config;
299  bool m_ReceivingPackets;
300  XdpUmem* m_Umem;
301  void* m_SocketInfo;
302  XdpDeviceStats m_Stats;
303  XdpPrevDeviceStats m_PrevStats;
304 
305  bool sendPackets(const std::function<RawPacket(uint32_t)>& getPacketAt,
306  const std::function<uint32_t()>& getPacketCount, bool waitForTxCompletion = false,
307  int waitForTxCompletionTimeoutMS = 5000);
308  bool populateFillRing(uint32_t count, uint32_t rxId = 0);
309  bool populateFillRing(const std::vector<uint64_t>& addresses, uint32_t rxId);
310  uint32_t checkCompletionRing();
311  bool configureSocket();
312  bool initUmem();
313  bool populateConfigDefaults(XdpDeviceConfiguration& config) const;
314  bool getSocketStats();
315  };
316 } // namespace pcpp
Definition: Device.h:20
Definition: PointerVector.h:50
Definition: RawPacket.h:259
Definition: XdpDevice.h:21
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:183
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:29
XdpDevice(std::string interfaceName)
XdpDeviceConfiguration * getConfig() const
Definition: XdpDevice.h:232
~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:34
uint16_t umemNumFrames
Definition: XdpDevice.h:53
uint32_t fillRingSize
Definition: XdpDevice.h:62
uint16_t umemFrameSize
Definition: XdpDevice.h:58
AttachMode
Definition: XdpDevice.h:38
@ DriverMode
Use this mode if the network driver has support for XDP.
Definition: XdpDevice.h:43
@ SkbMode
Definition: XdpDevice.h:41
@ AutoMode
Automatically detect whether driver mode is supported, otherwise fallback to SKB mode.
Definition: XdpDevice.h:45
uint32_t txSize
Definition: XdpDevice.h:74
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:90
uint32_t completionRingSize
Definition: XdpDevice.h:66
uint16_t rxTxBatchSize
The max number of packets to be received or sent in one batch.
Definition: XdpDevice.h:77
uint32_t rxSize
Definition: XdpDevice.h:70
AttachMode attachMode
AF_XDP operation mode.
Definition: XdpDevice.h:49
Definition: XdpDevice.h:109
uint64_t rxDroppedInvalidPackets
RX packets dropped due to invalid descriptor.
Definition: XdpDevice.h:123
timespec timestamp
The timestamp when the stats were collected.
Definition: XdpDevice.h:111
uint64_t rxPackets
Number of packets received.
Definition: XdpDevice.h:113
uint64_t txSentBytes
Number of bytes sent from the application.
Definition: XdpDevice.h:135
uint64_t txRingId
Current TX ring ID.
Definition: XdpDevice.h:147
uint64_t rxRingId
Current RX ring ID.
Definition: XdpDevice.h:145
uint64_t umemAllocatedFrames
Number of UMEM frames that are currently in-use (allocated)
Definition: XdpDevice.h:153
uint64_t txSentBytesPerSec
Bytes per second sent from the app. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:137
uint64_t txDroppedInvalidPackets
TX packets dropped due to invalid descriptor.
Definition: XdpDevice.h:143
uint64_t fqRingId
Current fill ring ID.
Definition: XdpDevice.h:149
uint64_t rxDroppedTotalPackets
Total number of dropped RX packets.
Definition: XdpDevice.h:121
uint64_t rxPacketsPerSec
Packets received per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:115
uint64_t rxDroppedRxRingFullPackets
RX packets dropped due to RX ring being full.
Definition: XdpDevice.h:125
uint64_t txCompletedPackets
Number of completed sent packets, meaning packets that were confirmed as sent by the kernel.
Definition: XdpDevice.h:139
uint64_t rxDroppedFillRingPackets
Failed RX packets to retrieve item from fill ring.
Definition: XdpDevice.h:127
uint64_t txCompletedPacketsPerSec
Completed sent packets per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:141
uint64_t rxBytesPerSec
Bytes per second received. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:119
uint64_t cqRingId
Current completion ring ID.
Definition: XdpDevice.h:151
uint64_t umemFreeFrames
Number of UMEM frames that are currently free (not allocated)
Definition: XdpDevice.h:155
uint64_t rxBytes
Number of bytes received.
Definition: XdpDevice.h:117
uint64_t txSentPackets
Number of packets sent from the application.
Definition: XdpDevice.h:131
uint64_t rxPollTimeout
Number of poll() timeouts.
Definition: XdpDevice.h:129
uint64_t txSentPacketsPerSec
Packets sent from the app per second. Measured from to the previous time stats were collected.
Definition: XdpDevice.h:133