|
| 1 | +#include <string> |
| 2 | +#include "logger.h" |
| 3 | +#include "dbconnector.h" |
| 4 | +#include "producerstatetable.h" |
| 5 | +#include "tokenize.h" |
| 6 | +#include "ipprefix.h" |
| 7 | +#include "portmgr.h" |
| 8 | +#include "exec.h" |
| 9 | +#include "shellcmd.h" |
| 10 | + |
| 11 | +using namespace std; |
| 12 | +using namespace swss; |
| 13 | + |
| 14 | +PortMgr::PortMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) : |
| 15 | + Orch(cfgDb, tableNames), |
| 16 | + m_cfgPortTable(cfgDb, CFG_PORT_TABLE_NAME), |
| 17 | + m_cfgLagTable(cfgDb, CFG_LAG_TABLE_NAME), |
| 18 | + m_statePortTable(stateDb, STATE_PORT_TABLE_NAME), |
| 19 | + m_stateLagTable(stateDb, STATE_LAG_TABLE_NAME) |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | +bool PortMgr::setPortMtu(const string &alias, const string &mtu) |
| 24 | +{ |
| 25 | + stringstream cmd; |
| 26 | + string res; |
| 27 | + |
| 28 | + cmd << IP_CMD << " link set dev " << alias << " mtu " << mtu; |
| 29 | + return exec(cmd.str(), res) == 0; |
| 30 | +} |
| 31 | + |
| 32 | +bool PortMgr::setPortAdminStatus(const string &alias, const bool up) |
| 33 | +{ |
| 34 | + stringstream cmd; |
| 35 | + string res; |
| 36 | + |
| 37 | + cmd << IP_CMD << " link set dev " << alias << (up ? " up" : " down"); |
| 38 | + return exec(cmd.str(), res) == 0; |
| 39 | +} |
| 40 | + |
| 41 | +bool PortMgr::isPortStateOk(const string &table, const string &alias) |
| 42 | +{ |
| 43 | + vector<FieldValueTuple> temp; |
| 44 | + |
| 45 | + if (table == CFG_PORT_TABLE_NAME) |
| 46 | + { |
| 47 | + if (m_statePortTable.get(alias, temp)) |
| 48 | + { |
| 49 | + SWSS_LOG_INFO("Port %s is ready", alias.c_str()); |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + else if (table == CFG_LAG_TABLE_NAME) |
| 54 | + { |
| 55 | + if (m_stateLagTable.get(alias, temp)) |
| 56 | + { |
| 57 | + SWSS_LOG_INFO("Lag %s is ready", alias.c_str()); |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | +} |
| 64 | + |
| 65 | +void PortMgr::doTask(Consumer &consumer) |
| 66 | +{ |
| 67 | + SWSS_LOG_ENTER(); |
| 68 | + |
| 69 | + auto table = consumer.getTableName(); |
| 70 | + |
| 71 | + auto it = consumer.m_toSync.begin(); |
| 72 | + while (it != consumer.m_toSync.end()) |
| 73 | + { |
| 74 | + KeyOpFieldsValuesTuple t = it->second; |
| 75 | + |
| 76 | + string alias = kfvKey(t); |
| 77 | + string op = kfvOp(t); |
| 78 | + |
| 79 | + if (op == SET_COMMAND) |
| 80 | + { |
| 81 | + if (!isPortStateOk(table, alias)) |
| 82 | + { |
| 83 | + SWSS_LOG_INFO("Port %s is not ready, pending", alias.c_str()); |
| 84 | + it++; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + for (auto i : kfvFieldsValues(t)) |
| 89 | + { |
| 90 | + if (fvField(i) == "mtu") |
| 91 | + { |
| 92 | + auto mtu = fvValue(i); |
| 93 | + setPortMtu(alias, mtu); |
| 94 | + SWSS_LOG_NOTICE("Configure %s MTU to %s", |
| 95 | + alias.c_str(), mtu.c_str()); |
| 96 | + } |
| 97 | + else if (fvField(i) == "admin_status") |
| 98 | + { |
| 99 | + auto status = fvValue(i); |
| 100 | + setPortAdminStatus(alias, status == "up"); |
| 101 | + SWSS_LOG_NOTICE("Configure %s %s", |
| 102 | + alias.c_str(), status.c_str()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + it = consumer.m_toSync.erase(it); |
| 108 | + } |
| 109 | +} |
0 commit comments