From 4547e2c0492276373c33d183464b5dfa926aa69b Mon Sep 17 00:00:00 2001 From: Dionna Glaze Date: Tue, 16 May 2023 23:46:49 +0000 Subject: [PATCH] Make ratelimiting flag-configurable, set burst to 1 Testing shows that burst=2 still can lead to VMPCK0 deletion. Make these configurable with flags so we can tweak values without requiring new releases. Signed-off-by: Dionna Glaze --- client/client_linux.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/client/client_linux.go b/client/client_linux.go index 1853e8c..b4a4393 100644 --- a/client/client_linux.go +++ b/client/client_linux.go @@ -18,6 +18,7 @@ package client import ( + "flag" "fmt" "time" @@ -28,8 +29,13 @@ import ( const ( // defaultSevGuestDevicePath is the platform's usual device path to the SEV guest. defaultSevGuestDevicePath = "/dev/sev-guest" - throttleDuration = 2 * time.Second - burstMax = 2 +) + +// These flags should not be needed for long term health of the project as the Linux kernel +// catches up with throttling-awareness. +var ( + throttleDuration = flag.Duration("self_throttle_duration", 2*time.Second, "Rate-limit library-initiated device commands to this duration") + burstMax = flag.Int("self_throttle_burst", 1, "Rate-limit library-initiated device commands to this many commands per duration") ) // LinuxDevice implements the Device interface with Linux ioctls. @@ -83,8 +89,8 @@ func (d *LinuxDevice) Ioctl(command uintptr, req any) (uintptr, error) { if d.burst == 0 { sinceLast := time.Since(d.lastCmd) // Self-throttle for tests without guest OS throttle detection - if sinceLast < throttleDuration { - time.Sleep(throttleDuration - sinceLast) + if sinceLast < *throttleDuration { + time.Sleep(*throttleDuration - sinceLast) } } switch sreq := req.(type) { @@ -92,7 +98,7 @@ func (d *LinuxDevice) Ioctl(command uintptr, req any) (uintptr, error) { abi := sreq.ABI() result, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(d.fd), command, uintptr(abi.Pointer())) abi.Finish(sreq) - d.burst = (d.burst + 1) % burstMax + d.burst = (d.burst + 1) % *burstMax if d.burst == 0 { d.lastCmd = time.Now() }