Skip to content

Commit 8b5a401

Browse files
Routed subinterface enhancements (sonic-net#2017)
* Routed subinterface library implementation
1 parent cdea5e9 commit 8b5a401

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

lib/subintf.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <cerrno>
2+
#include <cstring>
3+
#include <array>
4+
#include <net/if.h>
5+
#include "subintf.h"
6+
7+
using namespace swss;
8+
9+
subIntf::subIntf(const std::string &ifName)
10+
{
11+
alias = ifName;
12+
size_t found = alias.find(VLAN_SUB_INTERFACE_SEPARATOR);
13+
if (found != std::string::npos)
14+
{
15+
parentIf = alias.substr(0, found);
16+
if (!parentIf.compare(0, strlen("Eth"), "Eth"))
17+
{
18+
std::string parentIfName;
19+
if (!parentIf.compare(0, strlen("Ethernet"), "Ethernet"))
20+
{
21+
parentIfShortName = "Eth" + parentIf.substr(strlen("Ethernet"));
22+
isCompressed = false;
23+
parentIfName = "Ethernet" + parentIf.substr(strlen("Ethernet"));
24+
}
25+
else
26+
{
27+
parentIfShortName = "Eth" + parentIf.substr(strlen("Eth"));
28+
isCompressed = true;
29+
parentIfName = "Ethernet" + parentIf.substr(strlen("Eth"));
30+
}
31+
parentIf = parentIfName;
32+
subIfIdx = alias.substr(found + 1);
33+
}
34+
else if (!parentIf.compare(0, strlen("Po"), "Po"))
35+
{
36+
if (!parentIf.compare(0, strlen("PortChannel"), "PortChannel"))
37+
{
38+
isCompressed = false;
39+
parentIfShortName = "Po" + parentIf.substr(strlen("PortChannel"));
40+
parentIf = "PortChannel" + parentIf.substr(strlen("PortChannel"));
41+
}
42+
else
43+
{
44+
isCompressed = true;
45+
parentIfShortName = "Po" + parentIf.substr(strlen("Po"));
46+
parentIf = "PortChannel" + parentIf.substr(strlen("Po"));
47+
}
48+
subIfIdx = alias.substr(found + 1);
49+
}
50+
else
51+
{
52+
subIfIdx = "";
53+
}
54+
}
55+
}
56+
57+
bool subIntf::isValid() const
58+
{
59+
if (subIfIdx == "")
60+
{
61+
return false;
62+
}
63+
else if (alias.length() >= IFNAMSIZ)
64+
{
65+
return false;
66+
}
67+
68+
return true;
69+
}
70+
71+
std::string subIntf::parentIntf() const
72+
{
73+
return parentIf;
74+
}
75+
76+
int subIntf::subIntfIdx() const
77+
{
78+
uint16_t id;
79+
try
80+
{
81+
id = static_cast<uint16_t>(stoul(subIfIdx));
82+
}
83+
catch (const std::invalid_argument &e)
84+
{
85+
return -1;
86+
}
87+
catch (const std::out_of_range &e)
88+
{
89+
return -1;
90+
}
91+
return id;
92+
}
93+
94+
std::string subIntf::longName() const
95+
{
96+
if (isValid() == false)
97+
return "";
98+
99+
return (parentIf + VLAN_SUB_INTERFACE_SEPARATOR + subIfIdx);
100+
}
101+
102+
std::string subIntf::shortName() const
103+
{
104+
if (isValid() == false)
105+
return "";
106+
107+
return (parentIfShortName + VLAN_SUB_INTERFACE_SEPARATOR + subIfIdx);
108+
}
109+
110+
bool subIntf::isShortName() const
111+
{
112+
return ((isCompressed == true) ? true : false);
113+
}

lib/subintf.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#define VLAN_SUB_INTERFACE_SEPARATOR "."
4+
namespace swss {
5+
class subIntf
6+
{
7+
public:
8+
subIntf(const std::string &ifName);
9+
bool isValid() const;
10+
std::string parentIntf() const;
11+
int subIntfIdx() const;
12+
std::string longName() const;
13+
std::string shortName() const;
14+
bool isShortName() const;
15+
16+
private:
17+
std::string alias;
18+
std::string subIfIdx;
19+
std::string parentIf;
20+
std::string parentIfShortName;
21+
bool isCompressed = false;
22+
};
23+
}

0 commit comments

Comments
 (0)