PYTHON   46

traffic shaping test py

Guest on 25th July 2022 05:18:40 AM

  1. import * from qos_includes
  2.  
  3. def __init__():
  4.   # internal to run upon QoS initialization
  5.   global shaping_queue
  6.   global Tc
  7.   global Bc
  8.   global CIR
  9.   global token_bucket
  10.   global time_commit
  11.  
  12.   token_bucket = 0
  13.   time_commit = 0
  14.   shaping_queue = INIT_QMBUF()
  15.  
  16.   # cir = 1 Gbps, bc = 256 KB
  17.   CIR = 1000000000
  18.   Bc = 256000
  19.  
  20.   # Tc = Bc / CIR, or 2.048 milliseconds in this case.
  21.   Tc = (Bc * 8) / CIR
  22.  
  23. def token_bucket_filter( a_pkt_size ):
  24.   global Bc
  25.   global token_bucket
  26.  
  27.   if token_bucket <= 0:
  28.     return False
  29.   else:
  30.     token_bucket -= a_pkt_size
  31.     return True
  32.  
  33. def process_tx_queue(received_packet):
  34.   global Tc
  35.   global Bc
  36.   global CIR
  37.   global token_bucket
  38.   global time_commit
  39.   global shaping_queue
  40.  
  41.   # Main processing loop in the router -- this function is called every clock cycle, non-stop
  42.  
  43.   # get byte size of the packet received
  44.   pkt_size = len(received_packet)
  45.  
  46.   time_since = Get_Current_Time() - time_commit
  47.  
  48.   if time_since >= Tc:
  49.     token_bucket += Bc
  50.     time_commit = Get_Current_Time()
  51.  
  52.   ts_active = 0
  53.   if len(shaping_queue) > 0:
  54.      # shaping queue is NOT EMPTY
  55.      ts_active = 1
  56.  
  57.      if time_since >= Tc:
  58.        # For every Tc, dequeue the entire egress queue
  59.        # the *entire* shaping_queue will leave the router at line rate
  60.        try:
  61.          XMIT_OUT_IF( shaping_queue )
  62.        except InterfaceBusyError:
  63.          raise TxRing_Exhausted_Exception, TailDrop_M_Exception
  64.          QMBUF_FREE( shaping_queue )
  65.          return False
  66.  
  67.        # process new packet received just now
  68.        ts_active = 0
  69.      
  70.      else:
  71.        # keep shoving received_packet into the shaping_queue train, until it fills
  72.        try:
  73.          ENQUEUE_ADD( shaping_queue, received_packet )
  74.        except BufferSchedError:
  75.          raise TailDropException
  76.          QMBUF_FREE( received_packet )
  77.          return False
  78.  
  79.   # shaping queue is EMPTY
  80.   if ts_active is 0:
  81.      if self.token_bucket_filter( pkt_size ) is True:
  82.        # no further action required, send the packet straight to wire.
  83.        try:
  84.          XMIT_OUT_IF( received_packet )
  85.        except InterfaceBusyError:
  86.          # interface is locked -- this shouldn't happen, are you setting the CIR higher than the output interface's speed?
  87.          raise TxRing_Exhausted_Exception, TailDrop_Exception
  88.          QMBUF_FREE( received_packet )
  89.          return False
  90.      else:
  91.        # add the received_packet onto shaping_queue
  92.        ENQUEUE_ADD( shaping_queue, received_packet )
  93.  
  94.   # processing completed successfully
  95.   return True

Raw Paste


Login or Register to edit or fork this paste. It's free.