1818#
1919#
2020
21+ import os
2122from collections import OrderedDict
2223from ptf import config
2324from sai_utils import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
@@ -497,7 +498,22 @@ def turn_up_and_get_checked_ports(self, port_list: List['Port']):
497498 '''
498499
499500 # For brcm devices, need to init and setup the ports at once after start the switch.
500- retries = 2
501+ #
502+ # Waiting strategy is configurable via environment, defaulting to the original
503+ # behavior (retries=2, 1s poll interval). The original loop waited PER PORT
504+ # serially (retries x interval seconds for each of N ports), which on a large
505+ # port count where oper-status is slow to settle costs N * retries * interval
506+ # seconds (e.g. 32 ports x 2 x 1s ~= 64s). The shared-wait path below issues
507+ # admin-up to every port once, then polls ALL ports together for at most
508+ # (retries x interval) seconds total, re-asserting admin-up on stragglers each
509+ # round. Semantics are preserved: every port is set admin-up, oper-status is
510+ # polled, and down_port_list / the returned list are identical; only the total
511+ # wall-clock wait changes. Real-HW/ASIC consumers that do not set these env
512+ # vars keep the exact original timing.
513+ retries = int (os .environ .get ('SAI_PORT_UP_RETRIES' , '2' ))
514+ poll_interval = float (os .environ .get ('SAI_PORT_UP_POLL_INTERVAL' , '1' ))
515+ # Opt-in: poll all ports together in one bounded loop instead of per-port.
516+ shared_wait = os .environ .get ('SAI_PORT_UP_SHARED_WAIT' , '0' ) == '1'
501517 down_port_list = []
502518 test_port_list :List [Port ] = []
503519
@@ -513,29 +529,49 @@ def turn_up_and_get_checked_ports(self, port_list: List['Port']):
513529 port_oid = port .oid ,
514530 admin_state = True )
515531
516- for index , port in enumerate (test_port_list ):
517- port_attr = sai_thrift_get_port_attribute (
518- self .client , port .oid , oper_status = True )
519- print ("Turn up port {}" .format (index ))
520- port_up = True
521- if port_attr ['oper_status' ] != SAI_PORT_OPER_STATUS_UP :
522- port_up = False
523- for num_of_tries in range (retries ):
532+ if shared_wait :
533+ # Single bounded wait shared across all ports.
534+ pending = list (enumerate (test_port_list ))
535+ for num_of_tries in range (retries + 1 ):
536+ still_down = []
537+ for index , port in pending :
524538 port_attr = sai_thrift_get_port_attribute (
525539 self .client , port .oid , oper_status = True )
526- if port_attr ['oper_status' ] == SAI_PORT_OPER_STATUS_UP :
527- port_up = True
528- break
529- time .sleep (1 )
530- self .log_port_state (port , index )
531- print ("port {} , local index {} id {} is not up, status: {}. Retry. Reset Admin State." .format (
532- index , port .port_index , port .oid , port_attr ['oper_status' ]))
533- sai_thrift_set_port_attribute (
534- self .client ,
535- port_oid = port .oid ,
536- admin_state = True )
537- if not port_up :
538- down_port_list .append (index )
540+ if port_attr ['oper_status' ] != SAI_PORT_OPER_STATUS_UP :
541+ still_down .append ((index , port ))
542+ pending = still_down
543+ if not pending :
544+ break
545+ if num_of_tries < retries :
546+ time .sleep (poll_interval )
547+ for index , port in pending :
548+ sai_thrift_set_port_attribute (
549+ self .client , port_oid = port .oid , admin_state = True )
550+ down_port_list = [index for index , _ in pending ]
551+ else :
552+ for index , port in enumerate (test_port_list ):
553+ port_attr = sai_thrift_get_port_attribute (
554+ self .client , port .oid , oper_status = True )
555+ print ("Turn up port {}" .format (index ))
556+ port_up = True
557+ if port_attr ['oper_status' ] != SAI_PORT_OPER_STATUS_UP :
558+ port_up = False
559+ for num_of_tries in range (retries ):
560+ port_attr = sai_thrift_get_port_attribute (
561+ self .client , port .oid , oper_status = True )
562+ if port_attr ['oper_status' ] == SAI_PORT_OPER_STATUS_UP :
563+ port_up = True
564+ break
565+ time .sleep (poll_interval )
566+ self .log_port_state (port , index )
567+ print ("port {} , local index {} id {} is not up, status: {}. Retry. Reset Admin State." .format (
568+ index , port .port_index , port .oid , port_attr ['oper_status' ]))
569+ sai_thrift_set_port_attribute (
570+ self .client ,
571+ port_oid = port .oid ,
572+ admin_state = True )
573+ if not port_up :
574+ down_port_list .append (index )
539575 if down_port_list :
540576 print ("Ports {} are down after retries." .format (down_port_list ))
541577 return test_port_list
0 commit comments