igb: Implement Transmit Rate Limiting (TRL)
v4 cover igb: Implement Transmit Rate Limiting (TRL)
This is v4 of a patch series implementing the Transmit Rate Limiter (TRL) feature for the Intel 82576 (igb) network device emulation in QEMU.
v3: https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg03044.html
Changelog:
v4:
- Remove unused masks.
- Use BITS_PER_BYTE in definition of E1000_LINK_RATE_1GBPS.
- Add helper function to get rate factor and unify comments.
- Add helper function to return if TRL is enabled for a queue.
- Get packet length after calling igb_tx_insert_vlan().
- Use NANOSECONDS_PER_SECOND instead of 1000000000ULL.
v3:
- Remove redundant target_rate state and recalculate it dynamically from trlrc.
- Flush the transmit ring when TRL is disabled while a queue is throttled to prevent packet stall.
- Remove redundant updates to core->mac[TRLRC] since reads are multiplexed.
---
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 | 164 ++++++++++++++++++++++++++++++++++++-- hw/net/igb_core.h | 13 +++ hw/net/igb_regs.h | 6 ++ tests/qtest/igb-test.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 431 insertions(+), 5 deletions(-)
---
base-commit: 98b060da3a4f92b2a994ead5b16a87e783baf77c
change-id: 20260608-igb-trl-19be198da6f2
Best regards,
Signature · 1 line
Josh Hilke <[email protected]>
PATCH v4 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 | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/igb_core.h | 13 ++++++++++++ hw/net/igb_regs.h | 6 ++++++ 4 files changed, 79 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 +59 -0
89 lines not on this page · raw message
hw/net/igb_core.h +13 -0
@@ -63,6 +63,13 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlQueue_st { +QEMUTimer *timer; +IGBCore *core; +int queue_idx; +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,7 @@ igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt);void igb_start_recv (IGBCore *core); +bool igb_trl_enabled (uint32_t trlrc); +uint64_t igb_trl_get_target_rate (const IGBTrlQueue *q); +#endif
hw/net/igb_regs.h +6 -0
@@ -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.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 1/7] igb: Define TRL registers and core state
Hi,
Quoted 32 lines · Josh Hilke
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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_common.h | 1 + hw/net/igb_core.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/igb_core.h | 13 ++++++++++++ hw/net/igb_regs.h | 6 ++++++ 4 files changed, 79 insertions(+)hw/net/igb_core.c +2
-0@@ -96,6 +96,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 uint32_t igb_trl_get_scaled_rate_factor (uint32_t trlrc);static inline void igb_raise_legacy_irq (IGBCore *core)@@ -4282,6 +4283,53 @@ igb_autoneg_resume(IGBCore *core) } } +static uint32_t igb_trl_get_scaled_rate_factor (uint32_t trlrc)
hw/net/igb_core.c igb_autoneg_resume(IGBCore *core) · line 4296
Quoted 10 lines
hw/net/igb_core.c +10
-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. +*/ +return trlrc &0xFFFFFF ;
Alternatively:
return extract32(trlrc, 0, 24);
Quoted 60 lines
hw/net/igb_core.c +42
-0+} + +bool igb_trl_enabled (uint32_t trlrc) +{ +return trlrc &E1000_TRLRC_RS_ENA ; +} + +uint64_t igb_trl_get_target_rate (const IGBTrlQueue *q) +{ +const uint64_t rf_scaled =igb_trl_get_scaled_rate_factor (q->trlrc); + +if (!igb_trl_enabled (q->trlrc)) { +return 0 ; + } + +if (rf_scaled ==0 ) { +return 0 ; + } + +/* +* 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 +*/ +return (E1000_LINK_RATE_1GBPS <<14 ) / rf_scaled; +} + +static void igb_trl_timer_cb (void *opaque) +{ +/* Stub */ +} + void igb_core_pci_realize(IGBCore *core, const uint16_t *eeprom_templ,@@ -4296,6 +4344,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[i].timer = +timer_new_ns (QEMU_CLOCK_VIRTUAL , igb_trl_timer_cb, + &core->trl[i]); + core->trl[i].core = core; + core->trl[i].queue_idx = i; }net_rx_pkt_init (&core->rx_pkt);@@ -4319,6 +4372,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[i].timer); }net_rx_pkt_uninit (core->rx_pkt);
hw/net/igb_core.c static void igb_reset(IGBCore *core, bool sw) · line 4528
Quoted 6 lines
hw/net/igb_core.c +3
-0@@ -4469,6 +4523,11 @@ static void igb_reset(IGBCore *core, bool sw)igb_intrmgr_reset (core); +for (i =0 ; i <IGB_NUM_QUEUES ; i++) { +timer_del (core->trl[i].timer); + core->trl[i].trlrc =0 ;
Better introduce igb_trl_disable_queue() in this patch and use it here.
Quoted 10 lines
hw/net/igb_core.c +2
-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 +0
-0
hw/net/igb_core.h typedef struct IGBIntrDelayTimer_st { · line 69
Quoted 7 lines
hw/net/igb_core.h +4
-0@@ -63,6 +63,13 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlQueue_st { + QEMUTimer *timer; + IGBCore *core; + int queue_idx;
unsigned?
hw/net/igb_core.h typedef struct IGBIntrDelayTimer_st { · line 71
Quoted 2 lines
hw/net/igb_core.h +2
-0+uint32_t trlrc; +} IGBTrlQueue;
Patch LGTM otherwise.
Re: [PATCH v4 1/7] igb: Define TRL registers and core state
In reply to Josh HilkeQuoted 44 lines · Josh Hilke
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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_common.h | 1 + hw/net/igb_core.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/igb_core.h | 13 ++++++++++++ hw/net/igb_regs.h | 6 ++++++ 4 files changed, 79 insertions(+)hw/net/igb_core.h +10
-0@@ -63,6 +63,13 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlQueue_st { +QEMUTimer *timer; +IGBCore *core; +int queue_idx; +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
hw/net/igb_core.h igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt); · line 156
Quoted 4 lines
hw/net/igb_core.h +1
-0@@ -143,4 +153,7 @@ igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt);void igb_start_recv (IGBCore *core); +bool igb_trl_enabled (uint32_t trlrc);
bool igb_trl_enabled(const IGBTrlQueue *q);
Quoted 3 lines
hw/net/igb_core.h +2
-0+uint64_t igb_trl_get_target_rate (const IGBTrlQueue *q); +#endif
Re: [PATCH v4 1/7] igb: Define TRL registers and core state
In reply to Josh HilkeQuoted 51 lines · Josh Hilke
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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_common.h | 1 + hw/net/igb_core.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/igb_core.h | 13 ++++++++++++ hw/net/igb_regs.h | 6 ++++++ 4 files changed, 79 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 +10
-0@@ -96,6 +96,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 uint32_t igb_trl_get_scaled_rate_factor (uint32_t trlrc);static inline void igb_raise_legacy_irq (IGBCore *core)@@ -4282,6 +4283,53 @@ igb_autoneg_resume(IGBCore *core) } } +static uint32_t igb_trl_get_scaled_rate_factor (uint32_t trlrc) +{ +/* +* 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.
hw/net/igb_core.c igb_autoneg_resume(IGBCore *core) · line 4304
Quoted 10 lines
hw/net/igb_core.c +10
-0+*/ +return trlrc &0xFFFFFF ; +} + +bool igb_trl_enabled (uint32_t trlrc) +{ +return trlrc &E1000_TRLRC_RS_ENA ; +} + +uint64_t igb_trl_get_target_rate (const IGBTrlQueue *q)
Please add the static qualifier.
Quoted 115 lines
hw/net/igb_core.c +39
-0+{ +const uint64_t rf_scaled =igb_trl_get_scaled_rate_factor (q->trlrc); + +if (!igb_trl_enabled (q->trlrc)) { +return 0 ; + } + +if (rf_scaled ==0 ) { +return 0 ; + } + +/* +* 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 +*/ +return (E1000_LINK_RATE_1GBPS <<14 ) / rf_scaled; +} + +static void igb_trl_timer_cb (void *opaque) +{ +/* Stub */ +} + void igb_core_pci_realize(IGBCore *core, const uint16_t *eeprom_templ,@@ -4296,6 +4344,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[i].timer = +timer_new_ns (QEMU_CLOCK_VIRTUAL , igb_trl_timer_cb, + &core->trl[i]); + core->trl[i].core = core; + core->trl[i].queue_idx = i; }net_rx_pkt_init (&core->rx_pkt);@@ -4319,6 +4372,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[i].timer); }net_rx_pkt_uninit (core->rx_pkt);@@ -4469,6 +4523,11 @@ static void igb_reset(IGBCore *core, bool sw)igb_intrmgr_reset (core); +for (i =0 ; i <IGB_NUM_QUEUES ; i++) { +timer_del (core->trl[i].timer); + core->trl[i].trlrc =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 +13
-0@@ -63,6 +63,13 @@ typedef struct IGBIntrDelayTimer_st {IGBCore *core; } IGBIntrDelayTimer; +typedef struct IGBTrlQueue_st { +QEMUTimer *timer; +IGBCore *core; +int queue_idx; +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,7 @@ igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt);void igb_start_recv (IGBCore *core); +bool igb_trl_enabled (uint32_t trlrc); +uint64_t igb_trl_get_target_rate (const IGBTrlQueue *q); +#endif hw/net/igb_regs.h +6
-0@@ -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
PATCH v4 2/7 igb: Add VMState support for TRL state
In reply to Josh HilkeAdd 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
@@ -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 (igb_trl_enabled (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.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 2/7] igb: Add VMState support for TRL state
Quoted 14 lines · 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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)hw/net/igb.c +0
-0
hw/net/igb.c static const VMStateDescription igb_vmstate_intr_timer = { · line 571
Quoted 9 lines
hw/net/igb.c +6
-0@@ -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 (igb_trl_enabled (s->core.trl[i].trlrc)) {
Using 'bool igb_trl_enabled(const IGBTrlQueue *q)' in previous patch:
if (igb_trl_enabled(&s->core.trl[i])) {hw/net/igb.c static const VMStateDescription igb_vmstate_intr_timer = { · line 577
Quoted 6 lines
hw/net/igb.c +6
-0+return true; + } + } + +return false; +}
Re: [PATCH v4 2/7] igb: Add VMState support for TRL state
In reply to Josh HilkeQuoted 14 lines · 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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)hw/net/igb.c +0
-0
hw/net/igb.c static const VMStateDescription igb_vmstate_intr_timer = { · line 571
Quoted 9 lines
hw/net/igb.c +6
-0@@ -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 (igb_trl_enabled (s->core.trl[i].trlrc)) {
It will lose bits else E1000_TRLRC_RS_ENA when E1000_TRLRC_RS_ENA is not set. It should return true when any bit is set.
Regards,
Akihiko Odaki
Quoted 48 lines
hw/net/igb.c +37
-0+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 } };
PATCH v4 3/7 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 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)
hw/net/igb_core.c +56 -1
@@ -2137,11 +2137,27 @@ igb_set_phy_ctrl(IGBCore *core, uint16_t val) } } +static void igb_trl_disable_queue (IGBCore *core,int qidx) +{ + core->trl[qidx].trlrc =0 ; + +timer_del (core->trl[qidx].timer); +} + +static void igb_trl_disable (IGBCore *core) +{ +for (int i =0 ; i <IGB_NUM_QUEUES ; i++) { +igb_trl_disable_queue (core, i); + } +} +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) {
@@ -2624,6 +2640,42 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)*/ } +static uint32_t igb_get_trlrc (IGBCore *core,int index) +{ +int qidx = core->mac[TRLDQSEL ]; + +return core->trl[qidx].trlrc; +} + +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; + + qidx = core->mac[TRLDQSEL ]; + core->trl[qidx].trlrc = val; + +if (!igb_trl_enabled (val)) { + goto disable; + } + + rf_scaled =igb_trl_get_scaled_rate_factor (val); + +/* Zero is not a valid rate factor, treat as disabled */ +if (rf_scaled ==0 ) { + goto disable; + } +return ; + +disable: +igb_trl_disable_queue (core, qidx); +} +static inline void igb_autoneg_timer (void *opaque) {
@@ -3679,6 +3731,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) };
@@ -4127,7 +4180,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 v4 3/7] igb: Implement TRL register write and configuration
Quoted 13 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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_core.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)
hw/net/igb_core.c static const writeops igb_macreg_writeops[] = { · line 4185
Quoted 7 lines
hw/net/igb_core.c +3
-1@@ -4127,7 +4180,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
Please add a trailing comma to avoid the same code churn in the future ;)
Quoted 4 lines
hw/net/igb_core.c +0
-0};enum {IGB_NWRITEOPS =ARRAY_SIZE (igb_macreg_writeops) };
Re: [PATCH v4 3/7] igb: Implement TRL register write and configuration
In reply to Josh HilkeQuoted 71 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.
Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_core.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-)hw/net/igb_core.c +40
-0@@ -2137,11 +2137,27 @@ igb_set_phy_ctrl(IGBCore *core, uint16_t val) } } +static void igb_trl_disable_queue (IGBCore *core,int qidx) +{ + core->trl[qidx].trlrc =0 ; + +timer_del (core->trl[qidx].timer); +} + +static void igb_trl_disable (IGBCore *core) +{ +for (int i =0 ; i <IGB_NUM_QUEUES ; i++) { +igb_trl_disable_queue (core, i); + } +} +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) {@@ -2624,6 +2640,42 @@ static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)*/ } +static uint32_t igb_get_trlrc (IGBCore *core,int index) +{ +int qidx = core->mac[TRLDQSEL ]; + +return core->trl[qidx].trlrc; +} + +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; + + qidx = core->mac[TRLDQSEL ]; + core->trl[qidx].trlrc = val; + +if (!igb_trl_enabled (val)) { + goto disable; + } +
hw/net/igb_core.c static void igb_set_vtivar(IGBCore *core, int index, uint32_t val) · line 2676
Quoted 10 lines
hw/net/igb_core.c +10
-0+ rf_scaled =igb_trl_get_scaled_rate_factor (val); + +/* Zero is not a valid rate factor, treat as disabled */ +if (rf_scaled ==0 ) { + goto disable; + } +return ; + +disable: +igb_trl_disable_queue (core, qidx);
This behavior of overwriting TRLRC and dropping the pending work is not specified in the datasheet and extraneous.
Regards,
Akihiko Odaki
Quoted 25 lines
hw/net/igb_core.c +6
-1+} +static inline void igb_autoneg_timer (void *opaque) {@@ -3679,6 +3731,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) };@@ -4127,7 +4180,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 v4 4/7 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
@@ -600,7 +600,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,
@@ -633,10 +633,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 */
@@ -655,11 +655,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) &&
@@ -676,7 +678,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
Re: [PATCH v4 4/7] igb: Refactor TX path to return processed byte count
Quoted 9 lines · Josh Hilke
Modify
igb_process_tx_descto 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.Signed-off-by Josh Hilke <[email protected]>--- hw/net/igb_core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
PATCH v4 5/7 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 | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
hw/net/igb_core.c +39 -3
@@ -876,6 +876,12 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) (core->mac[TXDCTL0 + (qn *16 )] &E1000_TXDCTL_QUEUE_ENABLE ); } +static uint64_t igb_trl_calculate_delay (uint64_t target_rate, +size_t bytes_sent) +{ +return (uint64_t )bytes_sent *NANOSECONDS_PER_SECOND / target_rate; +} +static void igb_start_xmit (IGBCore *core,const IGB_TxRing *txr) {
@@ -884,12 +890,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 target_rate; +uint64_t delay_ns;if (!igb_tx_enabled (core, txi)) {trace_e1000e_tx_disabled ();return ; } +if (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;
@@ -903,9 +916,19 @@ 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); + + target_rate =igb_trl_get_target_rate (&core->trl[txi->idx]); +if (target_rate >0 ) { + delay_ns =igb_trl_calculate_delay (target_rate, 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) {
@@ -2143,9 +2166,16 @@ igb_set_phy_ctrl(IGBCore *core, uint16_t val)static void igb_trl_disable_queue (IGBCore *core,int qidx) { +IGB_TxRing txr; + core->trl[qidx].trlrc =0 ; -timer_del (core->trl[qidx].timer); +if (timer_pending (core->trl[qidx].timer)) { +timer_del (core->trl[qidx].timer); +/* Flush the queue of any pending packets */ +igb_tx_ring_init (core, &txr, qidx); +igb_start_xmit (core, &txr); + } }static void igb_trl_disable (IGBCore *core)
@@ -4386,7 +4416,13 @@ uint64_t igb_trl_get_target_rate(const IGBTrlQueue *q)static void igb_trl_timer_cb (void *opaque) { -/* Stub */ +IGBTrlQueue *trl = opaque; +IGBCore *core = trl->core; +int qidx = trl->queue_idx; +IGB_TxRing txr; + +igb_tx_ring_init (core, &txr, qidx); +igb_start_xmit (core, &txr); }void
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v4 5/7] igb: Implement TRL throttling and timer resumption
Hi Josh,
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 | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)hw/net/igb_core.c +0
-0
hw/net/igb_core.c igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) · line 882
Quoted 7 lines
hw/net/igb_core.c +4
-0@@ -876,6 +876,12 @@ igb_tx_enabled(IGBCore *core, const E1000ERingInfo *txi) (core->mac[TXDCTL0 + (qn *16 )] &E1000_TXDCTL_QUEUE_ENABLE ); } +static uint64_t igb_trl_calculate_delay (uint64_t target_rate, +size_t bytes_sent) +{ +return (uint64_t )bytes_sent *NANOSECONDS_PER_SECOND / target_rate;
Please use the safer muldiv64() helper. Rest LGTM.
Quoted 1 line
hw/net/igb_core.c +1
-0+}
PATCH v4 6/7 igb: Add QTest for Transmit Rate Limiter register multiplexing
In reply to Josh HilkeAdd 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
@@ -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.54.0.1136.gdb2ca164c4-goog
PATCH v4 7/7 igb: Add QTest for Transmit Rate Limiter traffic throttling
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 | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+)
tests/qtest/igb-test.c +158 -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
@@ -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 *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 */
@@ -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.54.0.1136.gdb2ca164c4-goog