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 
408  void 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:169
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: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:726
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
void 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:414
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: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: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:27
Definition: PointerVector.h:50
Definition: RawPacket.h:269
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