|
| 1 | +#include "logger.h" |
| 2 | +#include "dbconnector.h" |
| 3 | +#include "producerstatetable.h" |
| 4 | +#include "tokenize.h" |
| 5 | +#include "ipprefix.h" |
| 6 | +#include "sflowmgr.h" |
| 7 | +#include "exec.h" |
| 8 | +#include "shellcmd.h" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | +using namespace swss; |
| 12 | + |
| 13 | +map<string,string> sflowSpeedRateInitMap = |
| 14 | +{ |
| 15 | + {SFLOW_SAMPLE_RATE_KEY_400G, SFLOW_SAMPLE_RATE_VALUE_400G}, |
| 16 | + {SFLOW_SAMPLE_RATE_KEY_100G, SFLOW_SAMPLE_RATE_VALUE_100G}, |
| 17 | + {SFLOW_SAMPLE_RATE_KEY_50G, SFLOW_SAMPLE_RATE_VALUE_50G}, |
| 18 | + {SFLOW_SAMPLE_RATE_KEY_40G, SFLOW_SAMPLE_RATE_VALUE_40G}, |
| 19 | + {SFLOW_SAMPLE_RATE_KEY_25G, SFLOW_SAMPLE_RATE_VALUE_25G}, |
| 20 | + {SFLOW_SAMPLE_RATE_KEY_10G, SFLOW_SAMPLE_RATE_VALUE_10G}, |
| 21 | + {SFLOW_SAMPLE_RATE_KEY_1G, SFLOW_SAMPLE_RATE_VALUE_1G} |
| 22 | +}; |
| 23 | + |
| 24 | +SflowMgr::SflowMgr(DBConnector *cfgDb, DBConnector *appDb, const vector<string> &tableNames) : |
| 25 | + Orch(cfgDb, tableNames), |
| 26 | + m_cfgSflowTable(cfgDb, CFG_SFLOW_TABLE_NAME), |
| 27 | + m_cfgSflowSessionTable(cfgDb, CFG_SFLOW_SESSION_TABLE_NAME), |
| 28 | + m_appSflowTable(appDb, APP_SFLOW_TABLE_NAME), |
| 29 | + m_appSflowSessionTable(appDb, APP_SFLOW_SESSION_TABLE_NAME) |
| 30 | +{ |
| 31 | + m_intfAllConf = true; |
| 32 | + m_gEnable = false; |
| 33 | +} |
| 34 | + |
| 35 | +void SflowMgr::sflowHandleService(bool enable) |
| 36 | +{ |
| 37 | + stringstream cmd; |
| 38 | + string res; |
| 39 | + |
| 40 | + SWSS_LOG_ENTER(); |
| 41 | + |
| 42 | + if (enable) |
| 43 | + { |
| 44 | + cmd << "service hsflowd restart"; |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + cmd << "service hsflowd stop"; |
| 49 | + } |
| 50 | + |
| 51 | + int ret = swss::exec(cmd.str(), res); |
| 52 | + if (ret) |
| 53 | + { |
| 54 | + SWSS_LOG_ERROR("Command '%s' failed with rc %d", cmd.str().c_str(), ret); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + SWSS_LOG_NOTICE("Starting hsflowd service"); |
| 59 | + SWSS_LOG_INFO("Command '%s' succeeded", cmd.str().c_str()); |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +void SflowMgr::sflowUpdatePortInfo(Consumer &consumer) |
| 65 | +{ |
| 66 | + auto it = consumer.m_toSync.begin(); |
| 67 | + |
| 68 | + while (it != consumer.m_toSync.end()) |
| 69 | + { |
| 70 | + KeyOpFieldsValuesTuple t = it->second; |
| 71 | + |
| 72 | + string key = kfvKey(t); |
| 73 | + string op = kfvOp(t); |
| 74 | + auto values = kfvFieldsValues(t); |
| 75 | + |
| 76 | + if (op == SET_COMMAND) |
| 77 | + { |
| 78 | + SflowPortInfo port_info; |
| 79 | + bool new_port = false; |
| 80 | + |
| 81 | + auto sflowPortConf = m_sflowPortConfMap.find(key); |
| 82 | + if (sflowPortConf == m_sflowPortConfMap.end()) |
| 83 | + { |
| 84 | + new_port = true; |
| 85 | + port_info.local_conf = false; |
| 86 | + port_info.speed = SFLOW_ERROR_SPEED_STR; |
| 87 | + port_info.rate = ""; |
| 88 | + port_info.admin = ""; |
| 89 | + m_sflowPortConfMap[key] = port_info; |
| 90 | + } |
| 91 | + for (auto i : values) |
| 92 | + { |
| 93 | + if (fvField(i) == "speed") |
| 94 | + { |
| 95 | + m_sflowPortConfMap[key].speed = fvValue(i); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (new_port) |
| 100 | + { |
| 101 | + if (m_gEnable && m_intfAllConf) |
| 102 | + { |
| 103 | + vector<FieldValueTuple> fvs; |
| 104 | + sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed); |
| 105 | + m_appSflowSessionTable.set(key, fvs); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + else if (op == DEL_COMMAND) |
| 110 | + { |
| 111 | + auto sflowPortConf = m_sflowPortConfMap.find(key); |
| 112 | + if (sflowPortConf != m_sflowPortConfMap.end()) |
| 113 | + { |
| 114 | + bool local_cfg = m_sflowPortConfMap[key].local_conf; |
| 115 | + |
| 116 | + m_sflowPortConfMap.erase(key); |
| 117 | + if ((m_intfAllConf && m_gEnable) || local_cfg) |
| 118 | + { |
| 119 | + m_appSflowSessionTable.del(key); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + it = consumer.m_toSync.erase(it); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +void SflowMgr::sflowHandleSessionAll(bool enable) |
| 128 | +{ |
| 129 | + for (auto it: m_sflowPortConfMap) |
| 130 | + { |
| 131 | + if (!it.second.local_conf) |
| 132 | + { |
| 133 | + vector<FieldValueTuple> fvs; |
| 134 | + sflowGetGlobalInfo(fvs, it.second.speed); |
| 135 | + if (enable) |
| 136 | + { |
| 137 | + m_appSflowSessionTable.set(it.first, fvs); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + m_appSflowSessionTable.del(it.first); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void SflowMgr::sflowHandleSessionLocal(bool enable) |
| 148 | +{ |
| 149 | + for (auto it: m_sflowPortConfMap) |
| 150 | + { |
| 151 | + if (it.second.local_conf) |
| 152 | + { |
| 153 | + vector<FieldValueTuple> fvs; |
| 154 | + sflowGetPortInfo(fvs, it.second); |
| 155 | + if (enable) |
| 156 | + { |
| 157 | + m_appSflowSessionTable.set(it.first, fvs); |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + m_appSflowSessionTable.del(it.first); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void SflowMgr::sflowGetGlobalInfo(vector<FieldValueTuple> &fvs, string speed) |
| 168 | +{ |
| 169 | + string rate; |
| 170 | + FieldValueTuple fv1("admin_state", "up"); |
| 171 | + fvs.push_back(fv1); |
| 172 | + |
| 173 | + if (speed != SFLOW_ERROR_SPEED_STR) |
| 174 | + { |
| 175 | + rate = sflowSpeedRateInitMap[speed]; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + rate = SFLOW_ERROR_SPEED_STR; |
| 180 | + } |
| 181 | + FieldValueTuple fv2("sample_rate",rate); |
| 182 | + fvs.push_back(fv2); |
| 183 | +} |
| 184 | + |
| 185 | +void SflowMgr::sflowGetPortInfo(vector<FieldValueTuple> &fvs, SflowPortInfo &local_info) |
| 186 | +{ |
| 187 | + if (local_info.admin.length() > 0) |
| 188 | + { |
| 189 | + FieldValueTuple fv1("admin_state", local_info.admin); |
| 190 | + fvs.push_back(fv1); |
| 191 | + } |
| 192 | + |
| 193 | + FieldValueTuple fv2("sample_rate", local_info.rate); |
| 194 | + fvs.push_back(fv2); |
| 195 | +} |
| 196 | + |
| 197 | +void SflowMgr::sflowCheckAndFillValues(string alias, vector<FieldValueTuple> &fvs) |
| 198 | +{ |
| 199 | + string rate; |
| 200 | + bool admin_present = false; |
| 201 | + bool rate_present = false; |
| 202 | + |
| 203 | + for (auto i : fvs) |
| 204 | + { |
| 205 | + if (fvField(i) == "sample_rate") |
| 206 | + { |
| 207 | + rate_present = true; |
| 208 | + m_sflowPortConfMap[alias].rate = fvValue(i); |
| 209 | + } |
| 210 | + if (fvField(i) == "admin_state") |
| 211 | + { |
| 212 | + admin_present = true; |
| 213 | + m_sflowPortConfMap[alias].admin = fvValue(i); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if (!rate_present) |
| 218 | + { |
| 219 | + if (m_sflowPortConfMap[alias].rate == "") |
| 220 | + { |
| 221 | + string speed = m_sflowPortConfMap[alias].speed; |
| 222 | + |
| 223 | + if (speed != SFLOW_ERROR_SPEED_STR) |
| 224 | + { |
| 225 | + rate = sflowSpeedRateInitMap[speed]; |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + rate = SFLOW_ERROR_SPEED_STR; |
| 230 | + } |
| 231 | + m_sflowPortConfMap[alias].rate = rate; |
| 232 | + } |
| 233 | + FieldValueTuple fv("sample_rate", m_sflowPortConfMap[alias].rate); |
| 234 | + fvs.push_back(fv); |
| 235 | + } |
| 236 | + |
| 237 | + if (!admin_present) |
| 238 | + { |
| 239 | + if (m_sflowPortConfMap[alias].admin == "") |
| 240 | + { |
| 241 | + /* By default admin state is enable if not set explicitely */ |
| 242 | + m_sflowPortConfMap[alias].admin = "up"; |
| 243 | + } |
| 244 | + FieldValueTuple fv("admin_state", m_sflowPortConfMap[alias].admin); |
| 245 | + fvs.push_back(fv); |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +void SflowMgr::doTask(Consumer &consumer) |
| 250 | +{ |
| 251 | + SWSS_LOG_ENTER(); |
| 252 | + |
| 253 | + auto table = consumer.getTableName(); |
| 254 | + |
| 255 | + if (table == CFG_PORT_TABLE_NAME) |
| 256 | + { |
| 257 | + sflowUpdatePortInfo(consumer); |
| 258 | + return; |
| 259 | + } |
| 260 | + |
| 261 | + auto it = consumer.m_toSync.begin(); |
| 262 | + while (it != consumer.m_toSync.end()) |
| 263 | + { |
| 264 | + KeyOpFieldsValuesTuple t = it->second; |
| 265 | + |
| 266 | + string key = kfvKey(t); |
| 267 | + string op = kfvOp(t); |
| 268 | + auto values = kfvFieldsValues(t); |
| 269 | + |
| 270 | + if (op == SET_COMMAND) |
| 271 | + { |
| 272 | + if (table == CFG_SFLOW_TABLE_NAME) |
| 273 | + { |
| 274 | + for (auto i : values) |
| 275 | + { |
| 276 | + if (fvField(i) == "admin_state") |
| 277 | + { |
| 278 | + bool enable = false; |
| 279 | + if (fvValue(i) == "up") |
| 280 | + { |
| 281 | + enable = true; |
| 282 | + } |
| 283 | + if (enable == m_gEnable) |
| 284 | + { |
| 285 | + break; |
| 286 | + } |
| 287 | + m_gEnable = enable; |
| 288 | + sflowHandleService(enable); |
| 289 | + if (m_intfAllConf) |
| 290 | + { |
| 291 | + sflowHandleSessionAll(enable); |
| 292 | + } |
| 293 | + sflowHandleSessionLocal(enable); |
| 294 | + } |
| 295 | + } |
| 296 | + m_appSflowTable.set(key, values); |
| 297 | + } |
| 298 | + else if (table == CFG_SFLOW_SESSION_TABLE_NAME) |
| 299 | + { |
| 300 | + if (key == "all") |
| 301 | + { |
| 302 | + for (auto i : values) |
| 303 | + { |
| 304 | + if (fvField(i) == "admin_state") |
| 305 | + { |
| 306 | + bool enable = false; |
| 307 | + |
| 308 | + if (fvValue(i) == "up") |
| 309 | + { |
| 310 | + enable = true; |
| 311 | + } |
| 312 | + if ((enable != m_intfAllConf) && (m_gEnable)) |
| 313 | + { |
| 314 | + sflowHandleSessionAll(enable); |
| 315 | + } |
| 316 | + m_intfAllConf = enable; |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | + else |
| 321 | + { |
| 322 | + auto sflowPortConf = m_sflowPortConfMap.find(key); |
| 323 | + |
| 324 | + if (sflowPortConf == m_sflowPortConfMap.end()) |
| 325 | + { |
| 326 | + it++; |
| 327 | + continue; |
| 328 | + } |
| 329 | + sflowCheckAndFillValues(key,values); |
| 330 | + m_sflowPortConfMap[key].local_conf = true; |
| 331 | + m_appSflowSessionTable.set(key, values); |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + else if (op == DEL_COMMAND) |
| 336 | + { |
| 337 | + if (table == CFG_SFLOW_TABLE_NAME) |
| 338 | + { |
| 339 | + if (m_gEnable) |
| 340 | + { |
| 341 | + sflowHandleService(false); |
| 342 | + sflowHandleSessionAll(false); |
| 343 | + } |
| 344 | + m_gEnable = false; |
| 345 | + m_appSflowTable.del(key); |
| 346 | + } |
| 347 | + else if (table == CFG_SFLOW_SESSION_TABLE_NAME) |
| 348 | + { |
| 349 | + if (key == "all") |
| 350 | + { |
| 351 | + if (!m_intfAllConf) |
| 352 | + { |
| 353 | + sflowHandleSessionAll(true); |
| 354 | + } |
| 355 | + m_intfAllConf = true; |
| 356 | + } |
| 357 | + else |
| 358 | + { |
| 359 | + m_appSflowSessionTable.del(key); |
| 360 | + m_sflowPortConfMap[key].local_conf = false; |
| 361 | + m_sflowPortConfMap[key].rate = ""; |
| 362 | + m_sflowPortConfMap[key].admin = ""; |
| 363 | + |
| 364 | + /* If Global configured, set global session on port after local config is deleted */ |
| 365 | + if (m_intfAllConf) |
| 366 | + { |
| 367 | + vector<FieldValueTuple> fvs; |
| 368 | + sflowGetGlobalInfo(fvs, m_sflowPortConfMap[key].speed); |
| 369 | + m_appSflowSessionTable.set(key,fvs); |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | + } |
| 374 | + it = consumer.m_toSync.erase(it); |
| 375 | + } |
| 376 | +} |
0 commit comments