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 
58 
59 struct rte_mbuf;
60 struct rte_mempool;
61 struct rte_eth_conf;
62 struct rte_eth_dev_tx_buffer;
63 
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 
79  {
119  };
120 
129  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
130  DpdkDevice* device, void* userCookie);
131 
168  class DpdkDevice : public IDevice
169  {
170  friend class DpdkDeviceList;
171  friend class MBufRawPacket;
172 
173  public:
177  {
179  RSS_NONE = 0,
181  RSS_IPV4 = 0x1,
193  RSS_IPV6 = 0x40,
205  RSS_L2_PAYLOAD = 0x1000,
207  RSS_IPV6_EX = 0x2000,
209  RSS_IPV6_TCP_EX = 0x4000,
211  RSS_IPV6_UDP_EX = 0x8000,
213  RSS_PORT = 0x10000,
215  RSS_VXLAN = 0x20000,
217  RSS_GENEVE = 0x40000,
219  RSS_NVGRE = 0x80000,
223  RSS_DEFAULT = PCPP_RSS_HASH_MAGIC_NUMBER
224  };
225 
230  {
235 
240 
244 
250  uint8_t* rssKey;
251 
254  uint8_t rssKeyLength;
255 
260  uint64_t rssHashFunction;
261 
277  uint16_t transmitDescriptorsNumber = 512,
278  uint16_t flushTxBufferTimeout = 100,
279  uint64_t rssHashFunction = RSS_DEFAULT,
280  uint8_t* rssKey = DpdkDevice::m_RSSKey, uint8_t rssKeyLength = 40)
281  {
282  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
283  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
284  this->flushTxBufferTimeout = flushTxBufferTimeout;
285  this->rssKey = rssKey;
286  this->rssKeyLength = rssKeyLength;
287  this->rssHashFunction = rssHashFunction;
288  }
289  };
290 
293  struct LinkStatus
294  {
297  {
302  };
303 
305  bool linkUp;
310  };
311 
314  struct RxTxStats
315  {
317  uint64_t packets;
319  uint64_t bytes;
321  uint64_t packetsPerSec;
323  uint64_t bytesPerSec;
324  };
325 
329  {
331  uint8_t devId;
333  timespec timestamp;
335  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
337  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
349  };
350 
351  virtual ~DpdkDevice();
352 
354  int getDeviceId() const
355  {
356  return m_Id;
357  }
359  std::string getDeviceName() const
360  {
361  return m_DeviceName;
362  }
363 
366  {
367  return m_MacAddress;
368  }
369 
372  std::string getPMDName() const
373  {
374  return m_PMDName;
375  }
376 
380  {
381  return m_PMDType;
382  }
383 
385  std::string getPciAddress() const
386  {
387  return m_PciAddress;
388  }
389 
391  uint16_t getMtu() const
392  {
393  return m_DeviceMtu;
394  }
395 
400  bool setMtu(uint16_t newMtu);
401 
404  bool isVirtual() const;
405 
409  bool getLinkStatus(LinkStatus& linkStatus) const;
410 
412  uint32_t getCurrentCoreId() const;
413 
415  uint16_t getNumOfOpenedRxQueues() const
416  {
417  return m_NumOfRxQueuesOpened;
418  }
419 
421  uint16_t getNumOfOpenedTxQueues() const
422  {
423  return m_NumOfTxQueuesOpened;
424  }
425 
427  uint16_t getTotalNumOfRxQueues() const
428  {
429  return m_TotalAvailableRxQueues;
430  }
431 
433  uint16_t getTotalNumOfTxQueues() const
434  {
435  return m_TotalAvailableTxQueues;
436  }
437 
443  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
444 
457  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
458 
468  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
469 
491  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0,
492  bool useTxBuffer = false);
493 
516  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
517 
535  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
536 
559  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
560 
575  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
576 
588  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
589 
604  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
605 
609  bool setFilter(GeneralFilter& filter);
610 
614  bool setFilter(std::string filterAsString);
615 
627  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen,
629 
646  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
647 
664  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
665  CoreMask coreMask);
666 
669  void stopCapture();
670 
672  int getAmountOfFreeMbufs() const;
673 
676 
679  void getStatistics(DpdkDeviceStats& stats) const;
680 
683 
695  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
696 
701 
706  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
707 
711 
714 
718  std::vector<std::string> rssHashFunctionMaskToString(uint64_t rssHFMask) const;
719 
720  // overridden methods
721 
727  bool open() override
728  {
729  return openMultiQueues(1, 1);
730  };
731 
733  void close() override;
734 
735  private:
736  struct DpdkCoreConfiguration
737  {
738  int RxQueueId;
739  bool IsCoreInUse;
740 
741  void clear()
742  {
743  RxQueueId = -1;
744  IsCoreInUse = false;
745  }
746 
747  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false)
748  {}
749  };
750 
751  DpdkDevice(int port, uint32_t mBufPoolSize, uint16_t mMbufDataSize);
752  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
753 
754  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
755  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
756  bool startDevice();
757 
758  static int dpdkCaptureThreadMain(void* ptr);
759 
760  void clearCoreConfiguration();
761  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
762  int getCoresInUseCount() const;
763 
764  void setDeviceInfo();
765 
766  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
767  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength,
768  bool useTxBuffer);
769 
770  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
771  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
772 
773  std::string m_DeviceName;
774  DpdkPMDType m_PMDType;
775  std::string m_PMDName;
776  std::string m_PciAddress;
777 
778  DpdkDeviceConfiguration m_Config;
779 
780  int m_Id;
781  MacAddress m_MacAddress;
782  int16_t m_DeviceSocketId;
783  uint16_t m_DeviceMtu;
784  uint16_t m_MBufDataSize;
785  struct rte_mempool* m_MBufMempool;
786  struct rte_eth_dev_tx_buffer** m_TxBuffers;
787  uint64_t m_TxBufferDrainTsc;
788  uint64_t* m_TxBufferLastDrainTsc;
789  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
790  uint16_t m_TotalAvailableRxQueues;
791  uint16_t m_TotalAvailableTxQueues;
792  uint16_t m_NumOfRxQueuesOpened;
793  uint16_t m_NumOfTxQueuesOpened;
794  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
795  void* m_OnPacketsArriveUserCookie;
796  bool m_StopThread;
797 
798  bool m_WasOpened;
799 
800  // RSS key used by the NIC for load balancing the packets between cores
801  static uint8_t m_RSSKey[40];
802 
803  mutable DpdkDeviceStats m_PrevStats;
804  };
805 
806 } // namespace pcpp
807 
808 // GCOVR_EXCL_STOP
Definition: DpdkDevice.h:169
bool setFilter(std::string filterAsString)
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:427
int getAmountOfMbufsInUse() const
uint16_t getMtu() const
Definition: DpdkDevice.h:391
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
int getDeviceId() const
Definition: DpdkDevice.h:354
uint16_t receivePackets(Packet **packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const
bool open() override
Definition: DpdkDevice.h:727
bool sendPacket(MBufRawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
std::string getPciAddress() const
Definition: DpdkDevice.h:385
bool sendPacket(Packet &packet, uint16_t txQueueId=0, bool useTxBuffer=false)
uint64_t getConfiguredRssHashFunction() const
bool getLinkStatus(LinkStatus &linkStatus) const
MacAddress getMacAddress() const
Definition: DpdkDevice.h:365
int getAmountOfFreeMbufs() const
void close() override
Close the DpdkDevice. When device is closed it's not possible work with it.
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()
Clear device statistics.
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:372
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:415
DpdkRssHashFunction
Definition: DpdkDevice.h:177
@ RSS_NVGRE
NVGRE protocol based flow.
Definition: DpdkDevice.h:219
@ RSS_IPV4
IPv4 based flow.
Definition: DpdkDevice.h:181
@ RSS_ALL_SUPPORTED
All RSS functions supported by the device.
Definition: DpdkDevice.h:221
@ RSS_NONE
No RSS.
Definition: DpdkDevice.h:179
@ RSS_PORT
Consider device port number as a flow differentiator.
Definition: DpdkDevice.h:213
@ RSS_IPV6_TCP_EX
IPv6 + TCP Ex based flow.
Definition: DpdkDevice.h:209
@ RSS_GENEVE
GENEVE protocol based flow.
Definition: DpdkDevice.h:217
@ RSS_NONFRAG_IPV4_OTHER
Non-fragmented IPv4 + non TCP/UDP/SCTP flow.
Definition: DpdkDevice.h:191
@ RSS_FRAG_IPV6
Fragmented IPv6 based flow.
Definition: DpdkDevice.h:195
@ RSS_IPV6_EX
IPv6 Ex based flow.
Definition: DpdkDevice.h:207
@ RSS_IPV6_UDP_EX
IPv6 + UDP Ex based flow.
Definition: DpdkDevice.h:211
@ RSS_NONFRAG_IPV6_UDP
Non-fragmented IPv6 + UDP flow.
Definition: DpdkDevice.h:199
@ RSS_NONFRAG_IPV4_SCTP
Non-fragmented IPv4 + SCTP flow.
Definition: DpdkDevice.h:189
@ RSS_NONFRAG_IPV4_UDP
Non-fragmented IPv4 + UDP flow.
Definition: DpdkDevice.h:187
@ RSS_NONFRAG_IPV6_OTHER
Non-fragmented IPv6 + non TCP/UDP/SCTP flow.
Definition: DpdkDevice.h:203
@ RSS_NONFRAG_IPV6_TCP
Non-fragmented IPv6 + TCP flow.
Definition: DpdkDevice.h:197
@ RSS_L2_PAYLOAD
L2 payload based flow.
Definition: DpdkDevice.h:205
@ RSS_FRAG_IPV4
Fragmented IPv4 based flow.
Definition: DpdkDevice.h:183
@ RSS_DEFAULT
A default set of RSS functions supported by the device.
Definition: DpdkDevice.h:223
@ RSS_IPV6
IPv6 based flow.
Definition: DpdkDevice.h:193
@ RSS_NONFRAG_IPV6_SCTP
Non-fragmented IPv6 + SCTP flow.
Definition: DpdkDevice.h:201
@ RSS_NONFRAG_IPV4_TCP
Non-fragmented IPv4 + TCP flow.
Definition: DpdkDevice.h:185
@ RSS_VXLAN
VXLAN protocol based flow.
Definition: DpdkDevice.h:215
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:421
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:433
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:379
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:359
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
Definition: DpdkDeviceList.h:58
Definition: PcapFilter.h:134
Definition: Device.h:20
Definition: MBufRawPacket.h:45
Definition: MacAddress.h:21
Definition: Packet.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:259
The main namespace for the PcapPlusPlus lib.
DpdkPMDType
Definition: DpdkDevice.h:79
@ PMD_IGB
Intel 1GbE PMD.
Definition: DpdkDevice.h:88
@ PMD_MLX4
Mellanox ConnectX-3, ConnectX-3 Pro PMD.
Definition: DpdkDevice.h:104
@ PMD_RING
ring-based (memory) PMD
Definition: DpdkDevice.h:110
@ PMD_IXGBEVF
Intel 10GbE virtual function PMD.
Definition: DpdkDevice.h:102
@ PMD_BOND
Definition: DpdkDevice.h:84
@ PMD_AF_PACKET
AF_PACKET PMD.
Definition: DpdkDevice.h:118
@ PMD_I40EVF
Intel 40GbE virtual function PMD.
Definition: DpdkDevice.h:98
@ PMD_UNKNOWN
Unknown PMD type.
Definition: DpdkDevice.h:81
@ PMD_I40E
Intel 40GbE PMD.
Definition: DpdkDevice.h:96
@ PMD_E1000EM
Intel E1000 PMD.
Definition: DpdkDevice.h:86
@ PMD_PCAP
pcap file PMD
Definition: DpdkDevice.h:108
@ PMD_FM10K
Intel fm10k PMD.
Definition: DpdkDevice.h:94
@ PMD_XENVIRT
Xen Project PMD.
Definition: DpdkDevice.h:116
@ PMD_IXGBE
Intel 10GbE PMD.
Definition: DpdkDevice.h:100
@ PMD_VMXNET3
VMWare VMXNET3 PMD.
Definition: DpdkDevice.h:114
@ PMD_ENIC
Cisco enic (UCS Virtual Interface Card) PMD.
Definition: DpdkDevice.h:92
@ PMD_IGBVF
Intel 1GbE virtual function PMD.
Definition: DpdkDevice.h:90
@ PMD_VIRTIO
VirtIO PMD.
Definition: DpdkDevice.h:112
@ PMD_NULL
Null PMD.
Definition: DpdkDevice.h:106
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:129
Definition: DpdkDevice.h:230
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:243
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:276
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:239
uint8_t * rssKey
Definition: DpdkDevice.h:250
uint8_t rssKeyLength
Definition: DpdkDevice.h:254
uint64_t rssHashFunction
Definition: DpdkDevice.h:260
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:234
Definition: DpdkDevice.h:329
uint64_t rxMbufAlocFailed
Total number of RX mbuf allocation failures.
Definition: DpdkDevice.h:348
uint8_t devId
DpdkDevice ID.
Definition: DpdkDevice.h:331
RxTxStats aggregatedRxStats
RX statistics, aggregated for all RX queues.
Definition: DpdkDevice.h:339
timespec timestamp
The timestamp of when the stats were written.
Definition: DpdkDevice.h:333
uint64_t rxPacketsDroppedByHW
Definition: DpdkDevice.h:344
RxTxStats txStats[16]
TX statistics per TX queue.
Definition: DpdkDevice.h:337
RxTxStats aggregatedTxStats
TX statistics, aggregated for all TX queues.
Definition: DpdkDevice.h:341
RxTxStats rxStats[16]
RX statistics per RX queue.
Definition: DpdkDevice.h:335
uint64_t rxErroneousPackets
Total number of erroneous packets.
Definition: DpdkDevice.h:346
Definition: DpdkDevice.h:315
uint64_t packetsPerSec
Packets per second.
Definition: DpdkDevice.h:321
uint64_t bytesPerSec
Bytes per second.
Definition: DpdkDevice.h:323
uint64_t packets
Total number of packets.
Definition: DpdkDevice.h:317
uint64_t bytes
Total number of successfully received bytes.
Definition: DpdkDevice.h:319