PcapPlusPlus  21.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 "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 
78  {
117  };
118 
128  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
129 
165  class DpdkDevice : public IDevice
166  {
167  friend class DpdkDeviceList;
168  friend class MBufRawPacket;
169  public:
170 
176  {
178  RSS_IPV4 = 0x1,
190  RSS_IPV6 = 0x40,
202  RSS_L2_PAYLOAD = 0x1000,
204  RSS_IPV6_EX = 0x2000,
206  RSS_IPV6_TCP_EX = 0x4000,
208  RSS_IPV6_UDP_EX = 0x8000,
210  RSS_PORT = 0x10000,
212  RSS_VXLAN = 0x20000,
214  RSS_GENEVE = 0x40000,
216  RSS_NVGRE = 0x80000
217  };
218 
225  {
231 
237 
243 
250  uint8_t* rssKey;
251 
256  uint8_t rssKeyLength;
257 
263  uint64_t rssHashFunction;
264 
278  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
279  uint16_t transmitDescriptorsNumber = 512,
280  uint16_t flushTxBufferTimeout = 100,
281  uint64_t rssHashFunction = RSS_IPV4 | RSS_IPV6,
282  uint8_t* rssKey = DpdkDevice::m_RSSKey,
283  uint8_t rssKeyLength = 40)
284  {
285  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
286  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
287  this->flushTxBufferTimeout = flushTxBufferTimeout;
288  this->rssKey = rssKey;
289  this->rssKeyLength = rssKeyLength;
290  this->rssHashFunction = rssHashFunction;
291  }
292  };
293 
298  struct LinkStatus
299  {
302  {
306  HALF_DUPLEX
307  };
308 
310  bool linkUp;
315  };
316 
321  struct RxTxStats
322  {
324  uint64_t packets;
326  uint64_t bytes;
328  uint64_t packetsPerSec;
330  uint64_t bytesPerSec;
331  };
332 
338  {
340  uint8_t devId;
342  timespec timestamp;
344  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
346  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
357  };
358 
359  virtual ~DpdkDevice();
360 
364  int getDeviceId() const { return m_Id; }
368  std::string getDeviceName() const { return m_DeviceName; }
369 
373  MacAddress getMacAddress() const { return m_MacAddress; }
374 
379  std::string getPMDName() const { return m_PMDName; }
380 
385  DpdkPMDType getPMDType() const { return m_PMDType; }
386 
390  std::string getPciAddress() const { return m_PciAddress; }
391 
395  uint16_t getMtu() const { return m_DeviceMtu; }
396 
402  bool setMtu(uint16_t newMtu);
403 
407  bool isVirtual() const;
408 
413  void getLinkStatus(LinkStatus& linkStatus) const;
414 
418  uint32_t getCurrentCoreId() const;
419 
423  uint16_t getNumOfOpenedRxQueues() const { return m_NumOfRxQueuesOpened; }
424 
428  uint16_t getNumOfOpenedTxQueues() const { return m_NumOfTxQueuesOpened; }
429 
433  uint16_t getTotalNumOfRxQueues() const { return m_TotalAvailableRxQueues; }
434 
438  uint16_t getTotalNumOfTxQueues() const { return m_TotalAvailableTxQueues; }
439 
440 
447  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
448 
461  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
462 
472  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
473 
494  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
495 
516  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
517 
534  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
535 
556  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
557 
572  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
573 
586  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
587 
603  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
604 
610  bool setFilter(GeneralFilter& filter);
611 
617  bool setFilter(std::string filterAsString);
618 
629  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
630 
646  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
647 
663  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
664 
669  void stopCapture();
670 
674  int getAmountOfFreeMbufs() const;
675 
679  int getAmountOfMbufsInUse() const;
680 
685  void getStatistics(DpdkDeviceStats& stats) const;
686 
690  void clearStatistics();
691 
705  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
706 
713 
719  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
720 
725  uint64_t getSupportedRssHashFunctions() const;
726 
727 
728  //overridden methods
729 
736  bool open() { return openMultiQueues(1, 1); };
737 
741  void close();
742 
743  private:
744 
745  struct DpdkCoreConfiguration
746  {
747  int RxQueueId;
748  bool IsCoreInUse;
749 
750  void clear() { RxQueueId = -1; IsCoreInUse = false; }
751 
752  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
753  };
754 
755  DpdkDevice(int port, uint32_t mBufPoolSize);
756  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
757 
758  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
759  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
760  bool startDevice();
761 
762  static int dpdkCaptureThreadMain(void* ptr);
763 
764  void clearCoreConfiguration();
765  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
766  int getCoresInUseCount() const;
767 
768  void setDeviceInfo();
769 
770  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
771  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
772 
773  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
774  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
775 
776  std::string m_DeviceName;
777  DpdkPMDType m_PMDType;
778  std::string m_PMDName;
779  std::string m_PciAddress;
780 
781  DpdkDeviceConfiguration m_Config;
782 
783  int m_Id;
784  MacAddress m_MacAddress;
785  uint16_t m_DeviceMtu;
786  struct rte_mempool* m_MBufMempool;
787  struct rte_eth_dev_tx_buffer** m_TxBuffers;
788  uint64_t m_TxBufferDrainTsc;
789  uint64_t* m_TxBufferLastDrainTsc;
790  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
791  uint16_t m_TotalAvailableRxQueues;
792  uint16_t m_TotalAvailableTxQueues;
793  uint16_t m_NumOfRxQueuesOpened;
794  uint16_t m_NumOfTxQueuesOpened;
795  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
796  void* m_OnPacketsArriveUserCookie;
797  bool m_StopThread;
798 
799  bool m_WasOpened;
800 
801  // RSS key used by the NIC for load balancing the packets between cores
802  static uint8_t m_RSSKey[40];
803 
804  mutable DpdkDeviceStats m_PrevStats;
805  };
806 
807 } // namespace pcpp
808 
809 #endif /* PCAPPP_DPDK_DEVICE */
Definition: DpdkDevice.h:178
The main namespace for the PcapPlusPlus lib.
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:236
bool open()
Definition: DpdkDevice.h:736
Definition: DpdkDevice.h:196
int getAmountOfMbufsInUse() const
DpdkPMDType
Definition: DpdkDevice.h:77
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:348
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
Definition: DpdkDevice.h:92
std::string getDeviceName() const
Definition: DpdkDevice.h:368
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:242
uint64_t getSupportedRssHashFunctions() const
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:354
Definition: DpdkDevice.h:84
Definition: DpdkDevice.h:112
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: Packet.h:26
std::string getPciAddress() const
Definition: DpdkDevice.h:390
Definition: DpdkDevice.h:104
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:80
Definition: RawPacket.h:252
Definition: DpdkDevice.h:206
Definition: Device.h:24
bool isVirtual() const
Definition: DpdkDevice.h:96
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:350
void getLinkStatus(LinkStatus &linkStatus) const
Definition: MBufRawPacket.h:43
Definition: DpdkDevice.h:200
Definition: DpdkDevice.h:188
Definition: DpdkDevice.h:108
Definition: DpdkDevice.h:82
Definition: DpdkDevice.h:182
Definition: DpdkDevice.h:198
Definition: DpdkDevice.h:321
Definition: DpdkDevice.h:208
Definition: DpdkDevice.h:190
Definition: DpdkDevice.h:110
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
timespec timestamp
Definition: DpdkDevice.h:342
bool setFilter(GeneralFilter &filter)
int getDeviceId() const
Definition: DpdkDevice.h:364
uint64_t packetsPerSec
Definition: DpdkDevice.h:328
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:278
uint64_t bytes
Definition: DpdkDevice.h:326
MacAddress getMacAddress() const
Definition: DpdkDevice.h:373
Definition: DpdkDevice.h:165
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
Definition: DpdkDevice.h:186
uint8_t * rssKey
Definition: DpdkDevice.h:250
uint8_t devId
Definition: DpdkDevice.h:340
Definition: DpdkDevice.h:98
Definition: DpdkDevice.h:106
uint64_t packets
Definition: DpdkDevice.h:324
uint16_t getMtu() const
Definition: DpdkDevice.h:395
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:356
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
Definition: DpdkDevice.h:100
void clearStatistics()
Definition: DpdkDevice.h:180
Definition: DpdkDevice.h:212
Definition: DpdkDevice.h:102
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:385
Definition: DpdkDevice.h:216
void getStatistics(DpdkDeviceStats &stats) const
Definition: DpdkDevice.h:184
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:230
Definition: DpdkDevice.h:224
Definition: DpdkDevice.h:88
Definition: DpdkDevice.h:337
Definition: DpdkDevice.h:214
uint64_t bytesPerSec
Definition: DpdkDevice.h:330
Definition: DpdkDevice.h:86
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:428
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:433
Definition: DpdkDevice.h:202
Definition: DpdkDevice.h:94
Definition: DpdkDevice.h:116
Definition: DpdkDevice.h:204
Definition: DpdkDeviceList.h:72
uint32_t getCurrentCoreId() const
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
int getAmountOfFreeMbufs() const
uint64_t rxPacketsDropeedByHW
Definition: DpdkDevice.h:352
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
Definition: DpdkDevice.h:114
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:438
Definition: MacAddress.h:28
uint64_t rssHashFunction
Definition: DpdkDevice.h:263
uint8_t rssKeyLength
Definition: DpdkDevice.h:256
Definition: DpdkDevice.h:194
Definition: DpdkDevice.h:210
Definition: DpdkDevice.h:90
Definition: DpdkDevice.h:192
std::string getPMDName() const
Definition: DpdkDevice.h:379
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:128
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:423
DpdkRssHashFunction
Definition: DpdkDevice.h:175