net.i2p.util

## Class SyntheticREDQueue

- java.lang.Object
- - net.i2p.util.SyntheticREDQueue

- All Implemented Interfaces:
    BandwidthEstimator

      public class SyntheticREDQueue
      extends Object
      implements BandwidthEstimator

  A "synthetic" queue that does not store data but estimates the average queue size assuming a fixed output bandwidth, and applies the Random Early Detection (RED) algorithm to decide probabilistically whether to accept or drop packets. The purpose is to model queue behavior and preemptively drop packets to prevent congestion before an actual queue is affected downstream. The average queue size is measured in bytes and is updated continuously, assuming the queue drains at a constant rate defined at construction. Packet acceptance is based on the RED algorithm, which uses the current average queue size and the offered packet size to calculate a drop probability. The drop probability is adjusted by a factor parameter and limited by configurable minimum and maximum queue size thresholds. This class also incorporates a Westwood+ bandwidth estimator that updates bandwidth estimates based on acknowledged packet sizes and timing, using an exponential weighted moving average (EWMA). Both bandwidth and queue size estimates are only updated when packets are accepted. The implementation is adapted from the Linux kernel tcp_westwood.c logic and follows established research including: - Random Early Detection Gateways for Congestion Avoidance (Floyd & Jacobson) - TCP Westwood: End-to-End Congestion Control for Wired/Wireless Networks (Casetti et al.) - End-to-End Bandwidth Estimation for Congestion Control (Grieco & Mascolo) Note: minThB (minimum threshold) must be less than maxThB (maximum threshold) for correct RED operation.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SyntheticREDQueue(I2PAppContext ctx, int bwBps)` Construct with default queue size thresholds based on bandwidth.
    `SyntheticREDQueue(I2PAppContext ctx, int bwBps, int minThB, int maxThB)` Construct specifying bandwidth and queue size thresholds.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addSample(int size)` Unconditionally accepts the given packet size.
    `float` | `getBandwidthEstimate()` Returns the current bandwidth estimate in bytes/ms.
    `int` | `getMaxBandwidth()`
    `float` | `getQueueSizeEstimate()` Returns the current estimated average queue size in bytes.
    `boolean` | `offer(int size, float factor)` Offer a packet to the queue.
    `String` | `toString()` Returns a human-readable string displaying the current bandwidth and queue size estimates, formatted for debugging or informational output.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SyntheticREDQueue

          public SyntheticREDQueue(I2PAppContext ctx,
                                   int bwBps)

      Construct with default queue size thresholds based on bandwidth.
      Parameters:
        `ctx` - the I2P application context
        `bwBps` - nominal output bandwidth in bytes per second

    - #### SyntheticREDQueue

          public SyntheticREDQueue(I2PAppContext ctx,
                                   int bwBps,
                                   int minThB,
                                   int maxThB)

      Construct specifying bandwidth and queue size thresholds. Queue size thresholds: - minThB: queue size in bytes to start probabilistically dropping packets - maxThB: queue size in bytes at which all packets are dropped (100% drop probability) NOTE: minThB must be less than maxThB for proper operation.
      Parameters:
        `ctx` - the I2P application context
        `bwBps` - nominal output bandwidth in bytes per second
        `minThB` - minimum threshold queue size to start dropping (Bytes)
        `maxThB` - maximum threshold queue size to drop all packets (Bytes)

  - ### Method Detail

    - #### addSample

          public void addSample(int size)

      Unconditionally accepts the given packet size. Updates queue size and bandwidth estimates accordingly.
      Specified by:
        `addSample` in interface `BandwidthEstimator`
      Parameters:
        `size` - size of packet in bytes

    - #### getBandwidthEstimate

          public float getBandwidthEstimate()

      Returns the current bandwidth estimate in bytes/ms. Updates estimate if enough time elapsed since last update.
      Specified by:
        `getBandwidthEstimate` in interface `BandwidthEstimator`
      Returns:
        bandwidth estimate in bytes/ms

    - #### getMaxBandwidth

          public int getMaxBandwidth()

      Returns:
        the configured maximum bandwidth in bytes per second.

    - #### getQueueSizeEstimate

          public float getQueueSizeEstimate()

      Returns the current estimated average queue size in bytes. Updates estimate if enough time elapsed since last update.
      Returns:
        queue size estimate in bytes

    - #### offer

          public boolean offer(int size,
                               float factor)

      Offer a packet to the queue. The decision to accept or drop the packet is made based on RED algorithm using the current average queue size and the offered packet size with a drop probability. If packet is accepted, the queue size and bandwidth estimators are updated.
      Parameters:
        `size` - size in bytes of the packet
        `factor` - drop probability adjustment factor (1.0 for standard, 0 disables dropping)
      Returns:
        true if accepted, false if dropped

    - #### toString

          public String toString()

      Returns a human-readable string displaying the current bandwidth and queue size estimates, formatted for debugging or informational output.
      Overrides:
        `toString` in class `Object`
      Returns:
        formatted status string including bandwidth and average queue size