igb: Implement Transmit Rate Limiting (TRL)
cover igb: Implement Transmit Rate Limiting (TRL)
This patch series implements the Transmit Rate Limiter (TRL) feature for the Intel 82576 (igb) network device emulation in QEMU.
The Transmit Rate Limiter allows the guest OS (or virtual machines in an IOV setup) to limit the transmission bandwidth of individual transmit queues to prevent a single queue from saturating the link.
The specifications for TRL can be found in the Intel 82576eb datasheet.
Motivation
=========
Upstream Linux Kernel would like a universally accesible device to run tests for Live Update [1] (swapping kernel versions). The primary test case is verifying that a device can do continuous DMA for the duration of Live Update (about 4 minutes).
IGB is a good candidate device for this use case because it's ubiquitous on many systems, emulated in QEMU, and can do DMA memcpy operations using its loopback functionality.
I've written a VFIO selftest driver for IGB [2] which uses the loopback functionality do DMA. This driver works for both the real and emulated version of IGB.
The problem is that the IGB driver can only do DMA for about 1 second when using the QEMU device, because QEMU does not emulate the line rate of the hardware, i.e. data transmission is instantaneous.
However, with this implementation of TRL + enabling jumbo packets, I was able to get the driver to do DMA for 5 minutes, which easily meets the 4 minute requirement for Live Update testing.
[1] https://docs.kernel.org/core-api/liveupdate.html
[2] https://lore.kernel.org/kvm/[email protected]/
Implementation Details:
=======================
TRL is implemented per-queue. When a queue is configured with a target rate via the TRLRC register, QEMU calculates the transmission delay dynamically for each packet based on its size (Layer 2 packet length) and the target bandwidth. If the calculated transmission time exceeds the current virtual time, QEMU's TX loop defers further packet processing, marks the queue as throttled, and schedules a timer. When the timer expires, the throttled state is cleared, packet transmission resumes, and a writeback interrupt is raised to notify the driver of the completion.
Excluded Features / Simplifications:
====================================
I excluded the following TRL features that are mentioned in the IGB datasheet because they weren't necessary for my relatively simple use case. I figure they can be added later if people need them, or I can add them in a later version of the series if there's a use case for them now.
The numbers in parentheses are citations for sections in the IGB datasheet.
* Rate scheduling table (7.8): real hardware uses a rate scheudling table and a time stamp table instead of QEMU timers to determine when each rate-limited queue is allowed to transmit next.
* Max Memory Window (7.8): MMW allows a queue to transmit a burst of traffic if it has been idle or delayed by other traffic, allowing "catch up" to its average target rate.
* TRLDCS register (4.5.12.1): This register is used to avoid race conditions between hardware and software when re-enabling/configuring TRL when the link status/speed changes. However link changes in QEMU are synchronous, so this isn't needed.
Testing
=======
This series includes a Qtest for IGB which verifies that the transmission rate is throttled when TRL is enabled, and not throttled when TRL is disabled.
Ran the following command to test it:
ninja -C build && \
QTEST_QEMU_BINARY=./build/qemu-system-x86_64 \
./build/tests/qtest/qos-test \
-p /x86_64/pc/i440FX-pcihost/pci-bus-pc/pci-bus/igb/igb-tests
Josh Hilke (5):
igb: Define TRL registers and core 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 a test for Transmit Rate Limiter
hw/net/igb_common.h | 1 + hw/net/igb_core.c | 134 ++++++++++++++++++++++++++++++++-- hw/net/igb_core.h | 11 +++ hw/net/igb_regs.h | 11 +++ tests/qtest/igb-test.c | 161 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 313 insertions(+), 5 deletions(-)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH 1/5 igb: Define TRL registers and core state
Introduce register offsets and bitmask constants for the Transmit Rate Limiter (TRL). Add the IGBTrlTimer 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 | 17 +++++++++++++++++ hw/net/igb_core.h | 11 +++++++++++ hw/net/igb_regs.h | 11 +++++++++++ 4 files changed, 40 insertions(+)
hw/net/igb_common.h +1 -0
@@ -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 +17 -0
@@ -4282,6 +4282,11 @@ igb_autoneg_resume(IGBCore *core) } } +static void igb_trl_timer_cb (void *opaque) +{ +/* Stub */ +} + void igb_core_pci_realize(IGBCore *core, const uint16_t *eeprom_templ,
@@ -4296,6 +4301,11 @@ igb_core_pci_realize(IGBCore *core,for (i =0 ; i <IGB_NUM_QUEUES ; i++) {net_tx_pkt_init (&core->tx[i].tx_pkt,E1000E_MAX_TX_FRAGS ); + core->trl_timer[i].timer = +timer_new_ns (QEMU_CLOCK_VIRTUAL , igb_trl_timer_cb, + &core->trl_timer[i]); + core->trl_timer[i].core = core; + core->trl_timer[i].queue_idx = i; }net_rx_pkt_init (&core->rx_pkt);
@@ -4319,6 +4329,7 @@ igb_core_pci_uninit(IGBCore *core)for (i =0 ; i <IGB_NUM_QUEUES ; i++) {net_tx_pkt_uninit (core->tx[i].tx_pkt); +timer_free (core->trl_timer[i].timer); }net_rx_pkt_uninit (core->rx_pkt);
@@ -4469,6 +4480,12 @@ static void igb_reset(IGBCore *core, bool sw)igb_intrmgr_reset (core); +for (i =0 ; i <IGB_NUM_QUEUES ; i++) { +timer_del (core->trl_timer[i].timer); + core->trl_throttled[i] = false; + core->trl_target_rate[i] =0 ; + } +memset (core->phy,0 ,sizeof core->phy);memcpy (core->phy, igb_phy_reg_init,sizeof igb_phy_reg_init);
hw/net/igb_core.h +11 -0
@@ -63,6 +63,12 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlTimer_st { +QEMUTimer *timer; +IGBCore *core; +int queue_idx; +}IGBTrlTimer ; +struct IGBCore {uint32_t mac[E1000E_MAC_SIZE ];uint16_t phy[MAX_PHY_REG_ADDRESS +1 ];
@@ -99,6 +105,11 @@ struct IGBCore {void (*owner_start_recv)(PCIDevice *d);int64_t timadj; + +/* Transmit Rate Limiting */ +IGBTrlTimer trl_timer[IGB_NUM_QUEUES ]; +bool trl_throttled[IGB_NUM_QUEUES ]; +uint64_t trl_target_rate[IGB_NUM_QUEUES ];/* in bytes per second */ };void
hw/net/igb_regs.h +11 -0
@@ -718,4 +718,15 @@ 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_TRLRC_RF_DEC_MASK 0x3FFF +#define E1000_TRLRC_RF_INT_MASK 0x3FF + +/* 1 Gbps link rate in bytes per second (1,000,000,000 bits/s / 8) */ +#define E1000_LINK_RATE_1GBPS 125000000ULL +#endif
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH 1/5] igb: Define TRL registers and core state
Quoted 5 lines · Josh Hilke
Introduce register offsets and bitmask constants for the Transmit Rate Limiter (TRL). Add the IGBTrlTimer 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.
The added state should be included in VMState.
Please see: docs/devel/migration/main.rst
Regards,
Akihiko Odaki
Quoted 119 lines
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_common.h | 1 + hw/net/igb_core.c | 17 +++++++++++++++++ hw/net/igb_core.h | 11 +++++++++++ hw/net/igb_regs.h | 11 +++++++++++ 4 files changed, 40 insertions(+)hw/net/igb_common.h +1
-0@@ -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 +17
-0@@ -4282,6 +4282,11 @@ igb_autoneg_resume(IGBCore *core) } } +static void igb_trl_timer_cb (void *opaque) +{ +/* Stub */ +} + void igb_core_pci_realize(IGBCore *core, const uint16_t *eeprom_templ,@@ -4296,6 +4301,11 @@ igb_core_pci_realize(IGBCore *core,for (i =0 ; i <IGB_NUM_QUEUES ; i++) {net_tx_pkt_init (&core->tx[i].tx_pkt,E1000E_MAX_TX_FRAGS ); + core->trl_timer[i].timer = +timer_new_ns (QEMU_CLOCK_VIRTUAL , igb_trl_timer_cb, + &core->trl_timer[i]); + core->trl_timer[i].core = core; + core->trl_timer[i].queue_idx = i; }net_rx_pkt_init (&core->rx_pkt);@@ -4319,6 +4329,7 @@ igb_core_pci_uninit(IGBCore *core)for (i =0 ; i <IGB_NUM_QUEUES ; i++) {net_tx_pkt_uninit (core->tx[i].tx_pkt); +timer_free (core->trl_timer[i].timer); }net_rx_pkt_uninit (core->rx_pkt);@@ -4469,6 +4480,12 @@ static void igb_reset(IGBCore *core, bool sw)igb_intrmgr_reset (core); +for (i =0 ; i <IGB_NUM_QUEUES ; i++) { +timer_del (core->trl_timer[i].timer); + core->trl_throttled[i] = false; + core->trl_target_rate[i] =0 ; + } +memset (core->phy,0 ,sizeof core->phy);memcpy (core->phy, igb_phy_reg_init,sizeof igb_phy_reg_init);hw/net/igb_core.h +11
-0@@ -63,6 +63,12 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlTimer_st { +QEMUTimer *timer; +IGBCore *core; +int queue_idx; +}IGBTrlTimer ; +struct IGBCore {uint32_t mac[E1000E_MAC_SIZE ];uint16_t phy[MAX_PHY_REG_ADDRESS +1 ];@@ -99,6 +105,11 @@ struct IGBCore {void (*owner_start_recv)(PCIDevice *d);int64_t timadj; + +/* Transmit Rate Limiting */ +IGBTrlTimer trl_timer[IGB_NUM_QUEUES ]; +bool trl_throttled[IGB_NUM_QUEUES ]; +uint64_t trl_target_rate[IGB_NUM_QUEUES ];/* in bytes per second */ };void hw/net/igb_regs.h +11
-0@@ -718,4 +718,15 @@ 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_TRLRC_RF_DEC_MASK 0x3FFF +#define E1000_TRLRC_RF_INT_MASK 0x3FF + +/* 1 Gbps link rate in bytes per second (1,000,000,000 bits/s / 8) */ +#define E1000_LINK_RATE_1GBPS 125000000ULL +#endif
PATCH 2/5 igb: Implement TRL register write and configuration
In reply to Josh HilkeImplement 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 | 72 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-)
hw/net/igb_core.c +71 -1
@@ -2136,11 +2136,31 @@ igb_set_phy_ctrl(IGBCore *core, uint16_t val) } } +static void igb_trl_disable_queue (IGBCore *core,int qidx) +{ + core->trl_target_rate[qidx] =0 ; + +if (core->trl_throttled[qidx]) { +timer_del (core->trl_timer[qidx].timer); + core->trl_throttled[qidx] = false; + } +} + +static void igb_trl_disable (IGBCore *core) +{ +for (int i =0 ; i <IGB_NUM_QUEUES ; i++) { +igb_trl_disable_queue (core, i); + } + core->mac[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 ]; +igb_trl_disable (core); +trace_e1000e_link_status_changed (nc->link_down ? false : true);if (nc->link_down) {
@@ -2623,6 +2643,54 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)*/ } +static void igb_set_trldqsel (IGBCore *core,int index,uint32_t val) +{ + core->mac[index] = val &0xF ; +} + +static void igb_set_trlrc (IGBCore *core,int index,uint32_t val) +{ +uint64_t rf_scaled; +int qidx; + + core->mac[index] = val; + + qidx = core->mac[TRLDQSEL ]; + +if (!(val &E1000_TRLRC_RS_ENA )) { + goto disable; + } + +/* +* 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 = val &0xFFFFFF ; + +/* Zero is not a valid rate factor, treat as disabled */ +if (rf_scaled ==0 ) { + goto disable; + } +/* +* The rate factor is a fixed-point number with a 14-bit fractional part: +* Rate Factor = rf_scaled / 2^14 +* +* The target rate is: +* Target Rate = Link Rate / Rate Factor +* = Link Rate / (rf_scaled / 2^14) +* = (Link Rate * 2^14) / rf_scaled +*/ + core->trl_target_rate[qidx] = (E1000_LINK_RATE_1GBPS <<14 ) / rf_scaled; +return ; + +disable: +igb_trl_disable_queue (core, qidx); +} +static inline void igb_autoneg_timer (void *opaque) {
@@ -4126,7 +4194,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.54.0.1136.gdb2ca164c4-goog
Re: [PATCH 2/5] igb: Implement TRL register write and configuration
Quoted 5 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.
TRLRC may give an incorrect value when read after modifying TLDQSEL. I suggest creating an array of TRLRC values and indexing it with TLDQSEL instead of using core->mac[TRLRC], which is scalar.
Regards,
Akihiko Odaki
Quoted 108 lines
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_core.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-)hw/net/igb_core.c +71
-1@@ -2136,11 +2136,31 @@ igb_set_phy_ctrl(IGBCore *core, uint16_t val) } } +static void igb_trl_disable_queue (IGBCore *core,int qidx) +{ + core->trl_target_rate[qidx] =0 ; + +if (core->trl_throttled[qidx]) { +timer_del (core->trl_timer[qidx].timer); + core->trl_throttled[qidx] = false; + } +} + +static void igb_trl_disable (IGBCore *core) +{ +for (int i =0 ; i <IGB_NUM_QUEUES ; i++) { +igb_trl_disable_queue (core, i); + } + core->mac[TRLRC] &= ~E1000_TRLRC_RS_ENA;because +} +void igb_core_set_link_status (IGBCore *core) {NetClientState *nc =qemu_get_queue (core->owner_nic);uint32_t old_status = core->mac[STATUS ]; +igb_trl_disable (core); +trace_e1000e_link_status_changed (nc->link_down ? false : true);if (nc->link_down) {@@ -2623,6 +2643,54 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)*/ } +static void igb_set_trldqsel (IGBCore *core,int index,uint32_t val) +{ + core->mac[index] = val &0xF ; +} + +static void igb_set_trlrc (IGBCore *core,int index,uint32_t val) +{ +uint64_t rf_scaled; +int qidx; + + core->mac[index] = val; + + qidx = core->mac[TRLDQSEL ]; + +if (!(val &E1000_TRLRC_RS_ENA )) { + goto disable; + } + +/* +* 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 = val &0xFFFFFF ; + +/* Zero is not a valid rate factor, treat as disabled */ +if (rf_scaled ==0 ) { + goto disable; + } +/* +* The rate factor is a fixed-point number with a 14-bit fractional part: +* Rate Factor = rf_scaled / 2^14 +* +* The target rate is: +* Target Rate = Link Rate / Rate Factor +* = Link Rate / (rf_scaled / 2^14) +* = (Link Rate * 2^14) / rf_scaled +*/ + core->trl_target_rate[qidx] = (E1000_LINK_RATE_1GBPS <<14 ) / rf_scaled; +return ; + +disable: +igb_trl_disable_queue (core, qidx); +} +static inline void igb_autoneg_timer (void *opaque) {@@ -4126,7 +4194,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) };
PATCH 3/5 igb: Refactor TX path to return processed byte count
In reply to Josh HilkeModify 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
@@ -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,7 +654,9 @@ 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)) { + total_len =net_tx_pkt_get_total_len (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,
@@ -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.54.0.1136.gdb2ca164c4-goog
PATCH 4/5 igb: Implement TRL throttling and timer resumption
In reply to Josh HilkeIntegrate 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 | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)
hw/net/igb_core.c +35 -2
@@ -875,6 +875,14 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) (core->mac[TXDCTL0 + (qn *16 )] &E1000_TXDCTL_QUEUE_ENABLE ); } +static uint64_t igb_trl_calculate_delay (IGBCore *core,int qidx, +size_t bytes_sent) +{ +uint64_t ns_per_sec =1000000000ULL ; + +return (uint64_t )bytes_sent * ns_per_sec / core->trl_target_rate[qidx]; +} +static void igb_start_xmit (IGBCore *core,const IGB_TxRing *txr) {
@@ -883,17 +891,23 @@ 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;if (!igb_tx_enabled (core, txi)) {trace_e1000e_tx_disabled ();return ; } +if (core->trl_throttled[txi->idx]) { +return ; + } + d =pcie_sriov_get_vf_at_index (core->owner, txi->idx %8 );if (!d) { d = core->owner; } + bytes_sent =0 ;while (!igb_ring_empty (core, txi)) { base =igb_ring_head_descr (core, txi);
@@ -902,9 +916,20 @@ 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); + +if (core->trl_target_rate[txi->idx] >0 ) { +uint64_t delay_ns = +igb_trl_calculate_delay (core, txi->idx, bytes_sent); +if (delay_ns >0 ) { + core->trl_throttled[txi->idx] = true; +timer_mod (core->trl_timer[txi->idx].timer, +qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + delay_ns); +break ; + } + } }if (eic) {
@@ -4358,7 +4383,15 @@ igb_autoneg_resume(IGBCore *core)static void igb_trl_timer_cb (void *opaque) { -/* Stub */ +IGBTrlTimer *trl_timer = opaque; +IGBCore *core = trl_timer->core; +int qidx = trl_timer->queue_idx; +IGB_TxRing txr; + + core->trl_throttled[qidx] = false; + +igb_tx_ring_init (core, &txr, qidx); +igb_start_xmit (core, &txr); }void
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH 4/5] igb: Implement TRL throttling and timer resumption
Quoted 18 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.Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_core.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-)hw/net/igb_core.c +0
-0
hw/net/igb_core.c igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) · line 881
Quoted 7 lines
hw/net/igb_core.c +4
-0@@ -875,6 +875,14 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) (core->mac[TXDCTL0 + (qn *16 )] &E1000_TXDCTL_QUEUE_ENABLE ); } +static uint64_t igb_trl_calculate_delay (IGBCore *core,int qidx, +size_t bytes_sent) +{ +uint64_t ns_per_sec =1000000000ULL ;
Please use NANOSECONDS_PER_SECOND.
Quoted 35 lines
hw/net/igb_core.c +10
-0+ +return (uint64_t )bytes_sent * ns_per_sec / core->trl_target_rate[qidx]; +} +static void igb_start_xmit (IGBCore *core,const IGB_TxRing *txr) {@@ -883,17 +891,23 @@ 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;if (!igb_tx_enabled (core, txi)) {trace_e1000e_tx_disabled ();return ; } +if (core->trl_throttled[txi->idx]) { +return ; + } + d =pcie_sriov_get_vf_at_index (core->owner, txi->idx %8 );if (!d) { d = core->owner; } + bytes_sent =0 ;while (!igb_ring_empty (core, txi)) { base =igb_ring_head_descr (core, txi);@@ -902,9 +916,20 @@ 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);
hw/net/igb_core.c igb_start_xmit(IGBCore *core, const IGB_TxRing *txr) · line 927
Quoted 10 lines
hw/net/igb_core.c +7
-1-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); + +if (core->trl_target_rate[txi->idx] >0 ) { +uint64_t delay_ns = +igb_trl_calculate_delay (core, txi->idx, bytes_sent); +if (delay_ns >0 ) { + core->trl_throttled[txi->idx] = true;
I think you can use timer_pending() instead of having a separate field, trl_throttled.
Regards,
Akihiko Odaki
Quoted 25 lines
hw/net/igb_core.c +14
-1+timer_mod (core->trl_timer[txi->idx].timer, +qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + delay_ns); +break ; + } + } }if (eic) {@@ -4358,7 +4383,15 @@ igb_autoneg_resume(IGBCore *core)static void igb_trl_timer_cb (void *opaque) { -/* Stub */ +IGBTrlTimer *trl_timer = opaque; +IGBCore *core = trl_timer->core; +int qidx = trl_timer->queue_idx; +IGB_TxRing txr; + + core->trl_throttled[qidx] = false; + +igb_tx_ring_init (core, &txr, qidx); +igb_start_xmit (core, &txr); }void
PATCH 5/5 igb: Add a test for Transmit Rate Limiter
In reply to Josh HilkeAdd 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 | 161 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+)
tests/qtest/igb-test.c +161 -0
@@ -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
@@ -45,6 +47,71 @@ 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_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;
@@ -70,6 +137,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 );
@@ -133,6 +208,29 @@ static void igb_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *aguest_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 */
@@ -191,6 +289,67 @@ static void test_igb_multiple_transfers(void *obj, void *data, } +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;
@@ -247,6 +406,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" ,"igb" , + test_igb_transmit_rate_limiter, &opts);#endif opts.before = data_test_init_no_socket;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH 5/5] igb: Add a test for Transmit Rate Limiter
Quoted 41 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.
Signed-off-by Josh Hilke <[email protected]>--- tests/qtest/igb-test.c | 161 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+)tests/qtest/igb-test.c +13
-0@@ -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@@ -45,6 +47,71 @@ 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) +{
tests/qtest/igb-test.c static const struct eth_header packet = { · line 70
Quoted 10 lines
tests/qtest/igb-test.c +10
-0+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));
Indents are somewhat off in this file.
Regards,
Akihiko Odaki
Quoted 169 lines
tests/qtest/igb-test.c +138
-0+} + +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;@@ -70,6 +137,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 );@@ -133,6 +208,29 @@ static void igb_receive_verify(QE1000E *d, int *test_sockets, QGuestAllocator *aguest_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 */ @@ -191,6 +289,67 @@ static void test_igb_multiple_transfers(void *obj, void *data, } +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;@@ -247,6 +406,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" ,"igb" , + test_igb_transmit_rate_limiter, &opts);#endif opts.before = data_test_init_no_socket;