PcapPlusPlus  23.09
DpdkDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_DPDK_DEVICE
2 #define PCAPPP_DPDK_DEVICE
3 
4 // GCOVR_EXCL_START
5 
6 #include <pthread.h>
7 #include <time.h>
8 #include <vector>
9 #include "MacAddress.h"
10 #include "SystemUtils.h"
11 #include "Device.h"
12 #include "MBufRawPacket.h"
13 
59 struct rte_mbuf;
60 struct rte_mempool;
61 struct rte_eth_conf;
62 struct rte_eth_dev_tx_buffer;
63 
68 namespace pcpp
69 {
70 
71 #define DPDK_MAX_RX_QUEUES 16
72 #define DPDK_MAX_TX_QUEUES 16
73 #define PCPP_RSS_HASH_MAGIC_NUMBER 0x123456
74 
75  class DpdkDeviceList;
76  class DpdkDevice;
77 
82  {
121  };
122 
132  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
133 
169  class DpdkDevice : public IDevice
170  {
171  friend class DpdkDeviceList;
172  friend class MBufRawPacket;
173  public:
174 
180  {
182  RSS_NONE = 0,
184  RSS_IPV4 = 0x1,
196  RSS_IPV6 = 0x40,
208  RSS_L2_PAYLOAD = 0x1000,
210  RSS_IPV6_EX = 0x2000,
212  RSS_IPV6_TCP_EX = 0x4000,
214  RSS_IPV6_UDP_EX = 0x8000,
216  RSS_PORT = 0x10000,
218  RSS_VXLAN = 0x20000,
220  RSS_GENEVE = 0x40000,
222  RSS_NVGRE = 0x80000,
226  RSS_DEFAULT = PCPP_RSS_HASH_MAGIC_NUMBER
227  };
228 
235  {
241 
247 
253 
260  uint8_t* rssKey;
261 
266  uint8_t rssKeyLength;
267 
273  uint64_t rssHashFunction;
274 
289  explicit DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
290  uint16_t transmitDescriptorsNumber = 512,
291  uint16_t flushTxBufferTimeout = 100,
292  uint64_t rssHashFunction = RSS_DEFAULT,
293  uint8_t* rssKey = DpdkDevice::m_RSSKey,
294  uint8_t rssKeyLength = 40)
295  {
296  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
297  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
298  this->flushTxBufferTimeout = flushTxBufferTimeout;
299  this->rssKey = rssKey;
300  this->rssKeyLength = rssKeyLength;
301  this->rssHashFunction = rssHashFunction;
302  }
303  };
304 
309  struct LinkStatus
310  {
313  {
317  HALF_DUPLEX
318  };
319 
321  bool linkUp;
326  };
327 
332  struct RxTxStats
333  {
335  uint64_t packets;
337  uint64_t bytes;
339  uint64_t packetsPerSec;
341  uint64_t bytesPerSec;
342  };
343 
349  {
351  uint8_t devId;
353  timespec timestamp;
355  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
357  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
368  };
369 
370  virtual ~DpdkDevice();
371 
375  int getDeviceId() const { return m_Id; }
379  std::string getDeviceName() const { return m_DeviceName; }
380 
384  MacAddress getMacAddress() const { return m_MacAddress; }
385 
390  std::string getPMDName() const { return m_PMDName; }
391 
396  DpdkPMDType getPMDType() const { return m_PMDType; }
397 
401  std::string getPciAddress() const { return m_PciAddress; }
402 
406  uint16_t getMtu() const { return m_DeviceMtu; }
407 
413  bool setMtu(uint16_t newMtu);
414 
418  bool isVirtual() const;
419 
424  void getLinkStatus(LinkStatus& linkStatus) const;
425 
429  uint32_t getCurrentCoreId() const;
430 
434  uint16_t getNumOfOpenedRxQueues() const { return m_NumOfRxQueuesOpened; }
435 
439  uint16_t getNumOfOpenedTxQueues() const { return m_NumOfTxQueuesOpened; }
440 
444  uint16_t getTotalNumOfRxQueues() const { return m_TotalAvailableRxQueues; }
445 
449  uint16_t getTotalNumOfTxQueues() const { return m_TotalAvailableTxQueues; }
450 
451 
458  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
459 
472  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
473 
483  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
484 
505  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
506 
527  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
528 
545  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
546 
567  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
568 
583  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
584 
597  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
598 
614  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
615 
621  bool setFilter(GeneralFilter& filter);
622 
628  bool setFilter(std::string filterAsString);
629 
640  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
641 
657  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
658 
674  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
675 
680  void stopCapture();
681 
685  int getAmountOfFreeMbufs() const;
686 
690  int getAmountOfMbufsInUse() const;
691 
696  void getStatistics(DpdkDeviceStats& stats) const;
697 
701  void clearStatistics();
702 
716  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
717 
724 
730  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
731 
736  uint64_t getSupportedRssHashFunctions() const;
737 
741  uint64_t getConfiguredRssHashFunction() const;
742 
748  std::vector<std::string> rssHashFunctionMaskToString(uint64_t rssHFMask) const;
749 
750  //overridden methods
751 
758  bool open() { return openMultiQueues(1, 1); };
759 
763  void close();
764 
765  private:
766 
767  struct DpdkCoreConfiguration
768  {
769  int RxQueueId;
770  bool IsCoreInUse;
771 
772  void clear() { RxQueueId = -1; IsCoreInUse = false; }
773 
774  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
775  };
776 
777  DpdkDevice(int port, uint32_t mBufPoolSize);
778  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
779 
780  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
781  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
782  bool startDevice();
783 
784  static int dpdkCaptureThreadMain(void* ptr);
785 
786  void clearCoreConfiguration();
787  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
788  int getCoresInUseCount() const;
789 
790  void setDeviceInfo();
791 
792  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
793  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
794 
795  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
796  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
797 
798  std::string m_DeviceName;
799  DpdkPMDType m_PMDType;
800  std::string m_PMDName;
801  std::string m_PciAddress;
802 
803  DpdkDeviceConfiguration m_Config;
804 
805  int m_Id;
806  MacAddress m_MacAddress;
807  uint16_t m_DeviceMtu;
808  struct rte_mempool* m_MBufMempool;
809  struct rte_eth_dev_tx_buffer** m_TxBuffers;
810  uint64_t m_TxBufferDrainTsc;
811  uint64_t* m_TxBufferLastDrainTsc;
812  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
813  uint16_t m_TotalAvailableRxQueues;
814  uint16_t m_TotalAvailableTxQueues;
815  uint16_t m_NumOfRxQueuesOpened;
816  uint16_t m_NumOfTxQueuesOpened;
817  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
818  void* m_OnPacketsArriveUserCookie;
819  bool m_StopThread;
820 
821  bool m_WasOpened;
822 
823  // RSS key used by the NIC for load balancing the packets between cores
824  static uint8_t m_RSSKey[40];
825 
826  mutable DpdkDeviceStats m_PrevStats;
827  };
828 
829 } // namespace pcpp
830 
831 // GCOVR_EXCL_STOP
832 
833 #endif /* PCAPPP_DPDK_DEVICE */
Definition: DpdkDevice.h:184
The main namespace for the PcapPlusPlus lib.
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:246
bool open()
Definition: DpdkDevice.h:758
Definition: DpdkDevice.h:202
int getAmountOfMbufsInUse() const
DpdkPMDType
Definition: DpdkDevice.h:81
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:359
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
Definition: DpdkDevice.h:182
Definition: DpdkDevice.h:96
Definition: DpdkDevice.h:226
uint64_t getConfiguredRssHashFunction() const
std::string getDeviceName() const
Definition: DpdkDevice.h:379
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:252
uint64_t getSupportedRssHashFunctions() const
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:365
Definition: DpdkDevice.h:88
Definition: DpdkDevice.h:116
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: Packet.h:26
std::string getPciAddress() const
Definition: DpdkDevice.h:401
Definition: DpdkDevice.h:108
Definition: PointerVector.h:24
Definition: PcapFilter.h:136
bool setMtu(uint16_t newMtu)
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
Definition: DpdkDevice.h:84
Definition: RawPacket.h:254
Definition: DpdkDevice.h:212
Definition: Device.h:24
bool isVirtual() const
Definition: DpdkDevice.h:100
uint64_t rxPacketsDroppedByHW
Definition: DpdkDevice.h:363
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:361
void getLinkStatus(LinkStatus &linkStatus) const
Definition: MBufRawPacket.h:45
Definition: DpdkDevice.h:206
Definition: DpdkDevice.h:194
Definition: DpdkDevice.h:112
Definition: DpdkDevice.h:86
Definition: DpdkDevice.h:188
Definition: DpdkDevice.h:204
Definition: DpdkDevice.h:332
Definition: DpdkDevice.h:214
DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber=128, uint16_t transmitDescriptorsNumber=512, uint16_t flushTxBufferTimeout=100, uint64_t rssHashFunction=RSS_DEFAULT, uint8_t *rssKey=DpdkDevice::m_RSSKey, uint8_t rssKeyLength=40)
Definition: DpdkDevice.h:289
Definition: DpdkDevice.h:196
Definition: DpdkDevice.h:114
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
timespec timestamp
Definition: DpdkDevice.h:353
bool setFilter(GeneralFilter &filter)
int getDeviceId() const
Definition: DpdkDevice.h:375
uint64_t packetsPerSec
Definition: DpdkDevice.h:339
uint64_t bytes
Definition: DpdkDevice.h:337
MacAddress getMacAddress() const
Definition: DpdkDevice.h:384
Definition: DpdkDevice.h:169
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
Definition: DpdkDevice.h:192
uint8_t * rssKey
Definition: DpdkDevice.h:260
uint8_t devId
Definition: DpdkDevice.h:351
Definition: DpdkDevice.h:102
Definition: DpdkDevice.h:110
uint64_t packets
Definition: DpdkDevice.h:335
uint16_t getMtu() const
Definition: DpdkDevice.h:406
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:367
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
Definition: DpdkDevice.h:104
void clearStatistics()
Definition: DpdkDevice.h:186
Definition: DpdkDevice.h:218
Definition: DpdkDevice.h:106
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:396
Definition: DpdkDevice.h:222
void getStatistics(DpdkDeviceStats &stats) const
Definition: DpdkDevice.h:190
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:240
Definition: DpdkDevice.h:234
Definition: DpdkDevice.h:92
Definition: DpdkDevice.h:348
Definition: DpdkDevice.h:220
uint64_t bytesPerSec
Definition: DpdkDevice.h:341
Definition: DpdkDevice.h:90
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:439
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:444
Definition: DpdkDevice.h:208
Definition: DpdkDevice.h:98
Definition: DpdkDevice.h:120
Definition: DpdkDevice.h:210
Definition: DpdkDeviceList.h:73
uint32_t getCurrentCoreId() const
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
int getAmountOfFreeMbufs() const
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
Definition: DpdkDevice.h:118
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:449
std::vector< std::string > rssHashFunctionMaskToString(uint64_t rssHFMask) const
Definition: MacAddress.h:28
uint64_t rssHashFunction
Definition: DpdkDevice.h:273
uint8_t rssKeyLength
Definition: DpdkDevice.h:266
Definition: DpdkDevice.h:200
Definition: DpdkDevice.h:216
Definition: DpdkDevice.h:94
Definition: DpdkDevice.h:224
Definition: DpdkDevice.h:198
std::string getPMDName() const
Definition: DpdkDevice.h:390
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:132
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:434
DpdkRssHashFunction
Definition: DpdkDevice.h:179