Files

228 lines
5.9 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/KernelBypass/DpdkTransmitter.h
*
* @brief Header file for DpdkTransmitter class.
*/
#if (!defined ACTIV_FRAMEWORK_KERNEL_BYPASS_DPDK_TRANSMITTER_H)
#define ACTIV_FRAMEWORK_KERNEL_BYPASS_DPDK_TRANSMITTER_H
#include "ActivMiddleware/ActivFramework/External.h"
#if (!defined ACTIV_ENABLE_DPDK)
#error DPDK is not supported on this processor/platform/compiler.
#endif
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
#include <memory>
// Forward declare DPDK packet buffer.
struct rte_mbuf;
//! Activ namespace.
namespace Activ
{
class ManagedEndPoint;
//! KernelBypass namespace.
namespace KernelBypass
{
//! Class to transmit data over a DPDK interface.
class DpdkTransmitter
{
public:
class Impl;
//! Handle to a transmit packet.
class Packet
{
public:
//! Default constructor.
Packet();
// Deleted copy constructor.
Packet(const Packet &) = delete;
//! Move constructor.
Packet(Packet &&rhs);
//! Destructor.
~Packet();
//! Move assignment operator.
Packet &operator=(Packet &&rhs);
//! Release the object when the ownership of the underlying buffer has changed.
void Release();
//! Reset the object; the underlying buffer is returned.
void Reset();
//! Is the a packet attached?
operator bool() const;
//! Get the packet data, the a packet must be attached to use this method.
void *GetData() const;
private:
/**
* @brief Return an mbuf back to the pool.
*
* @param pImpl the transmitter impl that owns the pool.
* @param pMbuf the mbuf to return.
*/
ACTIV_FRAMEWORK_API static void FreeMbuf(Impl *pImpl, rte_mbuf *pMbuf);
//! The transmitter impl the packet belongs to, null means a packet is not attached.
Impl * m_pImpl;
rte_mbuf * m_pMbuf; ///< The mbuf pointer.
void * m_pBuffer; ///< Pointer to the payload of the packet.
friend class Impl;
};
//! Back pressure mode enumeration.
enum BackPressureMode
{
BACK_PRESSURE_MODE_SPIN, ///< Spin until space becomes available.
BACK_PRESSURE_MODE_RETURN ///< Return if sufficient space is not available.
};
/**
* @brief Constructor.
*
* @param managedEndPoint
* @param interfaceIndex
*
* @throw Exception
* @throw std::exception
*/
ACTIV_FRAMEWORK_API explicit DpdkTransmitter(ManagedEndPoint &managedEndPoint, const size_t interfaceIndex);
//! Destructor.
ACTIV_FRAMEWORK_API virtual ~DpdkTransmitter();
/**
* @brief Allocate a packet for transmission.
*
* An exception is thrown for a fatal condition where packets have been exhausted. This should never happen.
*
* @throw Exception
*/
ACTIV_FRAMEWORK_API Packet AllocatePacket();
/**
* @brief Transmit a raw Ethernet packet.
*
* @param packet
* @param length
* @param backPressureMode action to take if there is not enough space to write the packet.
*
* @throw Exception
* @throw std::exception
*
* @return whether the packet was written, always true if BACK_PRESSURE_MODE_SPIN is specified.
*/
ACTIV_FRAMEWORK_API bool TransmitPacket(Packet &packet, const uint16_t length,
const BackPressureMode backPressureMode = BACK_PRESSURE_MODE_SPIN);
//! Get the capacity of packets. User must ensure not to write more data into a packet than this.
uint16_t GetPacketCapacity() const;
private:
static const uint16_t MAX_PACKET_SIZE = 1522; ///< The maximum packet size.
std::unique_ptr<Impl> m_pImpl; ///< Private implementation.
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline uint16_t DpdkTransmitter::GetPacketCapacity() const
{
return MAX_PACKET_SIZE;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline DpdkTransmitter::Packet::Packet() :
m_pImpl(nullptr)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DpdkTransmitter::Packet::Packet(Packet &&rhs) :
m_pImpl(rhs.m_pImpl),
m_pMbuf(rhs.m_pMbuf),
m_pBuffer(rhs.m_pBuffer)
{
rhs.m_pImpl = nullptr;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DpdkTransmitter::Packet::~Packet()
{
if (m_pImpl)
{
assert(m_pMbuf);
FreeMbuf(m_pImpl, m_pMbuf);
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DpdkTransmitter::Packet &DpdkTransmitter::Packet::operator=(Packet &&rhs)
{
std::swap(m_pImpl, rhs.m_pImpl);
std::swap(m_pMbuf, rhs.m_pMbuf);
std::swap(m_pBuffer, rhs.m_pBuffer);
return *this;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void DpdkTransmitter::Packet::Release()
{
m_pImpl = nullptr;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void DpdkTransmitter::Packet::Reset()
{
if (m_pImpl)
{
assert(m_pMbuf);
FreeMbuf(m_pImpl, m_pMbuf);
m_pImpl = nullptr;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DpdkTransmitter::Packet::operator bool() const
{
return bool(m_pImpl);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void *DpdkTransmitter::Packet::GetData() const
{
assert(m_pImpl && m_pMbuf && m_pBuffer);
return m_pBuffer;
}
} // namespace KernelBypass
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_KERNEL_BYPASS_DPDK_TRANSMITTER_H)