PcapPlusPlus  19.12
DpdkDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_DPDK_DEVICE
2 #define PCAPPP_DPDK_DEVICE
3 
4 #include <pthread.h>
5 #include <time.h>
6 #include "MacAddress.h"
7 #include "SystemUtils.h"
8 #include "Device.h"
9 #include "MBufRawPacket.h"
10 
56 struct rte_mbuf;
57 struct rte_mempool;
58 struct rte_eth_conf;
59 struct rte_eth_dev_tx_buffer;
60 
65 namespace pcpp
66 {
67 
68 #define DPDK_MAX_RX_QUEUES 16
69 #define DPDK_MAX_TX_QUEUES 16
70 
71  class DpdkDeviceList;
72  class DpdkDevice;
73 
77  enum DpdkPMDType {
116  };
117 
127  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
128 
164  class DpdkDevice : public IDevice
165  {
166  friend class DpdkDeviceList;
167  friend class MBufRawPacket;
168  public:
169 
175  {
177  RSS_IPV4 = 0x1,
189  RSS_IPV6 = 0x40,
201  RSS_L2_PAYLOAD = 0x1000,
203  RSS_IPV6_EX = 0x2000,
205  RSS_IPV6_TCP_EX = 0x4000,
207  RSS_IPV6_UDP_EX = 0x8000,
209  RSS_PORT = 0x10000,
211  RSS_VXLAN = 0x20000,
213  RSS_GENEVE = 0x40000,
215  RSS_NVGRE = 0x80000
216  };
217 
224  {
230 
236 
242 
249  uint8_t* rssKey;
250 
255  uint8_t rssKeyLength;
256 
262  uint64_t rssHashFunction;
263 
278  uint16_t transmitDescriptorsNumber = 512,
279  uint16_t flushTxBufferTimeout = 100,
280  uint64_t rssHashFunction = RSS_IPV4 | RSS_IPV6,
281  uint8_t* rssKey = DpdkDevice::m_RSSKey,
282  uint8_t rssKeyLength = 40)
283  {
284  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
285  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
286  this->flushTxBufferTimeout = flushTxBufferTimeout;
287  this->rssKey = rssKey;
288  this->rssKeyLength = rssKeyLength;
289  this->rssHashFunction = rssHashFunction;
290  }
291  };
292 
297  struct LinkStatus
298  {
301  {
306  };
307 
309  bool linkUp;
314  };
315 
320  struct RxTxStats
321  {
323  uint64_t packets;
325  uint64_t bytes;
327  uint64_t packetsPerSec;
329  uint64_t bytesPerSec;
330  };
331 
337  {
339  uint8_t devId;
341  timespec timestamp;
343  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
345  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
356  };
357 
358  virtual ~DpdkDevice();
359 
363  int getDeviceId() const { return m_Id; }
367  std::string getDeviceName() const { return std::string(m_DeviceName); }
368 
372  MacAddress getMacAddress() const { return m_MacAddress; }
373 
378  std::string getPMDName() const { return m_PMDName; }
379 
384  DpdkPMDType getPMDType() const { return m_PMDType; }
385 
389  std::string getPciAddress() const { return m_PciAddress; }
390 
394  uint16_t getMtu() const { return m_DeviceMtu; }
395 
401  bool setMtu(uint16_t newMtu);
402 
406  bool isVirtual() const;
407 
412  void getLinkStatus(LinkStatus& linkStatus) const;
413 
417  uint32_t getCurrentCoreId() const;
418 
422  uint16_t getNumOfOpenedRxQueues() const { return m_NumOfRxQueuesOpened; }
423 
427  uint16_t getNumOfOpenedTxQueues() const { return m_NumOfTxQueuesOpened; }
428 
432  uint16_t getTotalNumOfRxQueues() const { return m_TotalAvailableRxQueues; }
433 
437  uint16_t getTotalNumOfTxQueues() const { return m_TotalAvailableTxQueues; }
438 
439 
446  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
447 
460  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
461 
471  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
472 
493  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
494 
515  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
516 
533  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
534 
555  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
556 
571  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
572 
585  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
586 
602  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
603 
608  bool setFilter(GeneralFilter& filter);
609 
614  bool setFilter(std::string filterAsString);
615 
626  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
627 
643  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
644 
660  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
661 
666  void stopCapture();
667 
671  int getAmountOfFreeMbufs() const;
672 
676  int getAmountOfMbufsInUse() const;
677 
682  void getStatistics(DpdkDeviceStats& stats) const;
683 
687  void clearStatistics();
688 
702  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
703 
710 
716  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
717 
722  uint64_t getSupportedRssHashFunctions() const;
723 
724 
725  //overridden methods
726 
733  bool open() { return openMultiQueues(1, 1); };
734 
738  void close();
739 
740  private:
741 
742  struct DpdkCoreConfiguration
743  {
744  int RxQueueId;
745  bool IsCoreInUse;
746 
747  void clear() { RxQueueId = -1; IsCoreInUse = false; }
748 
749  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
750  };
751 
752  DpdkDevice(int port, uint32_t mBufPoolSize);
753  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
754 
755  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
756  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
757  bool startDevice();
758 
759  static int dpdkCaptureThreadMain(void* ptr);
760 
761  void clearCoreConfiguration();
762  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
763  int getCoresInUseCount() const;
764 
765  void setDeviceInfo();
766 
767  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
768  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
769 
770  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
771  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
772 
773  char m_DeviceName[30];
774  DpdkPMDType m_PMDType;
775  std::string m_PMDName;
776  std::string m_PciAddress;
777 
778  DpdkDeviceConfiguration m_Config;
779 
780  int m_Id;
781  MacAddress m_MacAddress;
782  uint16_t m_DeviceMtu;
783  struct rte_mempool* m_MBufMempool;
784  struct rte_eth_dev_tx_buffer** m_TxBuffers;
785  uint64_t m_TxBufferDrainTsc;
786  uint64_t* m_TxBufferLastDrainTsc;
787  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
788  uint16_t m_TotalAvailableRxQueues;
789  uint16_t m_TotalAvailableTxQueues;
790  uint16_t m_NumOfRxQueuesOpened;
791  uint16_t m_NumOfTxQueuesOpened;
792  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
793  void* m_OnPacketsArriveUserCookie;
794  bool m_StopThread;
795 
796  bool m_WasOpened;
797 
798  // RSS key used by the NIC for load balancing the packets between cores
799  static uint8_t m_RSSKey[40];
800 
801  mutable DpdkDeviceStats m_PrevStats;
802  };
803 
804 } // namespace pcpp
805 
806 #endif /* PCAPPP_DPDK_DEVICE */
pcpp::DpdkDevice::stopCapture
void stopCapture()
pcpp::DpdkDevice::DpdkDeviceStats::aggregatedRxStats
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:347
pcpp::Packet
Definition: Packet.h:26
pcpp::DpdkDevice::DpdkDeviceStats::rxPacketsDropeedByHW
uint64_t rxPacketsDropeedByHW
Definition: DpdkDevice.h:351
pcpp::DpdkDevice::DpdkDeviceConfiguration::transmitDescriptorsNumber
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:235
pcpp::DpdkDevice::isVirtual
bool isVirtual() const
pcpp::DpdkDevice::sendPacket
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
pcpp::PMD_VMXNET3
Definition: DpdkDevice.h:111
pcpp::DpdkDevice
Definition: DpdkDevice.h:164
pcpp::DpdkDevice::clearStatistics
void clearStatistics()
pcpp::DpdkDevice::DpdkRssHashFunction
DpdkRssHashFunction
Definition: DpdkDevice.h:174
pcpp::DpdkDevice::RSS_NONFRAG_IPV4_TCP
Definition: DpdkDevice.h:181
pcpp::PMD_FM10K
Definition: DpdkDevice.h:91
pcpp::PMD_UNKNOWN
Definition: DpdkDevice.h:79
pcpp::DpdkDevice::RSS_IPV6
Definition: DpdkDevice.h:189
SystemUtils.h
pcpp::DpdkDevice::close
void close()
pcpp::DpdkDevice::getTotalNumOfTxQueues
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:437
MacAddress.h
pcpp::PMD_E1000EM
Definition: DpdkDevice.h:83
pcpp::DpdkDevice::DpdkDeviceConfiguration::flushTxBufferTimeout
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:241
pcpp::DpdkDevice::setFilter
bool setFilter(GeneralFilter &filter)
pcpp::DpdkDevice::flushTxBuffer
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
pcpp::PMD_NULL
Definition: DpdkDevice.h:103
pcpp::DpdkDevice::DpdkDeviceConfiguration::receiveDescriptorsNumber
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:229
pcpp::PMD_I40EVF
Definition: DpdkDevice.h:95
pcpp::DpdkDeviceList
Definition: DpdkDeviceList.h:71
pcpp::DpdkDevice::open
bool open()
Definition: DpdkDevice.h:733
pcpp::DpdkDevice::RSS_NONFRAG_IPV4_SCTP
Definition: DpdkDevice.h:185
pcpp::PMD_RING
Definition: DpdkDevice.h:107
pcpp::DpdkDevice::getPciAddress
std::string getPciAddress() const
Definition: DpdkDevice.h:389
pcpp::PMD_BOND
Definition: DpdkDevice.h:81
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::DpdkDevice::DpdkDeviceStats::txStats
RxTxStats txStats[16]
Definition: DpdkDevice.h:345
pcpp::PMD_VIRTIO
Definition: DpdkDevice.h:109
pcpp::DpdkDevice::getSupportedRssHashFunctions
uint64_t getSupportedRssHashFunctions() const
pcpp::DpdkDevice::RSS_GENEVE
Definition: DpdkDevice.h:213
pcpp::DpdkDevice::startCaptureMultiThreads
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
pcpp::PMD_IXGBE
Definition: DpdkDevice.h:97
pcpp::DpdkDevice::DpdkDeviceConfiguration::rssKey
uint8_t * rssKey
Definition: DpdkDevice.h:249
pcpp::PMD_IXGBEVF
Definition: DpdkDevice.h:99
pcpp::DpdkDevice::RSS_NONFRAG_IPV4_OTHER
Definition: DpdkDevice.h:187
pcpp::DpdkDevice::RSS_NONFRAG_IPV6_SCTP
Definition: DpdkDevice.h:197
pcpp::DpdkDevice::DpdkDeviceConfiguration::rssKeyLength
uint8_t rssKeyLength
Definition: DpdkDevice.h:255
pcpp::DpdkDevice::getAmountOfMbufsInUse
int getAmountOfMbufsInUse() const
pcpp::DpdkDevice::getTotalNumOfRxQueues
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:432
pcpp::DpdkDevice::RSS_IPV6_UDP_EX
Definition: DpdkDevice.h:207
pcpp::DpdkDevice::RSS_IPV6_TCP_EX
Definition: DpdkDevice.h:205
pcpp::DpdkDevice::setMtu
bool setMtu(uint16_t newMtu)
pcpp::DpdkDevice::openMultiQueues
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
pcpp::DpdkDevice::RxTxStats::packets
uint64_t packets
Definition: DpdkDevice.h:323
pcpp::DpdkDevice::DpdkDeviceStats::rxStats
RxTxStats rxStats[16]
Definition: DpdkDevice.h:343
pcpp::DpdkDevice::getCurrentCoreId
uint32_t getCurrentCoreId() const
pcpp::DpdkDevice::RSS_IPV6_EX
Definition: DpdkDevice.h:203
pcpp::PMD_PCAP
Definition: DpdkDevice.h:105
pcpp::DpdkDevice::DpdkDeviceStats::rxErroneousPackets
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:353
Device.h
pcpp::DpdkDevice::RxTxStats
Definition: DpdkDevice.h:320
pcpp::DpdkDevice::RSS_NONFRAG_IPV6_OTHER
Definition: DpdkDevice.h:199
pcpp::DpdkDevice::DpdkDeviceConfiguration::rssHashFunction
uint64_t rssHashFunction
Definition: DpdkDevice.h:262
pcpp::DpdkDevice::getAmountOfFreeMbufs
int getAmountOfFreeMbufs() const
pcpp::DpdkDevice::getMacAddress
MacAddress getMacAddress() const
Definition: DpdkDevice.h:372
pcpp::DpdkDevice::DpdkDeviceStats::timestamp
timespec timestamp
Definition: DpdkDevice.h:341
pcpp::DpdkDevice::RxTxStats::bytes
uint64_t bytes
Definition: DpdkDevice.h:325
pcpp::DpdkDevice::RSS_L2_PAYLOAD
Definition: DpdkDevice.h:201
pcpp::PMD_IGBVF
Definition: DpdkDevice.h:87
pcpp::DpdkDevice::DpdkDeviceConfiguration
Definition: DpdkDevice.h:223
pcpp::DpdkDevice::DpdkDeviceStats::devId
uint8_t devId
Definition: DpdkDevice.h:339
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::PMD_MLX4
Definition: DpdkDevice.h:101
pcpp::DpdkDevice::DpdkDeviceStats::rxMbufAlocFailed
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:355
pcpp::PMD_IGB
Definition: DpdkDevice.h:85
pcpp::DpdkDevice::RSS_NONFRAG_IPV6_TCP
Definition: DpdkDevice.h:193
pcpp::DpdkDevice::getNumOfOpenedRxQueues
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:422
pcpp::DpdkDevice::getDeviceId
int getDeviceId() const
Definition: DpdkDevice.h:363
pcpp::DpdkDevice::DpdkDeviceStats
Definition: DpdkDevice.h:336
pcpp::PMD_AF_PACKET
Definition: DpdkDevice.h:115
pcpp::DpdkDevice::sendPackets
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
pcpp::DpdkDevice::RSS_VXLAN
Definition: DpdkDevice.h:211
pcpp::DpdkDevice::DpdkDeviceConfiguration::DpdkDeviceConfiguration
DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber=128, uint16_t transmitDescriptorsNumber=512, uint16_t flushTxBufferTimeout=100, uint64_t rssHashFunction=RSS_IPV4|RSS_IPV6, uint8_t *rssKey=DpdkDevice::m_RSSKey, uint8_t rssKeyLength=40)
Definition: DpdkDevice.h:277
pcpp::DpdkDevice::receivePackets
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
pcpp::DpdkDevice::RSS_PORT
Definition: DpdkDevice.h:209
pcpp::DpdkDevice::RSS_NONFRAG_IPV6_UDP
Definition: DpdkDevice.h:195
pcpp::DpdkDevice::getPMDName
std::string getPMDName() const
Definition: DpdkDevice.h:378
pcpp::PMD_I40E
Definition: DpdkDevice.h:93
pcpp::DpdkDevice::RSS_FRAG_IPV6
Definition: DpdkDevice.h:191
pcpp::DpdkDevice::getDeviceName
std::string getDeviceName() const
Definition: DpdkDevice.h:367
pcpp::DpdkDevice::RxTxStats::packetsPerSec
uint64_t packetsPerSec
Definition: DpdkDevice.h:327
pcpp::DpdkDevice::RSS_IPV4
Definition: DpdkDevice.h:177
pcpp::DpdkDevice::RSS_FRAG_IPV4
Definition: DpdkDevice.h:179
pcpp::DpdkDevice::RxTxStats::bytesPerSec
uint64_t bytesPerSec
Definition: DpdkDevice.h:329
pcpp::PMD_XENVIRT
Definition: DpdkDevice.h:113
pcpp::DpdkDevice::DpdkDeviceStats::aggregatedTxStats
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:349
pcpp::DpdkDevice::getLinkStatus
void getLinkStatus(LinkStatus &linkStatus) const
pcpp::DpdkDevice::getMtu
uint16_t getMtu() const
Definition: DpdkDevice.h:394
pcpp::DpdkDevice::isDeviceSupportRssHashFunction
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
pcpp::PointerVector
Definition: PointerVector.h:24
pcpp::PMD_ENIC
Definition: DpdkDevice.h:89
pcpp::DpdkDevice::getNumOfOpenedTxQueues
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:427
pcpp::DpdkDevice::getPMDType
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:384
pcpp::DpdkDevice::RSS_NVGRE
Definition: DpdkDevice.h:215
pcpp::DpdkDevice::RSS_NONFRAG_IPV4_UDP
Definition: DpdkDevice.h:183
pcpp::GeneralFilter
Definition: PcapFilter.h:78
pcpp::IDevice
Definition: Device.h:24
pcpp::DpdkDevice::startCaptureSingleThread
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
pcpp::DpdkDevice::getStatistics
void getStatistics(DpdkDeviceStats &stats) const
pcpp::MBufRawPacket
Definition: MBufRawPacket.h:43
pcpp::RawPacket
Definition: RawPacket.h:219
pcpp::DpdkPMDType
DpdkPMDType
Definition: DpdkDevice.h:77
pcpp::OnDpdkPacketsArriveCallback
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:127