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 #2017

Merged
merged 2 commits into from
Nov 15, 2021
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
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;
};
}