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 "RawPacket.h"
9 #include "PcapLiveDevice.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 
303  {
304  public:
308  PciAddress() { domain = 0; bus = 0; devid = 0; function = 0; }
309 
317  PciAddress(uint16_t domain, uint8_t bus, uint8_t devid, uint8_t function)
318  {
319  this->domain = domain;
320  this->bus = bus;
321  this->devid = devid;
322  this->function = function;
323  }
324 
326  uint16_t domain;
328  uint8_t bus;
330  uint8_t devid;
332  uint8_t function;
333 
337  std::string toString()
338  {
339  char pciString[15];
340  snprintf(pciString, 15, "%04x:%02x:%02x.%x", domain, bus, devid, function);
341  return std::string(pciString);
342  }
343 
347  bool operator==(const PciAddress &other) const
348  {
349  return (domain == other.domain && bus == other.bus && devid == other.devid && function == other.function);
350  }
351  };
352 
353 
389  class DpdkDevice : public IPcapDevice
390  {
391  friend class DpdkDeviceList;
392  friend class MBufRawPacket;
393  public:
394 
400  {
402  RSS_IPV4 = 0x1,
414  RSS_IPV6 = 0x40,
426  RSS_L2_PAYLOAD = 0x1000,
428  RSS_IPV6_EX = 0x2000,
430  RSS_IPV6_TCP_EX = 0x4000,
432  RSS_IPV6_UDP_EX = 0x8000,
434  RSS_PORT = 0x10000,
436  RSS_VXLAN = 0x20000,
438  RSS_GENEVE = 0x40000,
440  RSS_NVGRE = 0x80000
441  };
442 
449  {
455 
461 
467 
474  uint8_t* rssKey;
475 
480  uint8_t rssKeyLength;
481 
487  uint64_t rssHashFunction;
488 
502  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128,
503  uint16_t transmitDescriptorsNumber = 512,
504  uint16_t flushTxBufferTimeout = 100,
505  uint64_t rssHashFunction = RSS_IPV4 | RSS_IPV6,
506  uint8_t* rssKey = DpdkDevice::m_RSSKey,
507  uint8_t rssKeyLength = 40)
508  {
509  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
510  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
511  this->flushTxBufferTimeout = flushTxBufferTimeout;
512  this->rssKey = rssKey;
513  this->rssKeyLength = rssKeyLength;
514  this->rssHashFunction = rssHashFunction;
515  }
516  };
517 
522  struct LinkStatus
523  {
526  {
531  };
532 
534  bool linkUp;
539  };
540 
545  struct RxTxStats
546  {
548  uint64_t packets;
550  uint64_t bytes;
552  uint64_t packetsPerSec;
554  uint64_t bytesPerSec;
555  };
556 
562  {
564  uint8_t devId;
566  timespec timestamp;
568  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
570  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
581  };
582 
583  virtual ~DpdkDevice();
584 
588  inline int getDeviceId() { return m_Id; }
592  inline std::string getDeviceName() { return std::string(m_DeviceName); }
593 
597  inline MacAddress getMacAddress() { return m_MacAddress; }
598 
603  inline std::string getPMDName() { return m_PMDName; }
604 
609  inline DpdkPMDType getPMDType() { return m_PMDType; }
610 
614  inline PciAddress getPciAddress() { return m_PciAddress; }
615 
619  inline uint16_t getMtu() { return m_DeviceMtu; }
620 
626  bool setMtu(uint16_t newMtu);
627 
631  bool isVirtual();
632 
637  void getLinkStatus(LinkStatus& linkStatus);
638 
642  uint32_t getCurrentCoreId();
643 
647  uint16_t getNumOfOpenedRxQueues() { return m_NumOfRxQueuesOpened; }
648 
652  uint16_t getNumOfOpenedTxQueues() { return m_NumOfTxQueuesOpened; }
653 
657  uint16_t getTotalNumOfRxQueues() { return m_TotalAvailableRxQueues; }
658 
662  uint16_t getTotalNumOfTxQueues() { return m_TotalAvailableTxQueues; }
663 
664 
671  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId);
672 
685  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId);
686 
696  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId);
697 
718  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
719 
740  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
741 
758  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
759 
780  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
781 
796  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
797 
810  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
811 
827  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
828 
833  bool setFilter(GeneralFilter& filter);
834 
839  bool setFilter(std::string filterAsString);
840 
851  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
852 
868  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
869 
885  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
886 
891  void stopCapture();
892 
896  int getAmountOfFreeMbufs();
897 
901  int getAmountOfMbufsInUse();
902 
903  //overridden methods
904 
911  bool open() { return openMultiQueues(1, 1); };
912 
916  void close();
917 
922  void getStatistics(pcap_stat& stats);
923 
928  void getStatistics(DpdkDeviceStats& stats);
929 
933  void clearStatistics();
934 
948  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
949 
956 
962  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask);
963 
968  uint64_t getSupportedRssHashFunctions();
969 
970  private:
971 
972  struct DpdkCoreConfiguration
973  {
974  int RxQueueId;
975  bool IsCoreInUse;
976 
977  void clear() { RxQueueId = -1; IsCoreInUse = false; }
978 
979  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
980  };
981 
982  DpdkDevice(int port, uint32_t mBufPoolSize);
983  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
984 
985  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
986  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
987  bool startDevice();
988 
989  static int dpdkCaptureThreadMain(void *ptr);
990 
991  void clearCoreConfiguration();
992  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
993  int getCoresInUseCount();
994 
995  void setDeviceInfo();
996 
997  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
998  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength, bool useTxBuffer);
999 
1000  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF);
1001  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF);
1002 
1003  char m_DeviceName[30];
1004  DpdkPMDType m_PMDType;
1005  std::string m_PMDName;
1006  PciAddress m_PciAddress;
1007 
1008  DpdkDeviceConfiguration m_Config;
1009 
1010  int m_Id;
1011  MacAddress m_MacAddress;
1012  uint16_t m_DeviceMtu;
1013  struct rte_mempool* m_MBufMempool;
1014  struct rte_eth_dev_tx_buffer** m_TxBuffers;
1015  uint64_t m_TxBufferDrainTsc;
1016  uint64_t* m_TxBufferLastDrainTsc;
1017  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
1018  uint16_t m_TotalAvailableRxQueues;
1019  uint16_t m_TotalAvailableTxQueues;
1020  uint16_t m_NumOfRxQueuesOpened;
1021  uint16_t m_NumOfTxQueuesOpened;
1022  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
1023  void* m_OnPacketsArriveUserCookie;
1024  bool m_StopThread;
1025 
1026  bool m_WasOpened;
1027 
1028  // RSS key used by the NIC for load balancing the packets between cores
1029  static uint8_t m_RSSKey[40];
1030 
1031  DpdkDeviceStats m_PrevStats;
1032  };
1033 
1034 } // namespace pcpp
1035 
1036 #endif /* PCAPPP_DPDK_DEVICE */
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:502
uint64_t bytesPerSec
Definition: DpdkDevice.h:554
LinkDuplex
Definition: DpdkDevice.h:525
uint16_t domain
Definition: DpdkDevice.h:326
int getAmountOfMbufsInUse()
DpdkRssHashFunction
Definition: DpdkDevice.h:399
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
Definition: DpdkDevice.h:404
Definition: DpdkDevice.h:80
Definition: DpdkDevice.h:561
Definition: DpdkDevice.h:402
int getDeviceId()
Definition: DpdkDevice.h:588
timespec timestamp
Definition: DpdkDevice.h:566
Definition: DpdkDevice.h:84
PointerVector< MBufRawPacket > MBufRawPacketVector
Definition: DpdkDevice.h:296
Definition: DpdkDevice.h:434
Definition: DpdkDevice.h:414
bool setMtu(uint16_t newMtu)
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:454
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:290
Definition: DpdkDeviceList.h:70
uint8_t devId
Definition: DpdkDevice.h:564
Definition: DpdkDevice.h:110
Definition: DpdkDevice.h:424
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF)
RxTxStats aggregatedTxStats
Definition: DpdkDevice.h:574
uint64_t bytes
Definition: DpdkDevice.h:550
Definition: DpdkDevice.h:112
Definition: RawPacket.h:30
Definition: DpdkDevice.h:90
bool init(DpdkDevice *device)
Definition: RawPacket.h:220
uint64_t rxPacketsDropeedByHW
Definition: DpdkDevice.h:576
Definition: PcapDevice.h:38
rte_mbuf * getMBuf()
Definition: DpdkDevice.h:199
bool linkUp
Definition: DpdkDevice.h:534
Definition: DpdkDevice.h:530
Definition: Packet.h:26
Definition: DpdkDevice.h:422
bool setRawData(const uint8_t *pRawData, int rawDataLen, timeval timestamp, LinkLayerType layerType=LINKTYPE_ETHERNET, int frameLength=-1)
uint64_t packets
Definition: DpdkDevice.h:548
The main namespace for the PcapPlusPlus lib.
Definition: DpdkDevice.h:448
Definition: DpdkDevice.h:440
Definition: DpdkDevice.h:143
LinkLayerType
Definition: RawPacket.h:25
Definition: PcapFilter.h:72
Definition: DpdkDevice.h:98
Definition: DpdkDevice.h:430
uint16_t getMtu()
Definition: DpdkDevice.h:619
PciAddress(uint16_t domain, uint8_t bus, uint8_t devid, uint8_t function)
Definition: DpdkDevice.h:317
PciAddress()
Definition: DpdkDevice.h:308
Definition: DpdkDevice.h:102
uint16_t getTotalNumOfRxQueues()
Definition: DpdkDevice.h:657
uint16_t getNumOfOpenedTxQueues()
Definition: DpdkDevice.h:652
Definition: DpdkDevice.h:545
Definition: DpdkDevice.h:438
Definition: DpdkDevice.h:389
Definition: PointerVector.h:24
uint64_t rxErroneousPackets
Definition: DpdkDevice.h:578
Definition: DpdkDevice.h:96
Definition: DpdkDevice.h:106
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
Definition: DpdkDevice.h:100
DpdkPMDType
Definition: DpdkDevice.h:78
uint8_t function
Definition: DpdkDevice.h:332
uint16_t getTotalNumOfTxQueues()
Definition: DpdkDevice.h:662
MBufRawPacket & operator=(const MBufRawPacket &other)
uint64_t rxMbufAlocFailed
Definition: DpdkDevice.h:580
Definition: DpdkDevice.h:522
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
DpdkPMDType getPMDType()
Definition: DpdkDevice.h:609
Definition: DpdkDevice.h:88
MacAddress getMacAddress()
Definition: DpdkDevice.h:597
Definition: DpdkDevice.h:94
Definition: DpdkDevice.h:116
void clearStatistics()
uint8_t devid
Definition: DpdkDevice.h:330
Definition: DpdkDevice.h:114
uint8_t rssKeyLength
Definition: DpdkDevice.h:480
Definition: DpdkDevice.h:104
Definition: MacAddress.h:21
Definition: DpdkDevice.h:436
uint64_t rssHashFunction
Definition: DpdkDevice.h:487
uint64_t packetsPerSec
Definition: DpdkDevice.h:552
Definition: DpdkDevice.h:108
Definition: DpdkDevice.h:412
Definition: DpdkDevice.h:432
void getStatistics(pcap_stat &stats)
RxTxStats aggregatedRxStats
Definition: DpdkDevice.h:572
Definition: DpdkDevice.h:82
void setFreeMbuf(bool val=true)
Definition: DpdkDevice.h:278
uint32_t getCurrentCoreId()
LinkDuplex linkDuplex
Definition: DpdkDevice.h:538
uint16_t getNumOfOpenedRxQueues()
Definition: DpdkDevice.h:647
RxTxStats rxStats[16]
Definition: DpdkDevice.h:568
Definition: DpdkDevice.h:428
Definition: DpdkDevice.h:86
Definition: DpdkDevice.h:426
bool initFromRawPacket(const RawPacket *rawPacket, DpdkDevice *device)
uint8_t bus
Definition: DpdkDevice.h:328
PciAddress getPciAddress()
Definition: DpdkDevice.h:614
bool setFilter(GeneralFilter &filter)
RxTxStats txStats[16]
Definition: DpdkDevice.h:570
int linkSpeedMbps
Definition: DpdkDevice.h:536
void insertData(int atIndex, const uint8_t *dataToInsert, size_t dataToInsertLen)
Definition: DpdkDevice.h:406
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:466
void appendData(const uint8_t *dataToAppend, size_t dataToAppendLen)
MBufRawPacket()
Definition: DpdkDevice.h:160
void getLinkStatus(LinkStatus &linkStatus)
bool removeData(int atIndex, size_t numOfBytesToRemove)
Definition: DpdkDevice.h:418
std::string toString()
Definition: DpdkDevice.h:337
bool operator==(const PciAddress &other) const
Definition: DpdkDevice.h:347
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
bool open()
Definition: DpdkDevice.h:911
uint8_t * rssKey
Definition: DpdkDevice.h:474
int getAmountOfFreeMbufs()
bool reallocateData(size_t newBufferLength)
Definition: DpdkDevice.h:410
Definition: DpdkDevice.h:408
Definition: DpdkDevice.h:528
uint64_t getSupportedRssHashFunctions()
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:460
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr, uint16_t rxQueueId)
Definition: DpdkDevice.h:92
virtual ~MBufRawPacket()
virtual uint8_t getObjectType() const
Definition: DpdkDevice.h:206
Definition: DpdkDevice.h:420
std::string getDeviceName()
Definition: DpdkDevice.h:592
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength, uint16_t txQueueId=0, bool useTxBuffer=false)
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
std::string getPMDName()
Definition: DpdkDevice.h:603
Definition: DpdkDevice.h:302
Definition: DpdkDevice.h:416