Skip to content

Commit 0fd7045

Browse files
Ihor Chekhms-junyi
authored andcommitted
BFD test fixes and improvements (sonic-net#6082)
*Single hop BFD test fixes and improvements
1 parent d438628 commit 0fd7045

1 file changed

Lines changed: 79 additions & 72 deletions

File tree

tests/bfd/test_bfd.py

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33
import time
44
import json
55

6-
from tests.common.fixtures.ptfhost_utils import change_mac_addresses, copy_arp_responder_py
7-
from tests.common.dualtor.dual_tor_utils import get_t1_ptf_ports
8-
from tests.common.dualtor.mux_simulator_control import mux_server_url
9-
from tests.common.dualtor.mux_simulator_control import toggle_all_simulator_ports_to_rand_selected_tor_m
10-
116
pytestmark = [
127
pytest.mark.topology('t1', 't1-lag', 't1-64-lag')
138
]
149

15-
def is_dualtor(tbinfo):
16-
"""Check if the testbed is dualtor."""
17-
return "dualtor" in tbinfo["topo"]["name"]
1810

1911
def get_t0_intfs(mg_facts):
2012
t0_intfs = []
@@ -27,21 +19,21 @@ def get_t0_intfs(mg_facts):
2719

2820

2921
def add_dut_ip(duthost, intfs, ips, prefix_len):
30-
cmd_buffer =""
22+
cmd_buffer = ""
3123
for idx in range(len(intfs)):
3224
cmd_buffer += 'sudo config interface ip add {} {}/{} ;'.format(intfs[idx], ips[idx], prefix_len)
33-
if idx%50 == 0:
25+
if idx % 50 == 0:
3426
duthost.shell(cmd_buffer)
35-
cmd_buffer =""
27+
cmd_buffer = ""
3628
if cmd_buffer != "":
3729
duthost.shell(cmd_buffer)
3830

3931

4032
def remove_dut_ip(duthost, intfs, ips, prefix_len):
41-
cmd_buffer =""
33+
cmd_buffer = ""
4234
for idx in range(len(intfs)):
43-
cmd_buffer += 'sudo config interface ip remove {} {}/{} ;'.format(intfs[idx], ips[idx], prefix_len)
44-
if idx%50 == 0:
35+
cmd_buffer += 'sudo config interface ip remove {} {}/{} ;'.format(intfs[idx], ips[idx], prefix_len)
36+
if idx % 50 == 0:
4537
duthost.shell(cmd_buffer)
4638
cmd_buffer = ""
4739
if cmd_buffer != "":
@@ -50,27 +42,30 @@ def remove_dut_ip(duthost, intfs, ips, prefix_len):
5042

5143
def get_neighbors(duthost, tbinfo, ipv6=False, count=1):
5244
mg_facts = duthost.get_extended_minigraph_facts(tbinfo)
53-
t1_ipv4_pattern = '101.0.0.{}'
54-
t1_ipv6_pattern = '2000:2000::{:x}'
45+
prefix_len = 127 if ipv6 else 31
46+
ip_pattern = '2000:2000::{:x}' if ipv6 else '101.0.0.{}'
5547
t0_intfs = get_t0_intfs(mg_facts)
5648
ptf_ports = [mg_facts['minigraph_ptf_indices'][port] for port in t0_intfs]
5749
count = min(count, len(t0_intfs))
5850
indices = random.sample(list(range(len(t0_intfs))), k=count)
5951
port_intfs = [t0_intfs[_] for _ in indices]
60-
neighbour_devs =[]
52+
neighbor_devs = []
6153
for intf in port_intfs:
6254
pc_member = False
6355
for pc in mg_facts['minigraph_portchannels']:
6456
if intf in mg_facts['minigraph_portchannels'][pc]['members']:
65-
neighbour_devs.append(pc)
66-
pc_member= True
57+
neighbor_devs.append(pc)
58+
pc_member = True
6759
break
6860
if not pc_member:
69-
neighbour_devs.append(intf)
70-
if ipv6:
71-
return [t1_ipv6_pattern.format(idx * 2) for idx in indices], 127, [t1_ipv6_pattern.format(idx * 2 + 1) for idx in indices], neighbour_devs, [ptf_ports[_] for _ in indices]
72-
else:
73-
return [t1_ipv4_pattern.format(idx * 2) for idx in indices], 31, [t1_ipv4_pattern.format(idx * 2 + 1) for idx in indices], neighbour_devs, [ptf_ports[_] for _ in indices]
61+
neighbor_devs.append(intf)
62+
63+
local_addrs = [ip_pattern.format(idx * 2) for idx in indices]
64+
neighbor_addrs = [ip_pattern.format(idx * 2 + 1) for idx in indices]
65+
neighbor_interfaces = [ptf_ports[_] for _ in indices]
66+
67+
return local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces
68+
7469

7570
def get_neighbors_scale(duthost, tbinfo, ipv6=False, scale_count=1):
7671
mg_facts = duthost.get_extended_minigraph_facts(tbinfo)
@@ -81,41 +76,41 @@ def get_neighbors_scale(duthost, tbinfo, ipv6=False, scale_count=1):
8176
count = min(2, len(t0_intfs))
8277
indices = random.sample(list(range(len(t0_intfs))), k=count)
8378
port_intfs = [t0_intfs[_] for _ in indices]
84-
neighbour_intfs =[]
79+
neighbor_intfs = []
8580
for intf in port_intfs:
8681
pc_member = False
8782
for pc in mg_facts['minigraph_portchannels']:
8883
if intf in mg_facts['minigraph_portchannels'][pc]['members']:
89-
neighbour_intfs.append(pc)
90-
pc_member= True
84+
neighbor_intfs.append(pc)
85+
pc_member = True
9186
break
92-
if not pc_member:
93-
neighbour_intfs.append(intf)
87+
if not pc_member:
88+
neighbor_intfs.append(intf)
9489
ptf_intfs = [ptf_ports[_] for _ in indices]
95-
#local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces
90+
# local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces
9691
local_addrs = []
97-
neighbour_addrs = []
98-
neighbour_devs = []
99-
ptf_devs =[]
100-
idx2 =0
92+
neighbor_addrs = []
93+
neighbor_devs = []
94+
ptf_devs = []
10195
index = 0
10296
for idx in range(1, scale_count):
103-
if idx !=0 and idx %127 == 0:
104-
index +=1
97+
if idx != 0 and idx % 127 == 0:
98+
index += 1
10599
if ipv6:
106100
local_addrs.append(t1_ipv6_pattern.format(idx * 2))
107-
neighbour_addrs.append(t1_ipv6_pattern.format(idx * 2 + 1))
108-
neighbour_devs.append(neighbour_intfs[index])
101+
neighbor_addrs.append(t1_ipv6_pattern.format(idx * 2 + 1))
102+
neighbor_devs.append(neighbor_intfs[index])
109103
ptf_devs.append(ptf_intfs[index])
110104
else:
111-
rolloveridx = idx %125
112-
idx2 = idx//125
105+
rolloveridx = idx % 125
106+
idx2 = idx // 125
113107
local_addrs.append(t1_ipv4_pattern.format(idx2, rolloveridx * 2))
114-
neighbour_addrs.append(t1_ipv4_pattern.format(idx2, rolloveridx * 2 + 1))
115-
neighbour_devs.append(neighbour_intfs[index])
108+
neighbor_addrs.append(t1_ipv4_pattern.format(idx2, rolloveridx * 2 + 1))
109+
neighbor_devs.append(neighbor_intfs[index])
116110
ptf_devs.append(ptf_intfs[index])
117-
prefix = 127 if ipv6 else 31
118-
return local_addrs, prefix, neighbour_addrs, neighbour_devs, ptf_devs
111+
prefix = 127 if ipv6 else 31
112+
return local_addrs, prefix, neighbor_addrs, neighbor_devs, ptf_devs
113+
119114

120115
def init_ptf_bfd(ptfhost):
121116
ptfhost.shell("bfdd-beacon")
@@ -129,59 +124,68 @@ def add_ipaddr(ptfhost, neighbor_addrs, prefix_len, neighbor_interfaces, ipv6=Fa
129124
cmd_buffer = ""
130125
for idx in range(len(neighbor_addrs)):
131126
if ipv6:
132-
cmd_buffer += "ip -6 addr add {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len, neighbor_interfaces[idx])
127+
cmd_buffer += "ip -6 addr add {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len,
128+
neighbor_interfaces[idx])
133129
else:
134-
cmd_buffer += "ip addr add {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len, neighbor_interfaces[idx])
135-
if idx%50 == 0:
130+
cmd_buffer += "ip addr add {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len,
131+
neighbor_interfaces[idx])
132+
if idx % 50 == 0:
136133
ptfhost.shell(cmd_buffer)
137134
cmd_buffer = ""
138135
if cmd_buffer != "":
139136
ptfhost.shell(cmd_buffer)
140137

138+
141139
def del_ipaddr(ptfhost, neighbor_addrs, prefix_len, neighbor_interfaces, ipv6=False):
142140
cmd_buffer = ""
143141
for idx in range(len(neighbor_addrs)):
144142
if ipv6:
145-
cmd_buffer += "ip -6 addr del {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len, neighbor_interfaces[idx])
143+
cmd_buffer += "ip -6 addr del {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len,
144+
neighbor_interfaces[idx])
146145
else:
147-
cmd_buffer += "ip addr del {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len, neighbor_interfaces[idx])
148-
if idx%50 == 0:
146+
cmd_buffer += "ip addr del {}/{} dev eth{} ;".format(neighbor_addrs[idx], prefix_len,
147+
neighbor_interfaces[idx])
148+
if idx % 50 == 0:
149149
ptfhost.shell(cmd_buffer)
150150
cmd_buffer = ""
151151
if cmd_buffer != "":
152-
ptfhost.shell(cmd_buffer, module_ignore_errors=True)
152+
ptfhost.shell(cmd_buffer, module_ignore_errors=True)
153+
153154

154155
def check_ptf_bfd_status(ptfhost, neighbor_addr, local_addr, expected_state):
155-
bfd_state = ptfhost.shell("bfdd-control status local {} remote {}".format(neighbor_addr, local_addr))["stdout"].split("\n")
156+
bfd_state = ptfhost.shell("bfdd-control status local {} remote {}"
157+
.format(neighbor_addr, local_addr))["stdout"].split("\n")
156158
for line in bfd_state:
157159
field = line.split('=')[0].strip()
158160
if field == "state":
159161
assert expected_state in line.split('=')[1].strip()
160162

161163

162164
def check_dut_bfd_status(duthost, neighbor_addr, expected_state):
163-
bfd_state = duthost.shell("sonic-db-cli STATE_DB HGET 'BFD_SESSION_TABLE|default|default|{}' 'state'".format(neighbor_addr), module_ignore_errors=False)['stdout_lines']
164-
assert expected_state in bfd_state[0]
165+
bfd_state = duthost.shell("sonic-db-cli STATE_DB HGET 'BFD_SESSION_TABLE|default|default|{}' 'state'"
166+
.format(neighbor_addr), module_ignore_errors=False)['stdout_lines']
167+
assert expected_state in bfd_state[0]
168+
165169

166-
def create_bfd_sessions(ptfhost, duthost, local_addrs, neighbor_addrs, dut_init_first, scale_test = False):
170+
def create_bfd_sessions(ptfhost, duthost, local_addrs, neighbor_addrs, dut_init_first, scale_test=False):
167171
# Create a tempfile for BFD sessions
168172
bfd_file_dir = duthost.shell('mktemp')['stdout']
169173
bfd_config = []
170174
ptf_buffer = ""
171175
if scale_test:
172-
# Force the PTF initialization to be first if runnign scale test. Doing so that we can send bathces fo 50 commadns to ptf
173-
# and keep the code readable.
174-
assert( dut_init_first == False )
175-
176+
# Force the PTF initialization to be first if running a scale test.
177+
# Doing so that we can send batches of 50 commands to PTF and keep the code readable.
178+
assert (dut_init_first is False)
179+
176180
for idx, neighbor_addr in enumerate(neighbor_addrs):
177181
bfd_config.append({
178182
"BFD_SESSION_TABLE:default:default:{}".format(neighbor_addr): {
179183
"local_addr": local_addrs[idx]
180184
},
181185
"OP": "SET"
182186
})
183-
ptf_buffer +="bfdd-control connect local {} remote {} ; ".format(neighbor_addr, local_addrs[idx])
184-
if scale_test and idx%50 == 0:
187+
ptf_buffer += "bfdd-control connect local {} remote {} ; ".format(neighbor_addr, local_addrs[idx])
188+
if scale_test and idx % 50 == 0:
185189
ptfhost.shell(ptf_buffer)
186190
ptf_buffer = ""
187191

@@ -195,7 +199,7 @@ def create_bfd_sessions(ptfhost, duthost, local_addrs, neighbor_addrs, dut_init_
195199
module_ignore_errors=True)
196200
if result['rc'] != 0:
197201
pytest.fail('Failed to apply BFD session configuration file: {}'.format(result['stderr']))
198-
if dut_init_first :
202+
if dut_init_first:
199203
ptfhost.shell(ptf_buffer)
200204

201205

@@ -224,15 +228,18 @@ def remove_bfd_sessions(duthost, local_addrs, neighbor_addrs):
224228
def update_bfd_session_state(ptfhost, neighbor_addr, local_addr, state):
225229
ptfhost.shell("bfdd-control session local {} remote {} state {}".format(neighbor_addr, local_addr, state))
226230

231+
227232
def update_bfd_state(ptfhost, neighbor_addr, local_addr, state):
228233
ptfhost.shell("bfdd-control session local {} remote {} {}".format(neighbor_addr, local_addr, state))
229234

235+
230236
@pytest.mark.parametrize('dut_init_first', [True, False], ids=['dut_init_first', 'ptf_init_first'])
231237
@pytest.mark.parametrize('ipv6', [False, True], ids=['ipv4', 'ipv6'])
232-
def test_bfd_basic(request, rand_selected_dut, ptfhost, tbinfo, toggle_all_simulator_ports_to_rand_selected_tor_m, ipv6, dut_init_first):
238+
def test_bfd_basic(request, rand_selected_dut, ptfhost, tbinfo, ipv6, dut_init_first):
233239
duthost = rand_selected_dut
234-
bfd_session_cnt = int(request.config.getoption('--num_sessions'))
235-
local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces = get_neighbors(duthost, tbinfo, ipv6, count = bfd_session_cnt)
240+
bfd_session_cnt = int(request.config.getoption('--num_sessions'))
241+
local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces = get_neighbors(duthost, tbinfo, ipv6,
242+
count=bfd_session_cnt)
236243
try:
237244
add_dut_ip(duthost, neighbor_devs, local_addrs, prefix_len)
238245
init_ptf_bfd(ptfhost)
@@ -255,17 +262,16 @@ def test_bfd_basic(request, rand_selected_dut, ptfhost, tbinfo, toggle_all_simul
255262
else:
256263
check_dut_bfd_status(duthost, neighbor_addr, "Up")
257264
check_ptf_bfd_status(ptfhost, neighbor_addr, local_addrs[idx], "Up")
258-
265+
259266
update_bfd_session_state(ptfhost, neighbor_addrs[update_idx], local_addrs[update_idx], "up")
260267
time.sleep(1)
261268

262269
check_dut_bfd_status(duthost, neighbor_addrs[update_idx], "Up")
263270
check_ptf_bfd_status(ptfhost, neighbor_addrs[update_idx], local_addrs[update_idx], "Up")
264271

265-
266272
update_idx = random.choice(range(bfd_session_cnt))
267273
update_bfd_state(ptfhost, neighbor_addrs[update_idx], local_addrs[update_idx], "suspend")
268-
time.sleep(1)
274+
time.sleep(5)
269275

270276
for idx, neighbor_addr in enumerate(neighbor_addrs):
271277
if idx == update_idx:
@@ -274,7 +280,7 @@ def test_bfd_basic(request, rand_selected_dut, ptfhost, tbinfo, toggle_all_simul
274280
else:
275281
check_dut_bfd_status(duthost, neighbor_addr, "Up")
276282
check_ptf_bfd_status(ptfhost, neighbor_addr, local_addrs[idx], "Up")
277-
283+
278284
finally:
279285
stop_ptf_bfd(ptfhost)
280286
del_ipaddr(ptfhost, neighbor_addrs, prefix_len, neighbor_interfaces, ipv6)
@@ -283,10 +289,11 @@ def test_bfd_basic(request, rand_selected_dut, ptfhost, tbinfo, toggle_all_simul
283289

284290

285291
@pytest.mark.parametrize('ipv6', [False, True], ids=['ipv4', 'ipv6'])
286-
def test_bfd_scale(rand_selected_dut, ptfhost, tbinfo, toggle_all_simulator_ports_to_rand_selected_tor_m, ipv6):
292+
def test_bfd_scale(request, rand_selected_dut, ptfhost, tbinfo, ipv6):
287293
duthost = rand_selected_dut
288-
bfd_session_cnt = int(request.config.getoption('--num_sessions_scale'))
289-
local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces = get_neighbors_scale(duthost, tbinfo, ipv6, scale_count = bfd_session_cnt)
294+
bfd_session_cnt = int(request.config.getoption('--num_sessions_scale'))
295+
local_addrs, prefix_len, neighbor_addrs, neighbor_devs, neighbor_interfaces = \
296+
get_neighbors_scale(duthost, tbinfo, ipv6, scale_count=bfd_session_cnt)
290297

291298
try:
292299
add_dut_ip(duthost, neighbor_devs, local_addrs, prefix_len)

0 commit comments

Comments
 (0)