PcapPlusPlus  19.12
KniDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_KNI_DEVICE
2 #define PCAPPP_KNI_DEVICE
3 
4 #include <string>
5 
6 #include "Device.h"
7 #include "MacAddress.h"
8 #include "MBufRawPacket.h"
9 #include "LinuxNicInformationSocket.h"
10 
79 struct rte_kni;
80 
85 namespace pcpp
86 {
87  class KniDevice;
88  class KniDeviceList;
89 
93  typedef bool (*OnKniPacketArriveCallback)(MBufRawPacket* packets, uint32_t numOfPackets, KniDevice* device, void* userCookie);
94 
120  class KniDevice : public IDevice
121  {
122  friend class KniDeviceList;
123  friend class MBufRawPacket;
124  public:
129  {
138  };
143  {
148  };
153  {
158  };
159 
172  {
177  int (*change_mtu)(uint16_t port_id, unsigned int new_mtu);
182  int (*config_network_if)(uint16_t port_id, uint8_t if_up);
190  int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]);
198  int (*config_promiscusity)(uint16_t port_id, uint8_t to_on);
199  };
200 
209  {
214  int (*change_mtu)(uint8_t port_id, unsigned int new_mtu);
219  int (*config_network_if)(uint8_t port_id, uint8_t if_up);
220  };
221 
229  {
235  std::string name;
236  union
237  {
238  KniIoctlCallbacks* callbacks;
239  KniOldIoctlCallbacks* oldCallbacks;
240  };
254  uint16_t portId;
256  uint16_t mtu;
266  uint32_t kthreadCoreId;
267  };
268 
269  private:
271  KniDevice(const KniDeviceConfiguration& conf, size_t mempoolSize, int unique);
273  KniDevice(const KniDevice&);
275  KniDevice& operator=(const KniDevice&);
277  ~KniDevice();
278 
279  public:
280  /* Information getters */
281 
285  inline bool isInitialized() const { return !(m_Device == NULL || m_MBufMempool == NULL); }
289  inline std::string getName() const { return std::string(m_DeviceInfo.name); }
293  inline uint16_t getPort() const { return m_DeviceInfo.portId; }
320  uint16_t getMtu(KniInfoState state = INFO_CACHED);
330 
331  /* Information setters */
332 
344  bool setLinkState(KniLinkState state);
354  bool setMacAddress(MacAddress mac);
365  bool setMtu(uint16_t mtu);
390 
391  /* Requests */
392 
408  bool handleRequests();
421  bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds = 0);
431 
432  /* Packet receive */
433 
439  uint16_t receivePackets(MBufRawPacketVector& rawPacketsArr);
452  uint16_t receivePackets(MBufRawPacket** rawPacketsArr, uint16_t rawPacketArrLength);
461  uint16_t receivePackets(Packet** packetsArr, uint16_t packetsArrLength);
462 
463  /* Packet send */
464 
479  uint16_t sendPackets(MBufRawPacket** rawPacketsArr, uint16_t arrLength);
495  uint16_t sendPackets(Packet** packetsArr, uint16_t arrLength);
506  uint16_t sendPackets(MBufRawPacketVector& rawPacketsVec);
521  uint16_t sendPackets(RawPacketVector& rawPacketsVec);
531  bool sendPacket(RawPacket& rawPacket);
538  bool sendPacket(MBufRawPacket& rawPacket);
548  bool sendPacket(Packet& packet);
549 
550  /* Packet capture */
551 
569  bool startCapture(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie);
592  int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void* onPacketArrivesUserCookie, int timeout);
596  void stopCapture();
597 
598  /* Device control */
599 
605  bool open();
611  void close();
612 
613  private:
614  struct rte_kni* m_Device;
615  struct rte_mempool* m_MBufMempool;
616  struct KniDeviceInfo
617  {
619  KniLinkState link;
620  KniPromiscuousMode promisc;
621  uint16_t portId;
622  uint16_t mtu;
623  MacAddress mac;
624  std::string name;
625 
626  bool init(const KniDeviceConfiguration& conf);
627  } m_DeviceInfo;
628  struct KniThread;
629  struct KniCapturing
630  {
631  OnKniPacketArriveCallback callback;
632  void* userCookie;
633  KniThread* thread;
634 
635  static void* runCapture(void* devicePointer);
636  inline bool isRunning() const { return thread != NULL; }
637  void cleanup();
638  } m_Capturing;
639  struct KniRequests
640  {
641  long sleepS;
642  long sleepNs;
643  KniThread* thread;
644 
645  static void* runRequests(void* devicePointer);
646  void cleanup();
647  } m_Requests;
648  };
649 
650 } // namespace pcpp
651 #endif /* PCAPPP_KNI_DEVICE */
pcpp::KniDevice::setLinkState
bool setLinkState(KniLinkState state)
Sets link state of KNI device. Firstly the link information is updated as by call to getLinkState(INF...
pcpp::KniDevice::LINK_ERROR
Definition: KniDevice.h:133
pcpp::KniDevice::getName
std::string getName() const
Definition: KniDevice.h:289
pcpp::KniDevice::handleRequests
bool handleRequests()
Handle requests from Linux kernel synchronously in calling thread. When one of events which is needed...
pcpp::Packet
Definition: Packet.h:26
pcpp::KniDevice::open
bool open()
pcpp::KniDevice::stopCapture
void stopCapture()
pcpp::KniDevice::KniIoctlCallbacks::config_network_if
int(* config_network_if)(uint16_t port_id, uint8_t if_up)
Definition: KniDevice.h:182
pcpp::KniDevice::PROMISC_ENABLE
Definition: KniDevice.h:157
pcpp::KniDevice::KniDeviceConfiguration::portId
uint16_t portId
Definition: KniDevice.h:254
pcpp::KniDevice::startCaptureBlockingMode
int startCaptureBlockingMode(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie, int timeout)
Start capturing packets synchronously on this KNI interface in blocking mode. Blocking mode means tha...
pcpp::KniDevice::getPromiscuous
KniPromiscuousMode getPromiscuous(KniInfoState state=INFO_CACHED)
Obtains information about promiscuous mode of KNI device. If called with INFO_CACHED - returns cached...
pcpp::KniDevice::KniOldIoctlCallbacks::change_mtu
int(* change_mtu)(uint8_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:214
pcpp::OnKniPacketArriveCallback
bool(* OnKniPacketArriveCallback)(MBufRawPacket *packets, uint32_t numOfPackets, KniDevice *device, void *userCookie)
Definition: KniDevice.h:93
pcpp::KniDevice::LINK_NOT_SUPPORTED
Definition: KniDevice.h:131
pcpp::KniDevice::setMtu
bool setMtu(uint16_t mtu)
Sets MTU of KNI device. Unconditionally changes MTU of KNI device. If MTU is updated successfully the...
pcpp::KniDevice::KniDeviceConfiguration
KNI device initialization data. Used to create new KNI device. Usage of callbacks member or oldCallba...
Definition: KniDevice.h:228
MacAddress.h
pcpp::KniDevice::INFO_CACHED
Definition: KniDevice.h:145
pcpp::KniDevice::updateLinkState
KniLinkState updateLinkState(KniLinkState state)
Updates link state of KNI device. Unconditionally updates link state of KNI device via call to DPDK l...
pcpp::KniDevice::LINK_UP
Definition: KniDevice.h:137
pcpp::KniDevice::KniDeviceConfiguration::mtu
uint16_t mtu
Definition: KniDevice.h:256
pcpp::KniDevice::KniDeviceConfiguration::mac
MacAddress mac
Definition: KniDevice.h:247
pcpp::KniDevice::KniOldIoctlCallbacks
Old callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:208
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::KniDevice::KniDeviceConfiguration::kthreadCoreId
uint32_t kthreadCoreId
Definition: KniDevice.h:266
pcpp::KniDevice
Definition: KniDevice.h:120
pcpp::KniDevice::KniDeviceConfiguration::name
std::string name
Definition: KniDevice.h:235
pcpp::KniDevice::KniPromiscuousMode
KniPromiscuousMode
Definition: KniDevice.h:152
pcpp::KniDevice::sendPacket
bool sendPacket(RawPacket &rawPacket)
Send a raw packet to kernel. Please notice that if the raw packet isn't of type MBufRawPacket,...
pcpp::KniDevice::receivePackets
uint16_t receivePackets(MBufRawPacketVector &rawPacketsArr)
Receive raw packets from kernel.
pcpp::KniDevice::PROMISC_DISABLE
Definition: KniDevice.h:155
pcpp::KniDevice::getLinkState
KniLinkState getLinkState(KniInfoState state=INFO_CACHED)
Obtains link status of KNI device. If called with INFO_CACHED - returns cached data about link state ...
pcpp::KniDeviceList
Definition: KniDeviceList.h:22
pcpp::KniDevice::sendPackets
uint16_t sendPackets(MBufRawPacket **rawPacketsArr, uint16_t arrLength)
Send an array of MBufRawPacket to kernel. Please notice the following:
pcpp::KniDevice::isInitialized
bool isInitialized() const
Definition: KniDevice.h:285
pcpp::KniDevice::getPort
uint16_t getPort() const
Definition: KniDevice.h:293
Device.h
pcpp::LinuxNicInformationSocket
Definition: LinuxNicInformationSocket.h:26
pcpp::KniDevice::KniIoctlCallbacks::config_promiscusity
int(* config_promiscusity)(uint16_t port_id, uint8_t to_on)
Definition: KniDevice.h:198
pcpp::KniDevice::LINK_DOWN
Definition: KniDevice.h:135
pcpp::KniDevice::KniLinkState
KniLinkState
Definition: KniDevice.h:128
pcpp::KniDevice::KniOldIoctlCallbacks::config_network_if
int(* config_network_if)(uint8_t port_id, uint8_t if_up)
Definition: KniDevice.h:219
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::KniDevice::KniInfoState
KniInfoState
Definition: KniDevice.h:142
pcpp::KniDevice::setPromiscuous
bool setPromiscuous(KniPromiscuousMode mode)
Sets promiscuous mode of KNI device. Firstly the promiscuous mode information is updated as by call t...
pcpp::KniDevice::getMacAddress
MacAddress getMacAddress(KniInfoState state=INFO_CACHED)
Obtains MAC address of KNI device. If called with INFO_CACHED - returns cached data about MAC address...
pcpp::KniDevice::stopRequestHandlerThread
void stopRequestHandlerThread()
Explicitly stops request thread for this device if it was running. See description of handleRequests(...
pcpp::KniDevice::setMacAddress
bool setMacAddress(MacAddress mac)
Sets MAC address of KNI device. Unconditionally changes MAC of KNI device. If MAC is updated successf...
pcpp::KniDevice::KniIoctlCallbacks::config_mac_address
int(* config_mac_address)(uint16_t port_id, uint8_t mac_addr[])
Definition: KniDevice.h:190
pcpp::KniDevice::startRequestHandlerThread
bool startRequestHandlerThread(long sleepSeconds, long sleepNanoSeconds=0)
Starts new thread (using pthread) to asynchronously handle KNI device requests. See description of ha...
pcpp::KniDevice::close
void close()
Close the KNI device. When device is closed it's not possible to work with it. Stops asynchronous pac...
pcpp::KniDevice::KniIoctlCallbacks
New callbacks for KNI device events. This structure MUST be used ONLY when KniDeviceList::callbackVer...
Definition: KniDevice.h:171
pcpp::KniDevice::INFO_RENEW
Definition: KniDevice.h:147
pcpp::KniDevice::startCapture
bool startCapture(OnKniPacketArriveCallback onPacketArrives, void *onPacketArrivesUserCookie)
Start capturing packets asynchronously on this KNI interface. Each time a burst of packets is capture...
pcpp::KniDevice::KniIoctlCallbacks::change_mtu
int(* change_mtu)(uint16_t port_id, unsigned int new_mtu)
Definition: KniDevice.h:177
pcpp::KniDevice::KniDeviceConfiguration::bindKthread
bool bindKthread
Definition: KniDevice.h:264
pcpp::KniDevice::getMtu
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...
pcpp::PointerVector
Definition: PointerVector.h:24
pcpp::IDevice
Definition: Device.h:24
pcpp::MBufRawPacket
Definition: MBufRawPacket.h:43
pcpp::RawPacket
Definition: RawPacket.h:219