PcapPlusPlus  Next
DpdkDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 // GCOVR_EXCL_START
4 
5 #include <time.h>
6 #include <vector>
7 #include "MacAddress.h"
8 #include "SystemUtils.h"
9 #include "Device.h"
10 #include "MBufRawPacket.h"
11 
57 
58 struct rte_mbuf;
59 struct rte_mempool;
60 struct rte_eth_conf;
61 struct rte_eth_dev_tx_buffer;
62 
65 namespace pcpp
66 {
67 
68 #define DPDK_MAX_RX_QUEUES 16
69 #define DPDK_MAX_TX_QUEUES 16
70 #define PCPP_RSS_HASH_MAGIC_NUMBER 0x123456
71 
72  class DpdkDeviceList;
73  class DpdkDevice;
74 
78  {
118  };
119 
128  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId,
129  DpdkDevice* device, void* userCookie);
130 
167  class DpdkDevice : public IDevice
168  {
169  friend class DpdkDeviceList;
170  friend class MBufRawPacket;
171 
172  public:
176  {
178  RSS_NONE = 0,
180  RSS_IPV4 = 0x1,
192  RSS_IPV6 = 0x40,
204  RSS_L2_PAYLOAD = 0x1000,
206  RSS_IPV6_EX = 0x2000,
208  RSS_IPV6_TCP_EX = 0x4000,
210  RSS_IPV6_UDP_EX = 0x8000,
212  RSS_PORT = 0x10000,
214  RSS_VXLAN = 0x20000,
216  RSS_GENEVE = 0x40000,
218  RSS_NVGRE = 0x80000,
222  RSS_DEFAULT = PCPP_RSS_HASH_MAGIC_NUMBER
223  };
224 
229  {
234 
239 
243 
249  uint8_t* rssKey;
250 
253  uint8_t rssKeyLength;
254 
259  uint64_t rssHashFunction;
260 
276  uint16_t transmitDescriptorsNumber = 512,
277  uint16_t flushTxBufferTimeout = 100,
278  uint64_t rssHashFunction = RSS_DEFAULT,
279  uint8_t* rssKey = DpdkDevice::m_RSSKey, uint8_t rssKeyLength = 40)
280  {
281  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
282  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
283  this->flushTxBufferTimeout = flushTxBufferTimeout;
284  this->rssKey = rssKey;
285  this->rssKeyLength = rssKeyLength;
286  this->rssHashFunction = rssHashFunction;
287  }
288  };
289 
292  struct LinkStatus
293  {
296  {
301  };
302 
304  bool linkUp;
309  };
310 
313  struct RxTxStats
314  {
316  uint64_t packets;
318  uint64_t bytes;
320  uint64_t packetsPerSec;
322  uint64_t bytesPerSec;
323  };
324 
328  {
330  uint8_t devId;
332  timespec timestamp;
334  RxTxStats rxStats[DPDK_MAX_RX_QUEUES];
336  RxTxStats txStats[DPDK_MAX_RX_QUEUES];
348  };
349 
350  virtual ~DpdkDevice();
351 
353  int getDeviceId() const
354  {
355  return m_Id;
356  }
358  std::string getDeviceName() const
359  {
360  return m_DeviceName;
361  }
362 
365  {
366  return m_MacAddress;
367  }
368 
371  std::string getPMDName() const
372  {
373  return m_PMDName;
374  }
375 
379  {
380  return m_PMDType;
381  }
382 
384  std::string getPciAddress() const
385  {
386  return m_PciAddress;
387  }
388 
390  uint16_t getMtu() const
391  {
392  return m_DeviceMtu;
393  }
394 
399  bool setMtu(uint16_t newMtu);
400 
403  bool isVirtual() const;
404 
408  bool getLinkStatus(LinkStatus& linkStatus) const;
409 
411  uint32_t getCurrentCoreId() const;
412 
414  uint16_t getNumOfOpenedRxQueues() const
415  {
416  return m_NumOfRxQueuesOpened;
417  }
418 
420  uint16_t getNumOfOpenedTxQueues() const
421  {
422  return m_NumOfTxQueuesOpened;
423  }
424 
426  uint16_t getTotalNumOfRxQueues() const
427  {
428  return m_TotalAvailableRxQueues;
429  }
430 
432  uint16_t getTotalNumOfTxQueues() const
433  {
434  return m_TotalAvailableTxQueues;
435  }
436 
442  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr, uint16_t rxQueueId) const;
443 
456  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength, uint16_t rxQueueId) const;
457 
467  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const;
468 
490  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength, uint16_t txQueueId = 0,
491  bool useTxBuffer = false);
492 
515  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId = 0, bool useTxBuffer = false);
516 
534  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
535 
558  uint16_t sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0, bool useTxBuffer = false);
559 
574  bool sendPacket(RawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
575 
587  bool sendPacket(MBufRawPacket& rawPacket, uint16_t txQueueId = 0, bool useTxBuffer = false);
588 
603  bool sendPacket(Packet& packet, uint16_t txQueueId = 0, bool useTxBuffer = false);
604 
608  bool setFilter(GeneralFilter& filter);
609 
613  bool setFilter(std::string filterAsString);
614 
626  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen,
628 
645  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
646 
663  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie,
664  CoreMask coreMask);
665 
668  void stopCapture();
669 
671  int getAmountOfFreeMbufs() const;
672 
675 
678  void getStatistics(DpdkDeviceStats& stats) const;
679 
682 
694  uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired = false, uint16_t txQueueId = 0);
695 
700 
705  bool isDeviceSupportRssHashFunction(uint64_t rssHFMask) const;
706 
710 
713 
717  std::vector<std::string> rssHashFunctionMaskToString(uint64_t rssHFMask) const;
718 
719  // overridden methods
720 
726  bool open() override
727  {
728  return openMultiQueues(1, 1);
729  };
730 
732  void close() override;
733 
734  private:
735  struct DpdkCoreConfiguration
736  {
737  int RxQueueId;
738  bool IsCoreInUse;
739 
740  void clear()
741  {
742  RxQueueId = -1;
743  IsCoreInUse = false;
744  }
745 
746  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false)
747  {}
748  };
749 
750  DpdkDevice(int port, uint32_t mBufPoolSize, uint16_t mMbufDataSize);
751  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
752 
753  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
754  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
755  bool startDevice();
756 
757  static int dpdkCaptureThreadMain(void* ptr);
758 
759  void clearCoreConfiguration();
760  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
761  int getCoresInUseCount() const;
762 
763  void setDeviceInfo();
764 
765  typedef rte_mbuf* (*PacketIterator)(void* packetStorage, int index);
766  uint16_t sendPacketsInner(uint16_t txQueueId, void* packetStorage, PacketIterator iter, int arrLength,
767  bool useTxBuffer);
768 
769  uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
770  uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
771 
772  std::string m_DeviceName;
773  DpdkPMDType m_PMDType;
774  std::string m_PMDName;
775  std::string m_PciAddress;
776 
777  DpdkDeviceConfiguration m_Config;
778 
779  int m_Id;
780  MacAddress m_MacAddress;
781  int16_t m_DeviceSocketId;
782  uint16_t m_DeviceMtu;
783  uint16_t m_MBufDataSize;
784  struct rte_mempool* m_MBufMempool;
785  struct rte_eth_dev_tx_buffer** m_TxBuffers;
786  uint64_t m_TxBufferDrainTsc;
787  uint64_t* m_TxBufferLastDrainTsc;
788  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
789  uint16_t m_TotalAvailableRxQueues;
790  uint16_t m_TotalAvailableTxQueues;
791  uint16_t m_NumOfRxQueuesOpened;
792  uint16_t m_NumOfTxQueuesOpened;
793  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
794  void* m_OnPacketsArriveUserCookie;
795  bool m_StopThread;
796 
797  bool m_WasOpened;
798 
799  // RSS key used by the NIC for load balancing the packets between cores
800  static uint8_t m_RSSKey[40];
801 
802  mutable DpdkDeviceStats m_PrevStats;
803  };
804 
805 } // namespace pcpp
806 
807 // GCOVR_EXCL_STOP
Definition: DpdkDevice.h:168
bool setFilter(std::string filterAsString)
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
uint16_t getTotalNumOfRxQueues() const
Definition: DpdkDevice.h:426
int getAmountOfMbufsInUse() const
uint16_t getMtu() const
Definition: DpdkDevice.h:390
bool sendPacket(RawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
int getDeviceId() const
Definition: DpdkDevice.h:353
uint16_t receivePackets(Packet **packetsArr, uint16_t packetsArrLength, uint16_t rxQueueId) const
bool open() override
Definition: DpdkDevice.h:726
bool sendPacket(MBufRawPacket &rawPacket, uint16_t txQueueId=0, bool useTxBuffer=false)
std::string getPciAddress() const
Definition: DpdkDevice.h:384
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:364
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:371
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:414
DpdkRssHashFunction
Definition: DpdkDevice.h:176
@ RSS_NVGRE
NVGRE protocol based flow.
Definition: DpdkDevice.h:218
@ RSS_IPV4
IPv4 based flow.
Definition: DpdkDevice.h:180
@ RSS_ALL_SUPPORTED
All RSS functions supported by the device.
Definition: DpdkDevice.h:220
@ RSS_NONE
No RSS.
Definition: DpdkDevice.h:178
@ RSS_PORT
Consider device port number as a flow differentiator.
Definition: DpdkDevice.h:212
@ RSS_IPV6_TCP_EX
IPv6 + TCP Ex based flow.
Definition: DpdkDevice.h:208
@ RSS_GENEVE
GENEVE protocol based flow.
Definition: DpdkDevice.h:216
@ RSS_NONFRAG_IPV4_OTHER
Non-fragmented IPv4 + non TCP/UDP/SCTP flow.
Definition: DpdkDevice.h:190
@ RSS_FRAG_IPV6
Fragmented IPv6 based flow.
Definition: DpdkDevice.h:194
@ RSS_IPV6_EX
IPv6 Ex based flow.
Definition: DpdkDevice.h:206
@ RSS_IPV6_UDP_EX
IPv6 + UDP Ex based flow.
Definition: DpdkDevice.h:210
@ RSS_NONFRAG_IPV6_UDP
Non-fragmented IPv6 + UDP flow.
Definition: DpdkDevice.h:198
@ RSS_NONFRAG_IPV4_SCTP
Non-fragmented IPv4 + SCTP flow.
Definition: DpdkDevice.h:188
@ RSS_NONFRAG_IPV4_UDP
Non-fragmented IPv4 + UDP flow.
Definition: DpdkDevice.h:186
@ RSS_NONFRAG_IPV6_OTHER
Non-fragmented IPv6 + non TCP/UDP/SCTP flow.
Definition: DpdkDevice.h:202
@ RSS_NONFRAG_IPV6_TCP
Non-fragmented IPv6 + TCP flow.
Definition: DpdkDevice.h:196
@ RSS_L2_PAYLOAD
L2 payload based flow.
Definition: DpdkDevice.h:204
@ RSS_FRAG_IPV4
Fragmented IPv4 based flow.
Definition: DpdkDevice.h:182
@ RSS_DEFAULT
A default set of RSS functions supported by the device.
Definition: DpdkDevice.h:222
@ RSS_IPV6
IPv6 based flow.
Definition: DpdkDevice.h:192
@ RSS_NONFRAG_IPV6_SCTP
Non-fragmented IPv6 + SCTP flow.
Definition: DpdkDevice.h:200
@ RSS_NONFRAG_IPV4_TCP
Non-fragmented IPv4 + TCP flow.
Definition: DpdkDevice.h:184
@ RSS_VXLAN
VXLAN protocol based flow.
Definition: DpdkDevice.h:214
uint16_t flushTxBuffer(bool flushOnlyIfTimeoutExpired=false, uint16_t txQueueId=0)
uint16_t getNumOfOpenedTxQueues() const
Definition: DpdkDevice.h:420
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:432
bool isDeviceSupportRssHashFunction(DpdkRssHashFunction rssHF) const
DpdkPMDType getPMDType() const
Definition: DpdkDevice.h:378
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:358
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
Definition: DpdkDeviceList.h:59
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:78
@ PMD_IGB
Intel 1GbE PMD.
Definition: DpdkDevice.h:87
@ PMD_MLX4
Mellanox ConnectX-3, ConnectX-3 Pro PMD.
Definition: DpdkDevice.h:103
@ PMD_RING
ring-based (memory) PMD
Definition: DpdkDevice.h:109
@ PMD_IXGBEVF
Intel 10GbE virtual function PMD.
Definition: DpdkDevice.h:101
@ PMD_BOND
Definition: DpdkDevice.h:83
@ PMD_AF_PACKET
AF_PACKET PMD.
Definition: DpdkDevice.h:117
@ PMD_I40EVF
Intel 40GbE virtual function PMD.
Definition: DpdkDevice.h:97
@ PMD_UNKNOWN
Unknown PMD type.
Definition: DpdkDevice.h:80
@ PMD_I40E
Intel 40GbE PMD.
Definition: DpdkDevice.h:95
@ PMD_E1000EM
Intel E1000 PMD.
Definition: DpdkDevice.h:85
@ PMD_PCAP
pcap file PMD
Definition: DpdkDevice.h:107
@ PMD_FM10K
Intel fm10k PMD.
Definition: DpdkDevice.h:93
@ PMD_XENVIRT
Xen Project PMD.
Definition: DpdkDevice.h:115
@ PMD_IXGBE
Intel 10GbE PMD.
Definition: DpdkDevice.h:99
@ PMD_VMXNET3
VMWare VMXNET3 PMD.
Definition: DpdkDevice.h:113
@ PMD_ENIC
Cisco enic (UCS Virtual Interface Card) PMD.
Definition: DpdkDevice.h:91
@ PMD_IGBVF
Intel 1GbE virtual function PMD.
Definition: DpdkDevice.h:89
@ PMD_VIRTIO
VirtIO PMD.
Definition: DpdkDevice.h:111
@ PMD_NULL
Null PMD.
Definition: DpdkDevice.h:105
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:128
Definition: DpdkDevice.h:229
uint16_t flushTxBufferTimeout
Definition: DpdkDevice.h:242
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:275
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:238
uint8_t * rssKey
Definition: DpdkDevice.h:249
uint8_t rssKeyLength
Definition: DpdkDevice.h:253
uint64_t rssHashFunction
Definition: DpdkDevice.h:259
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:233
Definition: DpdkDevice.h:328
uint64_t rxMbufAlocFailed
Total number of RX mbuf allocation failures.
Definition: DpdkDevice.h:347
uint8_t devId
DpdkDevice ID.
Definition: DpdkDevice.h:330
RxTxStats aggregatedRxStats
RX statistics, aggregated for all RX queues.
Definition: DpdkDevice.h:338
timespec timestamp
The timestamp of when the stats were written.
Definition: DpdkDevice.h:332
uint64_t rxPacketsDroppedByHW
Definition: DpdkDevice.h:343
RxTxStats txStats[16]
TX statistics per TX queue.
Definition: DpdkDevice.h:336
RxTxStats aggregatedTxStats
TX statistics, aggregated for all TX queues.
Definition: DpdkDevice.h:340
RxTxStats rxStats[16]
RX statistics per RX queue.
Definition: DpdkDevice.h:334
uint64_t rxErroneousPackets
Total number of erroneous packets.
Definition: DpdkDevice.h:345
Definition: DpdkDevice.h:314
uint64_t packetsPerSec
Packets per second.
Definition: DpdkDevice.h:320
uint64_t bytesPerSec
Bytes per second.
Definition: DpdkDevice.h:322
uint64_t packets
Total number of packets.
Definition: DpdkDevice.h:316
uint64_t bytes
Total number of successfully received bytes.
Definition: DpdkDevice.h:318