af_packet: Add support for defragmentation of IP packets.

This commit is contained in:
Jan Grashoefer 2020-05-07 18:53:49 +02:00 committed by Tim Wojtulewicz
parent 57dd239917
commit 4deb8f6402
4 changed files with 19 additions and 8 deletions

View file

@ -11,6 +11,8 @@ export {
const enable_hw_timestamping = F &redef; const enable_hw_timestamping = F &redef;
## Toggle whether to use PACKET_FANOUT. ## Toggle whether to use PACKET_FANOUT.
const enable_fanout = T &redef; const enable_fanout = T &redef;
## Toggle defragmentation of IP packets using PACKET_FANOUT_FLAG_DEFRAG.
const enable_defrag = F &redef;
## Fanout Mode. ## Fanout Mode.
const fanout_mode = FANOUT_HASH &redef; const fanout_mode = FANOUT_HASH &redef;
## Fanout ID. ## Fanout ID.

View file

@ -28,6 +28,7 @@ void AF_PacketSource::Open()
uint64_t buffer_size = BifConst::AF_Packet::buffer_size; uint64_t buffer_size = BifConst::AF_Packet::buffer_size;
bool enable_hw_timestamping = BifConst::AF_Packet::enable_hw_timestamping; bool enable_hw_timestamping = BifConst::AF_Packet::enable_hw_timestamping;
bool enable_fanout = BifConst::AF_Packet::enable_fanout; bool enable_fanout = BifConst::AF_Packet::enable_fanout;
bool enable_defrag = BifConst::AF_Packet::enable_defrag;
socket_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); socket_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
@ -129,7 +130,7 @@ inline bool AF_PacketSource::EnablePromiscMode()
return (ret >= 0); return (ret >= 0);
} }
inline bool AF_PacketSource::ConfigureFanoutGroup(bool enabled) inline bool AF_PacketSource::ConfigureFanoutGroup(bool enabled, bool defrag)
{ {
if ( enabled ) if ( enabled )
{ {
@ -137,7 +138,7 @@ inline bool AF_PacketSource::ConfigureFanoutGroup(bool enabled)
int ret; int ret;
fanout_id = BifConst::AF_Packet::fanout_id; fanout_id = BifConst::AF_Packet::fanout_id;
fanout_arg = ((fanout_id & 0xffff) | (GetFanoutMode() << 16)); fanout_arg = ((fanout_id & 0xffff) | (GetFanoutMode(defrag) << 16));
ret = setsockopt(socket_fd, SOL_PACKET, PACKET_FANOUT, ret = setsockopt(socket_fd, SOL_PACKET, PACKET_FANOUT,
&fanout_arg, sizeof(fanout_arg)); &fanout_arg, sizeof(fanout_arg));
@ -176,15 +177,22 @@ inline bool AF_PacketSource::ConfigureHWTimestamping(bool enabled)
return true; return true;
} }
inline uint32_t AF_PacketSource::GetFanoutMode() inline uint32_t AF_PacketSource::GetFanoutMode(bool defrag)
{ {
uint32_t fanout_mode;
switch ( BifConst::AF_Packet::fanout_mode->AsEnum() ) { switch ( BifConst::AF_Packet::fanout_mode->AsEnum() ) {
case BifEnum::AF_Packet::FANOUT_CPU: return PACKET_FANOUT_CPU; case BifEnum::AF_Packet::FANOUT_CPU: fanout_mode = PACKET_FANOUT_CPU;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
case BifEnum::AF_Packet::FANOUT_QM: return PACKET_FANOUT_QM; case BifEnum::AF_Packet::FANOUT_QM: fanout_mode = PACKET_FANOUT_QM;
#endif #endif
default: return PACKET_FANOUT_HASH; default: fanout_mode = PACKET_FANOUT_HASH;
} }
if ( defrag )
fanout_mode |= PACKET_FANOUT_FLAG_DEFRAG;
return fanout_mode;
} }
void AF_PacketSource::Close() void AF_PacketSource::Close()

View file

@ -69,9 +69,9 @@ private:
bool BindInterface(); bool BindInterface();
bool EnablePromiscMode(); bool EnablePromiscMode();
bool ConfigureFanoutGroup(bool enabled); bool ConfigureFanoutGroup(bool enabled, bool defrag=false);
bool ConfigureHWTimestamping(bool enabled); bool ConfigureHWTimestamping(bool enabled);
uint32_t GetFanoutMode(); uint32_t GetFanoutMode(bool defrag=false);
}; };
} }

View file

@ -12,5 +12,6 @@ enum FanoutMode %{
const buffer_size: count; const buffer_size: count;
const enable_hw_timestamping: bool; const enable_hw_timestamping: bool;
const enable_fanout: bool; const enable_fanout: bool;
const enable_defrag: bool;
const fanout_mode: FanoutMode; const fanout_mode: FanoutMode;
const fanout_id: count; const fanout_id: count;