PcapPlusPlus  21.05
pcpp::MBufRawPacket Class Reference

#include <MBufRawPacket.h>

Inheritance diagram for pcpp::MBufRawPacket:
pcpp::RawPacket

Public Member Functions

 MBufRawPacket ()
 
virtual ~MBufRawPacket ()
 
 MBufRawPacket (const MBufRawPacket &other)
 
bool init (DpdkDevice *device)
 Initialize an instance of this class from DpdkDevice. Initialization includes allocating an mbuf from the pool that resides in DpdkDevice. The user should call this method only once per instance. Calling it more than once will result with an error. More...
 
bool init (KniDevice *device)
 Initialize an instance of this class from KniDevice. Initialization includes allocating an mbuf from the pool that resides in KniDevice. The user should call this method only once per instance. Calling it more than once will result with an error. More...
 
bool initFromRawPacket (const RawPacket *rawPacket, DpdkDevice *device)
 Initialize an instance of this class and copies the content of a RawPacket object. Initialization includes allocating an mbuf from the pool that resides in provided DpdkDevice, and copying the data from the input RawPacket object into this mBuf. The user should call this method only once per instance. Calling it more than once will result with an error. More...
 
bool initFromRawPacket (const RawPacket *rawPacket, KniDevice *device)
 Initialize an instance of this class and copies the content of a RawPacket object. Initialization includes allocating an mbuf from the pool that resides in provided KniDevice, and copying the data from the input RawPacket object into this mBuf. The user should call this method only once per instance. Calling it more than once will result with an error. More...
 
rte_mbuf * getMBuf ()
 
virtual uint8_t getObjectType () const
 
MBufRawPacketoperator= (const MBufRawPacket &other)
 
bool setRawData (const uint8_t *pRawData, int rawDataLen, timespec timestamp, LinkLayerType layerType=LINKTYPE_ETHERNET, int frameLength=-1)
 
void clear ()
 
void appendData (const uint8_t *dataToAppend, size_t dataToAppendLen)
 
void insertData (int atIndex, const uint8_t *dataToInsert, size_t dataToInsertLen)
 
bool removeData (int atIndex, size_t numOfBytesToRemove)
 
bool reallocateData (size_t newBufferLength)
 
void setFreeMbuf (bool val=true)
 
- Public Member Functions inherited from pcpp::RawPacket
 RawPacket (const uint8_t *pRawData, int rawDataLen, timeval timestamp, bool deleteRawDataAtDestructor, LinkLayerType layerType=LINKTYPE_ETHERNET)
 
 RawPacket (const uint8_t *pRawData, int rawDataLen, timespec timestamp, bool deleteRawDataAtDestructor, LinkLayerType layerType=LINKTYPE_ETHERNET)
 
 RawPacket ()
 
virtual ~RawPacket ()
 
 RawPacket (const RawPacket &other)
 
RawPacketoperator= (const RawPacket &other)
 
virtual bool setRawData (const uint8_t *pRawData, int rawDataLen, timeval timestamp, LinkLayerType layerType=LINKTYPE_ETHERNET, int frameLength=-1)
 
const uint8_t * getRawData () const
 
LinkLayerType getLinkLayerType () const
 
int getRawDataLen () const
 
int getFrameLength () const
 
timespec getPacketTimeStamp () const
 
virtual bool setPacketTimeStamp (timeval timestamp)
 
virtual bool setPacketTimeStamp (timespec timestamp)
 
bool isPacketSet () const
 

Additional Inherited Members

- Static Public Member Functions inherited from pcpp::RawPacket
static bool isLinkTypeValid (int linkTypeValue)
 

Detailed Description

A class that inherits RawPacket and wraps DPDK's mbuf object (see some info about mbuf in DpdkDevice.h) but is compatible with PcapPlusPlus framework. Using MBufRawPacket is be almost similar to using RawPacket, the implementation differences are encapsulated in the class implementation. For example: user can create and manipulate a Packet object from MBufRawPacket the same way it is done with RawPacket; User can use PcapFileWriterDevice to save MBufRawPacket to pcap the same way it's used with RawPacket; etc.
The main difference is that RawPacket contains a pointer to the data itself and MBufRawPacket is holding a pointer to an mbuf object which contains a pointer to the data. This implies that MBufRawPacket without an mbuf allocated to it is not usable. Getting instances of MBufRawPacket can be done in one to the following ways:

  • Receiving packets from DpdkDevice. In this case DpdkDevice takes care of getting the mbuf from DPDK and wrapping it with MBufRawPacket
  • Creating MBufRawPacket from scratch (in order to send it with DpdkDevice, for example). In this case the user should call the init() method after constructing the object in order to allocate a new mbuf from DPDK port pool (encapsulated by DpdkDevice)

Limitations of this class:

  • Currently chained mbufs are not supported. An mbuf has the capability to be linked to another mbuf and create a linked list of mbufs. This is good for Jumbo packets or other uses. MBufRawPacket doesn't support this capability so there is no way to access the mbufs linked to the mbuf wrapped by MBufRawPacket instance. I hope I'll be able to add this support in the future

Constructor & Destructor Documentation

◆ MBufRawPacket() [1/2]

pcpp::MBufRawPacket::MBufRawPacket ( )
inline

A default c'tor for this class. Constructs an instance of this class without an mbuf attached to it. In order to allocate an mbuf the user should call the init() method. Without calling init() the instance of this class is not usable. This c'tor can be used for initializing an array of MBufRawPacket (which requires an empty c'tor)

◆ ~MBufRawPacket()

virtual pcpp::MBufRawPacket::~MBufRawPacket ( )
virtual

A d'tor for this class. Once called it frees the mbuf attached to it (returning it back to the mbuf pool it was allocated from)

◆ MBufRawPacket() [2/2]

pcpp::MBufRawPacket::MBufRawPacket ( const MBufRawPacket other)

A copy c'tor for this class. The copy c'tor allocates a new mbuf from the same pool the original mbuf was allocated from, attaches the new mbuf to this instance of MBufRawPacket and copies the data from the original mbuf to the new mbuf

Parameters
[in]otherThe MBufRawPacket instance to copy from

Member Function Documentation

◆ appendData()

void pcpp::MBufRawPacket::appendData ( const uint8_t *  dataToAppend,
size_t  dataToAppendLen 
)
virtual

Append packet data at the end of current data. This method uses the same mbuf already allocated and tries to append more space and copy the data to it. If MBufRawPacket is not initialize (mbuf is NULL) or mbuf append failed an error is printed to log

Parameters
[in]dataToAppendA pointer to the data to append
[in]dataToAppendLenLength in bytes of dataToAppend

Reimplemented from pcpp::RawPacket.

◆ clear()

void pcpp::MBufRawPacket::clear ( )
virtual

Clears the object and frees the mbuf

Reimplemented from pcpp::RawPacket.

◆ getMBuf()

rte_mbuf* pcpp::MBufRawPacket::getMBuf ( )
inline
Returns
A pointer to the DPDK mbuf stored in this object

◆ getObjectType()

virtual uint8_t pcpp::MBufRawPacket::getObjectType ( ) const
inlinevirtual
Returns
MBufRawPacket object type

Reimplemented from pcpp::RawPacket.

◆ init() [1/2]

bool pcpp::MBufRawPacket::init ( DpdkDevice device)

Initialize an instance of this class from DpdkDevice. Initialization includes allocating an mbuf from the pool that resides in DpdkDevice. The user should call this method only once per instance. Calling it more than once will result with an error.

Parameters
[in]deviceThe DpdkDevice which has the pool to allocate the mbuf from
Returns
True if initialization succeeded and false if this method was already called for this instance (and an mbuf is already attached) or if allocating an mbuf from the pool failed for some reason

◆ init() [2/2]

bool pcpp::MBufRawPacket::init ( KniDevice device)

Initialize an instance of this class from KniDevice. Initialization includes allocating an mbuf from the pool that resides in KniDevice. The user should call this method only once per instance. Calling it more than once will result with an error.

Parameters
[in]deviceThe KniDevice which has the pool to allocate the mbuf from
Returns
True if initialization succeeded and false if this method was already called for this instance (and an mbuf is already attached) or if allocating an mbuf from the pool failed for some reason

◆ initFromRawPacket() [1/2]

bool pcpp::MBufRawPacket::initFromRawPacket ( const RawPacket rawPacket,
DpdkDevice device 
)

Initialize an instance of this class and copies the content of a RawPacket object. Initialization includes allocating an mbuf from the pool that resides in provided DpdkDevice, and copying the data from the input RawPacket object into this mBuf. The user should call this method only once per instance. Calling it more than once will result with an error.

Parameters
[in]rawPacketA pointer to a RawPacket object from which data will be copied
[in]deviceThe DpdkDevice which has the pool to allocate the mbuf from
Returns
True if initialization succeeded and false if this method was already called for this instance (and an mbuf is already attached) or if allocating an mbuf from the pool failed for some reason

◆ initFromRawPacket() [2/2]

bool pcpp::MBufRawPacket::initFromRawPacket ( const RawPacket rawPacket,
KniDevice device 
)

Initialize an instance of this class and copies the content of a RawPacket object. Initialization includes allocating an mbuf from the pool that resides in provided KniDevice, and copying the data from the input RawPacket object into this mBuf. The user should call this method only once per instance. Calling it more than once will result with an error.

Parameters
[in]rawPacketA pointer to a RawPacket object from which data will be copied
[in]deviceThe KniDevice which has the pool to allocate the mbuf from
Returns
True if initialization succeeded and false if this method was already called for this instance (and an mbuf is already attached) or if allocating an mbuf from the pool failed for some reason

◆ insertData()

void pcpp::MBufRawPacket::insertData ( int  atIndex,
const uint8_t *  dataToInsert,
size_t  dataToInsertLen 
)
virtual

Insert raw data at some index of the current data and shift the remaining data to the end. This method uses the same mbuf already allocated and tries to append more space to it. Then it just copies dataToAppend at the relevant index and shifts the remaining data to the end. If MBufRawPacket is not initialize (mbuf is NULL) or mbuf append failed an error is printed to log

Parameters
[in]atIndexThe index to insert the new data to
[in]dataToInsertA pointer to the new data to insert
[in]dataToInsertLenLength in bytes of dataToInsert

Reimplemented from pcpp::RawPacket.

◆ operator=()

MBufRawPacket& pcpp::MBufRawPacket::operator= ( const MBufRawPacket other)

An assignment operator for this class. Copies the data from the mbuf attached to the other MBufRawPacket to the mbuf attached to this instance. If instance is not initialized (meaning no mbuf is attached) nothing will be copied and instance will remain uninitialized (also, an error will be printed)

Parameters
[in]otherThe MBufRawPacket to assign data from

◆ reallocateData()

bool pcpp::MBufRawPacket::reallocateData ( size_t  newBufferLength)
virtual

This overridden method,in contrast to its ancestor RawPacket::reallocateData() doesn't need to do anything because mbuf is already allocated to its maximum extent. So it only performs a check to verify the size after re-allocation doesn't exceed mbuf max size

Parameters
[in]newBufferLengthThe new buffer length as required by the user
Returns
True if new size is larger than current size but smaller than mbuf max size, false otherwise

Reimplemented from pcpp::RawPacket.

◆ removeData()

bool pcpp::MBufRawPacket::removeData ( int  atIndex,
size_t  numOfBytesToRemove 
)
virtual

Remove certain number of bytes from current raw data buffer. All data after the removed bytes will be shifted back. This method uses the mbuf already allocated and tries to trim space from it

Parameters
[in]atIndexThe index to start removing bytes from
[in]numOfBytesToRemoveNumber of bytes to remove
Returns
True if all bytes were removed successfully, or false if MBufRawPacket is not initialize (mbuf is NULL), mbuf trim failed or logatIndex+numOfBytesToRemove is out-of-bounds of the raw data buffer. In all of these cases an error is printed to log

Reimplemented from pcpp::RawPacket.

◆ setFreeMbuf()

void pcpp::MBufRawPacket::setFreeMbuf ( bool  val = true)
inline

Set an indication whether to free the mbuf when done using it or not ("done using it" means setting another mbuf or class d'tor). Default value is true.

Parameters
[in]valThe value to set. True means free the mbuf when done using it. Default it True

◆ setRawData()

bool pcpp::MBufRawPacket::setRawData ( const uint8_t *  pRawData,
int  rawDataLen,
timespec  timestamp,
LinkLayerType  layerType = LINKTYPE_ETHERNET,
int  frameLength = -1 
)
virtual

Set raw data to the mbuf by copying the data to it. In order to stay compatible with the ancestor method which takes control of the data pointer and frees it when RawPacket is destroyed, this method frees this pointer right away after data is copied to the mbuf. So when using this method please notice that after it's called pRawData memory is free, don't use this pointer again. In addition, if raw packet isn't initialized (mbuf is NULL), this method will call the init() method

Parameters
[in]pRawDataA pointer to the new raw data
[in]rawDataLenThe new raw data length in bytes
[in]timestampThe timestamp packet was received by the NIC
[in]layerTypeThe link layer type for this raw data. Default is Ethernet
[in]frameLengthWhen reading from pcap files, sometimes the captured length is different from the actual packet length. This parameter represents the packet length. This parameter is optional, if not set or set to -1 it is assumed both lengths are equal
Returns
True if raw data was copied to the mbuf successfully, false if rawDataLen is larger than mbuf max size, if initialization failed or if copying the data to the mbuf failed. In all of these cases an error will be printed to log

Reimplemented from pcpp::RawPacket.