|
| 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 "intfmgr.h" |
| 8 | +#include "exec.h" |
| 9 | +#include "shellcmd.h" |
| 10 | + |
| 11 | +using namespace std; |
| 12 | +using namespace swss; |
| 13 | + |
| 14 | +#define VLAN_PREFIX "Vlan" |
| 15 | +#define LAG_PREFIX "PortChannel" |
| 16 | + |
| 17 | +IntfMgr::IntfMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) : |
| 18 | + Orch(cfgDb, tableNames), |
| 19 | + m_cfgIntfTable(cfgDb, CFG_INTF_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR), |
| 20 | + m_cfgVlanIntfTable(cfgDb, CFG_VLAN_INTF_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR), |
| 21 | + m_statePortTable(stateDb, STATE_PORT_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR), |
| 22 | + m_stateLagTable(stateDb, STATE_LAG_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR), |
| 23 | + m_stateVlanTable(stateDb, STATE_VLAN_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR), |
| 24 | + m_appIntfTableProducer(appDb, APP_INTF_TABLE_NAME) |
| 25 | +{ |
| 26 | +} |
| 27 | + |
| 28 | +bool IntfMgr::setIntfIp(const string &alias, const string &opCmd, const string &ipPrefixStr) |
| 29 | +{ |
| 30 | + stringstream cmd; |
| 31 | + string res; |
| 32 | + |
| 33 | + cmd << IP_CMD << " address " << opCmd << " " << ipPrefixStr << " dev " << alias;; |
| 34 | + int ret = swss::exec(cmd.str(), res); |
| 35 | + return (ret == 0); |
| 36 | +} |
| 37 | + |
| 38 | +bool IntfMgr::isIntfStateOk(const string &alias) |
| 39 | +{ |
| 40 | + vector<FieldValueTuple> temp; |
| 41 | + |
| 42 | + if (!alias.compare(0, strlen(VLAN_PREFIX), VLAN_PREFIX)) |
| 43 | + { |
| 44 | + if (m_stateVlanTable.get(alias, temp)) |
| 45 | + { |
| 46 | + SWSS_LOG_DEBUG("Vlan %s is ready", alias.c_str()); |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + else if (!alias.compare(0, strlen(LAG_PREFIX), LAG_PREFIX)) |
| 51 | + { |
| 52 | + if (m_stateLagTable.get(alias, temp)) |
| 53 | + { |
| 54 | + SWSS_LOG_DEBUG("Lag %s is ready", alias.c_str()); |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + else if (m_statePortTable.get(alias, temp)) |
| 59 | + { |
| 60 | + SWSS_LOG_DEBUG("Port %s is ready", alias.c_str()); |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + return false; |
| 65 | +} |
| 66 | +void IntfMgr::doTask(Consumer &consumer) |
| 67 | +{ |
| 68 | + SWSS_LOG_ENTER(); |
| 69 | + |
| 70 | + auto it = consumer.m_toSync.begin(); |
| 71 | + while (it != consumer.m_toSync.end()) |
| 72 | + { |
| 73 | + KeyOpFieldsValuesTuple t = it->second; |
| 74 | + |
| 75 | + string keySeparator = CONFIGDB_KEY_SEPARATOR; |
| 76 | + vector<string> keys = tokenize(kfvKey(t), keySeparator[0]); |
| 77 | + string alias(keys[0]); |
| 78 | + |
| 79 | + if (alias.compare(0, strlen(VLAN_PREFIX), VLAN_PREFIX)) |
| 80 | + { |
| 81 | + /* handle IP over vlan Only for now, skip the rest */ |
| 82 | + it = consumer.m_toSync.erase(it); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + size_t pos = kfvKey(t).find(CONFIGDB_KEY_SEPARATOR); |
| 87 | + if (pos == string::npos) |
| 88 | + { |
| 89 | + SWSS_LOG_DEBUG("Invalid key %s", kfvKey(t).c_str()); |
| 90 | + it = consumer.m_toSync.erase(it); |
| 91 | + continue; |
| 92 | + } |
| 93 | + IpPrefix ip_prefix(kfvKey(t).substr(pos+1)); |
| 94 | + |
| 95 | + SWSS_LOG_DEBUG("intfs doTask: %s", (dumpTuple(consumer, t)).c_str()); |
| 96 | + |
| 97 | + string op = kfvOp(t); |
| 98 | + if (op == SET_COMMAND) |
| 99 | + { |
| 100 | + /* |
| 101 | + * Don't proceed if port/lag/VLAN is not ready yet. |
| 102 | + * The pending task will be checked periodially and retried. |
| 103 | + * TODO: Subscribe to stateDB for port/lag/VLAN state and retry |
| 104 | + * pending tasks immediately upon state change. |
| 105 | + */ |
| 106 | + if (!isIntfStateOk(alias)) |
| 107 | + { |
| 108 | + SWSS_LOG_DEBUG("Interface is not ready, skipping %s", kfvKey(t).c_str()); |
| 109 | + it++; |
| 110 | + continue; |
| 111 | + } |
| 112 | + string opCmd("add"); |
| 113 | + string ipPrefixStr = ip_prefix.to_string(); |
| 114 | + setIntfIp(alias, opCmd, ipPrefixStr); |
| 115 | + } |
| 116 | + else if (op == DEL_COMMAND) |
| 117 | + { |
| 118 | + string opCmd("del"); |
| 119 | + string ipPrefixStr = ip_prefix.to_string(); |
| 120 | + setIntfIp(alias, opCmd, ipPrefixStr); |
| 121 | + } |
| 122 | + |
| 123 | + it = consumer.m_toSync.erase(it); |
| 124 | + continue; |
| 125 | + } |
| 126 | +} |
0 commit comments