Skip to content

Commit

Permalink
Add TrafficForwarder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 23, 2021
1 parent 1b4d6d1 commit 5db4e72
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
3 changes: 2 additions & 1 deletion unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ tests_SOURCES = main.cpp \
TestSaiUnittests.cpp \
TestSelectableFd.cpp \
TestSignal.cpp \
TestSwitchConfigContainer.cpp
TestSwitchConfigContainer.cpp \
TestTrafficForwarder.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 \
Expand Down
66 changes: 66 additions & 0 deletions unittest/vslib/TestTrafficForwarder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "TrafficForwarder.h"

#include <linux/if_packet.h>

#include <gtest/gtest.h>

using namespace saivs;

typedef union _control
{
char control_data[CMSG_SPACE(sizeof(tpacket_auxdata))];
struct cmsghdr cmsg;

} control;

static_assert(sizeof(cmsghdr) == 16, "header must be 8 bytes");
static_assert(sizeof(control) >= (sizeof(cmsghdr) + sizeof(tpacket_auxdata)), "control must at least include both");

TEST(TrafficForwarder, addVlanTag)
{
uint8_t buffer[0x1000];

size_t length = 1;

struct msghdr hdr;

memset(&hdr, 0, sizeof(hdr));

EXPECT_FALSE(TrafficForwarder::addVlanTag(buffer, length, hdr));

control p;

memset(&p, 0, sizeof(p));

hdr.msg_controllen = sizeof(p);
hdr.msg_control = &p;

EXPECT_FALSE(TrafficForwarder::addVlanTag(buffer, length, hdr));

cmsghdr* cmsg = &p.cmsg;

cmsg->cmsg_level = SOL_PACKET;
cmsg->cmsg_type = PACKET_AUXDATA;

EXPECT_FALSE(TrafficForwarder::addVlanTag(buffer, length, hdr));

struct tpacket_auxdata* aux = (struct tpacket_auxdata*)CMSG_DATA(cmsg);

// https://en.wikipedia.org/wiki/IEEE_802.1Q
//
// TPID(16) | TCI(16)
// | PCP(3) DEI(1) VID(12)

aux->tp_status |= TP_STATUS_VLAN_VALID;
aux->tp_status |= TP_STATUS_VLAN_TPID_VALID;

length = ETH_FRAME_BUFFER_SIZE;

EXPECT_THROW(TrafficForwarder::addVlanTag(buffer, length, hdr), std::runtime_error);

length = 64;

EXPECT_TRUE(TrafficForwarder::addVlanTag(buffer, length, hdr));

EXPECT_EQ(length, 68);
}
6 changes: 4 additions & 2 deletions vslib/TrafficForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace saivs;
#define MAC_ADDRESS_SIZE (6)
#define VLAN_TAG_SIZE (4)

void TrafficForwarder::addVlanTag(
bool TrafficForwarder::addVlanTag(
_Inout_ unsigned char *buffer,
_Inout_ size_t &length,
_Inout_ struct msghdr &msg)
Expand Down Expand Up @@ -65,9 +65,11 @@ void TrafficForwarder::addVlanTag(

length += VLAN_TAG_SIZE;

break;
return true;
}
}

return false;
}

bool TrafficForwarder::sendTo(
Expand Down
2 changes: 1 addition & 1 deletion vslib/TrafficForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace saivs

public:

static void addVlanTag(
static bool addVlanTag(
_Inout_ unsigned char *buffer,
_Inout_ size_t &length,
_Inout_ struct msghdr &msg);
Expand Down

0 comments on commit 5db4e72

Please sign in to comment.