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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# VBVX - VPP Buffer View eXtensions

[![Build and Test](https://github.com/llmxio/vbvx/actions/workflows/ci.yml/badge.svg)](https://github.com/llmxio/vbvx/actions/workflows/ci.yml)

VBVX (VPP Buffer View eXtensions) is a small, header-only C++23 library for **zero-copy** parsing of packet buffers. It provides views over common on-wire headers (Ethernet, VLAN, ARP, IPv4/v6, TCP/UDP, ICMP, SRv6) without copying.

## About
Expand Down
12 changes: 6 additions & 6 deletions tests/test_buffer_view.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ TEST(BufferViewTest, EmptyBufferNullptr) {
EXPECT_FALSE(buf.ether_header());
EXPECT_FALSE(buf.ether_type().has_value());
EXPECT_FALSE(buf.vlan_header());
EXPECT_FALSE(buf.ipv4_header());
EXPECT_FALSE(buf.ipv6_header());
EXPECT_FALSE(buf.ip4_header());
EXPECT_FALSE(buf.ip6_header());
EXPECT_FALSE(buf.arp_header());
EXPECT_FALSE(buf.tcp_header());
EXPECT_FALSE(buf.udp_header());
EXPECT_FALSE(buf.icmp_header());
EXPECT_FALSE(buf.icmp4_header());
}

TEST(BufferViewTest, TruncatedEtherHeader) {
Expand Down Expand Up @@ -108,9 +108,9 @@ TEST(BufferViewTest, IPv4IhlTooSmall) {
std::memcpy(data.data() + sizeof(EtherHeader), &ip, sizeof(ip));

BufferView buf(data.data(), static_cast<uint16_t>(data.size()));
auto ip_hdr = buf.ipv4_header();
auto ip_hdr = buf.ip4_header();
ASSERT_TRUE(ip_hdr); // header struct is present (bounds ok)
EXPECT_FALSE(buf.ipv4_ihl_bytes().has_value());
EXPECT_FALSE(buf.ip4_ihl_bytes().has_value());
EXPECT_FALSE(buf.l4_offset().has_value());
EXPECT_FALSE(buf.tcp_header());
}
Expand Down Expand Up @@ -214,7 +214,7 @@ TEST(BufferViewTest, IPv6Icmpv6) {

BufferView buf(buf_bytes.data(), static_cast<uint16_t>(buf_bytes.size()));
EXPECT_EQ(buf.ip_protocol().value(), IpProtocol::ICMPv6);
auto ic = buf.icmpv6_header();
auto ic = buf.icmp6_header();
ASSERT_TRUE(ic);
EXPECT_EQ(ic->type_u8(), static_cast<uint8_t>(ICMPv4Type::EchoRequest));
EXPECT_EQ(ic->checksum(), 0x1234u);
Expand Down
26 changes: 13 additions & 13 deletions tests/test_ip4_header.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ TEST(BufferViewIPv4MalformedTest, Ipv4HeaderTooShort) {
eth->type_be = autoswap(std::to_underlying(EtherType::IPv4));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_FALSE(buf.ipv4_header());
EXPECT_FALSE(buf.ip4_header());
EXPECT_FALSE(buf.l4_offset());
EXPECT_FALSE(buf.ip_protocol().has_value());
}
Expand All @@ -279,7 +279,7 @@ TEST(BufferViewIPv4MalformedTest, Ipv4HeaderPresentAndL4Offset) {
autoswap(static_cast<uint16_t>(sizeof(IPv4Header) + sizeof(UDPHeader)));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
ASSERT_TRUE(buf.ip_protocol().has_value());
EXPECT_EQ(buf.ip_protocol().value(), IpProtocol::UDP);
ASSERT_TRUE(buf.l4_offset());
Expand Down Expand Up @@ -333,15 +333,15 @@ TEST(BufferViewIPv4MalformedTest, TcpOptionsTruncated) {
tcp->data_offset = static_cast<uint8_t>((10u << 4) & 0xF0u);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto th = buf.tcp_header();
ASSERT_TRUE(th);
EXPECT_EQ(th->header_words(), 10u);
EXPECT_EQ(th->header_bytes(), 40u);

const auto l3 = buf.l3_offset();
ASSERT_EQ(l3, static_cast<uint16_t>(sizeof(EtherHeader)));
const auto ihl = buf.ipv4_ihl_bytes();
const auto ihl = buf.ip4_ihl_bytes();
ASSERT_TRUE(ihl.has_value());
const auto avail_transport = static_cast<uint16_t>(raw.size()) - (l3 + *ihl);
// available transport bytes are smaller than declared header_bytes ->
Expand Down Expand Up @@ -370,7 +370,7 @@ TEST(BufferViewIPv4MalformedTest, TcpDataOffsetTooSmall) {
tcp->data_offset = static_cast<uint8_t>((4u << 4) & 0xF0u);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto th = buf.tcp_header();
// header_at requires at least 20 bytes so HeaderView exists, but data_offset
// indicates invalid header size
Expand Down Expand Up @@ -399,7 +399,7 @@ TEST(BufferViewIPv4MalformedTest, UdpLengthTooSmallAndTooLarge) {
uh->length_be = autoswap(static_cast<uint16_t>(4u)); // less than 8

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto u = buf.udp_header();
ASSERT_TRUE(u);
EXPECT_LT(u->length(), static_cast<uint16_t>(sizeof(UDPHeader)));
Expand Down Expand Up @@ -427,12 +427,12 @@ TEST(BufferViewIPv4MalformedTest, UdpLengthTooSmallAndTooLarge) {
static_cast<uint16_t>(512u)); // claims much larger than available

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto u = buf.udp_header();
ASSERT_TRUE(u);

const auto l3 = buf.l3_offset();
const auto ihl = buf.ipv4_ihl_bytes();
const auto ihl = buf.ip4_ihl_bytes();
ASSERT_TRUE(ihl.has_value());
const auto ip_payload_bytes = static_cast<uint16_t>(
ip->total_length_be ? ip->total_length_be / 1 : 0);
Expand All @@ -458,7 +458,7 @@ TEST(BufferViewIPv4MalformedTest, VlanIpv4Offset) {
BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_EQ(buf.l3_offset(),
static_cast<uint16_t>(sizeof(EtherHeader) + sizeof(VlanHeader)));
EXPECT_TRUE(buf.ipv4_header());
EXPECT_TRUE(buf.ip4_header());
}

TEST(BufferViewIPv4MalformedTest, IpProtocolNullOnNonIpFrames) {
Expand All @@ -467,8 +467,8 @@ TEST(BufferViewIPv4MalformedTest, IpProtocolNullOnNonIpFrames) {
eth->type_be = autoswap(std::to_underlying(EtherType::ARP));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_FALSE(buf.ipv4_header());
EXPECT_FALSE(buf.ipv6_header());
EXPECT_FALSE(buf.ip4_header());
EXPECT_FALSE(buf.ip6_header());
EXPECT_FALSE(buf.ip_protocol().has_value());
}

Expand All @@ -485,8 +485,8 @@ TEST(BufferViewIPv4MalformedTest, Ipv4HeaderIhlTooSmall) {
ip->protocol = static_cast<uint8_t>(IpProtocol::TCP);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
EXPECT_FALSE(buf.ipv4_ihl_bytes());
ASSERT_TRUE(buf.ip4_header());
EXPECT_FALSE(buf.ip4_ihl_bytes());
EXPECT_FALSE(buf.l4_offset());
// protocol is still readable from the header itself
ASSERT_TRUE(buf.ip_protocol().has_value());
Expand Down
20 changes: 10 additions & 10 deletions tests/test_ip6_header.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ TEST(BufferViewIPv6MalformedTest, Ipv6HeaderTooShort) {
eth->type_be = autoswap(std::to_underlying(EtherType::IPv6));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_FALSE(buf.ipv6_header());
EXPECT_FALSE(buf.ip6_header());
EXPECT_FALSE(buf.l4_offset());
EXPECT_FALSE(buf.ip_protocol().has_value());
}
Expand All @@ -230,7 +230,7 @@ TEST(BufferViewIPv6MalformedTest, Ipv6HeaderPresentAndL4Offset) {
ip->payload_length_be = autoswap(static_cast<uint16_t>(sizeof(UDPHeader)));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv6_header());
ASSERT_TRUE(buf.ip6_header());
ASSERT_TRUE(buf.ip_protocol().has_value());
EXPECT_EQ(buf.ip_protocol().value(), IpProtocol::UDP);
ASSERT_TRUE(buf.l4_offset());
Expand Down Expand Up @@ -276,7 +276,7 @@ TEST(BufferViewIPv6MalformedTest, VlanIpv6Offset) {
BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_EQ(buf.l3_offset(),
static_cast<uint16_t>(sizeof(EtherHeader) + sizeof(VlanHeader)));
EXPECT_TRUE(buf.ipv6_header());
EXPECT_TRUE(buf.ip6_header());
}

TEST(BufferViewIPv6MalformedTest, IpProtocolNullOnNonIpFrames) {
Expand All @@ -285,8 +285,8 @@ TEST(BufferViewIPv6MalformedTest, IpProtocolNullOnNonIpFrames) {
eth->type_be = autoswap(std::to_underlying(EtherType::ARP));

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
EXPECT_FALSE(buf.ipv4_header());
EXPECT_FALSE(buf.ipv6_header());
EXPECT_FALSE(buf.ip4_header());
EXPECT_FALSE(buf.ip6_header());
EXPECT_FALSE(buf.ip_protocol().has_value());
}

Expand All @@ -310,7 +310,7 @@ TEST(BufferViewIPv6MalformedTest, TcpOptionsTruncated) {
tcp->data_offset = static_cast<uint8_t>((10u << 4) & 0xF0u);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv6_header());
ASSERT_TRUE(buf.ip6_header());
auto th = buf.tcp_header();
ASSERT_TRUE(th);
EXPECT_EQ(th->header_words(), 10u);
Expand Down Expand Up @@ -344,7 +344,7 @@ TEST(BufferViewIPv6MalformedTest, TcpDataOffsetTooSmall) {
tcp->data_offset = static_cast<uint8_t>((4u << 4) & 0xF0u);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv6_header());
ASSERT_TRUE(buf.ip6_header());
auto th = buf.tcp_header();
// header_at requires at least 20 bytes so HeaderView exists, but data_offset
// indicates invalid header size
Expand All @@ -371,7 +371,7 @@ TEST(BufferViewIPv6MalformedTest, UdpLengthTooSmallAndTooLarge) {
uh->length_be = autoswap(static_cast<uint16_t>(4u)); // less than 8

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv6_header());
ASSERT_TRUE(buf.ip6_header());
auto u = buf.udp_header();
ASSERT_TRUE(u);
EXPECT_LT(u->length(), static_cast<uint16_t>(sizeof(UDPHeader)));
Expand All @@ -397,10 +397,10 @@ TEST(BufferViewIPv6MalformedTest, UdpLengthTooSmallAndTooLarge) {
static_cast<uint16_t>(sizeof(UDPHeader) + 10u)); // larger than payload

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv6_header());
ASSERT_TRUE(buf.ip6_header());
auto u = buf.udp_header();
ASSERT_TRUE(u);
// UDP length > IPv6 payload length
EXPECT_GT(u->length(), buf.ipv6_header()->payload_length());
EXPECT_GT(u->length(), buf.ip6_header()->payload_length());
}
}
12 changes: 6 additions & 6 deletions tests/test_tcp_header.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ TEST(TCPHeader, TcpOptionsExactlyFit) {
static_cast<uint8_t>((6u << 4) & 0xF0u); // 6 words -> 24 bytes

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto th = buf.tcp_header();
ASSERT_TRUE(th);
EXPECT_EQ(th->header_words(), 6u);
EXPECT_EQ(th->header_bytes(), 24u);

const auto l3 = buf.l3_offset();
ASSERT_EQ(l3, static_cast<uint16_t>(sizeof(EtherHeader)));
const auto ihl = buf.ipv4_ihl_bytes();
const auto ihl = buf.ip4_ihl_bytes();
ASSERT_TRUE(ihl.has_value());
const auto avail_transport = static_cast<uint16_t>(raw.size()) - (l3 + *ihl);
EXPECT_EQ(avail_transport, th->header_bytes());
Expand All @@ -180,15 +180,15 @@ TEST(TCPHeader, TcpMaxDataOffsetTruncated) {
static_cast<uint8_t>((15u << 4) & 0xF0u); // 15 words -> 60 bytes

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto th = buf.tcp_header();
ASSERT_TRUE(th);
EXPECT_EQ(th->header_words(), 15u);
EXPECT_EQ(th->header_bytes(), 60u);

const auto l3 = buf.l3_offset();
ASSERT_EQ(l3, static_cast<uint16_t>(sizeof(EtherHeader)));
const auto ihl = buf.ipv4_ihl_bytes();
const auto ihl = buf.ip4_ihl_bytes();
ASSERT_TRUE(ihl.has_value());
const auto avail_transport = static_cast<uint16_t>(raw.size()) - (l3 + *ihl);
EXPECT_LT(avail_transport, th->header_bytes());
Expand All @@ -215,7 +215,7 @@ TEST(TCPHeader, DataOffsetLowNibbleIgnored) {
tcp->data_offset = static_cast<uint8_t>(((7u << 4) & 0xF0u) | 0x0Fu);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
auto th = buf.tcp_header();
ASSERT_TRUE(th);
EXPECT_EQ(th->header_words(), 7u);
Expand All @@ -224,7 +224,7 @@ TEST(TCPHeader, DataOffsetLowNibbleIgnored) {
EXPECT_TRUE(th->valid_min_size());
const auto l3 = buf.l3_offset();
ASSERT_EQ(l3, static_cast<uint16_t>(sizeof(EtherHeader)));
const auto ihl = buf.ipv4_ihl_bytes();
const auto ihl = buf.ip4_ihl_bytes();
ASSERT_TRUE(ihl.has_value());
const auto avail_transport = static_cast<uint16_t>(raw.size()) - (l3 + *ihl);
EXPECT_EQ(avail_transport, th->header_bytes());
Expand Down
2 changes: 1 addition & 1 deletion tests/test_udp_header.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ TEST(UDPHeader, BufferViewIntegration) {
uh->set_dst_port(4321);

BufferView buf(raw.data(), static_cast<uint16_t>(raw.size()));
ASSERT_TRUE(buf.ipv4_header());
ASSERT_TRUE(buf.ip4_header());
ASSERT_TRUE(buf.ip_protocol().has_value());
EXPECT_EQ(buf.ip_protocol().value(), IpProtocol::UDP);

Expand Down
Loading