Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routed subinterface enhancements #529

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ libswsscommon_la_SOURCES = \
subscriberstatetable.cpp \
timestamp.cpp \
warm_restart.cpp \
redisutility.cpp
redisutility.cpp \
subintf.cpp

libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)
libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS) $(CODE_COVERAGE_CPPFLAGS)
Expand Down
113 changes: 113 additions & 0 deletions common/subintf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <cerrno>
#include <cstring>
#include <array>
#include <net/if.h>
#include "subintf.h"

using namespace swss;

subIntf::subIntf(const std::string &ifName)
{
alias = ifName;
size_t found = alias.find(VLAN_SUB_INTERFACE_SEPARATOR);
if (found != std::string::npos)
{
parentIf = alias.substr(0, found);
if (!parentIf.compare(0, strlen("Eth"), "Eth"))
{
std::string parentIfName;
if (!parentIf.compare(0, strlen("Ethernet"), "Ethernet"))
{
parentIfShortName = "Eth" + parentIf.substr(strlen("Ethernet"));
isCompressed = false;
parentIfName = "Ethernet" + parentIf.substr(strlen("Ethernet"));
}
else
{
parentIfShortName = "Eth" + parentIf.substr(strlen("Eth"));
isCompressed = true;
parentIfName = "Ethernet" + parentIf.substr(strlen("Eth"));
}
parentIf = parentIfName;
subIfIdx = alias.substr(found + 1);
}
else if (!parentIf.compare(0, strlen("Po"), "Po"))
{
if (!parentIf.compare(0, strlen("PortChannel"), "PortChannel"))
{
isCompressed = false;
parentIfShortName = "Po" + parentIf.substr(strlen("PortChannel"));
parentIf = "PortChannel" + parentIf.substr(strlen("PortChannel"));
}
else
{
isCompressed = true;
parentIfShortName = "Po" + parentIf.substr(strlen("Po"));
parentIf = "PortChannel" + parentIf.substr(strlen("Po"));
}
subIfIdx = alias.substr(found + 1);
}
else
{
subIfIdx = "";
}
}
}

bool subIntf::isValid() const
{
if (subIfIdx == "")
{
return false;
}
else if (alias.length() >= IFNAMSIZ)
{
return false;
}

return true;
}

std::string subIntf::parentIntf() const
{
return parentIf;
}

int subIntf::subIntfIdx() const
{
uint16_t id;
try
{
id = static_cast<uint16_t>(stoul(subIfIdx));
}
catch (const std::invalid_argument &e)
{
return -1;
}
catch (const std::out_of_range &e)
{
return -1;
}
return id;
}

std::string subIntf::longName() const
{
if (isValid() == false)
return "";

return (parentIf + VLAN_SUB_INTERFACE_SEPARATOR + subIfIdx);
}

std::string subIntf::shortName() const
{
if (isValid() == false)
return "";

return (parentIfShortName + VLAN_SUB_INTERFACE_SEPARATOR + subIfIdx);
}

bool subIntf::isShortName() const
{
return ((isCompressed == true) ? true : false);
}
22 changes: 22 additions & 0 deletions common/subintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#define VLAN_SUB_INTERFACE_SEPARATOR "."
namespace swss {
class subIntf
{
public:
subIntf(const std::string &ifName);
bool isValid() const;
std::string parentIntf() const;
int subIntfIdx() const;
std::string longName() const;
std::string shortName() const;
bool isShortName() const;
private:
std::string alias;
std::string subIfIdx;
std::string parentIf;
std::string parentIfShortName;
bool isCompressed = false;
};
}