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 <MacAddress.h>
6 #include <SystemUtils.h>
7 #include <RawPacket.h>
8 #include <PcapLiveDevice.h>
9 
55 struct rte_mbuf;
56 struct rte_mempool;
57 struct rte_eth_conf;
58 
63 namespace pcpp
64 {
65 
66  class DpdkDeviceList;
67  class DpdkDevice;
68 
72  enum DpdkPMDType {
111  };
112 
113  class DpdkDevice;
114 
135  class MBufRawPacket : public RawPacket
136  {
137  friend class DpdkDevice;
138 
139  private:
140  struct rte_mbuf* m_MBuf;
141  DpdkDevice* m_Device;
142 
143  void setMBuf(struct rte_mbuf* mBuf, timeval timestamp);
144  public:
145 
151  MBufRawPacket() : RawPacket(), m_MBuf(NULL), m_Device(NULL) { m_DeleteRawDataAtDestructor = false; }
152 
156  virtual ~MBufRawPacket();
157 
164  MBufRawPacket(const MBufRawPacket& other);
165 
173  bool init(DpdkDevice* device);
174 
175  // overridden methods
176 
183  MBufRawPacket& operator=(const MBufRawPacket& other);
184 
196  bool setRawData(const uint8_t* pRawData, int rawDataLen, timeval timestamp);
197 
201  void clear();
202 
209  void appendData(const uint8_t* dataToAppend, size_t dataToAppendLen);
210 
219  void insertData(int atIndex, const uint8_t* dataToInsert, size_t dataToInsertLen);
220 
229  bool removeData(int atIndex, size_t numOfBytesToRemove);
230 
237  bool reallocateData(size_t newBufferLength);
238  };
239 
249  typedef void (*OnDpdkPacketsArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice* device, void* userCookie);
250 
256  {
257  public:
261  PciAddress() { domain = 0; bus = 0; devid = 0; function = 0; }
262 
270  PciAddress(uint16_t domain, uint8_t bus, uint8_t devid, uint8_t function)
271  {
272  this->domain = domain;
273  this->bus = bus;
274  this->devid = devid;
275  this->function = function;
276  }
277 
279  uint16_t domain;
281  uint8_t bus;
283  uint8_t devid;
285  uint8_t function;
286 
290  std::string toString()
291  {
292  char pciString[15];
293  snprintf(pciString, 15, "%04x:%02x:%02x.%x", domain, bus, devid, function);
294  return std::string(pciString);
295  }
296 
300  bool operator==(const PciAddress &other) const
301  {
302  return (domain == other.domain && bus == other.bus && devid == other.devid && function == other.function);
303  }
304  };
305 
306 
340  class DpdkDevice : public IPcapDevice
341  {
342  friend class DpdkDeviceList;
343  friend class MBufRawPacket;
344  public:
345 
352  {
358 
364 
372  DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber = 128, uint16_t transmitDescriptorsNumber = 512)
373  {
374  this->receiveDescriptorsNumber = receiveDescriptorsNumber;
375  this->transmitDescriptorsNumber = transmitDescriptorsNumber;
376  }
377  };
378 
383  struct LinkStatus
384  {
387  {
392  };
393 
395  bool linkUp;
400  };
401 
402  virtual ~DpdkDevice() {}
403 
407  inline int getDeviceId() { return m_Id; }
411  inline std::string getDeviceName() { return std::string(m_DeviceName); }
412 
416  inline MacAddress getMacAddress() { return m_MacAddress; }
417 
422  inline std::string getPMDName() { return m_PMDName; }
423 
428  inline DpdkPMDType getPMDType() { return m_PMDType; }
429 
433  inline PciAddress getPciAddress() { return m_PciAddress; }
434 
438  inline uint16_t getMtu() { return m_DeviceMtu; }
439 
445  bool setMtu(uint16_t newMtu);
446 
450  bool isVirtual();
451 
456  void getLinkStatus(LinkStatus& linkStatus);
457 
461  uint32_t getCurrentCoreId();
462 
466  uint16_t getNumOfOpenedRxQueues() { return m_NumOfRxQueuesOpened; }
467 
471  uint16_t getNumOfOpenedTxQueues() { return m_NumOfTxQueuesOpened; }
472 
476  uint16_t getTotalNumOfRxQueues() { return m_TotalAvailableRxQueues; }
477 
481  uint16_t getTotalNumOfTxQueues() { return m_TotalAvailableTxQueues; }
482 
483 
492  bool receivePackets(RawPacketVector& rawPacketsArr, uint16_t rxQueueId);
493 
505  bool receivePackets(MBufRawPacket** rawPacketsArr, int& rawPacketArrLength, uint16_t rxQueueId);
506 
518  bool receivePackets(Packet** packetsArr, int& packetsArrLength, uint16_t rxQueueId);
519 
538  int sendPackets(const RawPacket* rawPacketsArr, int arrLength, uint16_t txQueueId = 0);
539 
548  int sendPackets(const Packet** packetsArr, int arrLength, uint16_t txQueueId = 0);
549 
557  int sendPackets(const RawPacketVector& rawPacketsVec, uint16_t txQueueId = 0);
558 
569  bool sendPacket(const uint8_t* packetData, int packetDataLength, uint16_t txQueueId = 0);
570 
580  bool sendPacket(const RawPacket& rawPacket, uint16_t txQueueId = 0);
581 
591  bool sendPacket(const Packet& packet, uint16_t txQueueId = 0);
592 
597  bool setFilter(GeneralFilter& filter);
598 
603  bool setFilter(std::string filterAsString);
604 
615  bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration& config = DpdkDeviceConfiguration());
616 
632  bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie);
633 
649  bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask);
650 
655  void stopCapture();
656 
660  int getAmountOfFreeMbufs();
661 
665  int getAmountOfMbufsInUse();
666 
667  //overridden methods
668 
675  bool open() { return openMultiQueues(1, 1); };
676 
680  void close();
681 
686  void getStatistics(pcap_stat& stats);
687 
688  private:
689 
690  struct DpdkCoreConfiguration
691  {
692  int RxQueueId;
693  bool IsCoreInUse;
694 
695  void clear() { RxQueueId = -1; IsCoreInUse = false; }
696 
697  DpdkCoreConfiguration() : RxQueueId(-1), IsCoreInUse(false) {}
698  };
699 
700  DpdkDevice(int port, uint32_t mBufPoolSize);
701  bool initMemPool(struct rte_mempool*& memPool, const char* mempoolName, uint32_t mBufPoolSize);
702 
703  bool configurePort(uint8_t numOfRxQueues, uint8_t numOfTxQueues);
704  bool initQueues(uint8_t numOfRxQueuesToInit, uint8_t numOfTxQueuesToInit);
705  bool startDevice();
706 
707  static int dpdkCaptureThreadMain(void *ptr);
708 
709  void clearCoreConfiguration();
710  bool initCoreConfigurationByCoreMask(CoreMask coreMask);
711  int getCoresInUseCount();
712 
713  void setDeviceInfo();
714 
715  typedef RawPacket* (*packetIterator)(void* packetStorage, int index);
716  int sendPacketsInner(uint16_t txQueueId, void* packetStorage, packetIterator iter, int arrLength);
717 
718  char m_DeviceName[30];
719  DpdkPMDType m_PMDType;
720  std::string m_PMDName;
721  PciAddress m_PciAddress;
722 
723  DpdkDeviceConfiguration m_Config;
724 
725  int m_Id;
726  MacAddress m_MacAddress;
727  uint16_t m_DeviceMtu;
728  struct rte_mempool* m_MBufMempool;
729  struct rte_mbuf* m_mBufArray[256];
730  DpdkCoreConfiguration m_CoreConfiguration[MAX_NUM_OF_CORES];
731  uint16_t m_TotalAvailableRxQueues;
732  uint16_t m_TotalAvailableTxQueues;
733  uint16_t m_NumOfRxQueuesOpened;
734  uint16_t m_NumOfTxQueuesOpened;
735  OnDpdkPacketsArriveCallback m_OnPacketsArriveCallback;
736  void* m_OnPacketsArriveUserCookie;
737  bool m_StopThread;
738 
739  bool m_WasOpened;
740 
741  // RSS key used by the NIC for load balancing the packets between cores
742  static uint8_t m_RSSKey[40];
743  };
744 
745 } // namespace pcpp
746 
747 #endif /* PCAPPP_DPDK_DEVICE */
Definition: PcapFilter.h:72
std::string toString()
Definition: DpdkDevice.h:290
Definition: DpdkDevice.h:80
Definition: DpdkDevice.h:74
Definition: DpdkDevice.h:389
Definition: DpdkDevice.h:340
Definition: RawPacket.h:220
void(* OnDpdkPacketsArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, uint8_t threadId, DpdkDevice *device, void *userCookie)
Definition: DpdkDevice.h:249
Definition: DpdkDevice.h:110
Definition: DpdkDevice.h:84
bool openMultiQueues(uint16_t numOfRxQueuesToOpen, uint16_t numOfTxQueuesToOpen, const DpdkDeviceConfiguration &config=DpdkDeviceConfiguration())
uint16_t getTotalNumOfRxQueues()
Definition: DpdkDevice.h:476
Definition: DpdkDevice.h:255
Definition: DpdkDevice.h:82
uint16_t getNumOfOpenedTxQueues()
Definition: DpdkDevice.h:471
uint16_t transmitDescriptorsNumber
Definition: DpdkDevice.h:363
bool linkUp
Definition: DpdkDevice.h:395
bool receivePackets(RawPacketVector &rawPacketsArr, uint16_t rxQueueId)
Definition: DpdkDevice.h:106
Definition: DpdkDevice.h:90
Definition: DpdkDevice.h:383
Definition: DpdkDevice.h:86
void getLinkStatus(LinkStatus &linkStatus)
Definition: PcapDevice.h:26
int linkSpeedMbps
Definition: DpdkDevice.h:397
Definition: DpdkDevice.h:108
Definition: DpdkDevice.h:135
bool open()
Definition: DpdkDevice.h:675
uint16_t getMtu()
Definition: DpdkDevice.h:438
bool operator==(const PciAddress &other) const
Definition: DpdkDevice.h:300
Definition: MacAddress.h:21
PciAddress getPciAddress()
Definition: DpdkDevice.h:433
std::string getPMDName()
Definition: DpdkDevice.h:422
DpdkPMDType
Definition: DpdkDevice.h:72
void insertData(int atIndex, const uint8_t *dataToInsert, size_t dataToInsertLen)
int getAmountOfMbufsInUse()
Definition: Packet.h:26
DpdkDeviceConfiguration(uint16_t receiveDescriptorsNumber=128, uint16_t transmitDescriptorsNumber=512)
Definition: DpdkDevice.h:372
Definition: DpdkDevice.h:102
Definition: DpdkDevice.h:78
uint16_t getNumOfOpenedRxQueues()
Definition: DpdkDevice.h:466
void getStatistics(pcap_stat &stats)
uint32_t getCurrentCoreId()
uint16_t getTotalNumOfTxQueues()
Definition: DpdkDevice.h:481
bool startCaptureSingleThread(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie)
Definition: DpdkDeviceList.h:70
Definition: DpdkDevice.h:100
uint16_t domain
Definition: DpdkDevice.h:279
uint16_t receiveDescriptorsNumber
Definition: DpdkDevice.h:357
int sendPackets(const RawPacket *rawPacketsArr, int arrLength, uint16_t txQueueId=0)
bool setMtu(uint16_t newMtu)
Definition: DpdkDevice.h:88
Definition: DpdkDevice.h:104
std::string getDeviceName()
Definition: DpdkDevice.h:411
bool startCaptureMultiThreads(OnDpdkPacketsArriveCallback onPacketsArrive, void *onPacketsArriveUserCookie, CoreMask coreMask)
uint8_t bus
Definition: DpdkDevice.h:281
MBufRawPacket & operator=(const MBufRawPacket &other)
Definition: DpdkDevice.h:76
bool removeData(int atIndex, size_t numOfBytesToRemove)
LinkDuplex
Definition: DpdkDevice.h:386
Definition: DpdkDevice.h:94
bool init(DpdkDevice *device)
PciAddress()
Definition: DpdkDevice.h:261
bool setFilter(GeneralFilter &filter)
int getAmountOfFreeMbufs()
Definition: DpdkDevice.h:96
virtual ~MBufRawPacket()
bool reallocateData(size_t newBufferLength)
int getDeviceId()
Definition: DpdkDevice.h:407
PciAddress(uint16_t domain, uint8_t bus, uint8_t devid, uint8_t function)
Definition: DpdkDevice.h:270
Definition: DpdkDevice.h:391
bool sendPacket(const uint8_t *packetData, int packetDataLength, uint16_t txQueueId=0)
The main namespace for the PcapPlusPlus lib.
DpdkPMDType getPMDType()
Definition: DpdkDevice.h:428
LinkDuplex linkDuplex
Definition: DpdkDevice.h:399
Definition: DpdkDevice.h:92
Definition: DpdkDevice.h:351
MacAddress getMacAddress()
Definition: DpdkDevice.h:416
bool setRawData(const uint8_t *pRawData, int rawDataLen, timeval timestamp)
Definition: PointerVector.h:24
MBufRawPacket()
Definition: DpdkDevice.h:151
void appendData(const uint8_t *dataToAppend, size_t dataToAppendLen)
Definition: DpdkDevice.h:98
uint8_t devid
Definition: DpdkDevice.h:283
uint8_t function
Definition: DpdkDevice.h:285