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