Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SAG]: Add SAG implementation #1974

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 163 additions & 3 deletions cfgmgr/intfmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ using namespace swss;
#define LOOPBACK_DEFAULT_MTU_STR "65536"
#define DEFAULT_MTU_STR 9100

extern MacAddress gMacAddress;

IntfMgr::IntfMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) :
Orch(cfgDb, tableNames),
m_cfgIntfTable(cfgDb, CFG_INTF_TABLE_NAME),
m_cfgVlanIntfTable(cfgDb, CFG_VLAN_INTF_TABLE_NAME),
m_cfgLagIntfTable(cfgDb, CFG_LAG_INTF_TABLE_NAME),
m_cfgLoopbackIntfTable(cfgDb, CFG_LOOPBACK_INTERFACE_TABLE_NAME),
m_cfgSagTable(cfgDb, CFG_SAG_TABLE_NAME),
m_statePortTable(stateDb, STATE_PORT_TABLE_NAME),
m_stateLagTable(stateDb, STATE_LAG_TABLE_NAME),
m_stateVlanTable(stateDb, STATE_VLAN_TABLE_NAME),
m_stateVrfTable(stateDb, STATE_VRF_TABLE_NAME),
m_stateIntfTable(stateDb, STATE_INTERFACE_TABLE_NAME),
m_appIntfTableProducer(appDb, APP_INTF_TABLE_NAME),
m_neighTable(appDb, APP_NEIGH_TABLE_NAME)
m_appSagTableProducer(appDb, APP_SAG_TABLE_NAME),
m_neighTable(appDb, APP_NEIGH_TABLE_NAME),
m_appLagTable(appDb, APP_LAG_TABLE_NAME)
{
auto subscriberStateTable = new swss::SubscriberStateTable(stateDb,
STATE_PORT_TABLE_NAME, TableConsumable::DEFAULT_POP_BATCH_SIZE, 100);
Expand Down Expand Up @@ -193,6 +198,27 @@ bool IntfMgr::setIntfMpls(const string &alias, const string& mpls)
return true;
}

void IntfMgr::setIntfState(const string &alias, bool isUp)
{
stringstream cmd;
string res;

if (isUp)
{
cmd << IP_CMD << " link set " << shellquote(alias) << " up";
}
else
{
cmd << IP_CMD << " link set " << shellquote(alias) << " down";
}

int ret = swss::exec(cmd.str(), res);
if (ret)
{
SWSS_LOG_ERROR("Command '%s' failed with rc %d", cmd.str().c_str(), ret);
}
}

void IntfMgr::addLoopbackIntf(const string &alias)
{
stringstream cmd;
Expand Down Expand Up @@ -765,6 +791,7 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
string grat_arp = "";
string mpls = "";
string ipv6_link_local_mode = "";
string sag = "";
string loopback_action = "";

for (auto idx : data)
Expand Down Expand Up @@ -804,6 +831,10 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
{
ipv6_link_local_mode = value;
}
else if (field == "static_anycast_gateway")
{
sag = value;
}
else if (field == "vlan")
{
vlanId = value;
Expand Down Expand Up @@ -979,8 +1010,43 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
}
else
{
FieldValueTuple fvTuple("mac_addr", MacAddress().to_string());
data.push_back(fvTuple);
if (!sag.empty())
{
// only VLAN interface can set static anycast gateway
if (!alias.compare(0, strlen(VLAN_PREFIX), VLAN_PREFIX))
{
string gwmac = "";
if (m_cfgSagTable.hget("GLOBAL", "gateway_mac", gwmac))
{
// before change interface MAC, set interface down and up to regenerate IPv6 LL by MAC
if (sag == "true")
{
setIntfState(alias, false);
setIntfMac(alias, gwmac);
setIntfState(alias, true);

FieldValueTuple fvTuple("mac_addr", gwmac);
data.push_back(fvTuple);
}
else if (sag == "false")
{
setIntfState(alias, false);
setIntfMac(alias, gMacAddress.to_string());
setIntfState(alias, true);

FieldValueTuple fvTuple("mac_addr", MacAddress().to_string());
data.push_back(fvTuple);
} else {
SWSS_LOG_ERROR("invalid SAG config \"%s\", it should be \"true\" or \"false\"", sag.c_str());
}
superchild marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
else
{
FieldValueTuple fvTuple("mac_addr", MacAddress().to_string());
data.push_back(fvTuple);
}
}

if (!proxy_arp.empty())
Expand Down Expand Up @@ -1115,6 +1181,42 @@ bool IntfMgr::doIntfAddrTask(const vector<string>& keys,
return true;
}

void IntfMgr::doSagTask(const vector<string>& keys,
const vector<FieldValueTuple> &data,
const string& op)
{
SWSS_LOG_ENTER();

string mac = "";
for (auto idx : data)
{
const auto &field = fvField(idx);
const auto &value = fvValue(idx);

if (field == "gateway_mac")
{
mac = value;
}
}

vector<FieldValueTuple> fvAppSag;
if (op == SET_COMMAND)
{
FieldValueTuple gwmac("gateway_mac", MacAddress(mac).to_string());
fvAppSag.push_back(gwmac);
m_appSagTableProducer.set("GLOBAL", fvAppSag);

updateSagMac(mac);
}
else if (op == DEL_COMMAND)
{
m_appSagTableProducer.del("GLOBAL");

// reset mac address for enabled static-anycast-gateway's VLAN interfaces
updateSagMac(gMacAddress.to_string());
}
}

void IntfMgr::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
Expand Down Expand Up @@ -1147,6 +1249,14 @@ void IntfMgr::doTask(Consumer &consumer)
it = consumer.m_toSync.erase(it);
continue;
}

if (table_name == CFG_SAG_TABLE_NAME)
{
doSagTask(keys, data, op);
it = consumer.m_toSync.erase(it);
continue;
}

if (!doIntfGeneralTask(keys, data, op))
{
it++;
Expand Down Expand Up @@ -1209,6 +1319,56 @@ void IntfMgr::doPortTableTask(const string& key, vector<FieldValueTuple> data, s
}
}

void IntfMgr::updateSagMac(const std::string &macAddr)
{
vector<string> keys;
m_cfgVlanIntfTable.getKeys(keys);
for (auto &key: keys)
{
vector<string> entryKeys = tokenize(key, config_db_key_delimiter);
if (key.compare(0, strlen(VLAN_PREFIX), VLAN_PREFIX))
{
continue;
}

// only process the entry includes the SAG's config
// e.g. VLAN_INTERFACE|Vlan201
if (entryKeys.size() != 1)
superchild marked this conversation as resolved.
Show resolved Hide resolved
{
continue;
}

string value = "";
if (m_cfgVlanIntfTable.hget(key, "static_anycast_gateway", value))
{
if (value == "true")
{
SWSS_LOG_NOTICE("set %s mac address to %s", key.c_str(), macAddr.c_str());

// enable SAG, set device down and up to regenerate IPv6 LL by MAC
setIntfState(key, false);
setIntfMac(key, macAddr);
setIntfState(key, true);

vector<FieldValueTuple> vlanIntFv;

// keep consistent with default MAC 00:00:00:00:00:00
string entryMac = MacAddress().to_string();
if (macAddr != gMacAddress.to_string())
{
entryMac = macAddr;
}

FieldValueTuple fvTuple("mac_addr", entryMac);
vlanIntFv.push_back(fvTuple);
m_appIntfTableProducer.set(key, vlanIntFv);
}
} else {
SWSS_LOG_INFO("can't get %s in VLAN_INTERFACE table", key.c_str());
}
}
}

bool IntfMgr::enableIpv6Flag(const string &alias)
{
stringstream cmd;
Expand Down
9 changes: 6 additions & 3 deletions cfgmgr/intfmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class IntfMgr : public Orch
using Orch::doTask;

private:
ProducerStateTable m_appIntfTableProducer;
Table m_cfgIntfTable, m_cfgVlanIntfTable, m_cfgLagIntfTable, m_cfgLoopbackIntfTable;
Table m_statePortTable, m_stateLagTable, m_stateVlanTable, m_stateVrfTable, m_stateIntfTable;
ProducerStateTable m_appIntfTableProducer, m_appSagTableProducer;
Table m_cfgIntfTable, m_cfgVlanIntfTable, m_cfgLagIntfTable, m_cfgLoopbackIntfTable, m_cfgSagTable;
Table m_statePortTable, m_stateLagTable, m_stateVlanTable, m_stateVrfTable, m_stateIntfTable, m_appLagTable;
Table m_neighTable;

SubIntfMap m_subIntfList;
Expand All @@ -43,9 +43,11 @@ class IntfMgr : public Orch
void setIntfVrf(const std::string &alias, const std::string &vrfName);
void setIntfMac(const std::string &alias, const std::string &macAddr);
bool setIntfMpls(const std::string &alias, const std::string &mpls);
void setIntfState(const std::string &alias, bool isUp);

bool doIntfGeneralTask(const std::vector<std::string>& keys, std::vector<FieldValueTuple> data, const std::string& op);
bool doIntfAddrTask(const std::vector<std::string>& keys, const std::vector<FieldValueTuple>& data, const std::string& op);
void doSagTask(const std::vector<std::string>& keys, const std::vector<FieldValueTuple>& data, const std::string& op);
void doTask(Consumer &consumer);
void doPortTableTask(const std::string& key, std::vector<FieldValueTuple> data, std::string op);

Expand Down Expand Up @@ -75,6 +77,7 @@ class IntfMgr : public Orch

void updateSubIntfAdminStatus(const std::string &alias, const std::string &admin);
void updateSubIntfMtu(const std::string &alias, const std::string &mtu);
void updateSagMac(const std::string &macAddr);
bool enableIpv6Flag(const std::string&);

bool m_replayDone {false};
Expand Down
12 changes: 12 additions & 0 deletions cfgmgr/intfmgrd.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <unistd.h>
#include <vector>
#include <mutex>
#include <algorithm>
#include "dbconnector.h"
#include "select.h"
#include "exec.h"
Expand Down Expand Up @@ -35,6 +36,7 @@ ofstream gResponsePublisherRecordOfs;
string gResponsePublisherRecordFile;
/* Global database mutex */
mutex gDbMutex;
MacAddress gMacAddress;

int main(int argc, char **argv)
{
Expand All @@ -52,6 +54,7 @@ int main(int argc, char **argv)
CFG_LOOPBACK_INTERFACE_TABLE_NAME,
CFG_VLAN_SUB_INTF_TABLE_NAME,
CFG_VOQ_INBAND_INTERFACE_TABLE_NAME,
CFG_SAG_TABLE_NAME,
};

DBConnector cfgDb("CONFIG_DB", 0);
Expand All @@ -70,6 +73,15 @@ int main(int argc, char **argv)
s.addSelectables(o->getSelectables());
}

Table table(&cfgDb, "DEVICE_METADATA");
string mac = "";
if (!table.hget("localhost", "mac", mac))
{
throw runtime_error("couldn't find MAC address of the device from config DB");
}

gMacAddress = MacAddress(mac);

SWSS_LOG_NOTICE("starting main loop");
while (true)
{
Expand Down
Loading
Loading