PcapPlusPlus  21.05
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 
277  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
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  {
305  HALF_DUPLEX
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 */
Definition: DpdkDevice.h:177
The main namespace for the PcapPlusPlus lib.
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:235
bool open()
Definition: DpdkDevice.h:733
Definition: DpdkDevice.h:195
int getAmountOfMbufsInUse() const
DpdkPMDType
Definition: DpdkDevice.h:77
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:347
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
Definition: DpdkDevice.h:91
std::string getDeviceName() const
Definition: DpdkDevice.h:367
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:241
uint64_t getSupportedRssHashFunctions() const
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:353
Definition: DpdkDevice.h:83
Definition: DpdkDevice.h:111
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: Packet.h:26
std::string getPciAddress() const
Definition: DpdkDevice.h:389
Definition: DpdkDevice.h:103
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:79
Definition: RawPacket.h:252
Definition: DpdkDevice.h:205
Definition: Device.h:24
bool isVirtual() const
Definition: DpdkDevice.h:95
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:349
void getLinkStatus(LinkStatus &linkStatus) const
Definition: MBufRawPacket.h:43
Definition: DpdkDevice.h:199
Definition: DpdkDevice.h:187
Definition: DpdkDevice.h:107
Definition: DpdkDevice.h:81
Definition: DpdkDevice.h:181
Definition: DpdkDevice.h:197
Definition: DpdkDevice.h:320
Definition: DpdkDevice.h:207
Definition: DpdkDevice.h:189
Definition: DpdkDevice.h:109
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
timespec timestamp
Definition: DpdkDevice.h:341
bool setFilter(GeneralFilter &filter)
int getDeviceId() const
Definition: DpdkDevice.h:363
uint64_t packetsPerSec
Definition: DpdkDevice.h:327
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
uint64_t bytes
Definition: DpdkDevice.h:325
MacAddress getMacAddress() const
Definition: DpdkDevice.h:372
Definition: DpdkDevice.h:164
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
Definition: DpdkDevice.h:185
uint8_t * rssKey
Definition: DpdkDevice.h:249
uint8_t devId
Definition: DpdkDevice.h:339
Definition: DpdkDevice.h:97
Definition: DpdkDevice.h:105
uint64_t packets
Definition: DpdkDevice.h:323
uint16_t getMtu() const
Definition: DpdkDevice.h:394
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:355
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
Definition: DpdkDevice.h:99
void clearStatistics()
Definition: DpdkDevice.h:179
Definition: DpdkDevice.h:211
Definition: DpdkDevice.h:101
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:384
Definition: DpdkDevice.h:215
void getStatistics(DpdkDeviceStats &stats) const
Definition: DpdkDevice.h:183
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:229
Definition: DpdkDevice.h:223
Definition: DpdkDevice.h:87
Definition: DpdkDevice.h:336
Definition: DpdkDevice.h:213
uint64_t bytesPerSec
Definition: DpdkDevice.h:329
Definition: DpdkDevice.h:85
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:427
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:432
Definition: DpdkDevice.h:201
Definition: DpdkDevice.h:93
Definition: DpdkDevice.h:115
Definition: DpdkDevice.h:203
Definition: DpdkDeviceList.h:71
uint32_t getCurrentCoreId() const
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
int getAmountOfFreeMbufs() const
uint64_t rxPacketsDropeedByHW
Definition: DpdkDevice.h:351
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
Definition: DpdkDevice.h:113
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:437
Definition: MacAddress.h:27
uint64_t rssHashFunction
Definition: DpdkDevice.h:262
uint8_t rssKeyLength
Definition: DpdkDevice.h:255
Definition: DpdkDevice.h:193
Definition: DpdkDevice.h:209
Definition: DpdkDevice.h:89
Definition: DpdkDevice.h:191
std::string getPMDName() const
Definition: DpdkDevice.h:378
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:127
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:422
DpdkRssHashFunction
Definition: DpdkDevice.h:174