1- # SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
1+ # SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
22# SPDX-License-Identifier: CC0-1.0
33import base64
44import io
@@ -55,7 +55,7 @@ def exec_cmd(self, cmd: str) -> str:
5555 error = stderr .read ().decode ().strip ()
5656 if error :
5757 out = ''
58- logging .error ('ssh_endnode_exec error: {}' . format ( error ) )
58+ logging .error (f 'ssh_endnode_exec error: { error } ' )
5959
6060 return out # type: ignore
6161
@@ -101,7 +101,7 @@ def exec_cmd(self, cmd: Union[str, List[str]]) -> str:
101101 error = stderr .read ().decode ().strip ()
102102
103103 if error != 'TSW Init OK!' :
104- raise Exception ('switch_5xp exec_cmd error: {}' . format ( error ) )
104+ raise Exception (f 'switch_5xp exec_cmd error: { error } ' )
105105 else :
106106 out = self .ssh_client .send_config_set (cmd , cmd_verify = False , exit_config_mode = False )
107107 return out # type: ignore
@@ -158,15 +158,15 @@ def get_host_interface_name_in_same_net(ip_addr: str) -> str:
158158
159159def get_host_mac_by_interface (interface_name : str , addr_type : int = netifaces .AF_LINK ) -> str :
160160 for _addr in netifaces .ifaddresses (interface_name )[addr_type ]:
161- host_mac = _addr ['addr' ].replace ('%{}' . format ( interface_name ) , '' )
161+ host_mac = _addr ['addr' ].replace (f '%{ interface_name } ' , '' )
162162 assert isinstance (host_mac , str )
163163 return host_mac
164164 return ''
165165
166166
167167def get_host_brcast_ip_by_interface (interface_name : str , ip_type : int = netifaces .AF_INET ) -> str :
168168 for _addr in netifaces .ifaddresses (interface_name )[ip_type ]:
169- host_ip = _addr ['broadcast' ].replace ('%{}' . format ( interface_name ) , '' )
169+ host_ip = _addr ['broadcast' ].replace (f '%{ interface_name } ' , '' )
170170 assert isinstance (host_ip , str )
171171 return host_ip
172172 return ''
@@ -180,21 +180,31 @@ def run_iperf(proto: str, endnode: EndnodeSsh, server_ip: str, bandwidth_lim:int
180180
181181 if ipaddress .ip_address (server_ip ).is_multicast :
182182 # Configure Multicast Server
183- server_proc = subprocess .Popen (['iperf' , '-u' , '-s' , '-i' , '1' , '-t' , '%i' % interval , '-B' , '%s%%%s'
184- % (server_ip , server_if )], text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
183+ server_proc = subprocess .Popen (
184+ ['iperf' , '-u' , '-s' , '-i' , '1' , '-t' , str (interval ), '-B' , f'{ server_ip } %{ server_if } ' ],
185+ text = True ,
186+ stdout = subprocess .PIPE ,
187+ stderr = subprocess .PIPE ,
188+ )
185189 # Configure Multicast Client
186190 endnode_ip = get_endnode_ip_by_interface (endnode , client_if )
187191 if endnode_ip == '' :
188192 raise RuntimeError ('End node IP address not found' )
189- client_res = endnode .exec_cmd ('iperf -u -c %s -t %i -i 1 -b %iM --ttl 5 -B %s' % (server_ip , interval , bandwidth_lim , endnode_ip ))
193+ client_res = endnode .exec_cmd (
194+ f'iperf -u -c { server_ip } -t { interval } -i 1 -b { bandwidth_lim } M --ttl 5 -B { endnode_ip } '
195+ )
190196 if server_proc .wait (10 ) is None : # Process did not finish.
191197 server_proc .terminate ()
192198 else :
193199 # Configure Server
194- server_proc = subprocess .Popen (['iperf' , '%s' % proto , '-s' , '-i' , '1' , '-t' , '%i' % interval ], text = True ,
195- stdout = subprocess .PIPE , stderr = subprocess .PIPE )
200+ server_proc = subprocess .Popen (
201+ ['iperf' , proto , '-s' , '-i' , '1' , '-t' , str (interval )],
202+ text = True ,
203+ stdout = subprocess .PIPE ,
204+ stderr = subprocess .PIPE ,
205+ )
196206 # Configure Client
197- client_res = endnode .exec_cmd ('iperf %s -c %s -t %i -i 1 -b %iM' % ( proto , server_ip , interval , bandwidth_lim ) )
207+ client_res = endnode .exec_cmd (f 'iperf { proto } -c { server_ip } -t { interval } -i 1 -b { bandwidth_lim } M' )
198208 if server_proc .wait (10 ) is None : # Process did not finish.
199209 server_proc .terminate ()
200210
@@ -224,8 +234,8 @@ def send_brcast_msg_host_to_endnode(endnode: EndnodeSsh, host_brcast_ip: str, te
224234 try :
225235 sock .setsockopt (socket .SOL_SOCKET , socket .SO_BROADCAST , 1 )
226236 sock .sendto (test_msg .encode ('utf-8' ), (host_brcast_ip , 5100 ))
227- except socket . error as e :
228- raise Exception ('Host brcast send failed %s' % e )
237+ except OSError as e :
238+ raise Exception (f 'Host brcast send failed { e } ' )
229239
230240 nc_endnode_out = endnode .get_async_res ()
231241 sock .close ()
@@ -234,18 +244,24 @@ def send_brcast_msg_host_to_endnode(endnode: EndnodeSsh, host_brcast_ip: str, te
234244
235245def send_brcast_msg_endnode_to_host (endnode : EndnodeSsh , host_brcast_ip : str , test_msg : str ) -> str :
236246 sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
247+ sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 ) # Allow binding if port still in TIME_WAIT
237248 sock .settimeout (5 )
238249 try :
239250 sock .bind (('' , 5100 ))
240- except socket .error as e :
241- raise Exception ('Host bind failed %s' % e )
251+ # Give socket time to be fully ready to receive before we tell endnode to send.
252+ # Even with SSH latency, there's a small window where a fast-received packet could be dropped.
253+ time .sleep (0.1 )
254+ except OSError as e :
255+ raise Exception (f'Host bind failed { e } ' )
242256
243- endnode .exec_cmd ('echo -n "%s " | nc -b -w0 -u %s 5100' % ( test_msg , host_brcast_ip ) )
257+ endnode .exec_cmd (f 'echo -n "{ test_msg } " | nc -b -w0 -u { host_brcast_ip } 5100' )
244258
245259 try :
246260 nc_host_out = sock .recv (1500 ).decode ('utf-8' )
247- except socket .error as e :
248- raise Exception ('Host recv failed %s' , e )
261+ except TimeoutError :
262+ raise Exception ('Host recv timed out after 5 seconds' )
263+ except OSError as e :
264+ raise Exception (f'Host recv failed { e } ' )
249265
250266 sock .close ()
251267 return nc_host_out
@@ -408,11 +424,17 @@ def test_esp_eth_bridge(
408424 logging .info ('Multicast UDP average bandwidth: %s Mbits/s' , bandwidth_mcast_udp )
409425
410426 if bandwidth_udp < MIN_UDP_THROUGHPUT :
411- raise RuntimeError ('Unicast UDP throughput expected %.2f, actual %.2f' % (MIN_UDP_THROUGHPUT , bandwidth_udp ) + ' Mbits/s' )
427+ raise RuntimeError (
428+ f'Unicast UDP throughput expected { MIN_UDP_THROUGHPUT :.2f} , actual { bandwidth_udp :.2f} Mbits/s'
429+ )
412430 if bandwidth_tcp < MIN_TCP_THROUGHPUT :
413- raise RuntimeError ('Unicast TCP throughput expected %.2f, actual %.2f' % (MIN_TCP_THROUGHPUT , bandwidth_tcp ) + ' Mbits/s' )
431+ raise RuntimeError (
432+ f'Unicast TCP throughput expected { MIN_TCP_THROUGHPUT :.2f} , actual { bandwidth_tcp :.2f} Mbits/s'
433+ )
414434 if bandwidth_mcast_udp < MIN_UDP_THROUGHPUT :
415- raise RuntimeError ('Multicast UDP throughput expected %.2f, actual %.2f' % (MIN_UDP_THROUGHPUT , bandwidth_mcast_udp ) + ' Mbits/s' )
435+ raise RuntimeError (
436+ f'Multicast UDP throughput expected { MIN_UDP_THROUGHPUT :.2f} , actual { bandwidth_mcast_udp :.2f} Mbits/s'
437+ )
416438
417439 # ------------------------------------------------
418440 # TEST Objective 4: adding/deleting entries in FDB
@@ -465,7 +487,7 @@ def test_esp_eth_bridge(
465487
466488 # try to add more FDB entries than configured max number
467489 for i in range (BR_PORTS_NUM + 1 ):
468- dut .write ('add --addr=01:02:03:00:00:%02x' % i + ' -d' )
490+ dut .write (f 'add --addr=01:02:03:00:00:{ i :02x } -d' )
469491 if i < BR_PORTS_NUM :
470492 dut .expect_exact ('Bridge Config OK!' )
471493 else :
@@ -479,7 +501,7 @@ def test_esp_eth_bridge(
479501
480502 # remove dummy entries
481503 for i in range (BR_PORTS_NUM ):
482- dut .write ('remove --addr=01:02:03:00:00:% 02x' % i )
504+ dut .write (f 'remove --addr=01:02:03:00:00:{ i : 02x} ' )
483505 dut .expect_exact ('Bridge Config OK!' )
484506
485507 # valid multiple ports at once
0 commit comments