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 3 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
35 changes: 35 additions & 0 deletions orchagent/intfsorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ 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
int mac_attr_set;

mac_attr_set = 0;
shine4chen marked this conversation as resolved.
Show resolved Hide resolved

vector<string> keys = tokenize(kfvKey(t), ':');
string alias(keys[0]);
Expand Down Expand Up @@ -208,6 +212,12 @@ void IntfsOrch::doTask(Consumer &consumer)
{
vnet_name = value;
}
else if (field == "mac_addr")
{
mac_attr_set = 1;

shine4chen marked this conversation as resolved.
Show resolved Hide resolved
mac = MacAddress(value);
}
}

if (alias == "eth0" || alias == "docker0")
Expand Down Expand Up @@ -299,6 +309,31 @@ void IntfsOrch::doTask(Consumer &consumer)
}
}

if (mac_attr_set == 1)
{
auto it_intfs = m_syncdIntfses.find(alias);
if (it_intfs != m_syncdIntfses.end())
{
/* 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));

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

it = consumer.m_toSync.erase(it);
}
else if (op == DEL_COMMAND)
Expand Down
248 changes: 248 additions & 0 deletions tests/test_intf_mac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
from swsscommon import swsscommon

import time
import json

class TestRouterInterfaceMac(object):
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
def setup_db(self, dvs):
self.pdb = swsscommon.DBConnector(0, dvs.redis_sock, 0)
self.adb = swsscommon.DBConnector(1, dvs.redis_sock, 0)
self.cdb = swsscommon.DBConnector(4, dvs.redis_sock, 0)

def add_ip_address(self, interface, ip):
tbl = swsscommon.Table(self.cdb, "INTERFACE")
fvs = swsscommon.FieldValuePairs([("NULL", "NULL")])
tbl.set(interface + "|" + ip, fvs)
time.sleep(2) # IPv6 netlink message needs longer time

def remove_ip_address(self, interface, ip):
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
tbl = swsscommon.Table(self.cdb, "INTERFACE")
tbl._del(interface + "|" + ip);
time.sleep(1)

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

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

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

# check application database
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:Ethernet8")
intf_entries = tbl.getKeys()
assert len(intf_entries) == 1
assert intf_entries[0] == "10.0.0.4/31"

(status, fvs) = tbl.get(tbl.getKeys()[0])
assert status == True
assert len(fvs) == 2
for fv in fvs:
if fv[0] == "scope":
assert fv[1] == "global"
elif fv[0] == "family":
assert fv[1] == "IPv4"
else:
assert False

# check ASIC router interface database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()
# one loopback router interface one port based router interface
#assert len(intf_entries) == 2

for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
# a port based router interface has five field/value tuples
if len(fvs) == 5:
for fv in fvs:
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_TYPE":
assert fv[1] == "SAI_ROUTER_INTERFACE_TYPE_PORT"
# the default MTU without any configuration is 9100
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_MTU":
assert fv[1] == "9100"

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

# check application database
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:Ethernet8")
(status, fvs) = tbl.get(tbl.getKeys()[0])
assert status == True
assert len(fvs) == 3
for fv in fvs:
if fv[0] == "scope":
assert fv[1] == "global"
elif fv[0] == "family":
assert fv[1] == "IPv4"
elif fv[0] == "mac_addr":
assert fv[1] == "6c:ec:5a:11:22:33"
else:
assert False

# check ASIC router interface database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()
# one loopback router interface one port based router interface
#assert len(intf_entries) == 2
shine4chen marked this conversation as resolved.
Show resolved Hide resolved

for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
# a port based router interface has five field/value tuples
if len(fvs) == 5:
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
for fv in fvs:
# check the MAC
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS":
assert fv[1] == "6C:EC:5A:11:22:33"

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

# check application database
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:Ethernet8")
intf_entries = tbl.getKeys()
assert len(intf_entries) == 0

class TestLagRouterInterfaceMac(object):
def setup_db(self, dvs):
self.pdb = swsscommon.DBConnector(0, dvs.redis_sock, 0)
self.adb = swsscommon.DBConnector(1, dvs.redis_sock, 0)
self.cdb = swsscommon.DBConnector(4, dvs.redis_sock, 0)

def create_port_channel(self, dvs, alias):
tbl = swsscommon.Table(self.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(self.cdb, "PORTCHANNEL")
tbl._del(alias)
time.sleep(1)

def add_port_channel_members(self, dvs, lag, members):
tbl = swsscommon.Table(self.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(self.cdb, "PORTCHANNEL_MEMBER")
for member in members:
tbl._del(lag + "|" + member)
time.sleep(1)

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

def remove_ip_address(self, interface, ip):
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
tbl = swsscommon.Table(self.cdb, "PORTCHANNEL_INTERFACE")
tbl._del(interface + "|" + ip);
time.sleep(1)

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

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

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

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

# check application database
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:PortChannel001")
intf_entries = tbl.getKeys()
assert len(intf_entries) == 1
assert intf_entries[0] == "30.0.0.4/31"

(status, fvs) = tbl.get(tbl.getKeys()[0])
assert status == True
assert len(fvs) == 2
for fv in fvs:
if fv[0] == "scope":
assert fv[1] == "global"
elif fv[0] == "family":
assert fv[1] == "IPv4"
else:
assert False

# check ASIC router interface database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()
# one loopback router interface one port based router interface
#assert len(intf_entries) == 2

for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
# a port based router interface has five field/value tuples
if len(fvs) == 5:
for fv in fvs:
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_TYPE":
assert fv[1] == "SAI_ROUTER_INTERFACE_TYPE_PORT"
# the default MTU without any configuration is 9100
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_MTU":
assert fv[1] == "9100"

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

# check application database
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:PortChannel001")
(status, fvs) = tbl.get(tbl.getKeys()[0])
assert status == True
assert len(fvs) == 3
for fv in fvs:
if fv[0] == "scope":
assert fv[1] == "global"
elif fv[0] == "family":
assert fv[1] == "IPv4"
elif fv[0] == "mac_addr":
assert fv[1] == "6c:ec:5a:11:22:33"
else:
assert False

# check ASIC router interface database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE")
intf_entries = tbl.getKeys()
# one loopback router interface one port based router interface
#assert len(intf_entries) == 2

for key in intf_entries:
(status, fvs) = tbl.get(key)
assert status == True
# a port based router interface has five field/value tuples
if len(fvs) == 5:
shine4chen marked this conversation as resolved.
Show resolved Hide resolved
for fv in fvs:
# check the MAC
if fv[0] == "SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS":
assert fv[1] == "6C:EC:5A:11:22:33"

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

# check application database
tbl = swsscommon.Table(self.pdb, "INTF_TABLE:PortChannel001")
intf_entries = tbl.getKeys()
assert len(intf_entries) == 0

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