Skip to content

Commit 3adbe90

Browse files
committed
Address review comments, add pytest for tunnel handling
1 parent fd20f0e commit 3adbe90

2 files changed

Lines changed: 270 additions & 6 deletions

File tree

orchagent/muxorch.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static sai_status_t create_route(IpPrefix &pfx, sai_object_id_t nh)
107107
sai_status_t status = sai_route_api->create_route_entry(&route_entry, (uint32_t)attrs.size(), attrs.data());
108108
if (status != SAI_STATUS_SUCCESS)
109109
{
110-
SWSS_LOG_ERROR("Failed to create tunnel route %s,nh %lx rv:%d",
110+
SWSS_LOG_ERROR("Failed to create tunnel route %s,nh %" PRIx64 " rv:%d",
111111
pfx.getIp().to_string().c_str(), nh, status);
112112
return status;
113113
}
@@ -326,7 +326,7 @@ bool MuxCable::stateInitActive()
326326

327327
bool MuxCable::stateActive()
328328
{
329-
SWSS_LOG_INFO("Set state to Active from %s", muxStateValToString.at(state_).c_str());
329+
SWSS_LOG_INFO("Set state to Active for %s", mux_name_.c_str());
330330

331331
Port port;
332332
if (!gPortsOrch->getPort(mux_name_, port))
@@ -363,7 +363,7 @@ bool MuxCable::stateActive()
363363

364364
bool MuxCable::stateStandby()
365365
{
366-
SWSS_LOG_INFO("Set state to Standby from %s", muxStateValToString.at(state_).c_str());
366+
SWSS_LOG_INFO("Set state to Standby for %s", mux_name_.c_str());
367367

368368
Port port;
369369
if (!gPortsOrch->getPort(mux_name_, port))
@@ -400,6 +400,8 @@ bool MuxCable::stateStandby()
400400

401401
if (!aclHandler(port.m_port_id))
402402
{
403+
remove_route(srv_ip4_);
404+
remove_route(srv_ip6_);
403405
SWSS_LOG_INFO("Add ACL drop rule failed for %s", mux_name_.c_str());
404406
return false;
405407
}
@@ -557,7 +559,7 @@ MuxAclHandler::MuxAclHandler(sai_object_id_t port)
557559
auto found = acl_table_.find(table_name);
558560
if (found == acl_table_.end())
559561
{
560-
SWSS_LOG_NOTICE("First time create for port %lx", port);
562+
SWSS_LOG_NOTICE("First time create for port %" PRIx64 "", port);
561563

562564
// First time handling of Mux Table, create ACL table, and bind
563565
createMuxAclTable(port, table_name);
@@ -567,7 +569,7 @@ MuxAclHandler::MuxAclHandler(sai_object_id_t port)
567569
}
568570
else
569571
{
570-
SWSS_LOG_NOTICE("Binding port %lx", port);
572+
SWSS_LOG_NOTICE("Binding port %" PRIx64 "", port);
571573
// Otherwise just bind ACL table with the port
572574
found->second.bind(port);
573575
}
@@ -578,7 +580,7 @@ MuxAclHandler::~MuxAclHandler(void)
578580
SWSS_LOG_ENTER();
579581
string table_name = MUX_ACL_TABLE_NAME;
580582

581-
SWSS_LOG_NOTICE("Un-Binding port %lx", port_);
583+
SWSS_LOG_NOTICE("Un-Binding port %" PRIx64 "", port_);
582584

583585
auto found = acl_table_.find(table_name);
584586
found->second.unbind(port_);

tests/test_mux.py

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
import time
2+
import pytest
3+
4+
from swsscommon import swsscommon
5+
6+
7+
def create_fvs(**kwargs):
8+
return swsscommon.FieldValuePairs(list(kwargs.items()))
9+
10+
def create_entry(tbl, key, pairs):
11+
fvs = swsscommon.FieldValuePairs(pairs)
12+
tbl.set(key, fvs)
13+
time.sleep(1)
14+
15+
def create_entry_tbl(db, table, separator, key, pairs):
16+
tbl = swsscommon.Table(db, table)
17+
create_entry(tbl, key, pairs)
18+
19+
20+
class TestMuxTunnelBase(object):
21+
APP_TUNNEL_DECAP_TABLE_NAME = "TUNNEL_DECAP_TABLE"
22+
ASIC_TUNNEL_TABLE = "ASIC_STATE:SAI_OBJECT_TYPE_TUNNEL"
23+
ASIC_TUNNEL_TERM_ENTRIES = "ASIC_STATE:SAI_OBJECT_TYPE_TUNNEL_TERM_TABLE_ENTRY"
24+
ASIC_RIF_TABLE = "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE"
25+
ASIC_VRF_TABLE = "ASIC_STATE:SAI_OBJECT_TYPE_VIRTUAL_ROUTER"
26+
27+
ecn_modes_map = {
28+
"standard" : "SAI_TUNNEL_DECAP_ECN_MODE_STANDARD",
29+
"copy_from_outer": "SAI_TUNNEL_DECAP_ECN_MODE_COPY_FROM_OUTER"
30+
}
31+
32+
dscp_modes_map = {
33+
"pipe" : "SAI_TUNNEL_DSCP_MODE_PIPE_MODEL",
34+
"uniform" : "SAI_TUNNEL_DSCP_MODE_UNIFORM_MODEL"
35+
}
36+
37+
ttl_modes_map = {
38+
"pipe" : "SAI_TUNNEL_TTL_MODE_PIPE_MODEL",
39+
"uniform" : "SAI_TUNNEL_TTL_MODE_UNIFORM_MODEL"
40+
}
41+
42+
43+
def check_interface_exists_in_asicdb(self, asicdb, sai_oid):
44+
if_table = swsscommon.Table(asicdb, self.ASIC_RIF_TABLE)
45+
status, fvs = if_table.get(sai_oid)
46+
return status
47+
48+
def check_vr_exists_in_asicdb(self, asicdb, sai_oid):
49+
vfr_table = swsscommon.Table(asicdb, self.ASIC_VRF_TABLE)
50+
status, fvs = vfr_table.get(sai_oid)
51+
return status
52+
53+
def create_and_test_peer(self, db, asicdb, peer_name, peer_ip, src_ip):
54+
""" Create PEER entry verify all needed enties in ASIC DB exists """
55+
56+
create_entry_tbl(
57+
db,
58+
"PEER_SWITCH", '|', "%s" % (peer_name),
59+
[
60+
("address_ipv4", peer_ip),
61+
]
62+
)
63+
64+
time.sleep(2)
65+
66+
# check asic db table
67+
tunnel_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TABLE)
68+
69+
tunnels = tunnel_table.getKeys()
70+
71+
# There will be two tunnels, one P2MP and another P2P
72+
assert len(tunnels) == 2
73+
74+
p2p_obj = None
75+
76+
for tunnel_sai_obj in tunnels:
77+
status, fvs = tunnel_table.get(tunnel_sai_obj)
78+
79+
assert status == True
80+
81+
for field, value in fvs:
82+
if field == "SAI_TUNNEL_ATTR_TYPE":
83+
assert value == "SAI_TUNNEL_TYPE_IPINIP"
84+
if field == "SAI_TUNNEL_ATTR_PEER_MODE":
85+
if value == "SAI_TUNNEL_PEER_MODE_P2P":
86+
p2p_obj = tunnel_sai_obj
87+
88+
assert p2p_obj != None
89+
90+
status, fvs = tunnel_table.get(p2p_obj)
91+
92+
assert status == True
93+
94+
for field, value in fvs:
95+
if field == "SAI_TUNNEL_ATTR_TYPE":
96+
assert value == "SAI_TUNNEL_TYPE_IPINIP"
97+
elif field == "SAI_TUNNEL_ATTR_ENCAP_SRC_IP":
98+
assert value == src_ip
99+
elif field == "SAI_TUNNEL_ATTR_ENCAP_DST_IP":
100+
assert value == peer_ip
101+
elif field == "SAI_TUNNEL_ATTR_PEER_MODE":
102+
assert value == "SAI_TUNNEL_PEER_MODE_P2P"
103+
elif field == "SAI_TUNNEL_ATTR_OVERLAY_INTERFACE":
104+
assert self.check_interface_exists_in_asicdb(asicdb, value)
105+
elif field == "SAI_TUNNEL_ATTR_UNDERLAY_INTERFACE":
106+
assert self.check_interface_exists_in_asicdb(asicdb, value)
107+
else:
108+
assert False, "Field %s is not tested" % field
109+
110+
111+
def check_tunnel_termination_entry_exists_in_asicdb(self, asicdb, tunnel_sai_oid, dst_ips):
112+
tunnel_term_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TERM_ENTRIES)
113+
114+
tunnel_term_entries = tunnel_term_table.getKeys()
115+
assert len(tunnel_term_entries) == len(dst_ips)
116+
117+
for term_entry in tunnel_term_entries:
118+
status, fvs = tunnel_term_table.get(term_entry)
119+
120+
assert status == True
121+
assert len(fvs) == 5
122+
123+
for field, value in fvs:
124+
if field == "SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_VR_ID":
125+
assert self.check_vr_exists_in_asicdb(asicdb, value)
126+
elif field == "SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TYPE":
127+
assert value == "SAI_TUNNEL_TERM_TABLE_ENTRY_TYPE_P2MP"
128+
elif field == "SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TUNNEL_TYPE":
129+
assert value == "SAI_TUNNEL_TYPE_IPINIP"
130+
elif field == "SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_ACTION_TUNNEL_ID":
131+
assert value == tunnel_sai_oid
132+
elif field == "SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_DST_IP":
133+
assert value in dst_ips
134+
else:
135+
assert False, "Field %s is not tested" % field
136+
137+
def create_and_test_tunnel(self, db, asicdb, tunnel_name, **kwargs):
138+
""" Create tunnel and verify all needed enties in ASIC DB exists """
139+
140+
is_symmetric_tunnel = "src_ip" in kwargs;
141+
142+
# create tunnel entry in DB
143+
ps = swsscommon.ProducerStateTable(db, self.APP_TUNNEL_DECAP_TABLE_NAME)
144+
145+
fvs = create_fvs(**kwargs)
146+
147+
ps.set(tunnel_name, fvs)
148+
149+
# wait till config will be applied
150+
time.sleep(1)
151+
152+
# check asic db table
153+
tunnel_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TABLE)
154+
155+
tunnels = tunnel_table.getKeys()
156+
assert len(tunnels) == 1
157+
158+
tunnel_sai_obj = tunnels[0]
159+
160+
status, fvs = tunnel_table.get(tunnel_sai_obj)
161+
162+
assert status == True
163+
# 6 parameters to check in case of decap tunnel
164+
# + 1 (SAI_TUNNEL_ATTR_ENCAP_SRC_IP) in case of symmetric tunnel
165+
assert len(fvs) == 7 if is_symmetric_tunnel else 6
166+
167+
expected_ecn_mode = self.ecn_modes_map[kwargs["ecn_mode"]]
168+
expected_dscp_mode = self.dscp_modes_map[kwargs["dscp_mode"]]
169+
expected_ttl_mode = self.ttl_modes_map[kwargs["ttl_mode"]]
170+
171+
for field, value in fvs:
172+
if field == "SAI_TUNNEL_ATTR_TYPE":
173+
assert value == "SAI_TUNNEL_TYPE_IPINIP"
174+
elif field == "SAI_TUNNEL_ATTR_ENCAP_SRC_IP":
175+
assert value == kwargs["src_ip"]
176+
elif field == "SAI_TUNNEL_ATTR_DECAP_ECN_MODE":
177+
assert value == expected_ecn_mode
178+
elif field == "SAI_TUNNEL_ATTR_DECAP_TTL_MODE":
179+
assert value == expected_ttl_mode
180+
elif field == "SAI_TUNNEL_ATTR_DECAP_DSCP_MODE":
181+
assert value == expected_dscp_mode
182+
elif field == "SAI_TUNNEL_ATTR_OVERLAY_INTERFACE":
183+
assert self.check_interface_exists_in_asicdb(asicdb, value)
184+
elif field == "SAI_TUNNEL_ATTR_UNDERLAY_INTERFACE":
185+
assert self.check_interface_exists_in_asicdb(asicdb, value)
186+
else:
187+
assert False, "Field %s is not tested" % field
188+
189+
self.check_tunnel_termination_entry_exists_in_asicdb(asicdb, tunnel_sai_obj, kwargs["dst_ip"].split(","))
190+
191+
def remove_and_test_tunnel(self, db, asicdb, tunnel_name):
192+
""" Removes tunnel and checks that ASIC db is clear"""
193+
194+
tunnel_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TABLE)
195+
tunnel_term_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TERM_ENTRIES)
196+
tunnel_app_table = swsscommon.Table(asicdb, self.APP_TUNNEL_DECAP_TABLE_NAME)
197+
198+
tunnels = tunnel_table.getKeys()
199+
tunnel_sai_obj = tunnels[0]
200+
201+
status, fvs = tunnel_table.get(tunnel_sai_obj)
202+
203+
# get overlay loopback interface oid to check if it is deleted with the tunnel
204+
overlay_infs_id = {f:v for f,v in fvs}["SAI_TUNNEL_ATTR_OVERLAY_INTERFACE"]
205+
206+
ps = swsscommon.ProducerStateTable(db, self.APP_TUNNEL_DECAP_TABLE_NAME)
207+
ps.set(tunnel_name, create_fvs(), 'DEL')
208+
209+
# wait till config will be applied
210+
time.sleep(1)
211+
212+
assert len(tunnel_table.getKeys()) == 0
213+
assert len(tunnel_term_table.getKeys()) == 0
214+
assert len(tunnel_app_table.getKeys()) == 0
215+
assert not self.check_interface_exists_in_asicdb(asicdb, overlay_infs_id)
216+
217+
def cleanup_left_over(self, db, asicdb):
218+
""" Cleanup APP and ASIC tables """
219+
220+
tunnel_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TABLE)
221+
for key in tunnel_table.getKeys():
222+
tunnel_table._del(key)
223+
224+
tunnel_term_table = swsscommon.Table(asicdb, self.ASIC_TUNNEL_TERM_ENTRIES)
225+
for key in tunnel_term_table.getKeys():
226+
tunnel_term_table._del(key)
227+
228+
tunnel_app_table = swsscommon.Table(asicdb, self.APP_TUNNEL_DECAP_TABLE_NAME)
229+
for key in tunnel_app_table.getKeys():
230+
tunnel_table._del(key)
231+
232+
233+
class TestMuxTunnel(TestMuxTunnelBase):
234+
""" Tests for Mux tunnel creation and removal """
235+
236+
def test_Tunnel(self, dvs, testlog):
237+
""" test IPv4 Mux tunnel creation """
238+
239+
db = swsscommon.DBConnector(swsscommon.APPL_DB, dvs.redis_sock, 0)
240+
asicdb = swsscommon.DBConnector(swsscommon.ASIC_DB, dvs.redis_sock, 0)
241+
242+
self.cleanup_left_over(db, asicdb)
243+
244+
# create tunnel IPv4 tunnel
245+
self.create_and_test_tunnel(db, asicdb, tunnel_name="MuxTunnel0", tunnel_type="IPINIP",
246+
dst_ip="10.1.0.32", dscp_mode="uniform",
247+
ecn_mode="standard", ttl_mode="pipe")
248+
249+
250+
def test_Peer(self, dvs, testlog):
251+
""" test IPv4 Mux tunnel creation """
252+
253+
db = swsscommon.DBConnector(swsscommon.CONFIG_DB, dvs.redis_sock, 0)
254+
asicdb = swsscommon.DBConnector(swsscommon.ASIC_DB, dvs.redis_sock, 0)
255+
256+
self.create_and_test_peer(db, asicdb, "peer", "1.1.1.1", "10.1.0.32")
257+
258+
259+
# Add Dummy always-pass test at end as workaroud
260+
# for issue when Flaky fail on final test it invokes module tear-down before retrying
261+
def test_nonflaky_dummy():
262+
pass

0 commit comments

Comments
 (0)