PcapPlusPlus  Next
pcpp::XdpDevice Class Reference

#include <XdpDevice.h>

Inheritance diagram for pcpp::XdpDevice:
pcpp::IDevice

Classes

struct  XdpDeviceConfiguration
 
struct  XdpDeviceStats
 

Public Types

typedef void(* OnPacketsArrive) (RawPacket packets[], uint32_t packetCount, XdpDevice *device, void *userCookie)
 

Public Member Functions

 XdpDevice (std::string interfaceName)
 
 ~XdpDevice () override
 
bool open () override
 
bool open (const XdpDeviceConfiguration &config)
 
void close () override
 
bool receivePackets (OnPacketsArrive onPacketsArrive, void *onPacketsArriveUserCookie, int timeoutMS=5000)
 
void stopReceivePackets ()
 
bool sendPackets (const RawPacketVector &packets, bool waitForTxCompletion=false, int waitForTxCompletionTimeoutMS=5000)
 
bool sendPackets (RawPacket packets[], size_t packetCount, bool waitForTxCompletion=false, int waitForTxCompletionTimeoutMS=5000)
 
XdpDeviceConfigurationgetConfig () const
 
XdpDeviceStats getStatistics ()
 
- Public Member Functions inherited from pcpp::IDevice
bool isOpened ()
 

Detailed Description

A class wrapping the main functionality of using AF_XDP (XSK) sockets which are optimized for high performance packet processing.

It provides methods for configuring and initializing an AF_XDP socket, and then send and receive packets through it. It also provides a method for gathering statistics from the socket.

Member Typedef Documentation

◆ OnPacketsArrive

pcpp::XdpDevice::OnPacketsArrive

The callback that is called whenever packets are received on the socket

Parameters
[in]packetsAn array of the raw packets received
[in]packetCountThe number of packets received
[in]deviceThe XdpDevice packets are received from (represents the AF_XDP socket)
[in]userCookieA pointer to an object set by the user when receivePackets() started

Constructor & Destructor Documentation

◆ XdpDevice()

pcpp::XdpDevice::XdpDevice ( std::string  interfaceName)
explicit

A c'tor for this class. Please note that calling this c'tor doesn't initialize the AF_XDP socket. In order to set up the socket call open().

Parameters
[in]interfaceNameThe interface name to open the AF_XDP socket on

◆ ~XdpDevice()

pcpp::XdpDevice::~XdpDevice ( )
override

A d'tor for this class. It closes the device if it's open.

Member Function Documentation

◆ close()

void pcpp::XdpDevice::close ( )
overridevirtual

Close the device. This method closes the AF_XDP socket and frees the UMEM that was allocated for it.

Implements pcpp::IDevice.

◆ getConfig()

XdpDeviceConfiguration* pcpp::XdpDevice::getConfig ( ) const
inline
Returns
A pointer to the current device configuration. If the device is not open this method returns nullptr

◆ getStatistics()

XdpDeviceStats pcpp::XdpDevice::getStatistics ( )
Returns
Current device statistics

◆ open() [1/2]

bool pcpp::XdpDevice::open ( )
overridevirtual

Open the device with default configuration. Call getConfig() after opening the device to get the current configuration. This method initializes the UMEM, and then creates and configures the AF_XDP socket. If it succeeds the socket is ready to receive and send packets.

Returns
True if device was opened successfully, false otherwise

Implements pcpp::IDevice.

◆ open() [2/2]

bool pcpp::XdpDevice::open ( const XdpDeviceConfiguration config)

Open the device with custom configuration set by the user. This method initializes the UMEM, and then creates and configures the AF_XDP socket. If it succeeds the socket is ready to receive and send packets.

Parameters
[in]configThe configuration to use for opening the device
Returns
True if device was opened successfully, false otherwise

◆ receivePackets()

bool pcpp::XdpDevice::receivePackets ( OnPacketsArrive  onPacketsArrive,
void *  onPacketsArriveUserCookie,
int  timeoutMS = 5000 
)

Start receiving packets. In order to use this method the device should be open. Note that this method is blocking and will return if:

  • stopReceivePackets() was called from within the user callback
  • timeoutMS passed without receiving any packets
  • Some error occurred (an error log will be printed)
    Parameters
    [in]onPacketsArriveA callback to be called when packets are received
    [in]onPacketsArriveUserCookieThe callback is invoked with this cookie as a parameter. It can be used to pass information from the user application to the callback
    [in]timeoutMSTimeout in milliseconds to stop if no packets are received. The default value is 5000 ms
    Returns
    True if stopped receiving packets because stopReceivePackets() was called or because timeoutMS passed, or false if an error occurred.

◆ sendPackets() [1/2]

bool pcpp::XdpDevice::sendPackets ( const RawPacketVector packets,
bool  waitForTxCompletion = false,
int  waitForTxCompletionTimeoutMS = 5000 
)

Send a vector of packet pointers.

Parameters
[in]packetsA vector of packet pointers to send
[in]waitForTxCompletionWait for confirmation from the kernel that packets were sent. If set to true this method will wait until the number of packets in the completion ring is equal or greater to the number of packets that were sent. The default value is false
[in]waitForTxCompletionTimeoutMSIf waitForTxCompletion is set to true, poll the completion ring with this timeout. The default value is 5000 ms
Returns
True if all packets were sent, or if waitForTxCompletion is true - all sent packets were confirmed. Returns false if an error occurred or if poll timed out.

◆ sendPackets() [2/2]

bool pcpp::XdpDevice::sendPackets ( RawPacket  packets[],
size_t  packetCount,
bool  waitForTxCompletion = false,
int  waitForTxCompletionTimeoutMS = 5000 
)

Send an array of packets.

Parameters
[in]packetsAn array of raw packets to send
[in]packetCountThe length of the packet array
[in]waitForTxCompletionWait for confirmation from the kernel that packets were sent. If set to true this method will wait until the number of packets in the completion ring is equal or greater to the number of packets sent. The default value is false
[in]waitForTxCompletionTimeoutMSIf waitForTxCompletion is set to true, poll the completion ring with this timeout. The default value is 5000 ms
Returns
True if all packets were sent, or if waitForTxCompletion is true - all sent packets were confirmed. Returns false if an error occurred or if poll timed out.

◆ stopReceivePackets()

void pcpp::XdpDevice::stopReceivePackets ( )

Stop receiving packets. Call this method from within the callback passed to receivePackets() whenever you want to stop receiving packets.