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 
79 struct rte_kni;
80 
83 namespace pcpp
84 {
85  class KniDevice;
86  class KniDeviceList;
87 
89  using OnKniPacketArriveCallback = bool (*)(MBufRawPacket* packets, uint32_t numOfPackets, KniDevice* device,
90  void* userCookie);
91 
115  class KniDevice : public IDevice
116  {
117  friend class KniDeviceList;
118  friend class MBufRawPacket;
119 
120  public:
123  {
131  LINK_UP = 1
132  };
135  {
139  INFO_RENEW = 1
140  };
143  {
147  PROMISC_ENABLE = 1
148  };
149 
160  {
163  int (*change_mtu)(uint16_t port_id, unsigned int new_mtu);
166  int (*config_network_if)(uint16_t port_id, uint8_t if_up);
172  int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]);
178  int (*config_promiscusity)(uint16_t port_id, uint8_t to_on);
179  };
180 
187  {
190  int (*change_mtu)(uint8_t port_id, unsigned int new_mtu);
193  int (*config_network_if)(uint8_t port_id, uint8_t if_up);
194  };
195 
201  {
205  std::string name;
206  union {
207  KniIoctlCallbacks* callbacks;
208  KniOldIoctlCallbacks* oldCallbacks;
209  };
219  uint16_t portId;
221  uint16_t mtu;
229  uint32_t kthreadCoreId;
230  };
231 
232  private:
234  KniDevice(const KniDeviceConfiguration& conf, size_t mempoolSize, int unique);
236  KniDevice(const KniDevice&);
238  KniDevice& operator=(const KniDevice&);
240  ~KniDevice();
241 
242  public:
244  inline bool isInitialized() const
245  {
246  return !(m_Device == nullptr || m_MBufMempool == nullptr);
247  }
249  inline std::string getName() const
250  {
251  return std::string(m_DeviceInfo.name);
252  }
254  inline uint16_t getPort() const
255  {
256  return m_DeviceInfo.portId;
257  }
278  uint16_t getMtu(KniInfoState state = INFO_CACHED);
286 
304  bool setMacAddress(const MacAddress& mac);
313  bool setMtu(uint16_t mtu);
334 
361  bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds = 0);
369 
374  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr);
387  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength);
396  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength);
397 
410  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength);
424  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength);
433  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec);
446  uint16_t sendPackets(RawPacketVector& rawPacketsVec);
455  bool sendPacket(RawPacket& rawPacket);
462  bool sendPacket(MBufRawPacket& rawPacket);
472  bool sendPacket(Packet& packet);
473 
491  bool startCapture(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie);
512 
513  int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie,
514  int timeout);
516  void stopCapture();
517 
521  bool open();
525  void close();
526 
527  bool isOpened() const override
528  {
529  return m_DeviceOpened;
530  }
531 
532  private:
533  bool m_DeviceOpened = false;
534 
535  struct rte_kni* m_Device;
536  struct rte_mempool* m_MBufMempool;
537  struct KniDeviceInfo
538  {
540  KniLinkState link;
541  KniPromiscuousMode promisc;
542  uint16_t portId;
543  uint16_t mtu;
544  MacAddress mac;
545  std::string name;
546 
547  bool init(const KniDeviceConfiguration& conf);
548  } m_DeviceInfo;
549  struct KniThread;
550  struct KniCapturing
551  {
552  OnKniPacketArriveCallback callback;
553  void* userCookie;
554  KniThread* thread;
555 
556  static void runCapture(void* devicePointer, std::atomic<bool>& stopThread);
557  inline bool isRunning() const
558  {
559  return thread != nullptr;
560  }
561  void cleanup();
562  } m_Capturing;
563  struct KniRequests
564  {
565  long sleepS;
566  long sleepNs;
567  KniThread* thread;
568 
569  static void runRequests(void* devicePointer, std::atomic<bool>& stopThread);
570  void cleanup();
571  } m_Requests;
572  };
573 
574 } // namespace pcpp
575 
576 // GCOVR_EXCL_STOP
Definition: Device.h:20
Definition: KniDevice.h:116
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
Obtains name of KNI device in form of C++ string.
Definition: KniDevice.h:249
bool startCapture(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie)
Start capturing packets asynchronously on this KNI interface. Each time a burst of packets is capture...
KniInfoState
Various information related constants for KNI device.
Definition: KniDevice.h:135
@ INFO_CACHED
Used to identify intent to obtain cached version of KNI device information.
Definition: KniDevice.h:137
@ INFO_RENEW
Used to identify intent to renew/update KNI device information.
Definition: KniDevice.h:139
bool isInitialized() const
Indicates whether the KNI device was initialized successfully.
Definition: KniDevice.h:244
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 isOpened() const override
Definition: KniDevice.h:527
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
Various link related constants for KNI device.
Definition: KniDevice.h:123
@ LINK_NOT_SUPPORTED
Returned by KNI functions if DPDK version used don't support link setup capability.
Definition: KniDevice.h:125
@ LINK_UP
Used to put link status on KNI device UP.
Definition: KniDevice.h:131
@ LINK_DOWN
Used to put link status on KNI device DOWN.
Definition: KniDevice.h:129
@ LINK_ERROR
Returned by KNI functions if link changing function meets an error.
Definition: KniDevice.h:127
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
Obtains port ID of KNI device.
Definition: KniDevice.h:254
void stopCapture()
Stop a currently running asynchronous packet capture.
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
Promiscuous mode related constants for KNI device.
Definition: KniDevice.h:143
@ PROMISC_ENABLE
Used to ENABLE promiscuous mode on KNI device.
Definition: KniDevice.h:147
@ PROMISC_DISABLE
Used to DISABLE promiscuous mode on KNI device.
Definition: KniDevice.h:145
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:19
Definition: LinuxNicInformationSocket.h:22
Definition: MBufRawPacket.h:45
Definition: MacAddress.h:24
Definition: Packet.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:259
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
bool(*)(MBufRawPacket *packets, uint32_t numOfPackets, KniDevice *device, void *userCookie) OnKniPacketArriveCallback
Defines the signature callback used by capturing API on KNI device.
Definition: KniDevice.h:90
KNI device initialization data. Used to create new KNI device. Usage of callbacks member or oldCallba...
Definition: KniDevice.h:201
uint16_t portId
Definition: KniDevice.h:219
std::string name
Definition: KniDevice.h:205
uint16_t mtu
MTU of new KNI device. Will be cached by new KNI device info structure.
Definition: KniDevice.h:221
uint32_t kthreadCoreId
ID of core to bind Linux kernel thread to (same as DPDK cores IDs)
Definition: KniDevice.h:229
bool bindKthread
Definition: KniDevice.h:227
MacAddress mac
Definition: KniDevice.h:214
New callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:160
int(* change_mtu)(uint16_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:163
int(* config_mac_address)(uint16_t port_id, uint8_t mac_addr[])
Definition: KniDevice.h:172
int(* config_network_if)(uint16_t port_id, uint8_t if_up)
Definition: KniDevice.h:166
int(* config_promiscusity)(uint16_t port_id, uint8_t to_on)
Definition: KniDevice.h:178
Old callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:187
int(* change_mtu)(uint8_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:190
int(* config_network_if)(uint8_t port_id, uint8_t if_up)
Definition: KniDevice.h:193