PcapPlusPlus
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 "Packet.h"
9 #include "Device.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 
72  class DpdkDeviceList;
73  class DpdkDevice;
74 
78  enum DpdkPMDType {
117  };
118 
119  class DpdkDevice;
120 
121  #define MBUFRAWPACKET_OBJECT_TYPE 1
122 
143  class MBufRawPacket : public RawPacket
144  {
145  friend class DpdkDevice;
146 
147  private:
148  struct rte_mbuf* m_MBuf;
149  DpdkDevice* m_Device;
150  bool m_FreeMbuf;
151 
152  void setMBuf(struct rte_mbuf* mBuf, timeval timestamp);
153  public:
154 
160  MBufRawPacket() : RawPacket(), m_MBuf(NULL), m_Device(NULL), m_FreeMbuf(true) { m_DeleteRawDataAtDestructor = false; }
161 
165  virtual ~MBufRawPacket();
166 
173  MBufRawPacket(const MBufRawPacket& other);
174 
182  bool init(DpdkDevice* device);
183 
194  bool initFromRawPacket(const RawPacket* rawPacket, DpdkDevice* device);
195 
199  inline rte_mbuf* getMBuf() { return m_MBuf; }
200 
201  // overridden methods
202 
206  virtual inline uint8_t getObjectType() const { return MBUFRAWPACKET_OBJECT_TYPE; }
207 
214  MBufRawPacket& operator=(const MBufRawPacket& other);
215 
230  bool setRawData(const uint8_t* pRawData, int rawDataLen, timeval timestamp, LinkLayerType layerType = LINKTYPE_ETHERNET, int frameLength = -1);
231 
235  void clear();
236 
243  void appendData(const uint8_t* dataToAppend, size_t dataToAppendLen);
244 
253  void insertData(int atIndex, const uint8_t* dataToInsert, size_t dataToInsertLen);
254 
263  bool removeData(int atIndex, size_t numOfBytesToRemove);
264 
271  bool reallocateData(size_t newBufferLength);
272 
278  inline void setFreeMbuf(bool val = true) { m_FreeMbuf = val; }
279  };
280 
290  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
291 
297 
298 
334  class DpdkDevice : public IDevice
335  {
336  friend class DpdkDeviceList;
337  friend class MBufRawPacket;
338  public:
339 
345  {
347  RSS_IPV4 = 0x1,
359  RSS_IPV6 = 0x40,
371  RSS_L2_PAYLOAD = 0x1000,
373  RSS_IPV6_EX = 0x2000,
375  RSS_IPV6_TCP_EX = 0x4000,
377  RSS_IPV6_UDP_EX = 0x8000,
379  RSS_PORT = 0x10000,
381  RSS_VXLAN = 0x20000,
383  RSS_GENEVE = 0x40000,
385  RSS_NVGRE = 0x80000
386  };
387 
394  {
400 
406 
412 
419  uint8_t* rssKey;
420 
425  uint8_t rssKeyLength;
426 
432  uint64_t rssHashFunction;
433 
447  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
448  uint16_t transmitDescriptorsNumber = 512,
449  uint16_t flushTxBufferTimeout = 100,
450  uint64_t rssHashFunction = RSS_IPV4 | RSS_IPV6,
451  uint8_t* rssKey = DpdkDevice::m_RSSKey,
452  uint8_t rssKeyLength = 40)
453  {
454  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
455  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
456  this->flushTxBufferTimeout = flushTxBufferTimeout;
457  this->rssKey = rssKey;
458  this->rssKeyLength = rssKeyLength;
459  this->rssHashFunction = rssHashFunction;
460  }
461  };
462 
467  struct LinkStatus
468  {
471  {
476  };
477 
479  bool linkUp;
484  };
485 
490  struct RxTxStats
491  {
493  uint64_t packets;
495  uint64_t bytes;
497  uint64_t packetsPerSec;
499  uint64_t bytesPerSec;
500  };
501 
507  {
509  uint8_t devId;
511  timespec timestamp;
513  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
515  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
526  };
527 
528  virtual ~DpdkDevice();
529 
533  inline int getDeviceId() { return m_Id; }
537  inline std::string getDeviceName() { return std::string(m_DeviceName); }
538 
542  inline MacAddress getMacAddress() { return m_MacAddress; }
543 
548  inline std::string getPMDName() { return m_PMDName; }
549 
554  inline DpdkPMDType getPMDType() { return m_PMDType; }
555 
559  inline std::string getPciAddress() { return m_PciAddress; }
560 
564  inline uint16_t getMtu() { return m_DeviceMtu; }
565 
571  bool setMtu(uint16_t newMtu);
572 
576  bool isVirtual();
577 
582  void getLinkStatus(LinkStatus& linkStatus);
583 
587  uint32_t getCurrentCoreId();
588 
592  uint16_t getNumOfOpenedRxQueues() { return m_NumOfRxQueuesOpened; }
593 
597  uint16_t getNumOfOpenedTxQueues() { return m_NumOfTxQueuesOpened; }
598 
602  uint16_t getTotalNumOfRxQueues() { return m_TotalAvailableRxQueues; }
603 
607  uint16_t getTotalNumOfTxQueues() { return m_TotalAvailableTxQueues; }
608 
609 
616  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId);
617 
630  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId);
631 
641  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId);
642 
663  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
664 
685  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
686 
703  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
704 
725  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
726 
741  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
742 
755  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
756 
772  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
773 
778  bool setFilter(GeneralFilter& filter);
779 
784  bool setFilter(std::string filterAsString);
785 
796  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
797 
813  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
814 
830  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
831 
836  void stopCapture();
837 
841  int getAmountOfFreeMbufs();
842 
846  int getAmountOfMbufsInUse();
847 
852  void getStatistics(DpdkDeviceStats& stats);
853 
857  void clearStatistics();
858 
872  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
873 
880 
886  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask);
887 
892  uint64_t getSupportedRssHashFunctions();
893 
894 
895  //overridden methods
896 
903  bool open() { return openMultiQueues(1, 1); };
904 
908  void close();
909 
910  private:
911 
912  struct DpdkCoreConfiguration
913  {
914  int RxQueueId;
915  bool IsCoreInUse;
916 
917  void clear() { RxQueueId = -1; IsCoreInUse = false; }
918 
919  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
920  };
921 
922  DpdkDevice(int port, uint32_t mBufPoolSize);
923  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
924 
925  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
926  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
927  bool startDevice();
928 
929  static int dpdkCaptureThreadMain(void *ptr);
930 
931  void clearCoreConfiguration();
932  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
933  int getCoresInUseCount();
934 
935  void setDeviceInfo();
936 
937  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
938  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
939 
940  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF);
941  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF);
942 
943  char m_DeviceName[30];
944  DpdkPMDType m_PMDType;
945  std::string m_PMDName;
946  std::string m_PciAddress;
947 
948  DpdkDeviceConfiguration m_Config;
949 
950  int m_Id;
951  MacAddress m_MacAddress;
952  uint16_t m_DeviceMtu;
953  struct rte_mempool* m_MBufMempool;
954  struct rte_eth_dev_tx_buffer** m_TxBuffers;
955  uint64_t m_TxBufferDrainTsc;
956  uint64_t* m_TxBufferLastDrainTsc;
957  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
958  uint16_t m_TotalAvailableRxQueues;
959  uint16_t m_TotalAvailableTxQueues;
960  uint16_t m_NumOfRxQueuesOpened;
961  uint16_t m_NumOfTxQueuesOpened;
962  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
963  void* m_OnPacketsArriveUserCookie;
964  bool m_StopThread;
965 
966  bool m_WasOpened;
967 
968  // RSS key used by the NIC for load balancing the packets between cores
969  static uint8_t m_RSSKey[40];
970 
971  DpdkDeviceStats m_PrevStats;
972  };
973 
974 } // namespace pcpp
975 
976 #endif /* PCAPPP_DPDK_DEVICE */
Definition: DpdkDevice.h:100
Definition: DpdkDevice.h:116
bool init(DpdkDevice *device)
uint64_t rssHashFunction
Definition: DpdkDevice.h:432
bool reallocateData(size_t newBufferLength)
MBufRawPacket & operator=(const MBufRawPacket &other)
int getDeviceId()
Definition: DpdkDevice.h:533
DpdkPMDType getPMDType()
Definition: DpdkDevice.h:554
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId)
Definition: DpdkDevice.h:381
DpdkPMDType
Definition: DpdkDevice.h:78
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:525
Definition: DpdkDevice.h:94
uint16_t getTotalNumOfRxQueues()
Definition: DpdkDevice.h:602
uint16_t getNumOfOpenedTxQueues()
Definition: DpdkDevice.h:597
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
Definition: DpdkDevice.h:393
uint16_t getMtu()
Definition: DpdkDevice.h:564
void clearStatistics()
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:411
uint64_t packetsPerSec
Definition: DpdkDevice.h:497
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:290
Definition: DpdkDevice.h:86
Definition: DpdkDevice.h:377
Definition: DpdkDevice.h:98
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF)
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:517
Definition: DpdkDevice.h:371
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:399
int linkSpeedMbps
Definition: DpdkDevice.h:481
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
Definition: DpdkDevice.h:373
Definition: DpdkDevice.h:490
Definition: PointerVector.h:24
uint8_t * rssKey
Definition: DpdkDevice.h:419
uint16_t getNumOfOpenedRxQueues()
Definition: DpdkDevice.h:592
uint64_t bytesPerSec
Definition: DpdkDevice.h:499
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
Definition: DpdkDevice.h:104
MacAddress getMacAddress()
Definition: DpdkDevice.h:542
void appendData(const uint8_t *dataToAppend, size_t dataToAppendLen)
Definition: DpdkDevice.h:357
Definition: DpdkDevice.h:353
Definition: DpdkDevice.h:351
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
int getAmountOfFreeMbufs()
Definition: DpdkDevice.h:355
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: DpdkDevice.h:80
Definition: MacAddress.h:21
Definition: DpdkDevice.h:363
Definition: DpdkDevice.h:361
Definition: DpdkDevice.h:114
PointerVector< MBufRawPacket > MBufRawPacketVector
Definition: DpdkDevice.h:296
RxTxStats txStats[16]
Definition: DpdkDevice.h:515
Definition: DpdkDevice.h:334
uint32_t getCurrentCoreId()
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:405
Definition: RawPacket.h:219
Definition: Packet.h:26
Definition: DpdkDevice.h:473
bool removeData(int atIndex, size_t numOfBytesToRemove)
bool open()
Definition: DpdkDevice.h:903
LinkDuplex
Definition: DpdkDevice.h:470
Definition: DpdkDevice.h:379
Definition: DpdkDevice.h:110
Definition: Device.h:24
Definition: DpdkDevice.h:467
Definition: DpdkDevice.h:365
Definition: DpdkDevice.h:84
Definition: DpdkDevice.h:96
Definition: DpdkDevice.h:349
bool initFromRawPacket(const RawPacket *rawPacket, DpdkDevice *device)
std::string getPMDName()
Definition: DpdkDevice.h:548
Definition: DpdkDevice.h:143
bool setRawData(const uint8_t *pRawData, int rawDataLen, timeval timestamp, LinkLayerType layerType=LINKTYPE_ETHERNET, int frameLength=-1)
Definition: PcapFilter.h:72
Definition: DpdkDevice.h:347
uint64_t rxPacketsDropeedByHW
Definition: DpdkDevice.h:521
RxTxStats rxStats[16]
Definition: DpdkDevice.h:513
Definition: DpdkDevice.h:369
void getLinkStatus(LinkStatus &linkStatus)
Definition: DpdkDeviceList.h:70
Definition: DpdkDevice.h:359
Definition: DpdkDevice.h:90
void setFreeMbuf(bool val=true)
Definition: DpdkDevice.h:278
Definition: DpdkDevice.h:506
std::string getPciAddress()
Definition: DpdkDevice.h:559
Definition: DpdkDevice.h:88
virtual ~MBufRawPacket()
Definition: DpdkDevice.h:102
Definition: DpdkDevice.h:112
Definition: RawPacket.h:29
Definition: DpdkDevice.h:367
MBufRawPacket()
Definition: DpdkDevice.h:160
Definition: DpdkDevice.h:92
Definition: DpdkDevice.h:475
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:523
bool setMtu(uint16_t newMtu)
Definition: DpdkDevice.h:375
Definition: DpdkDevice.h:108
timespec timestamp
Definition: DpdkDevice.h:511
Definition: DpdkDevice.h:385
bool setFilter(GeneralFilter &filter)
virtual uint8_t getObjectType() const
Definition: DpdkDevice.h:206
void insertData(int atIndex, const uint8_t *dataToInsert, size_t dataToInsertLen)
bool linkUp
Definition: DpdkDevice.h:479
int getAmountOfMbufsInUse()
void getStatistics(DpdkDeviceStats &stats)
DpdkRssHashFunction
Definition: DpdkDevice.h:344
std::string getDeviceName()
Definition: DpdkDevice.h:537
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:519
uint8_t devId
Definition: DpdkDevice.h:509
uint64_t getSupportedRssHashFunctions()
Definition: DpdkDevice.h:106
uint64_t bytes
Definition: DpdkDevice.h:495
Definition: DpdkDevice.h:383
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:447
The main namespace for the PcapPlusPlus lib.
LinkDuplex linkDuplex
Definition: DpdkDevice.h:483
uint16_t getTotalNumOfTxQueues()
Definition: DpdkDevice.h:607
LinkLayerType
Definition: RawPacket.h:24
uint8_t rssKeyLength
Definition: DpdkDevice.h:425
Definition: DpdkDevice.h:82
rte_mbuf * getMBuf()
Definition: DpdkDevice.h:199
uint64_t packets
Definition: DpdkDevice.h:493