Skip to contentSkip to conversation

igb: Implement Transmit Rate Limiting (TRL)

PATCH Superseded Josh Hilke 2 participants 11 messages Jun 17 – Jun 18

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Josh Hilke

author
#0 lore raw

v6 cover igb: Implement Transmit Rate Limiting (TRL)

This is v6 of a patch series implementing the Transmit Rate Limiter (TRL) feature for the Intel 82576 (igb) network device emulation in QEMU.

v4: https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg03717.html

Changelog:

v6:
- Use extract32() to get scaled rate factor from register.
- Use extract32() in igb_set_trldqsel() to extract queue index.
- Use uint32_t for qidx.
- Change igb_trl_enabled() signature to take const IGBTrlQueue* instead of register value.
- Merge target rate and delay calculation into igb_trl_calculate_delay() using muldiv64() and clamping.
- Change igb_trl_needed() to check for any non-zero trlrc value instead of just enabled state.
- Add trailing comma to TRLRC in igb_macreg_writeops.
- Remove flushing logic when TRL is disabled for a queue.
- Modify igb_trl_disable() to only clear the RS_ENA bit instead of setting TRLRC to 0.
- Update igb_reset() to explicitly clear TRLRC registers.
- Extract TRL queue initialization to a new helper function igb_trl_init().
- Introduce igb_trl_uninit() helper function to match igb_trl_init().
- Introduce igb_trl_reset() helper function to set trlrc to 0 and delete timers.
- Modify igb_set_trlrc() to only disable the queue if the guest explicitly cleared RS_ENA.

---
Josh Hilke (7):
igb: Define TRL registers and core state
igb: Add VMState support for TRL state
igb: Implement TRL register write and configuration
igb: Refactor TX path to return processed byte count
igb: Implement TRL throttling and timer resumption
igb: Add QTest for Transmit Rate Limiter register multiplexing
igb: Add QTest for Transmit Rate Limiter traffic throttling

 hw/net/igb.c           |  43 ++++++++++
 hw/net/igb_common.h    |   1 +
 hw/net/igb_core.c      | 144 ++++++++++++++++++++++++++++++++--
 hw/net/igb_core.h      |  12 +++
 hw/net/igb_regs.h      |   6 ++
 tests/qtest/igb-test.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 410 insertions(+), 5 deletions(-)

---
base-commit: 98b060da3a4f92b2a994ead5b16a87e783baf77c
change-id: 20260608-igb-trl-19be198da6f2

Best regards,

Signature · 1 line
Josh Hilke <[email protected]>
author
#1 lore raw

PATCH v6 1/7 igb: Define TRL registers and core state

Introduce register offsets and bitmask constants for the Transmit Rate Limiter (TRL). Add the IGBTrlQueue structure and corresponding fields to IGBCore to track TRL state (timers, throttling status, and target rates) per queue. Initialize and free these timers during the device realize/uninit lifecycle, and reset them on device reset.

---
 hw/net/igb_common.h |  1 +
 hw/net/igb_core.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 hw/net/igb_core.h   | 12 ++++++++++++
 hw/net/igb_regs.h   |  6 ++++++
 4 files changed, 60 insertions(+)
hw/net/igb_common.h +1 -0
index b316a5b..8c0a0cf 100644
@@ -148,6 +148,7 @@ enum {
     defreg(MTA_A),
 
     defreg(VTIVAR), defreg(VTIVAR_MISC),
+    defreg(TRLDQSEL), defreg(TRLRC),
 };
 
 uint64_t igb_mmio_read(void *opaque, hwaddr addr, unsigned size);
hw/net/igb_core.c +41 -0

71 lines not on this page · raw message

hw/net/igb_core.h +12 -0
index d70b54e..922d8cd 100644
@@ -63,6 +63,13 @@ typedef struct IGBIntrDelayTimer_st {
     IGBCore *core;
 } IGBIntrDelayTimer;
 
+typedef struct IGBTrlQueue_st {
+    QEMUTimer *timer;
+    IGBCore *core;
+    uint32_t qidx;
+    uint32_t trlrc;
+} IGBTrlQueue;
+
 struct IGBCore {
     uint32_t mac[E1000E_MAC_SIZE];
     uint16_t phy[MAX_PHY_REG_ADDRESS + 1];
@@ -99,6 +106,9 @@ struct IGBCore {
     void (*owner_start_recv)(PCIDevice *d);
 
     int64_t timadj;
+
+    /* Transmit Rate Limiting */
+    IGBTrlQueue trl[IGB_NUM_QUEUES];
 };
 
 void
@@ -143,4 +153,6 @@ igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt);
 void
 igb_start_recv(IGBCore *core);
 
+bool igb_trl_enabled(const IGBTrlQueue *q);
+
 #endif
hw/net/igb_regs.h +6 -0
index 4dc4c31..9ef628b 100644
@@ -718,4 +718,10 @@ static inline uint8_t igb_ivar_entry_tx(uint8_t i)
     return i < 8 ? i * 4 + 1 : (i - 8) * 4 + 3;
 }
 
+/* Transmit Rate Limiting */
+#define E1000_TRLDQSEL 0x03604
+#define E1000_TRLRC 0x036B0
+#define E1000_TRLRC_RS_ENA BIT(31)
+#define E1000_LINK_RATE_1GBPS (1000000000ULL / BITS_PER_BYTE)
+
 #endif
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
author
#2 lore raw

PATCH v6 2/7 igb: Add VMState support for TRL state

In reply to Josh Hilke

Add VMState representation for the Transmit Rate Limiter (TRL) state to support migration. This includes the TRL timers and target rates per queue.

---
 hw/net/igb.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
hw/net/igb.c +43 -0
index c076807..773da26 100644
@@ -563,6 +563,45 @@ static const VMStateDescription igb_vmstate_intr_timer = {
     VMSTATE_STRUCT_ARRAY(_f, _s, _num, 0,                           \
                          igb_vmstate_intr_timer, IGBIntrDelayTimer)
 
+static bool igb_trl_needed(void *opaque)
+{
+    IGBState *s = opaque;
+
+    for (int i = 0; i < IGB_NUM_QUEUES; i++) {
+        if (s->core.trl[i].trlrc) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+static const VMStateDescription igb_vmstate_trl_queue = {
+    .name = "igb-trl-queue",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_TIMER_PTR(timer, IGBTrlQueue),
+        VMSTATE_UINT32(trlrc, IGBTrlQueue),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+#define VMSTATE_IGB_TRL_QUEUE_ARRAY(_f, _s, _num)                    \
+    VMSTATE_STRUCT_ARRAY(_f, _s, _num, 0,                            \
+                         igb_vmstate_trl_queue, IGBTrlQueue)
+
+static const VMStateDescription igb_vmstate_trl_state = {
+    .name = "igb/trl_state",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = igb_trl_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_IGB_TRL_QUEUE_ARRAY(core.trl, IGBState, IGB_NUM_QUEUES),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static const VMStateDescription igb_vmstate = {
     .name = "igb",
     .version_id = 1,
@@ -591,6 +630,10 @@ static const VMStateDescription igb_vmstate = {
         VMSTATE_INT64(core.timadj, IGBState),
 
         VMSTATE_END_OF_LIST()
+    },
+    .subsections = (const VMStateDescription * const []) {
+        &igb_vmstate_trl_state,
+        NULL
     }
 };
 
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
author
#3 lore raw

PATCH v6 3/7 igb: Implement TRL register write and configuration

In reply to Josh Hilke

Implement write handlers for TLDQSEL and TRLRC registers to allow the guest to configure Transmit Rate Limiting. Translate the register Rate Factor into a physical target rate (bytes/sec) per queue. Disable TRL and clear timers when the link status changes or when TRL is disabled by the guest.

---
 hw/net/igb_core.c | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
hw/net/igb_core.c +36 -1
index 44e8e58..65403f9 100644
@@ -2145,11 +2145,20 @@ static void igb_trl_reset(IGBCore *core)
     }
 }
 
+static void igb_trl_disable(IGBTrlQueue *q)
+{
+    q->trlrc &= ~E1000_TRLRC_RS_ENA;
+}
+
 void igb_core_set_link_status(IGBCore *core)
 {
     NetClientState *nc = qemu_get_queue(core->owner_nic);
     uint32_t old_status = core->mac[STATUS];
 
+    for (int i = 0; i < IGB_NUM_QUEUES; i++) {
+        igb_trl_disable(&core->trl[i]);
+    }
+
     trace_e1000e_link_status_changed(nc->link_down ? false : true);
 
     if (nc->link_down) {
@@ -2632,6 +2641,29 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)
      */
 }
 
+static uint32_t igb_get_trlrc(IGBCore *core, int index)
+{
+    const uint32_t qidx = core->mac[TRLDQSEL];
+
+    return core->trl[qidx].trlrc;
+}
+
+static void igb_set_trldqsel(IGBCore *core, int index, uint32_t val)
+{
+    core->mac[index] = extract32(val, 0, 4);
+}
+
+static void igb_set_trlrc(IGBCore *core, int index, uint32_t val)
+{
+    const uint32_t qidx = core->mac[TRLDQSEL];
+
+    core->trl[qidx].trlrc = val;
+
+    if (!igb_trl_enabled(&core->trl[qidx])) {
+        igb_trl_disable(&core->trl[qidx]);
+    }
+}
+
 static inline void
 igb_autoneg_timer(void *opaque)
 {
@@ -3687,6 +3719,7 @@ static const readops igb_macreg_readops[] = {
     [RQDPC15] = igb_mac_read_clr4,
     [VTIVAR ... VTIVAR + 7] = igb_mac_readreg,
     [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_readreg,
+    [TRLRC] = igb_get_trlrc,
 };
 enum { IGB_NREADOPS = ARRAY_SIZE(igb_macreg_readops) };
 
@@ -4135,7 +4168,9 @@ static const writeops igb_macreg_writeops[] = {
     [PVTEICR6] = igb_set_vteicr,
     [PVTEICR7] = igb_set_vteicr,
     [VTIVAR ... VTIVAR + 7] = igb_set_vtivar,
-    [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg
+    [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg,
+    [TRLDQSEL] = igb_set_trldqsel,
+    [TRLRC]    = igb_set_trlrc,
 };
 enum { IGB_NWRITEOPS = ARRAY_SIZE(igb_macreg_writeops) };
 
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
#4 lore raw

Re: [PATCH v6 3/7] igb: Implement TRL register write and configuration

Quoted 51 lines · Josh Hilke

Implement write handlers for TLDQSEL and TRLRC registers to allow the guest to configure Transmit Rate Limiting. Translate the register Rate Factor into a physical target rate (bytes/sec) per queue. Disable TRL and clear timers when the link status changes or when TRL is disabled by the guest.

---
  hw/net/igb_core.c | 37 ++++++++++++++++++++++++++++++++++++-
  1 file changed, 36 insertions(+), 1 deletion(-)
hw/net/igb_core.c +20 -0
index 44e8e58..65403f9 100644
@@ -2145,11 +2145,20 @@ static void igb_trl_reset(IGBCore *core)
      }
  }
 
+static void igb_trl_disable(IGBTrlQueue *q)
+{
+    q->trlrc &= ~E1000_TRLRC_RS_ENA;
+}
+
  void igb_core_set_link_status(IGBCore *core)
  {
      NetClientState *nc = qemu_get_queue(core->owner_nic);
      uint32_t old_status = core->mac[STATUS];
 
+    for (int i = 0; i < IGB_NUM_QUEUES; i++) {
+        igb_trl_disable(&core->trl[i]);
+    }
+
      trace_e1000e_link_status_changed(nc->link_down ? false : true);
 
      if (nc->link_down) {
@@ -2632,6 +2641,29 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)
       */
  }
 
+static uint32_t igb_get_trlrc(IGBCore *core, int index)
+{
+    const uint32_t qidx = core->mac[TRLDQSEL];
+
+    return core->trl[qidx].trlrc;
+}
+
+static void igb_set_trldqsel(IGBCore *core, int index, uint32_t val)
+{
+    core->mac[index] = extract32(val, 0, 4);
+}

hw/net/igb_core.c static void igb_set_vtivar(IGBCore *core, int index, uint32_t val) · line 2664

Quoted 10 lines
hw/net/igb_core.c +10 -0
+
+static void igb_set_trlrc(IGBCore *core, int index, uint32_t val)
+{
+    const uint32_t qidx = core->mac[TRLDQSEL];
+
+    core->trl[qidx].trlrc = val;
+
+    if (!igb_trl_enabled(&core->trl[qidx])) {
+        igb_trl_disable(&core->trl[qidx]);
+    }

This igb_trl_disable() is now effectively no-op and redundant.

Regards,
Akihiko Odaki

Quoted 25 lines
hw/net/igb_core.c +6 -1
+}
+
  static inline void
  igb_autoneg_timer(void *opaque)
  {
@@ -3687,6 +3719,7 @@ static const readops igb_macreg_readops[] = {
      [RQDPC15] = igb_mac_read_clr4,
      [VTIVAR ... VTIVAR + 7] = igb_mac_readreg,
      [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_readreg,
+    [TRLRC] = igb_get_trlrc,
  };
  enum { IGB_NREADOPS = ARRAY_SIZE(igb_macreg_readops) };
 
@@ -4135,7 +4168,9 @@ static const writeops igb_macreg_writeops[] = {
      [PVTEICR6] = igb_set_vteicr,
      [PVTEICR7] = igb_set_vteicr,
      [VTIVAR ... VTIVAR + 7] = igb_set_vtivar,
-    [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg
+    [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg,
+    [TRLDQSEL] = igb_set_trldqsel,
+    [TRLRC]    = igb_set_trlrc,
  };
  enum { IGB_NWRITEOPS = ARRAY_SIZE(igb_macreg_writeops) };
 
author
#5 lore raw

PATCH v6 4/7 igb: Refactor TX path to return processed byte count

In reply to Josh Hilke

Modify igb_process_tx_desc to return the total size of the transmitted packet (in bytes) when processing the End of Packet (EOP) descriptor, and 0 otherwise. This allows the transmission loop to track how many bytes are being sent for rate-limiting calculations.

---
 hw/net/igb_core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/net/igb_core.c +7 -3
index 65403f9..693353e 100644
@@ -599,7 +599,7 @@ igb_on_tx_done_update_stats(IGBCore *core, struct NetTxPkt *tx_pkt, int qn)
     }
 }
 
-static void
+static size_t
 igb_process_tx_desc(IGBCore *core,
                     PCIDevice *dev,
                     struct igb_tx *tx,
@@ -632,10 +632,10 @@ igb_process_tx_desc(IGBCore *core,
             tx->ctx[idx].seqnum_seed = le32_to_cpu(tx_ctx_desc->seqnum_seed);
             tx->ctx[idx].type_tucmd_mlhl = le32_to_cpu(tx_ctx_desc->type_tucmd_mlhl);
             tx->ctx[idx].mss_l4len_idx = le32_to_cpu(tx_ctx_desc->mss_l4len_idx);
-            return;
+            return 0;
         } else {
             /* unknown descriptor type */
-            return;
+            return 0;
         }
     } else {
         /* legacy descriptor */
@@ -654,11 +654,13 @@ igb_process_tx_desc(IGBCore *core,
     }
 
     if (cmd_type_len & E1000_TXD_CMD_EOP) {
+        size_t total_len = 0;
         if (!tx->skip_cp && net_tx_pkt_parse(tx->tx_pkt)) {
             idx = (tx->first_olinfo_status >> 4) & 1;
             igb_tx_insert_vlan(core, queue_index, tx,
                 tx->ctx[idx].vlan_macip_lens >> IGB_TX_FLAGS_VLAN_SHIFT,
                 !!(tx->first_cmd_type_len & E1000_TXD_CMD_VLE));
+            total_len = net_tx_pkt_get_total_len(tx->tx_pkt);
 
             if ((tx->first_cmd_type_len & E1000_ADVTXD_MAC_TSTAMP) &&
                 (core->mac[TSYNCTXCTL] & E1000_TSYNCTXCTL_ENABLED) &&
@@ -675,7 +677,9 @@ igb_process_tx_desc(IGBCore *core,
         tx->first = true;
         tx->skip_cp = false;
         net_tx_pkt_reset(tx->tx_pkt, net_tx_pkt_unmap_frag_pci, dev);
+        return total_len;
     }
+    return 0;
 }
 
 static uint32_t igb_tx_wb_eic(IGBCore *core, int queue_idx)
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
author
#6 lore raw

PATCH v6 5/7 igb: Implement TRL throttling and timer resumption

In reply to Josh Hilke

Integrate throttling logic into the transmission path. In igb_start_xmit, check if the queue is currently throttled. Accumulate bytes sent, calculate the transmission delay based on the configured target rate, and if a throttle is triggered, set the virtual timer, mark the queue as throttled, and defer remaining packets. Implement the timer callback to clear the throttled state and resume transmission when the timer expires.

---
 hw/net/igb_core.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 57 insertions(+), 3 deletions(-)
hw/net/igb_core.c +57 -3
index 693353e..7d61d7d 100644
@@ -97,6 +97,7 @@ igb_receive_internal(IGBCore *core, const struct iovec *iov, int iovcnt,
 static void igb_raise_interrupts(IGBCore *core, size_t index, uint32_t causes);
 static void igb_reset(IGBCore *core, bool sw);
 
+
 static inline void
 igb_raise_legacy_irq(IGBCore *core)
 {
@@ -875,6 +876,42 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi)
         (core->mac[TXDCTL0 + (qn * 16)] & E1000_TXDCTL_QUEUE_ENABLE);
 }
 
+static uint64_t igb_trl_calculate_delay(const IGBTrlQueue *q,
+                                        size_t bytes_sent)
+{
+    uint32_t target_rate;
+    uint32_t rf_scaled;
+
+    if (!igb_trl_enabled(q)) {
+        return 0;
+    }
+
+    /*
+     * The TRLRC register stores the Rate Factor in 10.14 fixed-point format:
+     * - Bits 13:0: Fractional part
+     * - Bits 23:14: Integer part
+     *
+     * Combined, the lower 24 bits of the register (23:0) represent the
+     * concatenated 24-bit scaled Rate Factor directly.
+     */
+    rf_scaled = extract32(q->trlrc, 0, 24);
+    if (rf_scaled == 0) {
+        return 0;
+    }
+
+    /*
+     * Rate Factor = rf_scaled / 2^14
+     * Target Rate = Link Rate / Rate Factor
+     *             = (Link Rate * 2^14) / rf_scaled
+     *
+     * Clamp target rate to link speed.
+     */
+    target_rate = MIN(E1000_LINK_RATE_1GBPS,
+                      (E1000_LINK_RATE_1GBPS << 14) / rf_scaled);
+
+    return muldiv64(bytes_sent, NANOSECONDS_PER_SECOND, target_rate);
+}
+
 static void
 igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
 {
@@ -883,12 +920,19 @@ igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
     union e1000_adv_tx_desc desc;
     const E1000ERingInfo *txi = txr->i;
     uint32_t eic = 0;
+    size_t bytes_sent = 0;
+    uint64_t delay_ns;
 
     if (!igb_tx_enabled(core, txi)) {
         trace_e1000e_tx_disabled();
         return;
     }
 
+    if (igb_trl_enabled(&core->trl[txi->idx]) &&
+        timer_pending(core->trl[txi->idx].timer)) {
+        return;
+    }
+
     d = pcie_sriov_get_vf_at_index(core->owner, txi->idx % 8);
     if (!d) {
         d = core->owner;
@@ -902,9 +946,16 @@ igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
         trace_e1000e_tx_descr((void *)(intptr_t)desc.read.buffer_addr,
                               desc.read.cmd_type_len, desc.wb.status);
 
-        igb_process_tx_desc(core, d, txr->tx, &desc, txi->idx);
+        bytes_sent += igb_process_tx_desc(core, d, txr->tx, &desc, txi->idx);
         igb_ring_advance(core, txi, 1);
         eic |= igb_txdesc_writeback(core, base, &desc, txi);
+
+        delay_ns = igb_trl_calculate_delay(&core->trl[txi->idx], bytes_sent);
+        if (delay_ns > 0) {
+            timer_mod(core->trl[txi->idx].timer,
+                      qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
+            break;
+        }
     }
 
     if (eic) {
@@ -4329,7 +4380,6 @@ igb_autoneg_resume(IGBCore *core)
                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
     }
 }
-
 bool igb_trl_enabled(const IGBTrlQueue *q)
 {
     return q->trlrc & E1000_TRLRC_RS_ENA;
@@ -4337,7 +4387,11 @@ bool igb_trl_enabled(const IGBTrlQueue *q)
 
 static void igb_trl_timer_cb(void *opaque)
 {
-    /* Stub */
+    IGBTrlQueue *trl = opaque;
+    IGB_TxRing txr;
+
+    igb_tx_ring_init(trl->core, &txr, trl->qidx);
+    igb_start_xmit(trl->core, &txr);
 }
 
 static void igb_trl_init(IGBCore *core)
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
#7 lore raw

Re: [PATCH v6 5/7] igb: Implement TRL throttling and timer resumption

Quoted 26 lines · Josh Hilke

Integrate throttling logic into the transmission path. In igb_start_xmit, check if the queue is currently throttled. Accumulate bytes sent, calculate the transmission delay based on the configured target rate, and if a throttle is triggered, set the virtual timer, mark the queue as throttled, and defer remaining packets. Implement the timer callback to clear the throttled state and resume transmission when the timer expires.

---
  hw/net/igb_core.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 57 insertions(+), 3 deletions(-)
hw/net/igb_core.c +1 -0
index 693353e..7d61d7d 100644
@@ -97,6 +97,7 @@ igb_receive_internal(IGBCore *core, const struct iovec *iov, int iovcnt,
  static void igb_raise_interrupts(IGBCore *core, size_t index, uint32_t causes);
  static void igb_reset(IGBCore *core, bool sw);
 
+
  static inline void
  igb_raise_legacy_irq(IGBCore *core)
  {

hw/net/igb_core.c igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) · line 880

Quoted 5 lines
hw/net/igb_core.c +2 -0
@@ -875,6 +876,42 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi)
          (core->mac[TXDCTL0 + (qn * 16)] & E1000_TXDCTL_QUEUE_ENABLE);
  }
 
+static uint64_t igb_trl_calculate_delay(const IGBTrlQueue *q,
+                                        size_t bytes_sent)

The signature fits in one line.

Quoted 22 lines
hw/net/igb_core.c +22 -0
+{
+    uint32_t target_rate;
+    uint32_t rf_scaled;
+
+    if (!igb_trl_enabled(q)) {
+        return 0;
+    }
+
+    /*
+     * The TRLRC register stores the Rate Factor in 10.14 fixed-point format:
+     * - Bits 13:0: Fractional part
+     * - Bits 23:14: Integer part
+     *
+     * Combined, the lower 24 bits of the register (23:0) represent the
+     * concatenated 24-bit scaled Rate Factor directly.
+     */
+    rf_scaled = extract32(q->trlrc, 0, 24);
+    if (rf_scaled == 0) {
+        return 0;
+    }
+
+    /*

hw/net/igb_core.c igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) · line 912

Quoted 10 lines
hw/net/igb_core.c +10 -0
+     * Rate Factor = rf_scaled / 2^14
+     * Target Rate = Link Rate / Rate Factor
+     *             = (Link Rate * 2^14) / rf_scaled
+     *
+     * Clamp target rate to link speed.
+     */
+    target_rate = MIN(E1000_LINK_RATE_1GBPS,
+                      (E1000_LINK_RATE_1GBPS << 14) / rf_scaled);
+
+    return muldiv64(bytes_sent, NANOSECONDS_PER_SECOND, target_rate);

By merging igb_trl_get_target_rate() and igb_trl_calculate_delay(), I meant not just to put the literal formulae into one place, but to fold them so that we don't have to worry about overflows and fractions until we really need them. Concretely:

uint32_t divisor;
uint32_t rf;

if (!igb_trl_enabled(q)) {
return 0;
}

/*
* 7.8 Transmit Rate Limiting (TRL)
*
* > RF = 1Gb/s / Target-Rate (Rate Factor) is the ratio between the
* > nominal link rate and the target maximum rate to achieve for
* > that rate controlled queue. It is a decimal number ranging from
* > 1 to 1,000 (1 Mb/s minimum target rate) at least 10-bits before
* > the hexadecimal point and 14-bits after, as required for the
* > maximum PL by which it is multiplied.
*
* The TRLRC register stores the RF in 10.14 fixed-point format:
* - Bits 13:0: Fractional part
* - Bits 23:14: Integer part
*
* Combined, the lower 24 bits of the register (23:0) represent the
* RF directly.
*/
rf = MAX(extract32(q->trlrc, 0, 24), 1 << 14);

/*
* Delay = PL (Packet Length) / Target-Rate
* = PL / (1Gb/s / RF)
* = PL * RF / 1Gb/s
* = PL * RF / (1G / b/B / ns/s)B/ns
* = (bytes_per_sent * (RF / 2^14) / (1G / b/B / ns/s))ns
* = (bytes_per_sent * RF / (1G / b/B * 2^14 / ns/s))ns
*/
divisor = ((1000000000ULL / BITS_PER_BYTE) << 14) /
NANOSECONDS_PER_SECOND;

return bytes_sent * rf / divisor;

I also folded E1000_LINK_RATE_1GBPS here since its unit is unclear and we can centralize all unit conversions here by doing so.

Regards,
Akihiko Odaki

Quoted 65 lines
hw/net/igb_core.c +22 -3
+}
+
  static void
  igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
  {
@@ -883,12 +920,19 @@ igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
      union e1000_adv_tx_desc desc;
      const E1000ERingInfo *txi = txr->i;
      uint32_t eic = 0;
+    size_t bytes_sent = 0;
+    uint64_t delay_ns;
 
      if (!igb_tx_enabled(core, txi)) {
          trace_e1000e_tx_disabled();
          return;
      }
 
+    if (igb_trl_enabled(&core->trl[txi->idx]) &&
+        timer_pending(core->trl[txi->idx].timer)) {
+        return;
+    }
+
      d = pcie_sriov_get_vf_at_index(core->owner, txi->idx % 8);
      if (!d) {
          d = core->owner;
@@ -902,9 +946,16 @@ igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
          trace_e1000e_tx_descr((void *)(intptr_t)desc.read.buffer_addr,
                                desc.read.cmd_type_len, desc.wb.status);
 
-        igb_process_tx_desc(core, d, txr->tx, &desc, txi->idx);
+        bytes_sent += igb_process_tx_desc(core, d, txr->tx, &desc, txi->idx);
          igb_ring_advance(core, txi, 1);
          eic |= igb_txdesc_writeback(core, base, &desc, txi);
+
+        delay_ns = igb_trl_calculate_delay(&core->trl[txi->idx], bytes_sent);
+        if (delay_ns > 0) {
+            timer_mod(core->trl[txi->idx].timer,
+                      qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
+            break;
+        }
      }
 
      if (eic) {
@@ -4329,7 +4380,6 @@ igb_autoneg_resume(IGBCore *core)
                    qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
      }
  }
-
  bool igb_trl_enabled(const IGBTrlQueue *q)
  {
      return q->trlrc & E1000_TRLRC_RS_ENA;
@@ -4337,7 +4387,11 @@ bool igb_trl_enabled(const IGBTrlQueue *q)
 
  static void igb_trl_timer_cb(void *opaque)
  {
-    /* Stub */
+    IGBTrlQueue *trl = opaque;
+    IGB_TxRing txr;
+
+    igb_tx_ring_init(trl->core, &txr, trl->qidx);
+    igb_start_xmit(trl->core, &txr);
  }
 
  static void igb_trl_init(IGBCore *core)
author
#8 lore raw

PATCH v6 6/7 igb: Add QTest for Transmit Rate Limiter register multiplexing

In reply to Josh Hilke

Add a unit test to verify register writes/reads to the rate limit register (TRLC) return the correct value based on which queue is currently selected in the queue selection register (TRLDQSEL).

---
 tests/qtest/igb-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tests/qtest/igb-test.c +51 -0
index 3d397ea..119be6a 100644
@@ -45,6 +45,20 @@ static const struct eth_header packet = {
     .h_source = E1000E_ADDRESS,
 };
 
+/*
+ * Calculate TRL rate factor for a given target rate.
+ *
+ * Rate Factor = 1 Gbps / Target Rate.
+ *
+ * The hardware TRLRC register stores the rate factor as a fixed-point number
+ * with 14 fractional bits. Scale 1Gbps by 2^14 before dividing to preserve
+ * precision in integer arithmetic.
+ */
+static uint32_t igb_trl_get_rate_factor(uint64_t rate_bytes_s)
+{
+    return (E1000_LINK_RATE_1GBPS << 14) / rate_bytes_s;
+}
+
 static void igb_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
 {
     union e1000_adv_tx_desc descr;
@@ -191,6 +205,41 @@ static void test_igb_multiple_transfers(void *obj, void *data,
 
 }
 
+/*
+ * Verify the rate limit value register (TRLC) returns the correct value based
+ * on which queue is selected (TRLDQSEL).
+ */
+static void test_igb_transmit_rate_limiter_regs(void *obj, void *data,
+                                                QGuestAllocator *alloc)
+{
+    QOSGraphObject *e_object = obj;
+    QE1000E_PCI *e1000e = obj;
+    QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
+    QE1000E *d = &e1000e->e1000e;
+    const int qidx0 = 0, qidx1 = 1;
+    uint32_t val0, val1;
+
+    if (qpci_check_buggy_msi(dev)) {
+        return;
+    }
+
+    e1000e_macreg_write(d, E1000_TRLDQSEL, qidx0);
+    val0 = E1000_TRLRC_RS_ENA |
+           igb_trl_get_rate_factor(E1000_LINK_RATE_1GBPS / 1000);
+    e1000e_macreg_write(d, E1000_TRLRC, val0);
+
+    e1000e_macreg_write(d, E1000_TRLDQSEL, qidx1);
+    val1 = E1000_TRLRC_RS_ENA |
+           igb_trl_get_rate_factor(E1000_LINK_RATE_1GBPS / 500);
+    e1000e_macreg_write(d, E1000_TRLRC, val1);
+
+    e1000e_macreg_write(d, E1000_TRLDQSEL, qidx0);
+    g_assert_cmphex(e1000e_macreg_read(d, E1000_TRLRC), ==, val0);
+
+    e1000e_macreg_write(d, E1000_TRLDQSEL, qidx1);
+    g_assert_cmphex(e1000e_macreg_read(d, E1000_TRLRC), ==, val1);
+}
+
 static void data_test_clear(void *sockets)
 {
     int *test_sockets = sockets;
@@ -247,6 +296,8 @@ static void register_igb_test(void)
     qos_add_test("rx", "igb", test_igb_rx, &opts);
     qos_add_test("multiple_transfers", "igb",
                  test_igb_multiple_transfers, &opts);
+    qos_add_test("transmit_rate_limiter_regs", "igb",
+                 test_igb_transmit_rate_limiter_regs, &opts);
 #endif
 
     opts.before = data_test_init_no_socket;
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
author
#9 lore raw

PATCH v6 7/7 igb: Add QTest for Transmit Rate Limiter traffic throttling

In reply to Josh Hilke

Add a test for the IGB transmit rate limiting functionality. The test verifies that TRL is enabled by setting the appropriate registers and that data is transmitted at the appropriate rate based on chosen target rate.

---
 tests/qtest/igb-test.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 158 insertions(+)
tests/qtest/igb-test.c +158 -0
index 119be6a..153f5fb 100644
@@ -34,9 +34,11 @@
 #include "qemu/iov.h"
 #include "qemu/module.h"
 #include "qemu/bitops.h"
+#include "qemu/timer.h"
 #include "libqos/libqos-malloc.h"
 #include "libqos/e1000e.h"
 #include "hw/net/igb_regs.h"
+#include "hw/pci/pci_regs.h"
 
 #ifndef _WIN32
 
@@ -59,6 +61,58 @@ static uint32_t igb_trl_get_rate_factor(uint64_t rate_bytes_s)
     return (E1000_LINK_RATE_1GBPS << 14) / rate_bytes_s;
 }
 
+static void igb_trl_enable(QE1000E *d, uint64_t rate_bytes_s)
+{
+    /* Select Queue 0 */
+    e1000e_macreg_write(d, E1000_TRLDQSEL, 0);
+    /* Enable TRL with target rate */
+    e1000e_macreg_write(d, E1000_TRLRC,
+                        E1000_TRLRC_RS_ENA |
+                        igb_trl_get_rate_factor(rate_bytes_s));
+}
+
+static void igb_trl_disable(QE1000E *d)
+{
+    /* Select Queue 0 */
+    e1000e_macreg_write(d, E1000_TRLDQSEL, 0);
+    /* Disable TRL */
+    e1000e_macreg_write(d, E1000_TRLRC, 0);
+}
+
+static void igb_msix_clear_pending(QPCIDevice *dev, uint16_t entry)
+{
+    uint64_t vector_offset =
+        dev->msix_table_off + (entry * PCI_MSIX_ENTRY_SIZE);
+    uint32_t val = qpci_io_readl(dev, dev->msix_table_bar,
+                                 vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL);
+
+    /* Unmask (clears pending bit in QEMU) */
+    qpci_io_writel(dev, dev->msix_table_bar,
+                    vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL,
+                    val & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
+
+    /* Mask again */
+    qpci_io_writel(dev, dev->msix_table_bar,
+                    vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL,
+                    val | PCI_MSIX_ENTRY_CTRL_MASKBIT);
+}
+
+static void igb_read_tx_tail(QE1000E *d, void *descr)
+{
+    QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e);
+    uint32_t last_entry;
+    uint32_t tail;
+    uint32_t len;
+
+    tail = e1000e_macreg_read(d, E1000_TDT_A(0));
+    len = e1000e_macreg_read(d, E1000_TDLEN_A(0)) / E1000_RING_DESC_LEN;
+    last_entry = (tail + len - 1) % len;
+
+    qtest_memread(d_pci->pci_dev.bus->qts,
+                  d->tx_ring + last_entry * E1000_RING_DESC_LEN, descr,
+                  E1000_RING_DESC_LEN);
+}
+
 static void igb_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
 {
     union e1000_adv_tx_desc descr;
@@ -84,6 +138,14 @@ static void igb_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *allo
     /* Wait for TX WB interrupt */
     e1000e_wait_isr(d, E1000E_TX0_MSG_ID);
 
+    /*
+     * Read the descriptor back from guest memory again, now that WB has
+     * occurred. This is required because if the packet was delayed by the
+     * trasmit rate limiter, e1000e_tx_ring_push read it back before QEMU
+     * actually processed it.
+     */
+    igb_read_tx_tail(d, &descr);
+
     /* Check DD bit */
     g_assert_cmphex(le32_to_cpu(descr.wb.status) & E1000_TXD_STAT_DD, ==,
                     E1000_TXD_STAT_DD);
@@ -147,6 +209,29 @@ static void igb_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *a
     guest_free(alloc, data);
 }
 
+/* Returns the amount of microseconds it takes to transmit a 64-byte packet.*/
+static uint64_t igb_send_and_measure(QE1000E *d, int *test_sockets,
+                                     QGuestAllocator *alloc)
+{
+    QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e);
+    QPCIDevice *pdev = &d_pci->pci_dev;
+    QTestState *qts = global_qtest;
+    uint64_t start_time, end_time;
+
+    /* Clear any stale pending interrupt for this vector before we start */
+    igb_msix_clear_pending(pdev, E1000E_TX0_MSG_ID);
+
+    /* Clear EICR to allow new interrupts to be raised in MSI-X mode */
+    e1000e_macreg_write(d, E1000_EICR, 0xFFFFFFFF);
+
+    /* Send a test packet and measure the time it takes. */
+    start_time = qtest_clock_step(qts, 1);
+    igb_send_verify(d, test_sockets, alloc);
+    end_time = qtest_clock_step(qts, 1);
+
+    return end_time - start_time;
+}
+
 static void test_e1000e_init(void *obj, void *data, QGuestAllocator * alloc)
 {
     /* init does nothing */
@@ -240,6 +325,77 @@ static void test_igb_transmit_rate_limiter_regs(void *obj, void *data,
     g_assert_cmphex(e1000e_macreg_read(d, E1000_TRLRC), ==, val1);
 }
 
+/*
+ * Test for Transmit Rate Limiter (TRL) traffic throttling.
+ *
+ * Verifies that:
+ * 1. Packets are transmitted without delay when TRL is disabled.
+ * 2. Enabling TRL on a queue throttles subsequent transmissions.
+ * 3. The delay matches the expected transmission time calculated from the
+ *    packet size and the configured target rate.
+ * 4. Disabling TRL restores normal (unthrottled) transmission behavior.
+ */
+static void test_igb_transmit_rate_limiter(void *obj, void *data,
+                                           QGuestAllocator *alloc)
+{
+    QOSGraphObject *e_object = obj;
+    QE1000E_PCI *e1000e = obj;
+    QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
+    QE1000E *d = &e1000e->e1000e;
+    const uint64_t expected_baseline_elapsed = 10 * SCALE_US;
+    const uint64_t expected_max_elapsed = 530 * SCALE_US;
+    const uint64_t expected_min_elapsed = 500 * SCALE_US;
+    uint64_t elapsed;
+
+    if (qpci_check_buggy_msi(dev)) {
+        return;
+    }
+
+    /* Send a test packet to verify there's no rate limit. */
+    elapsed = igb_send_and_measure(d, data, alloc);
+
+    /*
+     * QEMU doesn't emulate the hardware line rate of the device, so the packet
+     * is processed instantly (requiring 0 clock steps in wait_isr, which steps
+     * by 10us). Give a 10us margin for error.
+     */
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+
+    /*
+     * Enable TRL and set the target rate to:
+     * (E1000_LINK_RATE_1GBPS / 1000) = 125 KB/s.
+     */
+    igb_trl_enable(d, E1000_LINK_RATE_1GBPS / 1000);
+
+    /*
+     * Send Packet 1 (no delay)
+     * The first packet triggers rate limiting for subsequent transmissions.
+     */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+
+    /* Send Packet 2 (should be delayed) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    /*
+     * Expected delay: 64 byte packet / 125 KB/s = 512 us.
+     * Since QEMU steps the clock by 10 us on each wait_isr iteration,
+     * the delay is rounded up to the next 10 us step, which is 520 us.
+     */
+    g_assert_cmpint(elapsed, >=, expected_min_elapsed);
+    g_assert_cmpint(elapsed, <=, expected_max_elapsed);
+
+    /* Send Packet 3 (should also be delayed) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, >=, expected_min_elapsed);
+    g_assert_cmpint(elapsed, <=, expected_max_elapsed);
+
+    igb_trl_disable(d);
+
+    /* Send Packet 4 (no delay since TRL is disabled) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+}
+
 static void data_test_clear(void *sockets)
 {
     int *test_sockets = sockets;
@@ -298,6 +454,8 @@ static void register_igb_test(void)
                  test_igb_multiple_transfers, &opts);
     qos_add_test("transmit_rate_limiter_regs", "igb",
                  test_igb_transmit_rate_limiter_regs, &opts);
+    qos_add_test("transmit_rate_limiter", "igb",
+                 test_igb_transmit_rate_limiter, &opts);
 #endif
 
     opts.before = data_test_init_no_socket;
Signature · 1 line
2.55.0.rc0.738.g0c8ab3ebcc-goog
#10 lore raw

Re: [PATCH v6 7/7] igb: Add QTest for Transmit Rate Limiter traffic throttling

Quoted 54 lines · Josh Hilke

Add a test for the IGB transmit rate limiting functionality. The test verifies that TRL is enabled by setting the appropriate registers and that data is transmitted at the appropriate rate based on chosen target rate.

---
  tests/qtest/igb-test.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 158 insertions(+)
tests/qtest/igb-test.c +26 -0
index 119be6a..153f5fb 100644
@@ -34,9 +34,11 @@
  #include "qemu/iov.h"
  #include "qemu/module.h"
  #include "qemu/bitops.h"
+#include "qemu/timer.h"
  #include "libqos/libqos-malloc.h"
  #include "libqos/e1000e.h"
  #include "hw/net/igb_regs.h"
+#include "hw/pci/pci_regs.h"
 
  #ifndef _WIN32
 
@@ -59,6 +61,58 @@ static uint32_t igb_trl_get_rate_factor(uint64_t rate_bytes_s)
      return (E1000_LINK_RATE_1GBPS << 14) / rate_bytes_s;
  }
 
+static void igb_trl_enable(QE1000E *d, uint64_t rate_bytes_s)
+{
+    /* Select Queue 0 */
+    e1000e_macreg_write(d, E1000_TRLDQSEL, 0);
+    /* Enable TRL with target rate */
+    e1000e_macreg_write(d, E1000_TRLRC,
+                        E1000_TRLRC_RS_ENA |
+                        igb_trl_get_rate_factor(rate_bytes_s));
+}
+
+static void igb_trl_disable(QE1000E *d)
+{
+    /* Select Queue 0 */
+    e1000e_macreg_write(d, E1000_TRLDQSEL, 0);
+    /* Disable TRL */
+    e1000e_macreg_write(d, E1000_TRLRC, 0);
+}
+
+static void igb_msix_clear_pending(QPCIDevice *dev, uint16_t entry)
+{
+    uint64_t vector_offset =
+        dev->msix_table_off + (entry * PCI_MSIX_ENTRY_SIZE);
+    uint32_t val = qpci_io_readl(dev, dev->msix_table_bar,
+                                 vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL);

tests/qtest/igb-test.c static uint32_t igb_trl_get_rate_factor(uint64_t rate_bytes_s) · line 97

Quoted 10 lines
tests/qtest/igb-test.c +10 -0
+
+    /* Unmask (clears pending bit in QEMU) */
+    qpci_io_writel(dev, dev->msix_table_bar,
+                    vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL,
+                    val & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
+
+    /* Mask again */
+    qpci_io_writel(dev, dev->msix_table_bar,
+                    vector_offset + PCI_MSIX_ENTRY_VECTOR_CTRL,
+                    val | PCI_MSIX_ENTRY_CTRL_MASKBIT);

"| PCI_MSIX_ENTRY_CTRL_MASKBIT" is unnecessary to clear pending interrupts; restoring val should be sufficient. It causes an extraneous side effect.

Also indents are off here though the other indents seems to have been fixed.

Regards,
Akihiko Odaki

Quoted 154 lines
tests/qtest/igb-test.c +122 -0
+}
+
+static void igb_read_tx_tail(QE1000E *d, void *descr)
+{
+    QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e);
+    uint32_t last_entry;
+    uint32_t tail;
+    uint32_t len;
+
+    tail = e1000e_macreg_read(d, E1000_TDT_A(0));
+    len = e1000e_macreg_read(d, E1000_TDLEN_A(0)) / E1000_RING_DESC_LEN;
+    last_entry = (tail + len - 1) % len;
+
+    qtest_memread(d_pci->pci_dev.bus->qts,
+                  d->tx_ring + last_entry * E1000_RING_DESC_LEN, descr,
+                  E1000_RING_DESC_LEN);
+}
+
  static void igb_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *alloc)
  {
      union e1000_adv_tx_desc descr;
@@ -84,6 +138,14 @@ static void igb_send_verify(QE1000E *d, int *test_sockets, QGuestAllocator *allo
      /* Wait for TX WB interrupt */
      e1000e_wait_isr(d, E1000E_TX0_MSG_ID);
 
+    /*
+     * Read the descriptor back from guest memory again, now that WB has
+     * occurred. This is required because if the packet was delayed by the
+     * trasmit rate limiter, e1000e_tx_ring_push read it back before QEMU
+     * actually processed it.
+     */
+    igb_read_tx_tail(d, &descr);
+
      /* Check DD bit */
      g_assert_cmphex(le32_to_cpu(descr.wb.status) & E1000_TXD_STAT_DD, ==,
                      E1000_TXD_STAT_DD);
@@ -147,6 +209,29 @@ static void igb_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *a
      guest_free(alloc, data);
  }
 
+/* Returns the amount of microseconds it takes to transmit a 64-byte packet.*/
+static uint64_t igb_send_and_measure(QE1000E *d, int *test_sockets,
+                                     QGuestAllocator *alloc)
+{
+    QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e);
+    QPCIDevice *pdev = &d_pci->pci_dev;
+    QTestState *qts = global_qtest;
+    uint64_t start_time, end_time;
+
+    /* Clear any stale pending interrupt for this vector before we start */
+    igb_msix_clear_pending(pdev, E1000E_TX0_MSG_ID);
+
+    /* Clear EICR to allow new interrupts to be raised in MSI-X mode */
+    e1000e_macreg_write(d, E1000_EICR, 0xFFFFFFFF);
+
+    /* Send a test packet and measure the time it takes. */
+    start_time = qtest_clock_step(qts, 1);
+    igb_send_verify(d, test_sockets, alloc);
+    end_time = qtest_clock_step(qts, 1);
+
+    return end_time - start_time;
+}
+
  static void test_e1000e_init(void *obj, void *data, QGuestAllocator * alloc)
  {
      /* init does nothing */
@@ -240,6 +325,77 @@ static void test_igb_transmit_rate_limiter_regs(void *obj, void *data,
      g_assert_cmphex(e1000e_macreg_read(d, E1000_TRLRC), ==, val1);
  }
 
+/*
+ * Test for Transmit Rate Limiter (TRL) traffic throttling.
+ *
+ * Verifies that:
+ * 1. Packets are transmitted without delay when TRL is disabled.
+ * 2. Enabling TRL on a queue throttles subsequent transmissions.
+ * 3. The delay matches the expected transmission time calculated from the
+ *    packet size and the configured target rate.
+ * 4. Disabling TRL restores normal (unthrottled) transmission behavior.
+ */
+static void test_igb_transmit_rate_limiter(void *obj, void *data,
+                                           QGuestAllocator *alloc)
+{
+    QOSGraphObject *e_object = obj;
+    QE1000E_PCI *e1000e = obj;
+    QPCIDevice *dev = e_object->get_driver(e_object, "pci-device");
+    QE1000E *d = &e1000e->e1000e;
+    const uint64_t expected_baseline_elapsed = 10 * SCALE_US;
+    const uint64_t expected_max_elapsed = 530 * SCALE_US;
+    const uint64_t expected_min_elapsed = 500 * SCALE_US;
+    uint64_t elapsed;
+
+    if (qpci_check_buggy_msi(dev)) {
+        return;
+    }
+
+    /* Send a test packet to verify there's no rate limit. */
+    elapsed = igb_send_and_measure(d, data, alloc);
+
+    /*
+     * QEMU doesn't emulate the hardware line rate of the device, so the packet
+     * is processed instantly (requiring 0 clock steps in wait_isr, which steps
+     * by 10us). Give a 10us margin for error.
+     */
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+
+    /*
+     * Enable TRL and set the target rate to:
+     * (E1000_LINK_RATE_1GBPS / 1000) = 125 KB/s.
+     */
+    igb_trl_enable(d, E1000_LINK_RATE_1GBPS / 1000);
+
+    /*
+     * Send Packet 1 (no delay)
+     * The first packet triggers rate limiting for subsequent transmissions.
+     */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+
+    /* Send Packet 2 (should be delayed) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    /*
+     * Expected delay: 64 byte packet / 125 KB/s = 512 us.
+     * Since QEMU steps the clock by 10 us on each wait_isr iteration,
+     * the delay is rounded up to the next 10 us step, which is 520 us.
+     */
+    g_assert_cmpint(elapsed, >=, expected_min_elapsed);
+    g_assert_cmpint(elapsed, <=, expected_max_elapsed);
+
+    /* Send Packet 3 (should also be delayed) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, >=, expected_min_elapsed);
+    g_assert_cmpint(elapsed, <=, expected_max_elapsed);
+
+    igb_trl_disable(d);
+
+    /* Send Packet 4 (no delay since TRL is disabled) */
+    elapsed = igb_send_and_measure(d, data, alloc);
+    g_assert_cmpint(elapsed, <, expected_baseline_elapsed);
+}
+
  static void data_test_clear(void *sockets)
  {
      int *test_sockets = sockets;
@@ -298,6 +454,8 @@ static void register_igb_test(void)
                   test_igb_multiple_transfers, &opts);
      qos_add_test("transmit_rate_limiter_regs", "igb",
                   test_igb_transmit_rate_limiter_regs, &opts);
+    qos_add_test("transmit_rate_limiter", "igb",
+                 test_igb_transmit_rate_limiter, &opts);
  #endif
 
      opts.before = data_test_init_no_socket;