PcapPlusPlus  23.09
KniDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_KNI_DEVICE
2 #define PCAPPP_KNI_DEVICE
3 
4 // GCOVR_EXCL_START
5 
6 #include <string>
7 #include <atomic>
8 
9 #include "Device.h"
10 #include "MacAddress.h"
11 #include "MBufRawPacket.h"
12 #include "LinuxNicInformationSocket.h"
13 
82 struct rte_kni;
83 
88 namespace pcpp
89 {
90  class KniDevice;
91  class KniDeviceList;
92 
96  typedef bool (*OnKniPacketArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, KniDevice* device, void* userCookie);
97 
123  class KniDevice : public IDevice
124  {
125  friend class KniDeviceList;
126  friend class MBufRawPacket;
127  public:
132  {
141  };
146  {
151  };
156  {
161  };
162 
175  {
180  int (*change_mtu)(uint16_t port_id, unsigned int new_mtu);
185  int (*config_network_if)(uint16_t port_id, uint8_t if_up);
193  int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]);
201  int (*config_promiscusity)(uint16_t port_id, uint8_t to_on);
202  };
203 
212  {
217  int (*change_mtu)(uint8_t port_id, unsigned int new_mtu);
222  int (*config_network_if)(uint8_t port_id, uint8_t if_up);
223  };
224 
232  {
238  std::string name;
239  union
240  {
241  KniIoctlCallbacks* callbacks;
242  KniOldIoctlCallbacks* oldCallbacks;
243  };
257  uint16_t portId;
259  uint16_t mtu;
269  uint32_t kthreadCoreId;
270  };
271 
272  private:
274  KniDevice(const KniDeviceConfiguration& conf, size_t mempoolSize, int unique);
276  KniDevice(const KniDevice&);
278  KniDevice& operator=(const KniDevice&);
280  ~KniDevice();
281 
282  public:
283  /* Information getters */
284 
288  inline bool isInitialized() const { return !(m_Device == NULL || m_MBufMempool == NULL); }
292  inline std::string getName() const { return std::string(m_DeviceInfo.name); }
296  inline uint16_t getPort() const { return m_DeviceInfo.portId; }
323  uint16_t getMtu(KniInfoState state = INFO_CACHED);
333 
334  /* Information setters */
335 
347  bool setLinkState(KniLinkState state);
357  bool setMacAddress(MacAddress mac);
368  bool setMtu(uint16_t mtu);
393 
394  /* Requests */
395 
411  bool handleRequests();
424  bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds = 0);
434 
435  /* Packet receive */
436 
442  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr);
455  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength);
464  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength);
465 
466  /* Packet send */
467 
482  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength);
498  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength);
509  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec);
524  uint16_t sendPackets(RawPacketVector& rawPacketsVec);
534  bool sendPacket(RawPacket& rawPacket);
541  bool sendPacket(MBufRawPacket& rawPacket);
551  bool sendPacket(Packet& packet);
552 
553  /* Packet capture */
554 
572  bool startCapture(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie);
595  int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie, int timeout);
599  void stopCapture();
600 
601  /* Device control */
602 
608  bool open();
614  void close();
615 
616  private:
617  struct rte_kni* m_Device;
618  struct rte_mempool* m_MBufMempool;
619  struct KniDeviceInfo
620  {
622  KniLinkState link;
623  KniPromiscuousMode promisc;
624  uint16_t portId;
625  uint16_t mtu;
626  MacAddress mac;
627  std::string name;
628 
629  bool init(const KniDeviceConfiguration& conf);
630  } m_DeviceInfo;
631  struct KniThread;
632  struct KniCapturing
633  {
634  OnKniPacketArriveCallback callback;
635  void* userCookie;
636  KniThread* thread;
637 
638  static void runCapture(void* devicePointer, std::atomic<bool>& stopThread);
639  inline bool isRunning() const { return thread != NULL; }
640  void cleanup();
641  } m_Capturing;
642  struct KniRequests
643  {
644  long sleepS;
645  long sleepNs;
646  KniThread* thread;
647 
648  static void runRequests(void* devicePointer, std::atomic<bool>& stopThread);
649  void cleanup();
650  } m_Requests;
651  };
652 
653 } // namespace pcpp
654 
655 // GCOVR_EXCL_STOP
656 
657 #endif /* PCAPPP_KNI_DEVICE */
The main namespace for the PcapPlusPlus lib.
Definition: KniDevice.h:158
KniPromiscuousMode getPromiscuous(KniInfoState state=INFO_CACHED)
Obtains information about promiscuous mode of KNI device. If called with INFO_CACHED - returns cached...
int(* change_mtu)(uint16_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:180
Definition: KniDevice.h:123
void close()
Close the KNI device. When device is closed it&#39;s not possible to work with it. Stops asynchronous pac...
Definition: KniDevice.h:138
bool startCapture(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie)
Start capturing packets asynchronously on this KNI interface. Each time a burst of packets is capture...
bool setPromiscuous(KniPromiscuousMode mode)
Sets promiscuous mode of KNI device. Firstly the promiscuous mode information is updated as by call t...
KniInfoState
Definition: KniDevice.h:145
uint16_t getMtu(KniInfoState state=INFO_CACHED)
Obtains MTU of KNI device. If called with INFO_CACHED - returns cached data about MTU (SUPER FAST may...
Definition: KniDevice.h:150
Definition: Packet.h:26
bool bindKthread
Definition: KniDevice.h:267
bool isInitialized() const
Definition: KniDevice.h:288
Definition: PointerVector.h:24
MacAddress getMacAddress(KniInfoState state=INFO_CACHED)
Obtains MAC address of KNI device. If called with INFO_CACHED - returns cached data about MAC address...
Definition: RawPacket.h:254
Definition: Device.h:24
Definition: LinuxNicInformationSocket.h:26
uint32_t kthreadCoreId
Definition: KniDevice.h:269
Definition: MBufRawPacket.h:45
bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds=0)
Starts new thread (using pthread) to asynchronously handle KNI device requests. See description of ha...
int(* config_promiscusity)(uint16_t port_id, uint8_t to_on)
Definition: KniDevice.h:201
bool(* OnKniPacketArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, KniDevice *device, void *userCookie)
Definition: KniDevice.h:96
KniLinkState
Definition: KniDevice.h:131
New callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:174
KniLinkState getLinkState(KniInfoState state=INFO_CACHED)
Obtains link status of KNI device. If called with INFO_CACHED - returns cached data about link state ...
Old callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:211
MacAddress mac
Definition: KniDevice.h:250
int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie, int timeout)
Start capturing packets synchronously on this KNI interface in blocking mode. Blocking mode means tha...
int(* config_network_if)(uint16_t port_id, uint8_t if_up)
Definition: KniDevice.h:185
Definition: KniDevice.h:136
bool handleRequests()
Handle requests from Linux kernel synchronously in calling thread. When one of events which is needed...
Definition: KniDeviceList.h:24
KniLinkState updateLinkState(KniLinkState state)
Updates link state of KNI device. Unconditionally updates link state of KNI device via call to DPDK l...
bool setMacAddress(MacAddress mac)
Sets MAC address of KNI device. Unconditionally changes MAC of KNI device. If MAC is updated successf...
Definition: KniDevice.h:148
std::string name
Definition: KniDevice.h:238
Definition: KniDevice.h:160
KNI device initialization data. Used to create new KNI device. Usage of callbacks member or oldCallba...
Definition: KniDevice.h:231
bool setLinkState(KniLinkState state)
Sets link state of KNI device. Firstly the link information is updated as by call to getLinkState(INF...
std::string getName() const
Definition: KniDevice.h:292
bool setMtu(uint16_t mtu)
Sets MTU of KNI device. Unconditionally changes MTU of KNI device. If MTU is updated successfully the...
uint16_t portId
Definition: KniDevice.h:257
KniPromiscuousMode
Definition: KniDevice.h:155
uint16_t mtu
Definition: KniDevice.h:259
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr)
Receive raw packets from kernel.
Definition: KniDevice.h:140
void stopRequestHandlerThread()
Explicitly stops request thread for this device if it was running. See description of handleRequests(...
uint16_t getPort() const
Definition: KniDevice.h:296
Definition: KniDevice.h:134
Definition: MacAddress.h:28
bool sendPacket(RawPacket &rawPacket)
Send a raw packet to kernel. Please notice that if the raw packet isn&#39;t of type MBufRawPacket, a new temp MBufRawPacket will be created and the data will be copied to it. This is necessary to allocate an mbuf which will store the data to be sent. If performance is a critical factor please make sure you send a raw packet of type MBufRawPacket. Please also notice that the mbuf used or allocated in this method isn&#39;t freed by this method, it will be transparently freed by DPDK.
int(* config_mac_address)(uint16_t port_id, uint8_t mac_addr[])
Definition: KniDevice.h:193
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength)
Send an array of MBufRawPacket to kernel. Please notice the following: