|
| 1 | +#include <string.h> |
| 2 | +#include "logger.h" |
| 3 | +#include "dbconnector.h" |
| 4 | +#include "producerstatetable.h" |
| 5 | +#include "tokenize.h" |
| 6 | +#include "ipprefix.h" |
| 7 | +#include "vrfmgr.h" |
| 8 | +#include "exec.h" |
| 9 | +#include "shellcmd.h" |
| 10 | + |
| 11 | +#define VRF_TABLE_START 1001 |
| 12 | +#define VRF_TABLE_END 2000 |
| 13 | + |
| 14 | +using namespace swss; |
| 15 | + |
| 16 | +VrfMgr::VrfMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) : |
| 17 | + Orch(cfgDb, tableNames) |
| 18 | +{ |
| 19 | + for (uint32_t i = VRF_TABLE_START; i < VRF_TABLE_END; i++) |
| 20 | + { |
| 21 | + m_freeTables.emplace(i); |
| 22 | + } |
| 23 | + |
| 24 | + /* Get existing VRFs from Linux */ |
| 25 | + stringstream cmd; |
| 26 | + string res; |
| 27 | + |
| 28 | + cmd << IP_CMD << " -d link show type vrf"; |
| 29 | + EXEC_WITH_ERROR_THROW(cmd.str(), res); |
| 30 | + |
| 31 | + enum IpShowRowType |
| 32 | + { |
| 33 | + LINK_ROW, |
| 34 | + MAC_ROW, |
| 35 | + DETAILS_ROW, |
| 36 | + }; |
| 37 | + |
| 38 | + string vrfName; |
| 39 | + uint32_t table; |
| 40 | + IpShowRowType rowType = LINK_ROW; |
| 41 | + const auto& rows = tokenize(res, '\n'); |
| 42 | + for (const auto& row : rows) |
| 43 | + { |
| 44 | + const auto& items = tokenize(row, ' '); |
| 45 | + switch(rowType) |
| 46 | + { |
| 47 | + case LINK_ROW: |
| 48 | + vrfName = items[1]; |
| 49 | + vrfName.pop_back(); |
| 50 | + rowType = MAC_ROW; |
| 51 | + break; |
| 52 | + case MAC_ROW: |
| 53 | + rowType = DETAILS_ROW; |
| 54 | + break; |
| 55 | + case DETAILS_ROW: |
| 56 | + table = static_cast<uint32_t>(stoul(items[6])); |
| 57 | + m_vrfTableMap[vrfName] = table; |
| 58 | + m_freeTables.erase(table); |
| 59 | + rowType = LINK_ROW; |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +uint32_t VrfMgr::getFreeTable(void) |
| 66 | +{ |
| 67 | + SWSS_LOG_ENTER(); |
| 68 | + |
| 69 | + if (m_freeTables.empty()) |
| 70 | + { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + uint32_t table = *m_freeTables.begin(); |
| 75 | + m_freeTables.erase(table); |
| 76 | + |
| 77 | + return table; |
| 78 | +} |
| 79 | + |
| 80 | +void VrfMgr::recycleTable(uint32_t table) |
| 81 | +{ |
| 82 | + SWSS_LOG_ENTER(); |
| 83 | + |
| 84 | + m_freeTables.emplace(table); |
| 85 | +} |
| 86 | + |
| 87 | +bool VrfMgr::delLink(const string& vrfName) |
| 88 | +{ |
| 89 | + SWSS_LOG_ENTER(); |
| 90 | + |
| 91 | + stringstream cmd; |
| 92 | + string res; |
| 93 | + |
| 94 | + if (m_vrfTableMap.find(vrfName) == m_vrfTableMap.end()) |
| 95 | + { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + cmd << IP_CMD << " link del " << vrfName; |
| 100 | + EXEC_WITH_ERROR_THROW(cmd.str(), res); |
| 101 | + |
| 102 | + recycleTable(m_vrfTableMap[vrfName]); |
| 103 | + m_vrfTableMap.erase(vrfName); |
| 104 | + |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +bool VrfMgr::setLink(const string& vrfName) |
| 109 | +{ |
| 110 | + SWSS_LOG_ENTER(); |
| 111 | + |
| 112 | + stringstream cmd; |
| 113 | + string res; |
| 114 | + |
| 115 | + if (m_vrfTableMap.find(vrfName) != m_vrfTableMap.end()) |
| 116 | + { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + uint32_t table = getFreeTable(); |
| 121 | + if (table == 0) |
| 122 | + { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + cmd << IP_CMD << " link add " << vrfName << " type vrf table " << table; |
| 127 | + EXEC_WITH_ERROR_THROW(cmd.str(), res); |
| 128 | + |
| 129 | + m_vrfTableMap.emplace(vrfName, table); |
| 130 | + |
| 131 | + cmd.str(""); |
| 132 | + cmd.clear(); |
| 133 | + cmd << IP_CMD << " link set " << vrfName << " up"; |
| 134 | + EXEC_WITH_ERROR_THROW(cmd.str(), res); |
| 135 | + |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +void VrfMgr::doTask(Consumer &consumer) |
| 140 | +{ |
| 141 | + SWSS_LOG_ENTER(); |
| 142 | + |
| 143 | + auto it = consumer.m_toSync.begin(); |
| 144 | + while (it != consumer.m_toSync.end()) |
| 145 | + { |
| 146 | + KeyOpFieldsValuesTuple t = it->second; |
| 147 | + auto vrfName = kfvKey(t); |
| 148 | + |
| 149 | + string op = kfvOp(t); |
| 150 | + if (op == SET_COMMAND) |
| 151 | + { |
| 152 | + if (!setLink(vrfName)) |
| 153 | + { |
| 154 | + SWSS_LOG_ERROR("Failed to create vrf netdev %s", vrfName.c_str()); |
| 155 | + } |
| 156 | + |
| 157 | + SWSS_LOG_NOTICE("Created vrf netdev %s", vrfName.c_str()); |
| 158 | + } |
| 159 | + else if (op == DEL_COMMAND) |
| 160 | + { |
| 161 | + if (!delLink(vrfName)) |
| 162 | + { |
| 163 | + SWSS_LOG_ERROR("Failed to remove vrf netdev %s", vrfName.c_str()); |
| 164 | + } |
| 165 | + |
| 166 | + SWSS_LOG_NOTICE("Removed vrf netdev %s", vrfName.c_str()); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + SWSS_LOG_ERROR("Unknown operation: %s", op.c_str()); |
| 171 | + } |
| 172 | + |
| 173 | + it = consumer.m_toSync.erase(it); |
| 174 | + } |
| 175 | +} |
0 commit comments