PcapPlusPlus  22.11
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 <vector>
7 #include "MacAddress.h"
8 #include "SystemUtils.h"
9 #include "Device.h"
10 #include "MBufRawPacket.h"
11 
57 struct rte_mbuf;
58 struct rte_mempool;
59 struct rte_eth_conf;
60 struct rte_eth_dev_tx_buffer;
61 
66 namespace pcpp
67 {
68 
69 #define DPDK_MAX_RX_QUEUES 16
70 #define DPDK_MAX_TX_QUEUES 16
71 #define PCPP_RSS_HASH_MAGIC_NUMBER 0x123456
72 
73  class DpdkDeviceList;
74  class DpdkDevice;
75 
80  {
119  };
120 
130  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
131 
167  class DpdkDevice : public IDevice
168  {
169  friend class DpdkDeviceList;
170  friend class MBufRawPacket;
171  public:
172 
178  {
180  RSS_NONE = 0,
182  RSS_IPV4 = 0x1,
194  RSS_IPV6 = 0x40,
206  RSS_L2_PAYLOAD = 0x1000,
208  RSS_IPV6_EX = 0x2000,
210  RSS_IPV6_TCP_EX = 0x4000,
212  RSS_IPV6_UDP_EX = 0x8000,
214  RSS_PORT = 0x10000,
216  RSS_VXLAN = 0x20000,
218  RSS_GENEVE = 0x40000,
220  RSS_NVGRE = 0x80000,
224  RSS_DEFAULT = PCPP_RSS_HASH_MAGIC_NUMBER
225  };
226 
233  {
239 
245 
251 
258  uint8_t* rssKey;
259 
264  uint8_t rssKeyLength;
265 
271  uint64_t rssHashFunction;
272 
287  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
288  uint16_t transmitDescriptorsNumber = 512,
289  uint16_t flushTxBufferTimeout = 100,
290  uint64_t rssHashFunction = RSS_DEFAULT,
291  uint8_t* rssKey = DpdkDevice::m_RSSKey,
292  uint8_t rssKeyLength = 40)
293  {
294  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
295  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
296  this->flushTxBufferTimeout = flushTxBufferTimeout;
297  this->rssKey = rssKey;
298  this->rssKeyLength = rssKeyLength;
299  this->rssHashFunction = rssHashFunction;
300  }
301  };
302 
307  struct LinkStatus
308  {
311  {
315  HALF_DUPLEX
316  };
317 
319  bool linkUp;
324  };
325 
330  struct RxTxStats
331  {
333  uint64_t packets;
335  uint64_t bytes;
337  uint64_t packetsPerSec;
339  uint64_t bytesPerSec;
340  };
341 
347  {
349  uint8_t devId;
351  timespec timestamp;
353  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
355  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
366  };
367 
368  virtual ~DpdkDevice();
369 
373  int getDeviceId() const { return m_Id; }
377  std::string getDeviceName() const { return m_DeviceName; }
378 
382  MacAddress getMacAddress() const { return m_MacAddress; }
383 
388  std::string getPMDName() const { return m_PMDName; }
389 
394  DpdkPMDType getPMDType() const { return m_PMDType; }
395 
399  std::string getPciAddress() const { return m_PciAddress; }
400 
404  uint16_t getMtu() const { return m_DeviceMtu; }
405 
411  bool setMtu(uint16_t newMtu);
412 
416  bool isVirtual() const;
417 
422  void getLinkStatus(LinkStatus& linkStatus) const;
423 
427  uint32_t getCurrentCoreId() const;
428 
432  uint16_t getNumOfOpenedRxQueues() const { return m_NumOfRxQueuesOpened; }
433 
437  uint16_t getNumOfOpenedTxQueues() const { return m_NumOfTxQueuesOpened; }
438 
442  uint16_t getTotalNumOfRxQueues() const { return m_TotalAvailableRxQueues; }
443 
447  uint16_t getTotalNumOfTxQueues() const { return m_TotalAvailableTxQueues; }
448 
449 
456  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
457 
470  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
471 
481  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
482 
503  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
504 
525  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
526 
543  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
544 
565  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
566 
581  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
582 
595  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
596 
612  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
613 
619  bool setFilter(GeneralFilter& filter);
620 
626  bool setFilter(std::string filterAsString);
627 
638  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
639 
655  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
656 
672  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
673 
678  void stopCapture();
679 
683  int getAmountOfFreeMbufs() const;
684 
688  int getAmountOfMbufsInUse() const;
689 
694  void getStatistics(DpdkDeviceStats& stats) const;
695 
699  void clearStatistics();
700 
714  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
715 
722 
728  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
729 
734  uint64_t getSupportedRssHashFunctions() const;
735 
739  uint64_t getConfiguredRssHashFunction() const;
740 
746  std::vector<std::string> rssHashFunctionMaskToString(uint64_t rssHFMask) const;
747 
748  //overridden methods
749 
756  bool open() { return openMultiQueues(1, 1); };
757 
761  void close();
762 
763  private:
764 
765  struct DpdkCoreConfiguration
766  {
767  int RxQueueId;
768  bool IsCoreInUse;
769 
770  void clear() { RxQueueId = -1; IsCoreInUse = false; }
771 
772  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
773  };
774 
775  DpdkDevice(int port, uint32_t mBufPoolSize);
776  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
777 
778  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
779  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
780  bool startDevice();
781 
782  static int dpdkCaptureThreadMain(void* ptr);
783 
784  void clearCoreConfiguration();
785  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
786  int getCoresInUseCount() const;
787 
788  void setDeviceInfo();
789 
790  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
791  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
792 
793  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
794  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
795 
796  std::string m_DeviceName;
797  DpdkPMDType m_PMDType;
798  std::string m_PMDName;
799  std::string m_PciAddress;
800 
801  DpdkDeviceConfiguration m_Config;
802 
803  int m_Id;
804  MacAddress m_MacAddress;
805  uint16_t m_DeviceMtu;
806  struct rte_mempool* m_MBufMempool;
807  struct rte_eth_dev_tx_buffer** m_TxBuffers;
808  uint64_t m_TxBufferDrainTsc;
809  uint64_t* m_TxBufferLastDrainTsc;
810  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
811  uint16_t m_TotalAvailableRxQueues;
812  uint16_t m_TotalAvailableTxQueues;
813  uint16_t m_NumOfRxQueuesOpened;
814  uint16_t m_NumOfTxQueuesOpened;
815  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
816  void* m_OnPacketsArriveUserCookie;
817  bool m_StopThread;
818 
819  bool m_WasOpened;
820 
821  // RSS key used by the NIC for load balancing the packets between cores
822  static uint8_t m_RSSKey[40];
823 
824  mutable DpdkDeviceStats m_PrevStats;
825  };
826 
827 } // namespace pcpp
828 
829 #endif /* PCAPPP_DPDK_DEVICE */
Definition: DpdkDevice.h:182
The main namespace for the PcapPlusPlus lib.
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:244
bool open()
Definition: DpdkDevice.h:756
Definition: DpdkDevice.h:200
int getAmountOfMbufsInUse() const
DpdkPMDType
Definition: DpdkDevice.h:79
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:357
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
Definition: DpdkDevice.h:180
Definition: DpdkDevice.h:94
Definition: DpdkDevice.h:224
uint64_t getConfiguredRssHashFunction() const
std::string getDeviceName() const
Definition: DpdkDevice.h:377
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:250
uint64_t getSupportedRssHashFunctions() const
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:363
Definition: DpdkDevice.h:86
Definition: DpdkDevice.h:114
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: Packet.h:26
std::string getPciAddress() const
Definition: DpdkDevice.h:399
Definition: DpdkDevice.h:106
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:82
Definition: RawPacket.h:252
Definition: DpdkDevice.h:210
Definition: Device.h:24
bool isVirtual() const
Definition: DpdkDevice.h:98
uint64_t rxPacketsDroppedByHW
Definition: DpdkDevice.h:361
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:359
void getLinkStatus(LinkStatus &linkStatus) const
Definition: MBufRawPacket.h:43
Definition: DpdkDevice.h:204
Definition: DpdkDevice.h:192
Definition: DpdkDevice.h:110
Definition: DpdkDevice.h:84
Definition: DpdkDevice.h:186
Definition: DpdkDevice.h:202
Definition: DpdkDevice.h:330
Definition: DpdkDevice.h:212
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:287
Definition: DpdkDevice.h:194
Definition: DpdkDevice.h:112
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
timespec timestamp
Definition: DpdkDevice.h:351
bool setFilter(GeneralFilter &filter)
int getDeviceId() const
Definition: DpdkDevice.h:373
uint64_t packetsPerSec
Definition: DpdkDevice.h:337
uint64_t bytes
Definition: DpdkDevice.h:335
MacAddress getMacAddress() const
Definition: DpdkDevice.h:382
Definition: DpdkDevice.h:167
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
Definition: DpdkDevice.h:190
uint8_t * rssKey
Definition: DpdkDevice.h:258
uint8_t devId
Definition: DpdkDevice.h:349
Definition: DpdkDevice.h:100
Definition: DpdkDevice.h:108
uint64_t packets
Definition: DpdkDevice.h:333
uint16_t getMtu() const
Definition: DpdkDevice.h:404
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:365
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
Definition: DpdkDevice.h:102
void clearStatistics()
Definition: DpdkDevice.h:184
Definition: DpdkDevice.h:216
Definition: DpdkDevice.h:104
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:394
Definition: DpdkDevice.h:220
void getStatistics(DpdkDeviceStats &stats) const
Definition: DpdkDevice.h:188
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:238
Definition: DpdkDevice.h:232
Definition: DpdkDevice.h:90
Definition: DpdkDevice.h:346
Definition: DpdkDevice.h:218
uint64_t bytesPerSec
Definition: DpdkDevice.h:339
Definition: DpdkDevice.h:88
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:437
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:442
Definition: DpdkDevice.h:206
Definition: DpdkDevice.h:96
Definition: DpdkDevice.h:118
Definition: DpdkDevice.h:208
Definition: DpdkDeviceList.h:72
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:116
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:447
std::vector< std::string > rssHashFunctionMaskToString(uint64_t rssHFMask) const
Definition: MacAddress.h:28
uint64_t rssHashFunction
Definition: DpdkDevice.h:271
uint8_t rssKeyLength
Definition: DpdkDevice.h:264
Definition: DpdkDevice.h:198
Definition: DpdkDevice.h:214
Definition: DpdkDevice.h:92
Definition: DpdkDevice.h:222
Definition: DpdkDevice.h:196
std::string getPMDName() const
Definition: DpdkDevice.h:388
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:130
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:432
DpdkRssHashFunction
Definition: DpdkDevice.h:177