PcapPlusPlus  Next
KniDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 // GCOVR_EXCL_START
4 
5 #include <string>
6 #include <atomic>
7 
8 #include "Device.h"
9 #include "MacAddress.h"
10 #include "MBufRawPacket.h"
11 #include "LinuxNicInformationSocket.h"
12 
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,
97  void* userCookie);
98 
124  class KniDevice : public IDevice
125  {
126  friend class KniDeviceList;
127  friend class MBufRawPacket;
128 
129  public:
134  {
142  LINK_UP = 1
143  };
148  {
152  INFO_RENEW = 1
153  };
158  {
162  PROMISC_ENABLE = 1
163  };
164 
177  {
182  int (*change_mtu)(uint16_t port_id, unsigned int new_mtu);
187  int (*config_network_if)(uint16_t port_id, uint8_t if_up);
195  int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]);
203  int (*config_promiscusity)(uint16_t port_id, uint8_t to_on);
204  };
205 
214  {
219  int (*change_mtu)(uint8_t port_id, unsigned int new_mtu);
224  int (*config_network_if)(uint8_t port_id, uint8_t if_up);
225  };
226 
234  {
240  std::string name;
241  union {
242  KniIoctlCallbacks* callbacks;
243  KniOldIoctlCallbacks* oldCallbacks;
244  };
258  uint16_t portId;
260  uint16_t mtu;
270  uint32_t kthreadCoreId;
271  };
272 
273  private:
275  KniDevice(const KniDeviceConfiguration& conf, size_t mempoolSize, int unique);
277  KniDevice(const KniDevice&);
279  KniDevice& operator=(const KniDevice&);
281  ~KniDevice();
282 
283  public:
284  /* Information getters */
285 
289  inline bool isInitialized() const
290  {
291  return !(m_Device == nullptr || m_MBufMempool == nullptr);
292  }
296  inline std::string getName() const
297  {
298  return std::string(m_DeviceInfo.name);
299  }
303  inline uint16_t getPort() const
304  {
305  return m_DeviceInfo.portId;
306  }
333  uint16_t getMtu(KniInfoState state = INFO_CACHED);
343 
344  /* Information setters */
345 
367  bool setMacAddress(const MacAddress& mac);
378  bool setMtu(uint16_t mtu);
403 
404  /* Requests */
405 
436  bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds = 0);
446 
447  /* Packet receive */
448 
455  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr);
470  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength);
481  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength);
482 
483  /* Packet send */
484 
499  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength);
515  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength);
526  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec);
541  uint16_t sendPackets(RawPacketVector& rawPacketsVec);
552  bool sendPacket(RawPacket& rawPacket);
561  bool sendPacket(MBufRawPacket& rawPacket);
573  bool sendPacket(Packet& packet);
574 
575  /* Packet capture */
576 
596  bool startCapture(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie);
619  int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie,
620  int timeout);
624  void stopCapture();
625 
626  /* Device control */
627 
633  bool open();
639  void close();
640 
641  private:
642  struct rte_kni* m_Device;
643  struct rte_mempool* m_MBufMempool;
644  struct KniDeviceInfo
645  {
647  KniLinkState link;
648  KniPromiscuousMode promisc;
649  uint16_t portId;
650  uint16_t mtu;
651  MacAddress mac;
652  std::string name;
653 
654  bool init(const KniDeviceConfiguration& conf);
655  } m_DeviceInfo;
656  struct KniThread;
657  struct KniCapturing
658  {
659  OnKniPacketArriveCallback callback;
660  void* userCookie;
661  KniThread* thread;
662 
663  static void runCapture(void* devicePointer, std::atomic<bool>& stopThread);
664  inline bool isRunning() const
665  {
666  return thread != nullptr;
667  }
668  void cleanup();
669  } m_Capturing;
670  struct KniRequests
671  {
672  long sleepS;
673  long sleepNs;
674  KniThread* thread;
675 
676  static void runRequests(void* devicePointer, std::atomic<bool>& stopThread);
677  void cleanup();
678  } m_Requests;
679  };
680 
681 } // namespace pcpp
682 
683 // GCOVR_EXCL_STOP
Definition: Device.h:24
Definition: KniDevice.h:125
bool sendPacket(MBufRawPacket &rawPacket)
Send a MBufRawPacket to kernel. Please notice that the mbuf used in this method isn't freed by this m...
bool sendPacket(RawPacket &rawPacket)
Send a raw packet to kernel. Please notice that if the raw packet isn't of type MBufRawPacket,...
KniPromiscuousMode getPromiscuous(KniInfoState state=INFO_CACHED)
Obtains information about promiscuous mode of KNI device. If called with INFO_CACHED - returns cached...
uint16_t receivePackets(Packet **packetsArr, uint16_t packetsArrLength)
Receive parsed packets from kernel.
bool sendPacket(Packet &packet)
Send a parsed packet to kernel. Please notice that the mbuf used or allocated in this method isn't fr...
std::string getName() const
Definition: KniDevice.h:296
bool startCapture(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie)
Start capturing packets asynchronously on this KNI interface. Each time a burst of packets is capture...
KniInfoState
Definition: KniDevice.h:148
@ INFO_CACHED
Definition: KniDevice.h:150
@ INFO_RENEW
Definition: KniDevice.h:152
bool isInitialized() const
Definition: KniDevice.h:289
bool setPromiscuous(KniPromiscuousMode mode)
Sets promiscuous mode of KNI device. Firstly the promiscuous mode information is updated as by call t...
KniLinkState updateLinkState(KniLinkState state)
Updates link state of KNI device. Unconditionally updates link state of KNI device via call to DPDK l...
uint16_t receivePackets(MBufRawPacket **rawPacketsArr, uint16_t rawPacketArrLength)
Receive raw packets from kernel. Please notice that in terms of performance, this is the best method ...
MacAddress getMacAddress(KniInfoState state=INFO_CACHED)
Obtains MAC address of KNI device. If called with INFO_CACHED - returns cached data about MAC address...
KniLinkState getLinkState(KniInfoState state=INFO_CACHED)
Obtains link status of KNI device. If called with INFO_CACHED - returns cached data about link state ...
int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie, int timeout)
Start capturing packets synchronously on this KNI interface in blocking mode. Blocking mode means tha...
bool setMtu(uint16_t mtu)
Sets MTU of KNI device. Unconditionally changes MTU of KNI device. If MTU is updated successfully the...
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr)
Receive raw packets from kernel.
KniLinkState
Definition: KniDevice.h:134
@ LINK_NOT_SUPPORTED
Definition: KniDevice.h:136
@ LINK_UP
Definition: KniDevice.h:142
@ LINK_DOWN
Definition: KniDevice.h:140
@ LINK_ERROR
Definition: KniDevice.h:138
void stopRequestHandlerThread()
Explicitly stops request thread for this device if it was running. See description of handleRequests(...
bool setLinkState(KniLinkState state)
Sets link state of KNI device. Firstly the link information is updated as by call to getLinkState(INF...
uint16_t sendPackets(MBufRawPacketVector &rawPacketsVec)
Send a vector of MBufRawPacket pointers to kernel. Please notice the following:
uint16_t getPort() const
Definition: KniDevice.h:303
bool handleRequests()
Handle requests from Linux kernel synchronously in calling thread. When one of events which is needed...
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength)
Send an array of MBufRawPacket to kernel. Please notice the following:
uint16_t sendPackets(Packet **packetsArr, uint16_t arrLength)
Send an array of parsed packets to kernel. Please notice the following:
uint16_t sendPackets(RawPacketVector &rawPacketsVec)
Send a vector of RawPacket pointers to kernel. Please notice the following:
KniPromiscuousMode
Definition: KniDevice.h:158
@ PROMISC_ENABLE
Definition: KniDevice.h:162
@ PROMISC_DISABLE
Definition: KniDevice.h:160
bool setMacAddress(const MacAddress &mac)
Sets MAC address of KNI device. Unconditionally changes MAC of KNI device. If MAC is updated successf...
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...
bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds=0)
Starts new thread (using pthread) to asynchronously handle KNI device requests. See description of ha...
void close()
Close the KNI device. When device is closed it's not possible to work with it. Stops asynchronous pac...
Definition: KniDeviceList.h:24
Definition: LinuxNicInformationSocket.h:26
Definition: MBufRawPacket.h:49
Definition: MacAddress.h:25
Definition: Packet.h:27
Definition: PointerVector.h:58
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
bool(* OnKniPacketArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, KniDevice *device, void *userCookie)
Definition: KniDevice.h:96
KNI device initialization data. Used to create new KNI device. Usage of callbacks member or oldCallba...
Definition: KniDevice.h:234
uint16_t portId
Definition: KniDevice.h:258
std::string name
Definition: KniDevice.h:240
uint16_t mtu
Definition: KniDevice.h:260
uint32_t kthreadCoreId
Definition: KniDevice.h:270
bool bindKthread
Definition: KniDevice.h:268
MacAddress mac
Definition: KniDevice.h:251
New callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:177
int(* change_mtu)(uint16_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:182
int(* config_mac_address)(uint16_t port_id, uint8_t mac_addr[])
Definition: KniDevice.h:195
int(* config_network_if)(uint16_t port_id, uint8_t if_up)
Definition: KniDevice.h:187
int(* config_promiscusity)(uint16_t port_id, uint8_t to_on)
Definition: KniDevice.h:203
Old callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:214
int(* change_mtu)(uint8_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:219
int(* config_network_if)(uint8_t port_id, uint8_t if_up)
Definition: KniDevice.h:224