Skip to content

Commit 3778673

Browse files
[vs] SAI support for VOQ switches (sonic-net#698)
Core Port Index Map for VOQ system port vs test support. This class has stores <core, core port index> mapping for all the local ports. The core port index mapping is supplied via coreportindexmap.ini file
1 parent 036ffba commit 3778673

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed

vslib/inc/CorePortIndexMap.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include "swss/sal.h"
4+
5+
#include <inttypes.h>
6+
7+
#include <map>
8+
#include <vector>
9+
#include <string>
10+
#include <memory>
11+
12+
namespace saivs
13+
{
14+
class CorePortIndexMap
15+
{
16+
public:
17+
18+
constexpr static uint32_t DEFAULT_SWITCH_INDEX = 0;
19+
20+
public:
21+
22+
CorePortIndexMap(
23+
_In_ uint32_t switchIndex);
24+
25+
virtual ~CorePortIndexMap() = default;
26+
27+
public:
28+
29+
bool add(
30+
_In_ const std::string& ifname,
31+
_In_ const std::vector<uint32_t>& lanes);
32+
33+
bool remove(
34+
_In_ const std::string& ifname);
35+
36+
uint32_t getSwitchIndex() const;
37+
38+
bool isEmpty() const;
39+
40+
bool hasInterface(
41+
_In_ const std::string& ifname) const;
42+
43+
const std::vector<std::vector<uint32_t>> getCorePortIndexVector() const;
44+
45+
/**
46+
* @brief Get interface from core and core port index.
47+
*
48+
* @return Interface name or empty string if core and core port index are not found.
49+
*/
50+
std::string getInterfaceFromCorePortIndex(
51+
_In_ const std::vector<uint32_t>& corePortIndex) const;
52+
53+
public:
54+
55+
static std::shared_ptr<CorePortIndexMap> getDefaultCorePortIndexMap(
56+
_In_ uint32_t switchIndex = DEFAULT_SWITCH_INDEX);
57+
58+
private:
59+
60+
uint32_t m_switchIndex;
61+
62+
std::map<std::vector<uint32_t>, std::string> m_corePortIndexToIfName;
63+
64+
std::map<std::string, std::vector<uint32_t>> m_ifNameToCorePortIndex;
65+
66+
std::vector<std::vector<uint32_t>> m_corePortIndexMap;
67+
};
68+
}

vslib/src/CorePortIndexMap.cpp

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include "CorePortIndexMap.h"
2+
3+
#include "swss/logger.h"
4+
5+
#include <set>
6+
7+
using namespace saivs;
8+
9+
#define VS_IF_PREFIX "eth"
10+
11+
CorePortIndexMap::CorePortIndexMap(
12+
_In_ uint32_t switchIndex):
13+
m_switchIndex(switchIndex)
14+
{
15+
SWSS_LOG_ENTER();
16+
17+
// empty
18+
}
19+
20+
uint32_t CorePortIndexMap::getSwitchIndex() const
21+
{
22+
SWSS_LOG_ENTER();
23+
24+
return m_switchIndex;
25+
}
26+
27+
bool CorePortIndexMap::add(
28+
_In_ const std::string& ifname,
29+
_In_ const std::vector<uint32_t>& corePortIndex)
30+
{
31+
SWSS_LOG_ENTER();
32+
33+
auto n = corePortIndex.size();
34+
35+
if (n != 2)
36+
{
37+
SWSS_LOG_ERROR("Invalid corePortIndex. Core port index must have core and core port index %d, %s", n, ifname.c_str());
38+
return false;
39+
}
40+
41+
if (m_ifNameToCorePortIndex.find(ifname) != m_ifNameToCorePortIndex.end())
42+
{
43+
SWSS_LOG_ERROR("interface %s already in core port index map (%d, %d)", ifname.c_str(), corePortIndex[0], corePortIndex[1]);
44+
return false;
45+
}
46+
47+
m_corePortIndexMap.push_back(corePortIndex);
48+
49+
m_corePortIndexToIfName[corePortIndex] = ifname;
50+
51+
m_ifNameToCorePortIndex[ifname] = corePortIndex;
52+
53+
return true;
54+
}
55+
56+
bool CorePortIndexMap::remove(
57+
_In_ const std::string& ifname)
58+
{
59+
SWSS_LOG_ENTER();
60+
61+
auto it = m_ifNameToCorePortIndex.find(ifname);
62+
63+
if (it == m_ifNameToCorePortIndex.end())
64+
{
65+
SWSS_LOG_ERROR("interfce %s does not have core port index in switch %d", ifname.c_str(), m_switchIndex);
66+
return false;
67+
}
68+
69+
auto corePortIndex = it->second;
70+
71+
m_corePortIndexToIfName.erase(corePortIndex);
72+
73+
for (size_t idx = 0; idx < m_corePortIndexMap.size(); idx++)
74+
{
75+
if (m_corePortIndexMap[idx][0] == corePortIndex[0] && m_corePortIndexMap[idx][1] == corePortIndex[1])
76+
{
77+
m_corePortIndexMap.erase(m_corePortIndexMap.begin() + idx);
78+
break;
79+
}
80+
}
81+
82+
m_ifNameToCorePortIndex.erase(it);
83+
84+
return true;
85+
}
86+
87+
std::shared_ptr<CorePortIndexMap> CorePortIndexMap::getDefaultCorePortIndexMap(
88+
_In_ uint32_t switchIndex)
89+
{
90+
SWSS_LOG_ENTER();
91+
92+
const uint32_t defaultPortCount = 32;
93+
94+
uint32_t defaultCorePortIndexMap[defaultPortCount * 2] = {
95+
0,1,
96+
0,2,
97+
0,3,
98+
0,4,
99+
0,5,
100+
0,6,
101+
0,7,
102+
0,8,
103+
0,9,
104+
0,10,
105+
0,11,
106+
0,12,
107+
0,13,
108+
0,14,
109+
0,15,
110+
0,16,
111+
1,1,
112+
1,2,
113+
1,3,
114+
1,4,
115+
1,5,
116+
1,6,
117+
1,7,
118+
1,8,
119+
1,9,
120+
1,10,
121+
1,11,
122+
1,12,
123+
1,13,
124+
1,14,
125+
1,15,
126+
1,16
127+
};
128+
129+
auto corePortIndexMap = std::make_shared<CorePortIndexMap>(switchIndex);
130+
131+
for (uint32_t idx = 0; idx < defaultPortCount; idx++)
132+
{
133+
auto ifname = VS_IF_PREFIX + std::to_string(idx);
134+
135+
std::vector<uint32_t> cpidx;
136+
137+
cpidx.push_back(defaultCorePortIndexMap[idx * 2]);
138+
cpidx.push_back(defaultCorePortIndexMap[idx * 2 + 1]);
139+
140+
corePortIndexMap->add(ifname, cpidx);
141+
}
142+
143+
return corePortIndexMap;
144+
}
145+
146+
bool CorePortIndexMap::isEmpty() const
147+
{
148+
SWSS_LOG_ENTER();
149+
150+
return m_ifNameToCorePortIndex.size() == 0;
151+
}
152+
153+
bool CorePortIndexMap::hasInterface(
154+
_In_ const std::string& ifname) const
155+
{
156+
SWSS_LOG_ENTER();
157+
158+
return m_ifNameToCorePortIndex.find(ifname) != m_ifNameToCorePortIndex.end();
159+
}
160+
161+
const std::vector<std::vector<uint32_t>> CorePortIndexMap::getCorePortIndexVector() const
162+
{
163+
SWSS_LOG_ENTER();
164+
165+
return m_corePortIndexMap;
166+
}
167+
168+
std::string CorePortIndexMap::getInterfaceFromCorePortIndex(
169+
_In_ const std::vector<uint32_t>& corePortIndex) const
170+
{
171+
SWSS_LOG_ENTER();
172+
173+
auto it = m_corePortIndexToIfName.find(corePortIndex);
174+
175+
if (it == m_corePortIndexToIfName.end())
176+
{
177+
SWSS_LOG_WARN("Core port index (%d, %d) not found on index %u", corePortIndex.at(0), corePortIndex.at(1), m_switchIndex);
178+
179+
return "";
180+
}
181+
182+
return it->second;
183+
}

0 commit comments

Comments
 (0)