Skip to content

Commit 29c7293

Browse files
PriyanshTratiyaytzur1
authored andcommitted
Fix/nonlinear dataplane downtime (sonic-net#21936)
* ptf dataplane cleaners for in between test runs Signed-off-by: Priyansh Tratiya <[email protected]> Signed-off-by: Yael Tzur <[email protected]>
1 parent 2ebd0c3 commit 29c7293

2 files changed

Lines changed: 66 additions & 11 deletions

File tree

tests/bgp/test_ipv6_bgp_scale.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,26 @@ def check_bgp_routes_converged(duthost, expected_routes, shutdown_connections=No
415415
pytest.fail(f"BGP routes aren't stable in {timeout} seconds")
416416

417417

418+
@pytest.fixture(scope="function")
419+
def clean_ptf_dataplane(ptfadapter):
420+
"""
421+
Drain queued packets and clear mask counters before and after each test.
422+
The idea is that each test should start with clean dataplane state without
423+
having to restart ptfadapter fixture for each test.
424+
Takes in the function scope so that each parametrized test case also gets a clean dataplane.
425+
"""
426+
dp = ptfadapter.dataplane
427+
428+
def _perform_cleanup_on_dp():
429+
dp.drain()
430+
dp.clear_masks()
431+
# Before test run DP cleanup
432+
_perform_cleanup_on_dp()
433+
yield
434+
# After test run DP cleanup
435+
_perform_cleanup_on_dp()
436+
437+
418438
def compress_expected_routes(expected_routes):
419439
json_str = json.dumps(expected_routes)
420440
compressed = gzip.compress(json_str.encode('utf-8'))
@@ -503,7 +523,7 @@ def _select_targets_to_flap(bgp_peers_info, all_flap, flapping_count):
503523
return flapping_neighbors, injection_neighbor, flapping_ports, injection_port
504524

505525

506-
def flapper(duthost, pdp, bgp_peers_info, transient_setup, flapping_count, connection_type, action):
526+
def flapper(duthost, ptfadapter, bgp_peers_info, transient_setup, flapping_count, connection_type, action):
507527
"""
508528
Orchestrates interface/BGP session flapping and recovery on the DUT, generating test traffic to assess both
509529
control and data plane convergence behavior. This function is designed for use in test scenarios
@@ -525,6 +545,9 @@ def flapper(duthost, pdp, bgp_peers_info, transient_setup, flapping_count, conne
525545
global global_icmp_type, current_test, test_results
526546
current_test = f"flapper_{action}_{connection_type}_count_{flapping_count}"
527547
global_icmp_type += 1
548+
pdp = ptfadapter.dataplane
549+
pdp.clear_masks()
550+
pdp.set_qlen(PACKET_QUEUE_LENGTH)
528551
exp_mask = setup_packet_mask_counters(pdp, global_icmp_type)
529552
all_flap = (flapping_count == 'all')
530553

@@ -788,7 +811,9 @@ def test_bgp_admin_flap(
788811
duthost,
789812
ptfadapter,
790813
bgp_peers_info,
791-
flapping_neighbor_count
814+
clean_ptf_dataplane,
815+
flapping_neighbor_count,
816+
setup_routes_before_test
792817
):
793818
"""
794819
Validates that both control plane and data plane remain functional with acceptable downtime when BGP sessions are
@@ -801,12 +826,11 @@ def test_bgp_admin_flap(
801826
Expected result:
802827
Dataplane downtime is less than MAX_BGP_SESSION_DOWNTIME or MAX_DOWNTIME_UNISOLATION for all ports.
803828
"""
804-
pdp = ptfadapter.dataplane
805-
pdp.set_qlen(PACKET_QUEUE_LENGTH)
806829
# Measure shutdown convergence
807-
transient_setup = flapper(duthost, pdp, bgp_peers_info, None, flapping_neighbor_count, 'bgp_sessions', 'shutdown')
830+
transient_setup = flapper(duthost, ptfadapter, bgp_peers_info, None, flapping_neighbor_count,
831+
'bgp_sessions', 'shutdown')
808832
# Measure startup convergence
809-
flapper(duthost, pdp, None, transient_setup, flapping_neighbor_count, 'bgp_sessions', 'startup')
833+
flapper(duthost, ptfadapter, None, transient_setup, flapping_neighbor_count, 'bgp_sessions', 'startup')
810834

811835

812836
@pytest.mark.parametrize("flapping_port_count", [1, 10, 20, 'all'])
@@ -815,6 +839,7 @@ def test_sessions_flapping(
815839
duthost,
816840
ptfadapter,
817841
bgp_peers_info,
842+
clean_ptf_dataplane,
818843
flapping_port_count,
819844
setup_routes_before_test
820845
):
@@ -829,10 +854,7 @@ def test_sessions_flapping(
829854
Expected result:
830855
Dataplane downtime is less than MAX_DOWNTIME_PORT_FLAPPING or MAX_DOWNTIME_UNISOLATION for all ports.
831856
'''
832-
pdp = ptfadapter.dataplane
833-
pdp.set_qlen(PACKET_QUEUE_LENGTH)
834-
835857
# Measure shutdown convergence
836-
transient_setup = flapper(duthost, pdp, bgp_peers_info, None, flapping_port_count, 'ports', 'shutdown')
858+
transient_setup = flapper(duthost, ptfadapter, bgp_peers_info, None, flapping_port_count, 'ports', 'shutdown')
837859
# Measure startup convergence
838-
flapper(duthost, pdp, None, transient_setup, flapping_port_count, 'ports', 'startup')
860+
flapper(duthost, ptfadapter, None, transient_setup, flapping_port_count, 'ports', 'startup')

tests/common/plugins/ptfadapter/ptfadapter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ def _init_ptf_dataplane(self, ptf_config=None):
129129
packet
130130
)
131131
self.dataplane = ptf.dataplane_instance
132+
self._attach_cleanup_helpers()
133+
134+
def _attach_cleanup_helpers(self):
135+
dp = self.dataplane
136+
137+
def drain(max_per_port=800):
138+
"""
139+
Best-effort non-blocking drain of residual queued packets per port.
140+
Prevents backlog from prior test affecting pps/downtime.
141+
"""
142+
try:
143+
for (dev, port) in list(getattr(dp, "ports", {}).keys()):
144+
drained = 0
145+
while drained < max_per_port:
146+
pkt = dp.poll(device_number=dev, port_number=port, timeout=0)
147+
if pkt is None:
148+
break
149+
drained += 1
150+
except Exception:
151+
pass
152+
153+
def clear_masks():
154+
"""
155+
Remove any previously registered Mask counters to avoid cumulative match overhead.
156+
"""
157+
try:
158+
dp.mask_rx_cnt.clear()
159+
dp.mask_tx_cnt.clear()
160+
dp.masked_packets.clear()
161+
except Exception:
162+
pass
163+
dp.drain = drain
164+
dp.clear_masks = clear_masks
132165

133166
def kill(self):
134167
""" Close dataplane socket and kill data plane thread """

0 commit comments

Comments
 (0)