Skip to content

Commit

Permalink
Routed subinterface enhancements (sonic-net#2017)
Browse files Browse the repository at this point in the history
* Routed subinterface library implementation
  • Loading branch information
preetham-singh authored Nov 15, 2021
1 parent cdea5e9 commit 8b5a401
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
113 changes: 113 additions & 0 deletions lib/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);
}
23 changes: 23 additions & 0 deletions lib/subintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#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;
};
}

0 comments on commit 8b5a401

Please sign in to comment.