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

[intfsorch]:add support to change rif mac address #814

Merged
merged 25 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9326978
Merge pull request #1 from Azure/master
shine4chen Feb 13, 2019
7796b10
[intfsorch]: add support to change rif mac-address
Mar 11, 2019
af68d10
add the corresponding vs-test
leoli-nps Jun 2, 2019
2da2d4a
[vstest] update the corresponding vstest for pr#814
leoli-nps Jun 12, 2019
115f9aa
refine code according to prsunny/stcheng review
Jun 18, 2019
065c10f
fix bool value condition judgement
Jul 8, 2019
dfcae9b
[vstest] update the corresponding vstest for pr#814
leoli-nps Jul 9, 2019
d6fbb23
Merge branch 'master' of https://github.com/Azure/sonic-swss into rif…
Jul 9, 2019
4b26a95
move mac-address change code to intfGeneralTask
Jul 10, 2019
76fa655
Merge branch 'rif-change-mac' of https://github.com/shine4chen/sonic-…
Jul 10, 2019
d34689d
remove unnecessary condition statement
Jul 10, 2019
53063a0
[vstest] update the corresponding vstest for pr#814
leoli-nps Jul 10, 2019
39a5b31
minor format adjustment in intfmgr.cpp
shine4chen Jul 11, 2019
b9bed3a
Merge branch 'master' into rif-change-mac
prsunny Aug 12, 2019
b80f60c
[vstest] update the corresponding vstest for pr#814
leoli-nps Aug 15, 2019
165964a
fix the issue that intf mac can't be recovered correctly after warm-r…
Sep 9, 2019
295136b
Merge branch 'master' into rif-change-mac
shine4chen Oct 4, 2019
961ff0c
Merge branch 'master' into rif-change-mac
shine4chen Nov 16, 2019
f577cc7
reformat intmgr.h
shine4chen Nov 16, 2019
b190f4d
Update intfmgr.cpp
shine4chen Nov 16, 2019
405f413
set {mac:""} to app-db if not existed in cfg-db
shine4chen Nov 30, 2019
5484fe0
move mac variable definition down
shine4chen Nov 30, 2019
9bd1b17
Merge pull request #4 from Azure/master
shine4chen Nov 30, 2019
eff6af9
add some sleep on test case
Nov 30, 2019
7283a4c
Merge branch 'master' into rif-change-mac
shine4chen Feb 3, 2020
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
28 changes: 26 additions & 2 deletions cfgmgr/intfmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -386,6 +400,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 = "";
Expand All @@ -399,8 +414,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 @@ -437,6 +455,12 @@ bool IntfMgr::doIntfGeneralTask(const vector<string>& keys,
setIntfVrf(alias, vrf_name);
}

/*Set the mac of interface*/
if (!mac.empty())
{
setIntfMac(alias, mac);
}
wendani marked this conversation as resolved.
Show resolved Hide resolved

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);

Expand Down
34 changes: 34 additions & 0 deletions orchagent/intfsorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ void IntfsOrch::doTask(Consumer &consumer)
while (it != consumer.m_toSync.end())
{
KeyOpFieldsValuesTuple t = it->second;
MacAddress mac;
shine4chen marked this conversation as resolved.
Show resolved Hide resolved

vector<string> keys = tokenize(kfvKey(t), ':');
string alias(keys[0]);
Expand Down Expand Up @@ -419,6 +420,10 @@ void IntfsOrch::doTask(Consumer &consumer)
{
vnet_name = value;
}
else if (field == "mac_addr")
{
mac = MacAddress(value);
wendani marked this conversation as resolved.
Show resolved Hide resolved
}
else if (field == "mtu")
{
try
Expand Down Expand Up @@ -557,6 +562,35 @@ void IntfsOrch::doTask(Consumer &consumer)
}
}

if (mac)
{
/* Get mac information and update mac of the interface*/
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
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
244 changes: 244 additions & 0 deletions tests/test_intf_mac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
from swsscommon import swsscommon

import time
import json

class TestRouterInterfaceMac(object):
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
def add_ip_address(self, dvs, interface, ip):
tbl = swsscommon.Table(dvs.cdb, "INTERFACE")
fvs = swsscommon.FieldValuePairs([("NULL", "NULL")])
tbl.set(interface, fvs)
tbl.set(interface + "|" + ip, fvs)
time.sleep(1)

def remove_ip_address(self, dvs, interface, ip):
tbl = swsscommon.Table(dvs.cdb, "INTERFACE")
tbl._del(interface + "|" + ip);
tbl.hdel(interface, "NULL")
time.sleep(1)

def set_mac(self, dvs, interface, mac):
tbl = swsscommon.Table(dvs.cdb, "INTERFACE")
fvs = swsscommon.FieldValuePairs([("mac_addr", mac)])
tbl.set(interface, fvs)
time.sleep(1)

def remove_mac(self, dvs, interface):
tbl = swsscommon.Table(dvs.cdb, "INTERFACE")
tbl.hdel(interface, "mac_addr")
time.sleep(1)

def find_mac(self, dvs, interface, mac):
port_oid = dvs.asicdb.portnamemap[interface]

tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()
for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
values = dict(fvs)
if "SAI_ROUTER_INTERFACE_TYPE_PORT" != values["SAI_ROUTER_INTERFACE_ATTR_TYPE"]:
continue
if port_oid == values["SAI_ROUTER_INTERFACE_ATTR_PORT_ID"] and mac == values["SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS"]:
return True
return False

def test_InterfaceSetMac(self, dvs, testlog):
dvs.setup_db()

# assign IP to interface
self.add_ip_address(dvs, "Ethernet8", "10.0.0.4/31")

# set MAC to interface
self.set_mac(dvs, "Ethernet8", "6C:EC:5A:11:22:33")

# check application database
tbl = swsscommon.Table(dvs.pdb, "INTF_TABLE")
(status, fvs) = tbl.get("Ethernet8")
assert status == True
values = dict(fvs)
assert values["mac_addr"] == "6C:EC:5A:11:22:33"

# check ASIC router interface database
src_mac_addr_found = self.find_mac(dvs, "Ethernet8", "6C:EC:5A:11:22:33")
assert src_mac_addr_found == True

# remove IP from interface
self.remove_ip_address(dvs, "Ethernet8", "10.0.0.4/31")

# remove MAC from interface
self.remove_mac(dvs, "Ethernet8")

def test_InterfaceChangeMac(self, dvs, testlog):
dvs.setup_db()

# assign IP to interface
self.add_ip_address(dvs, "Ethernet12", "12.0.0.4/31")

# set MAC to interface
self.set_mac(dvs, "Ethernet12", "6C:EC:5A:22:33:44")

# change interface MAC
self.set_mac(dvs, "Ethernet12", "6C:EC:5A:33:44:55")

# check application database
tbl = swsscommon.Table(dvs.pdb, "INTF_TABLE")
(status, fvs) = tbl.get("Ethernet12")
assert status == True
values = dict(fvs)
assert values["mac_addr"] == "6C:EC:5A:33:44:55"

# check ASIC router interface database
src_mac_addr_found = self.find_mac(dvs, "Ethernet12", "6C:EC:5A:33:44:55")
assert src_mac_addr_found == True

# remove IP from interface
self.remove_ip_address(dvs, "Ethernet12", "12.0.0.4/31")

# remove MAC from interface
self.remove_mac(dvs, "Ethernet12")

class TestLagRouterInterfaceMac(object):
def create_port_channel(self, dvs, alias):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL")
fvs = swsscommon.FieldValuePairs([("admin_status", "up"),
("mtu", "9100")])
tbl.set(alias, fvs)
time.sleep(1)

def remove_port_channel(self, dvs, alias):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL")
tbl._del(alias)
time.sleep(1)

def add_port_channel_members(self, dvs, lag, members):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_MEMBER")
fvs = swsscommon.FieldValuePairs([("NULL", "NULL")])
for member in members:
tbl.set(lag + "|" + member, fvs)
time.sleep(1)

def remove_port_channel_members(self, dvs, lag, members):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_MEMBER")
for member in members:
tbl._del(lag + "|" + member)
time.sleep(1)

def add_ip_address(self, dvs, interface, ip):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_INTERFACE")
fvs = swsscommon.FieldValuePairs([("NULL", "NULL")])
tbl.set(interface, fvs)
tbl.set(interface + "|" + ip, fvs)
time.sleep(1)

def remove_ip_address(self, dvs, interface, ip):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_INTERFACE")
tbl._del(interface + "|" + ip);
tbl.hdel(interface, "NULL")
time.sleep(1)

def set_mac(self, dvs, interface, mac):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_INTERFACE")
fvs = swsscommon.FieldValuePairs([("mac_addr", mac)])
tbl.set(interface, fvs)
time.sleep(1)

def remove_mac(self, dvs, interface):
tbl = swsscommon.Table(dvs.cdb, "PORTCHANNEL_INTERFACE")
tbl.hdel(interface, "mac_addr")
time.sleep(1)

def find_mac(self, dvs, lag_oid, mac):
tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()

for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
values = dict(fvs)
if "SAI_ROUTER_INTERFACE_TYPE_PORT" != values["SAI_ROUTER_INTERFACE_ATTR_TYPE"]:
continue
if lag_oid == values["SAI_ROUTER_INTERFACE_ATTR_PORT_ID"] and mac == values["SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS"]:
return True
return False

def test_InterfaceSetMac(self, dvs, testlog):
dvs.setup_db()

# create port channel
self.create_port_channel(dvs, "PortChannel001")

# assign IP to interface
self.add_ip_address(dvs, "PortChannel001", "30.0.0.4/31")

# set MAC to interface
self.set_mac(dvs, "PortChannel001", "6C:EC:5A:11:22:33")

# check application database
tbl = swsscommon.Table(dvs.pdb, "INTF_TABLE")
(status, fvs) = tbl.get("PortChannel001")
assert status == True
values = dict(fvs)
assert values["mac_addr"] == "6C:EC:5A:11:22:33"

# get PortChannel oid; When sonic-swss pr885 is complete, you can get oid directly from COUNTERS_LAG_NAME_MAP, which would be better.
lag_tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_LAG")
lag_entries = lag_tbl.getKeys()
# At this point there should be only one lag in the system, which is PortChannel001.
assert len(lag_entries) == 1
lag_oid = lag_entries[0]

# check ASIC router interface database
src_mac_addr_found = self.find_mac(dvs, lag_oid, "6C:EC:5A:11:22:33")
assert src_mac_addr_found == True

# remove IP from interface
self.remove_ip_address(dvs, "PortChannel001", "30.0.0.4/31")

# remove MAC from interface
self.remove_mac(dvs, "PortChannel001")

# remove port channel
self.remove_port_channel(dvs, "PortChannel001")

def test_InterfaceChangeMac(self, dvs, testlog):
dvs.setup_db()

# create port channel
self.create_port_channel(dvs, "PortChannel002")

# assign IP to interface
self.add_ip_address(dvs, "PortChannel002", "32.0.0.4/31")

# set MAC to interface
self.set_mac(dvs, "PortChannel002", "6C:EC:5A:22:33:44")

# change interface MAC
self.set_mac(dvs, "PortChannel002", "6C:EC:5A:33:44:55")

# check application database
tbl = swsscommon.Table(dvs.pdb, "INTF_TABLE")
(status, fvs) = tbl.get("PortChannel002")
assert status == True
values = dict(fvs)
assert values["mac_addr"] == "6C:EC:5A:33:44:55"

# get PortChannel oid; When sonic-swss pr885 is complete, you can get oid directly from COUNTERS_LAG_NAME_MAP, which would be better.
lag_tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_LAG")
lag_entries = lag_tbl.getKeys()
# At this point there should be only one lag in the system, which is PortChannel002.
assert len(lag_entries) == 1
lag_oid = lag_entries[0]

# check ASIC router interface database
src_mac_addr_found = self.find_mac(dvs, lag_oid, "6C:EC:5A:33:44:55")
assert src_mac_addr_found == True

# remove IP from interface
self.remove_ip_address(dvs, "PortChannel002", "32.0.0.4/31")

# remove MAC from interface
self.remove_mac(dvs, "PortChannel002")

# remove port channel
self.remove_port_channel(dvs, "PortChannel002")