Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Pcap++/header/XdpDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// @file

#include "Device.h"
#include <memory>
#include <utility>
#include <functional>

Expand Down Expand Up @@ -230,7 +231,8 @@ namespace pcpp
/// @return A pointer to the current device configuration. If the device is not open this method returns nullptr
XdpDeviceConfiguration* getConfig() const
{
return m_Config;
// TODO: Return a copy or const ref to avoid user modifying config?
return m_Config.get();
}

/// @return Current device statistics
Expand Down Expand Up @@ -293,7 +295,7 @@ namespace pcpp
bool m_DeviceOpened = false;

std::string m_InterfaceName;
XdpDeviceConfiguration* m_Config;
std::unique_ptr<XdpDeviceConfiguration> m_Config;
bool m_ReceivingPackets;
XdpUmem* m_Umem;
void* m_SocketInfo;
Expand All @@ -308,7 +310,7 @@ namespace pcpp
uint32_t checkCompletionRing();
bool configureSocket();
bool initUmem();
bool initConfig();
bool populateConfigDefaults(XdpDeviceConfiguration& config) const;
bool getSocketStats();
};
} // namespace pcpp
62 changes: 29 additions & 33 deletions Pcap++/src/XdpDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,16 @@ namespace pcpp
return true;
}

bool XdpDevice::initConfig()
bool XdpDevice::populateConfigDefaults(XdpDeviceConfiguration& config) const
{
if (!m_Config)
{
m_Config = new XdpDeviceConfiguration();
}

uint16_t numFrames = m_Config->umemNumFrames ? m_Config->umemNumFrames : DEFAULT_UMEM_NUM_FRAMES;
uint16_t frameSize = m_Config->umemFrameSize ? m_Config->umemFrameSize : getpagesize();
uint32_t fillRingSize = m_Config->fillRingSize ? m_Config->fillRingSize : DEFAULT_FILL_RING_SIZE;
uint16_t numFrames = config.umemNumFrames ? config.umemNumFrames : DEFAULT_UMEM_NUM_FRAMES;
uint16_t frameSize = config.umemFrameSize ? config.umemFrameSize : getpagesize();
uint32_t fillRingSize = config.fillRingSize ? config.fillRingSize : DEFAULT_FILL_RING_SIZE;
uint32_t completionRingSize =
m_Config->completionRingSize ? m_Config->completionRingSize : DEFAULT_COMPLETION_RING_SIZE;
uint32_t rxSize = m_Config->rxSize ? m_Config->rxSize : XSK_RING_CONS__DEFAULT_NUM_DESCS;
uint32_t txSize = m_Config->txSize ? m_Config->txSize : XSK_RING_PROD__DEFAULT_NUM_DESCS;
uint32_t batchSize = m_Config->rxTxBatchSize ? m_Config->rxTxBatchSize : DEFAULT_BATCH_SIZE;
config.completionRingSize ? config.completionRingSize : DEFAULT_COMPLETION_RING_SIZE;
uint32_t rxSize = config.rxSize ? config.rxSize : XSK_RING_CONS__DEFAULT_NUM_DESCS;
uint32_t txSize = config.txSize ? config.txSize : XSK_RING_PROD__DEFAULT_NUM_DESCS;
uint32_t batchSize = config.rxTxBatchSize ? config.rxTxBatchSize : DEFAULT_BATCH_SIZE;

if (frameSize != getpagesize())
{
Expand Down Expand Up @@ -504,26 +499,38 @@ namespace pcpp
return false;
}

m_Config->umemNumFrames = numFrames;
m_Config->umemFrameSize = frameSize;
m_Config->fillRingSize = fillRingSize;
m_Config->completionRingSize = completionRingSize;
m_Config->rxSize = rxSize;
m_Config->txSize = txSize;
m_Config->rxTxBatchSize = batchSize;
config.umemNumFrames = numFrames;
config.umemFrameSize = frameSize;
config.fillRingSize = fillRingSize;
config.completionRingSize = completionRingSize;
config.rxSize = rxSize;
config.txSize = txSize;
config.rxTxBatchSize = batchSize;

return true;
}

bool XdpDevice::open()
{
return open(XdpDeviceConfiguration{});
}

bool XdpDevice::open(const XdpDeviceConfiguration& config)
{
if (m_DeviceOpened)
{
PCPP_LOG_ERROR("Device already opened");
return false;
}

if (!(initConfig() && initUmem() &&
auto configCopy = std::make_unique<XdpDeviceConfiguration>(config);
if (!populateConfigDefaults(*configCopy))
{
return false;
}
m_Config = std::move(configCopy);

if (!(initUmem() &&
populateFillRing(std::min(m_Config->fillRingSize, static_cast<uint32_t>(m_Config->umemNumFrames / 2))) &&
configureSocket()))
{
Expand All @@ -532,11 +539,7 @@ namespace pcpp
delete m_Umem;
m_Umem = nullptr;
}
if (m_Config)
{
delete m_Config;
m_Config = nullptr;
}
m_Config.reset();
return false;
}

Expand All @@ -547,12 +550,6 @@ namespace pcpp
return m_DeviceOpened;
}

bool XdpDevice::open(const XdpDeviceConfiguration& config)
{
m_Config = new XdpDeviceConfiguration(config);
return open();
}

void XdpDevice::close()
{
if (m_DeviceOpened)
Expand All @@ -561,7 +558,6 @@ namespace pcpp
xsk_socket__delete(socketInfo->xsk);
m_DeviceOpened = false;
delete m_Umem;
delete m_Config;
m_Config = nullptr;
m_Umem = nullptr;
}
Expand Down
Loading