Skip to content

Commit

Permalink
Polish code
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Nov 26, 2020
1 parent fe58aaa commit 8b7f3f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
8 changes: 5 additions & 3 deletions vslib/inc/SwitchStateBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ namespace saivs
sai_status_t removeDebugCounter(
_In_ sai_object_id_t objectId);

public:

static int promisc(
_In_ const char *dev);

protected: // custom hostif

sai_status_t createHostif(
Expand Down Expand Up @@ -394,9 +399,6 @@ namespace saivs
_In_ const char *dev,
_In_ const sai_mac_t& mac);

static int promisc(
_In_ const char *dev);

static int get_default_gw_mac_address(
_Out_ sai_mac_t& mac);

Expand Down
11 changes: 7 additions & 4 deletions vslib/inc/TrafficForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <stddef.h>
#include <sys/socket.h>

static constexpr size_t ETH_FRAME_BUFFER_SIZE = 0x4000;
static constexpr size_t CONTROL_MESSAGE_BUFFER_SIZE = 0x1000;

namespace saivs
{
class TrafficForwarder
Expand All @@ -15,15 +18,15 @@ namespace saivs
protected:
TrafficForwarder() = default;

void add_vlan_tag(
static void addVlanTag(
_Inout_ unsigned char *buffer,
_Inout_ size_t &length,
_Inout_ struct msghdr &msg) const;
_Inout_ struct msghdr &msg);

bool send_to(
bool sendTo(
_In_ int fd,
_In_ const unsigned char *buffer,
_In_ size_t &length) const;
_In_ size_t length) const;

};
}
10 changes: 2 additions & 8 deletions vslib/src/HostInterfaceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ bool HostInterfaceInfo::uninstallTap2EthFilter(
return m_t2eFilters.uninstallFilter(filter);
}

#define ETH_FRAME_BUFFER_SIZE (0x4000)
#define CONTROL_MESSAGE_BUFFER_SIZE (0x1000)
#define IEEE_8021Q_ETHER_TYPE (0x8100)
#define MAC_ADDRESS_SIZE (6)
#define VLAN_TAG_SIZE (4)

void HostInterfaceInfo::veth2tap_fun()
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -210,11 +204,11 @@ void HostInterfaceInfo::veth2tap_fun()
return;
}

add_vlan_tag(buffer, length, msg);
addVlanTag(buffer, length, msg);

async_process_packet_for_fdb_event(buffer, length);

if (!send_to(m_tapfd, buffer, length))
if (!sendTo(m_tapfd, buffer, length))
{
break;
}
Expand Down
10 changes: 2 additions & 8 deletions vslib/src/MACsecForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

using namespace saivs;

#define ETH_FRAME_BUFFER_SIZE (0x4000)
#define CONTROL_MESSAGE_BUFFER_SIZE (0x1000)
#define IEEE_8021Q_ETHER_TYPE (0x8100)
#define MAC_ADDRESS_SIZE (6)
#define VLAN_TAG_SIZE (4)

MACsecForwarder::MACsecForwarder(
_In_ const std::string &macsecInterfaceName,
_In_ std::shared_ptr<HostInterfaceInfo> info):
Expand Down Expand Up @@ -188,11 +182,11 @@ void MACsecForwarder::forward()

size_t length = static_cast<size_t>(size);

add_vlan_tag(buffer, length, msg);
addVlanTag(buffer, length, msg);

m_info->async_process_packet_for_fdb_event(buffer, length);

if (!send_to(m_info->m_tapfd, buffer, length))
if (!sendTo(m_info->m_tapfd, buffer, length))
{
break;
}
Expand Down
19 changes: 12 additions & 7 deletions vslib/src/TrafficForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@

using namespace saivs;

#define ETH_FRAME_BUFFER_SIZE (0x4000)
#define CONTROL_MESSAGE_BUFFER_SIZE (0x1000)
#define IEEE_8021Q_ETHER_TYPE (0x8100)
#define MAC_ADDRESS_SIZE (6)
#define VLAN_TAG_SIZE (4)

void TrafficForwarder::add_vlan_tag(
void TrafficForwarder::addVlanTag(
_Inout_ unsigned char *buffer,
_Inout_ size_t &length,
_Inout_ struct msghdr &msg) const
_Inout_ struct msghdr &msg)
{
SWSS_LOG_ENTER();

Expand All @@ -46,6 +44,11 @@ void TrafficForwarder::add_vlan_tag(
{
SWSS_LOG_DEBUG("got vlan tci: 0x%x, vlanid: %d", aux->tp_vlan_tci, aux->tp_vlan_tci & 0xFFF);

if ((length + VLAN_TAG_SIZE) > ETH_FRAME_BUFFER_SIZE)
{
SWSS_LOG_THROW("The VLAN packet size %lu exceeds the ETH_FRAME_BUFFER_SIZE", length + VLAN_TAG_SIZE);
}

// inject vlan tag into frame

// for overlapping buffers
Expand All @@ -67,10 +70,10 @@ void TrafficForwarder::add_vlan_tag(
}
}

bool TrafficForwarder::send_to(
bool TrafficForwarder::sendTo(
_In_ int fd,
_In_ const unsigned char *buffer,
_In_ size_t &length) const
_In_ size_t length) const
{
SWSS_LOG_ENTER();

Expand All @@ -84,7 +87,9 @@ bool TrafficForwarder::send_to(
if (errno != ENETDOWN && errno != EIO)
{
SWSS_LOG_ERROR("failed to write to device fd %d, errno(%d): %s",
fd, errno, strerror(errno));
fd,
errno,
strerror(errno));
}

if (errno == EBADF)
Expand Down

0 comments on commit 8b7f3f8

Please sign in to comment.