Skip to content

Commit

Permalink
[intfsorch]:add support to change rif mac address (#814)
Browse files Browse the repository at this point in the history
* [intfsorch]: add support to change rif mac-address
Signed-off-by: shine.chen <shine.chen@nephosinc.com>
  • Loading branch information
shine4chen authored Feb 12, 2020
1 parent 88f7a2a commit 853d822
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 4 deletions.
35 changes: 32 additions & 3 deletions cfgmgr/intfmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "intfmgr.h"
#include "exec.h"
#include "shellcmd.h"
#include "macaddress.h"
#include "warm_restart.h"

using namespace std;
Expand Down Expand Up @@ -65,6 +66,20 @@ void IntfMgr::setIntfIp(const string &alias, const string &opCmd,
}
}

void IntfMgr::setIntfMac(const string &alias, const string &mac_str)
{
stringstream cmd;
string res;

cmd << IP_CMD << " link set " << alias << " address " << mac_str;

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::setIntfVrf(const string &alias, const string &vrfName)
{
stringstream cmd;
Expand Down Expand Up @@ -326,7 +341,7 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
alias = alias.substr(0, found);
}
bool is_lo = !alias.compare(0, strlen(LOOPBACK_PREFIX), LOOPBACK_PREFIX);

string mac = "";
string vrf_name = "";
string mtu = "";
string adminStatus = "";
Expand All @@ -340,8 +355,11 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
{
vrf_name = value;
}

if (field == "admin_status")
else if (field == "mac_addr")
{
mac = value;
}
else if (field == "admin_status")
{
adminStatus = value;
}
Expand Down Expand Up @@ -392,6 +410,17 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
setIntfVrf(alias, vrf_name);
}

/*Set the mac of interface*/
if (!mac.empty())
{
setIntfMac(alias, mac);
}
else
{
FieldValueTuple fvTuple("mac_addr", MacAddress().to_string());
data.push_back(fvTuple);
}

if (!subIntfAlias.empty())
{
if (m_subIntfList.find(subIntfAlias) == m_subIntfList.end())
Expand Down
4 changes: 4 additions & 0 deletions cfgmgr/intfmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ class IntfMgr : public Orch

void setIntfIp(const std::string &alias, const std::string &opCmd, const IpPrefix &ipPrefix);
void setIntfVrf(const std::string &alias, const std::string &vrfName);
void setIntfMac(const std::string &alias, const std::string &macAddr);

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 doTask(Consumer &consumer);

bool isIntfStateOk(const std::string &alias);
bool isIntfCreated(const std::string &alias);
bool isIntfChangeVrf(const std::string &alias, const std::string &vrfName);
int getIntfIpCount(const std::string &alias);

void addLoopbackIntf(const std::string &alias);
void delLoopbackIntf(const std::string &alias);
void flushLoopbackIntfs(void);
Expand Down
44 changes: 43 additions & 1 deletion orchagent/intfsorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ void IntfsOrch::doTask(Consumer &consumer)
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;

vector<string> keys = tokenize(kfvKey(t), ':');
string alias(keys[0]);

Expand All @@ -436,6 +435,8 @@ void IntfsOrch::doTask(Consumer &consumer)

const vector<FieldValueTuple>& data = kfvFieldsValues(t);
string vrf_name = "", vnet_name = "", nat_zone = "";
MacAddress mac;

uint32_t mtu;
bool adminUp;
uint32_t nat_zone_id = 0;
Expand All @@ -452,6 +453,18 @@ void IntfsOrch::doTask(Consumer &consumer)
{
vnet_name = value;
}
else if (field == "mac_addr")
{
try
{
mac = MacAddress(value);
}
catch (const std::invalid_argument &e)
{
SWSS_LOG_ERROR("Invalid mac argument %s to %s()", value.c_str(), e.what());
continue;
}
}
else if (field == "nat_zone")
{
try
Expand Down Expand Up @@ -624,6 +637,35 @@ void IntfsOrch::doTask(Consumer &consumer)
}
}

if (mac)
{
/* Get mac information and update mac of the interface*/
sai_attribute_t attr;
attr.id = SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS;
memcpy(attr.value.mac, mac.getMac(), sizeof(sai_mac_t));

/*port.m_rif_id is set in setIntf(), need get port again*/
if (gPortsOrch->getPort(alias, port))
{
sai_status_t status = sai_router_intfs_api->set_router_interface_attribute(port.m_rif_id, &attr);
if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to set router interface mac %s for port %s, rv:%d",
mac.to_string().c_str(), port.m_alias.c_str(), status);
}
else
{
SWSS_LOG_NOTICE("Set router interface mac %s for port %s success",
mac.to_string().c_str(), port.m_alias.c_str());
}
}
else
{
SWSS_LOG_ERROR("Failed to set router interface mac %s for port %s, getPort fail",
mac.to_string().c_str(), alias.c_str());
}
}

it = consumer.m_toSync.erase(it);
}
else if (op == DEL_COMMAND)
Expand Down
Loading

0 comments on commit 853d822

Please sign in to comment.