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 10 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
137 changes: 134 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 @@ -765,6 +770,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 +810,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 +989,45 @@ 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, update sub-interface admin staus to down,
// it can prevent unwanted events received during interface changed in kernel
// bring it up after the changed
if (sag == "true")
{
updateSubIntfAdminStatus(alias, "down");
superchild marked this conversation as resolved.
Show resolved Hide resolved
setIntfMac(alias, gwmac);
updateSubIntfAdminStatus(alias, "up");

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

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 +1162,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 +1230,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 +1300,46 @@ 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
updateSubIntfAdminStatus(key, "down");
setIntfMac(key, macAddr);
updateSubIntfAdminStatus(key, "up");

vector<FieldValueTuple> vlanIntFv;
FieldValueTuple fvTuple("mac_addr", macAddr);
vlanIntFv.push_back(fvTuple);
m_appIntfTableProducer.set(key, vlanIntFv);
}
}
}
}

bool IntfMgr::enableIpv6Flag(const string &alias)
{
stringstream cmd;
Expand Down
8 changes: 5 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 @@ -46,6 +46,7 @@ class IntfMgr : public Orch

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 +76,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