PcapPlusPlus  Next
DpdkDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 // GCOVR_EXCL_START
4 
5 #include <pthread.h>
6 #include <time.h>
7 #include <vector>
8 #include "MacAddress.h"
9 #include "SystemUtils.h"
10 #include "Device.h"
11 #include "MBufRawPacket.h"
12 
61 struct rte_mbuf;
62 struct rte_mempool;
63 struct rte_eth_conf;
64 struct rte_eth_dev_tx_buffer;
65 
70 namespace pcpp
71 {
72 
73 #define DPDK_MAX_RX_QUEUES 16
74 #define DPDK_MAX_TX_QUEUES 16
75 #define PCPP_RSS_HASH_MAGIC_NUMBER 0x123456
76 
77  class DpdkDeviceList;
78  class DpdkDevice;
79 
85  {
125  };
126 
137  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
138  DpdkDevice* device, void* userCookie);
139 
178  class DpdkDevice : public IDevice
179  {
180  friend class DpdkDeviceList;
181  friend class MBufRawPacket;
182 
183  public:
189  {
191  RSS_NONE = 0,
193  RSS_IPV4 = 0x1,
205  RSS_IPV6 = 0x40,
217  RSS_L2_PAYLOAD = 0x1000,
219  RSS_IPV6_EX = 0x2000,
221  RSS_IPV6_TCP_EX = 0x4000,
223  RSS_IPV6_UDP_EX = 0x8000,
225  RSS_PORT = 0x10000,
227  RSS_VXLAN = 0x20000,
229  RSS_GENEVE = 0x40000,
231  RSS_NVGRE = 0x80000,
235  RSS_DEFAULT = PCPP_RSS_HASH_MAGIC_NUMBER
236  };
237 
244  {
251 
258 
264 
272  uint8_t* rssKey;
273 
278  uint8_t rssKeyLength;
279 
286  uint64_t rssHashFunction;
287 
305  uint16_t transmitDescriptorsNumber = 512,
306  uint16_t flushTxBufferTimeout = 100,
307  uint64_t rssHashFunction = RSS_DEFAULT,
308  uint8_t* rssKey = DpdkDevice::m_RSSKey, uint8_t rssKeyLength = 40)
309  {
310  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
311  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
312  this->flushTxBufferTimeout = flushTxBufferTimeout;
313  this->rssKey = rssKey;
314  this->rssKeyLength = rssKeyLength;
315  this->rssHashFunction = rssHashFunction;
316  }
317  };
318 
323  struct LinkStatus
324  {
327  {
332  };
333 
335  bool linkUp;
340  };
341 
346  struct RxTxStats
347  {
349  uint64_t packets;
351  uint64_t bytes;
353  uint64_t packetsPerSec;
355  uint64_t bytesPerSec;
356  };
357 
363  {
365  uint8_t devId;
367  timespec timestamp;
369  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
371  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
383  };
384 
385  virtual ~DpdkDevice();
386 
390  int getDeviceId() const
391  {
392  return m_Id;
393  }
397  std::string getDeviceName() const
398  {
399  return m_DeviceName;
400  }
401 
406  {
407  return m_MacAddress;
408  }
409 
414  std::string getPMDName() const
415  {
416  return m_PMDName;
417  }
418 
424  {
425  return m_PMDType;
426  }
427 
431  std::string getPciAddress() const
432  {
433  return m_PciAddress;
434  }
435 
439  uint16_t getMtu() const
440  {
441  return m_DeviceMtu;
442  }
443 
450  bool setMtu(uint16_t newMtu);
451 
456  bool isVirtual() const;
457 
462  void getLinkStatus(LinkStatus& linkStatus) const;
463 
467  uint32_t getCurrentCoreId() const;
468 
472  uint16_t getNumOfOpenedRxQueues() const
473  {
474  return m_NumOfRxQueuesOpened;
475  }
476 
480  uint16_t getNumOfOpenedTxQueues() const
481  {
482  return m_NumOfTxQueuesOpened;
483  }
484 
488  uint16_t getTotalNumOfRxQueues() const
489  {
490  return m_TotalAvailableRxQueues;
491  }
492 
496  uint16_t getTotalNumOfTxQueues() const
497  {
498  return m_TotalAvailableTxQueues;
499  }
500 
508  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
509 
524  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
525 
537  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
538 
562  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0,
563  bool useTxBuffer = false);
564 
589  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
590 
610  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
611 
635  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
636 
653  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
654 
668  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
669 
686  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
687 
693  bool setFilter(GeneralFilter& filter);
694 
700  bool setFilter(std::string filterAsString);
701 
715  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen,
717 
736  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
737 
756  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
757  CoreMask coreMask);
758 
763  void stopCapture();
764 
768  int getAmountOfFreeMbufs() const;
769 
774 
779  void getStatistics(DpdkDeviceStats& stats) const;
780 
785 
799  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
800 
807 
814  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
815 
821 
826 
832  std::vector<std::string> rssHashFunctionMaskToString(uint64_t rssHFMask) const;
833 
834  // overridden methods
835 
843  bool open() override
844  {
845  return openMultiQueues(1, 1);
846  };
847 
851  void close() override;
852 
853  private:
854  struct DpdkCoreConfiguration
855  {
856  int RxQueueId;
857  bool IsCoreInUse;
858 
859  void clear()
860  {
861  RxQueueId = -1;
862  IsCoreInUse = false;
863  }
864 
865  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false)
866  {}
867  };
868 
869  DpdkDevice(int port, uint32_t mBufPoolSize, uint16_t mMbufDataSize);
870  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
871 
872  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
873  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
874  bool startDevice();
875 
876  static int dpdkCaptureThreadMain(void* ptr);
877 
878  void clearCoreConfiguration();
879  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
880  int getCoresInUseCount() const;
881 
882  void setDeviceInfo();
883 
884  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
885  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength,
886  bool useTxBuffer);
887 
888  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
889  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
890 
891  std::string m_DeviceName;
892  DpdkPMDType m_PMDType;
893  std::string m_PMDName;
894  std::string m_PciAddress;
895 
896  DpdkDeviceConfiguration m_Config;
897 
898  int m_Id;
899  MacAddress m_MacAddress;
900  int16_t m_DeviceSocketId;
901  uint16_t m_DeviceMtu;
902  uint16_t m_MBufDataSize;
903  struct rte_mempool* m_MBufMempool;
904  struct rte_eth_dev_tx_buffer** m_TxBuffers;
905  uint64_t m_TxBufferDrainTsc;
906  uint64_t* m_TxBufferLastDrainTsc;
907  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
908  uint16_t m_TotalAvailableRxQueues;
909  uint16_t m_TotalAvailableTxQueues;
910  uint16_t m_NumOfRxQueuesOpened;
911  uint16_t m_NumOfTxQueuesOpened;
912  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
913  void* m_OnPacketsArriveUserCookie;
914  bool m_StopThread;
915 
916  bool m_WasOpened;
917 
918  // RSS key used by the NIC for load balancing the packets between cores
919  static uint8_t m_RSSKey[40];
920 
921  mutable DpdkDeviceStats m_PrevStats;
922  };
923 
924 } // namespace pcpp
925 
926 // GCOVR_EXCL_STOP
Definition: DpdkDevice.h:179
bool setFilter(std::string filterAsString)
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:488
int getAmountOfMbufsInUse() const
uint16_t getMtu() const
Definition: DpdkDevice.h:439
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
int getDeviceId() const
Definition: DpdkDevice.h:390
uint16_t receivePackets(Packet **packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const
bool open() override
Definition: DpdkDevice.h:843
bool sendPacket(MBufRawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
std::string getPciAddress() const
Definition: DpdkDevice.h:431
bool sendPacket(Packet &packet, uint16_t txQueueId=0, bool useTxBuffer=false)
uint64_t getConfiguredRssHashFunction() const
void getLinkStatus(LinkStatus &linkStatus) const
MacAddress getMacAddress() const
Definition: DpdkDevice.h:405
int getAmountOfFreeMbufs() const
void close() override
uint16_t receivePackets(MBufRawPacket **rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const
uint16_t sendPackets(Packet **packetsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
void clearStatistics()
bool setFilter(GeneralFilter &filter)
uint16_t sendPackets(MBufRawPacketVector &rawPacketsVec, uint16_t txQueueId=0, bool useTxBuffer=false)
uint64_t getSupportedRssHashFunctions() const
std::string getPMDName() const
Definition: DpdkDevice.h:414
uint16_t sendPackets(RawPacketVector &rawPacketsVec, uint16_t txQueueId=0, bool useTxBuffer=false)
bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const
bool setMtu(uint16_t newMtu)
uint16_t getNumOfOpenedRxQueues() const
Definition: DpdkDevice.h:472
DpdkRssHashFunction
Definition: DpdkDevice.h:189
@ RSS_NVGRE
Definition: DpdkDevice.h:231
@ RSS_IPV4
Definition: DpdkDevice.h:193
@ RSS_ALL_SUPPORTED
Definition: DpdkDevice.h:233
@ RSS_NONE
Definition: DpdkDevice.h:191
@ RSS_PORT
Definition: DpdkDevice.h:225
@ RSS_IPV6_TCP_EX
Definition: DpdkDevice.h:221
@ RSS_GENEVE
Definition: DpdkDevice.h:229
@ RSS_NONFRAG_IPV4_OTHER
Definition: DpdkDevice.h:203
@ RSS_FRAG_IPV6
Definition: DpdkDevice.h:207
@ RSS_IPV6_EX
Definition: DpdkDevice.h:219
@ RSS_IPV6_UDP_EX
Definition: DpdkDevice.h:223
@ RSS_NONFRAG_IPV6_UDP
Definition: DpdkDevice.h:211
@ RSS_NONFRAG_IPV4_SCTP
Definition: DpdkDevice.h:201
@ RSS_NONFRAG_IPV4_UDP
Definition: DpdkDevice.h:199
@ RSS_NONFRAG_IPV6_OTHER
Definition: DpdkDevice.h:215
@ RSS_NONFRAG_IPV6_TCP
Definition: DpdkDevice.h:209
@ RSS_L2_PAYLOAD
Definition: DpdkDevice.h:217
@ RSS_FRAG_IPV4
Definition: DpdkDevice.h:195
@ RSS_DEFAULT
Definition: DpdkDevice.h:235
@ RSS_IPV6
Definition: DpdkDevice.h:205
@ RSS_NONFRAG_IPV6_SCTP
Definition: DpdkDevice.h:213
@ RSS_NONFRAG_IPV4_TCP
Definition: DpdkDevice.h:197
@ RSS_VXLAN
Definition: DpdkDevice.h:227
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:480
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
void getStatistics(DpdkDeviceStats &stats) const
uint16_t getTotalNumOfTxQueues() const
Definition: DpdkDevice.h:496
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:423
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId) const
uint32_t getCurrentCoreId() const
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
bool isVirtual() const
std::vector< std::string > rssHashFunctionMaskToString(uint64_t rssHFMask) const
std::string getDeviceName() const
Definition: DpdkDevice.h:397
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
Definition: DpdkDeviceList.h:76
Definition: PcapFilter.h:160
Definition: Device.h:24
Definition: MBufRawPacket.h:49
Definition: MacAddress.h:25
Definition: Packet.h:27
Definition: PointerVector.h:58
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
DpdkPMDType
Definition: DpdkDevice.h:85
@ PMD_IGB
Definition: DpdkDevice.h:94
@ PMD_MLX4
Definition: DpdkDevice.h:110
@ PMD_RING
Definition: DpdkDevice.h:116
@ PMD_IXGBEVF
Definition: DpdkDevice.h:108
@ PMD_BOND
Definition: DpdkDevice.h:90
@ PMD_AF_PACKET
Definition: DpdkDevice.h:124
@ PMD_I40EVF
Definition: DpdkDevice.h:104
@ PMD_UNKNOWN
Definition: DpdkDevice.h:87
@ PMD_I40E
Definition: DpdkDevice.h:102
@ PMD_E1000EM
Definition: DpdkDevice.h:92
@ PMD_PCAP
Definition: DpdkDevice.h:114
@ PMD_FM10K
Definition: DpdkDevice.h:100
@ PMD_XENVIRT
Definition: DpdkDevice.h:122
@ PMD_IXGBE
Definition: DpdkDevice.h:106
@ PMD_VMXNET3
Definition: DpdkDevice.h:120
@ PMD_ENIC
Definition: DpdkDevice.h:98
@ PMD_IGBVF
Definition: DpdkDevice.h:96
@ PMD_VIRTIO
Definition: DpdkDevice.h:118
@ PMD_NULL
Definition: DpdkDevice.h:112
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:137
Definition: DpdkDevice.h:244
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:263
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:304
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:257
uint8_t * rssKey
Definition: DpdkDevice.h:272
uint8_t rssKeyLength
Definition: DpdkDevice.h:278
uint64_t rssHashFunction
Definition: DpdkDevice.h:286
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:250
Definition: DpdkDevice.h:363
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:382
uint8_t devId
Definition: DpdkDevice.h:365
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:373
timespec timestamp
Definition: DpdkDevice.h:367
uint64_t rxPacketsDroppedByHW
Definition: DpdkDevice.h:378
RxTxStats txStats[16]
Definition: DpdkDevice.h:371
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:375
RxTxStats rxStats[16]
Definition: DpdkDevice.h:369
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:380
Definition: DpdkDevice.h:347
uint64_t packetsPerSec
Definition: DpdkDevice.h:353
uint64_t bytesPerSec
Definition: DpdkDevice.h:355
uint64_t packets
Definition: DpdkDevice.h:349
uint64_t bytes
Definition: DpdkDevice.h:351