Skip to content

Commit 853d822

Browse files
authored
[intfsorch]:add support to change rif mac address (#814)
* [intfsorch]: add support to change rif mac-address Signed-off-by: shine.chen <[email protected]>
1 parent 88f7a2a commit 853d822

File tree

4 files changed

+333
-4
lines changed

4 files changed

+333
-4
lines changed

cfgmgr/intfmgr.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "intfmgr.h"
88
#include "exec.h"
99
#include "shellcmd.h"
10+
#include "macaddress.h"
1011
#include "warm_restart.h"
1112

1213
using namespace std;
@@ -65,6 +66,20 @@ void IntfMgr::setIntfIp(const string &alias, const string &opCmd,
6566
}
6667
}
6768

69+
void IntfMgr::setIntfMac(const string &alias, const string &mac_str)
70+
{
71+
stringstream cmd;
72+
string res;
73+
74+
cmd << IP_CMD << " link set " << alias << " address " << mac_str;
75+
76+
int ret = swss::exec(cmd.str(), res);
77+
if (ret)
78+
{
79+
SWSS_LOG_ERROR("Command '%s' failed with rc %d", cmd.str().c_str(), ret);
80+
}
81+
}
82+
6883
void IntfMgr::setIntfVrf(const string &alias, const string &vrfName)
6984
{
7085
stringstream cmd;
@@ -326,7 +341,7 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
326341
alias = alias.substr(0, found);
327342
}
328343
bool is_lo = !alias.compare(0, strlen(LOOPBACK_PREFIX), LOOPBACK_PREFIX);
329-
344+
string mac = "";
330345
string vrf_name = "";
331346
string mtu = "";
332347
string adminStatus = "";
@@ -340,8 +355,11 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
340355
{
341356
vrf_name = value;
342357
}
343-
344-
if (field == "admin_status")
358+
else if (field == "mac_addr")
359+
{
360+
mac = value;
361+
}
362+
else if (field == "admin_status")
345363
{
346364
adminStatus = value;
347365
}
@@ -392,6 +410,17 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
392410
setIntfVrf(alias, vrf_name);
393411
}
394412

413+
/*Set the mac of interface*/
414+
if (!mac.empty())
415+
{
416+
setIntfMac(alias, mac);
417+
}
418+
else
419+
{
420+
FieldValueTuple fvTuple("mac_addr", MacAddress().to_string());
421+
data.push_back(fvTuple);
422+
}
423+
395424
if (!subIntfAlias.empty())
396425
{
397426
if (m_subIntfList.find(subIntfAlias) == m_subIntfList.end())

cfgmgr/intfmgr.h

+4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ class IntfMgr : public Orch
2525

2626
void setIntfIp(const std::string &alias, const std::string &opCmd, const IpPrefix &ipPrefix);
2727
void setIntfVrf(const std::string &alias, const std::string &vrfName);
28+
void setIntfMac(const std::string &alias, const std::string &macAddr);
29+
2830
bool doIntfGeneralTask(const std::vector<std::string>& keys, std::vector<FieldValueTuple> data, const std::string& op);
2931
bool doIntfAddrTask(const std::vector<std::string>& keys, const std::vector<FieldValueTuple>& data, const std::string& op);
3032
void doTask(Consumer &consumer);
33+
3134
bool isIntfStateOk(const std::string &alias);
3235
bool isIntfCreated(const std::string &alias);
3336
bool isIntfChangeVrf(const std::string &alias, const std::string &vrfName);
3437
int getIntfIpCount(const std::string &alias);
38+
3539
void addLoopbackIntf(const std::string &alias);
3640
void delLoopbackIntf(const std::string &alias);
3741
void flushLoopbackIntfs(void);

orchagent/intfsorch.cpp

+43-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ void IntfsOrch::doTask(Consumer &consumer)
413413
while (it != consumer.m_toSync.end())
414414
{
415415
KeyOpFieldsValuesTuple t = it->second;
416-
417416
vector<string> keys = tokenize(kfvKey(t), ':');
418417
string alias(keys[0]);
419418

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

437436
const vector<FieldValueTuple>& data = kfvFieldsValues(t);
438437
string vrf_name = "", vnet_name = "", nat_zone = "";
438+
MacAddress mac;
439+
439440
uint32_t mtu;
440441
bool adminUp;
441442
uint32_t nat_zone_id = 0;
@@ -452,6 +453,18 @@ void IntfsOrch::doTask(Consumer &consumer)
452453
{
453454
vnet_name = value;
454455
}
456+
else if (field == "mac_addr")
457+
{
458+
try
459+
{
460+
mac = MacAddress(value);
461+
}
462+
catch (const std::invalid_argument &e)
463+
{
464+
SWSS_LOG_ERROR("Invalid mac argument %s to %s()", value.c_str(), e.what());
465+
continue;
466+
}
467+
}
455468
else if (field == "nat_zone")
456469
{
457470
try
@@ -624,6 +637,35 @@ void IntfsOrch::doTask(Consumer &consumer)
624637
}
625638
}
626639

640+
if (mac)
641+
{
642+
/* Get mac information and update mac of the interface*/
643+
sai_attribute_t attr;
644+
attr.id = SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS;
645+
memcpy(attr.value.mac, mac.getMac(), sizeof(sai_mac_t));
646+
647+
/*port.m_rif_id is set in setIntf(), need get port again*/
648+
if (gPortsOrch->getPort(alias, port))
649+
{
650+
sai_status_t status = sai_router_intfs_api->set_router_interface_attribute(port.m_rif_id, &attr);
651+
if (status != SAI_STATUS_SUCCESS)
652+
{
653+
SWSS_LOG_ERROR("Failed to set router interface mac %s for port %s, rv:%d",
654+
mac.to_string().c_str(), port.m_alias.c_str(), status);
655+
}
656+
else
657+
{
658+
SWSS_LOG_NOTICE("Set router interface mac %s for port %s success",
659+
mac.to_string().c_str(), port.m_alias.c_str());
660+
}
661+
}
662+
else
663+
{
664+
SWSS_LOG_ERROR("Failed to set router interface mac %s for port %s, getPort fail",
665+
mac.to_string().c_str(), alias.c_str());
666+
}
667+
}
668+
627669
it = consumer.m_toSync.erase(it);
628670
}
629671
else if (op == DEL_COMMAND)

0 commit comments

Comments
 (0)