|
| 1 | +#include <hiredis/hiredis.h> |
| 2 | +#include <system_error> |
| 3 | + |
| 4 | +#include "common/logger.h" |
| 5 | +#include "common/redisreply.h" |
| 6 | +#include "common/rediscommand.h" |
| 7 | +#include "common/redisapi.h" |
| 8 | +#include "common/json.hpp" |
| 9 | +#include "common/schema.h" |
| 10 | +#include "common/countertable.h" |
| 11 | + |
| 12 | +using namespace std; |
| 13 | +using namespace swss; |
| 14 | + |
| 15 | +const std::string Counter::defaultLuaScript = "return nil"; |
| 16 | + |
| 17 | +/* |
| 18 | + * Port counter type |
| 19 | + */ |
| 20 | +unique_ptr<KeyCache<string>> PortCounter::keyCachePtr = nullptr; |
| 21 | + |
| 22 | +PortCounter::PortCounter(PortCounter::Mode mode) |
| 23 | + : m_mode(mode) |
| 24 | +{ |
| 25 | + m_luaScript = loadLuaScript("portcounter.lua"); |
| 26 | +} |
| 27 | + |
| 28 | +const std::string& |
| 29 | +PortCounter::getLuaScript() const |
| 30 | +{ |
| 31 | + return m_luaScript; |
| 32 | +} |
| 33 | + |
| 34 | +bool |
| 35 | +PortCounter::usingLuaTable(const CounterTable& t, const std::string &name) const |
| 36 | +{ |
| 37 | + // Use LuaTable if and only if it is in UNION mode and has gearbox part. |
| 38 | + // That name map exists means the port has gearbox port part in PHY chip. |
| 39 | + return m_mode == Mode::UNION && |
| 40 | + t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_line"); |
| 41 | +} |
| 42 | + |
| 43 | +std::vector<std::string> |
| 44 | +PortCounter::getLuaKeys(const CounterTable& t, const std::string &name) const |
| 45 | +{ |
| 46 | + if (m_mode != Mode::UNION) |
| 47 | + return {}; |
| 48 | + |
| 49 | + KeyCache<string> &cache = keyCacheInstance(); |
| 50 | + if (cache.enabled()) |
| 51 | + { |
| 52 | + try { |
| 53 | + return {cache.at(name), cache.at(name + "_system"), cache.at(name + "_line")}; |
| 54 | + } |
| 55 | + catch (const std::out_of_range&) { |
| 56 | + return {}; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + auto oidLinesidePtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_line"); |
| 61 | + auto oidSystemsidePtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, name + "_system"); |
| 62 | + auto oidAsicPtr = t.getCountersDB()->hget(COUNTERS_PORT_NAME_MAP, name); |
| 63 | + |
| 64 | + if (oidAsicPtr && oidSystemsidePtr && oidLinesidePtr) |
| 65 | + { |
| 66 | + return {*oidAsicPtr, *oidSystemsidePtr, *oidLinesidePtr}; |
| 67 | + } |
| 68 | + |
| 69 | + return {}; |
| 70 | +} |
| 71 | + |
| 72 | +Counter::KeyPair |
| 73 | +PortCounter::getKey(const CounterTable& t, const std::string &name) const |
| 74 | +{ |
| 75 | + int dbId = COUNTERS_DB; |
| 76 | + string portName = name; |
| 77 | + if (m_mode != Mode::ASIC && m_mode != Mode::UNION) |
| 78 | + { |
| 79 | + dbId = GB_COUNTERS_DB; |
| 80 | + if (m_mode == Mode::SYSTEMSIDE) |
| 81 | + { |
| 82 | + portName = name + "_system"; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + portName = name + "_line"; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + KeyCache<string> &cache = keyCacheInstance(); |
| 91 | + if (cache.enabled()) |
| 92 | + { |
| 93 | + try { |
| 94 | + return {dbId, cache.at(portName)}; |
| 95 | + } |
| 96 | + catch (const std::out_of_range&) { |
| 97 | + return {-1, ""}; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + shared_ptr<std::string> oidPtr = nullptr; |
| 102 | + if (m_mode == Mode::ASIC || m_mode == Mode::UNION) |
| 103 | + { |
| 104 | + oidPtr = t.getCountersDB()->hget(COUNTERS_PORT_NAME_MAP, portName); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + oidPtr = t.getGbcountersDB()->hget(COUNTERS_PORT_NAME_MAP, portName); |
| 109 | + } |
| 110 | + |
| 111 | + if (oidPtr == nullptr) |
| 112 | + return {-1, ""}; |
| 113 | + return {dbId, *oidPtr}; |
| 114 | +} |
| 115 | + |
| 116 | +KeyCache<string>& |
| 117 | +PortCounter::keyCacheInstance(void) |
| 118 | +{ |
| 119 | + if (keyCachePtr == nullptr) |
| 120 | + { |
| 121 | + keyCachePtr.reset(new KeyCache<string>(PortCounter::cachingKey)); |
| 122 | + } |
| 123 | + return *keyCachePtr; |
| 124 | +} |
| 125 | + |
| 126 | +void PortCounter::cachingKey(const CounterTable& t) |
| 127 | +{ |
| 128 | + auto fvs = t.getCountersDB()->hgetall(COUNTERS_PORT_NAME_MAP); |
| 129 | + keyCachePtr->insert(fvs.begin(), fvs.end()); |
| 130 | + |
| 131 | + fvs = t.getGbcountersDB()->hgetall(COUNTERS_PORT_NAME_MAP); |
| 132 | + keyCachePtr->insert(fvs.begin(), fvs.end()); |
| 133 | +} |
| 134 | + |
| 135 | +/* |
| 136 | + * MACSEC counter type |
| 137 | + */ |
| 138 | +unique_ptr<KeyCache<Counter::KeyPair>> MacsecCounter::keyCachePtr = nullptr; |
| 139 | + |
| 140 | +Counter::KeyPair |
| 141 | +MacsecCounter::getKey(const CounterTable& t, const std::string &name) const |
| 142 | +{ |
| 143 | + KeyCache<Counter::KeyPair> &cache = keyCacheInstance(); |
| 144 | + if (cache.enabled()) |
| 145 | + { |
| 146 | + try { |
| 147 | + return cache.at(name); |
| 148 | + } |
| 149 | + catch (const std::out_of_range&) { |
| 150 | + return {-1, ""}; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + int dbId = COUNTERS_DB; |
| 155 | + auto oidPtr = t.getCountersDB()->hget(COUNTERS_MACSEC_NAME_MAP, name); |
| 156 | + |
| 157 | + if (oidPtr == nullptr) |
| 158 | + { |
| 159 | + dbId = GB_COUNTERS_DB; |
| 160 | + oidPtr = t.getGbcountersDB()->hget(COUNTERS_MACSEC_NAME_MAP, name); |
| 161 | + } |
| 162 | + |
| 163 | + if (oidPtr == nullptr) |
| 164 | + return {-1, ""}; |
| 165 | + return {dbId, *oidPtr}; |
| 166 | +} |
| 167 | + |
| 168 | +KeyCache<Counter::KeyPair>& |
| 169 | +MacsecCounter::keyCacheInstance(void) |
| 170 | +{ |
| 171 | + if (keyCachePtr == nullptr) |
| 172 | + { |
| 173 | + keyCachePtr.reset(new KeyCache<Counter::KeyPair>(MacsecCounter::cachingKey)); |
| 174 | + } |
| 175 | + return *keyCachePtr; |
| 176 | +} |
| 177 | + |
| 178 | +void MacsecCounter::cachingKey(const CounterTable& t) |
| 179 | +{ |
| 180 | + auto fvs = t.getCountersDB()->hgetall(COUNTERS_MACSEC_NAME_MAP); |
| 181 | + for (auto fv: fvs) |
| 182 | + { |
| 183 | + keyCachePtr->insert(fv.first, Counter::KeyPair(COUNTERS_DB, fv.second)); |
| 184 | + } |
| 185 | + |
| 186 | + fvs = t.getGbcountersDB()->hgetall(COUNTERS_MACSEC_NAME_MAP); |
| 187 | + for (auto fv: fvs) |
| 188 | + { |
| 189 | + keyCachePtr->insert(fv.first, Counter::KeyPair(GB_COUNTERS_DB, fv.second)); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +CounterTable::CounterTable(const DBConnector *db, const string &tableName) |
| 194 | + : TableBase(tableName, SonicDBConfig::getSeparator(db)) |
| 195 | + , m_countersDB(db->newConnector(0)) |
| 196 | +{ |
| 197 | + unique_ptr<DBConnector> ptr(new DBConnector(GB_COUNTERS_DB, *m_countersDB)); |
| 198 | + m_gbcountersDB = std::move(ptr); |
| 199 | +} |
| 200 | + |
| 201 | +bool CounterTable::get(const Counter &counter, const std::string &name, std::vector<FieldValueTuple> &values) |
| 202 | +{ |
| 203 | + if (counter.usingLuaTable(*this, name)) |
| 204 | + { |
| 205 | + LuaTable luaTable(m_countersDB.get(), getTableName(), counter.getLuaScript()); |
| 206 | + return luaTable.get(counter.getLuaKeys(*this, name), values); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + auto keyPair = counter.getKey(*this, name); |
| 211 | + if (keyPair.second.empty()) |
| 212 | + { |
| 213 | + values.clear(); |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + if (keyPair.first == GB_COUNTERS_DB) |
| 218 | + { |
| 219 | + Table table(m_gbcountersDB.get(), getTableName()); |
| 220 | + return table.get(keyPair.second, values); |
| 221 | + } |
| 222 | + else |
| 223 | + { |
| 224 | + Table table(m_countersDB.get(), getTableName()); |
| 225 | + return table.get(keyPair.second, values); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | + |
| 231 | +bool CounterTable::hget(const Counter &counter, const std::string &name, const std::string &field, std::string &value) |
| 232 | +{ |
| 233 | + if (counter.usingLuaTable(*this, name)) |
| 234 | + { |
| 235 | + LuaTable luaTable(m_countersDB.get(), getTableName(), counter.getLuaScript()); |
| 236 | + return luaTable.hget(counter.getLuaKeys(*this, name), field, value); |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + auto keyPair = counter.getKey(*this, name); |
| 241 | + if (keyPair.second.empty()) |
| 242 | + { |
| 243 | + value.clear(); |
| 244 | + return false; |
| 245 | + } |
| 246 | + |
| 247 | + if (keyPair.first == GB_COUNTERS_DB) |
| 248 | + { |
| 249 | + Table table(getGbcountersDB().get(), getTableName()); |
| 250 | + return table.hget(keyPair.second, field, value); |
| 251 | + } |
| 252 | + else |
| 253 | + { |
| 254 | + Table table(m_countersDB.get(), getTableName()); |
| 255 | + return table.hget(keyPair.second, field, value); |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments