Skip to content

Commit

Permalink
bpf: check for little endian CPU for OS VM comparison
Browse files Browse the repository at this point in the history
When I wrote these tests, I assumed the native endianness for the machine
was little endian. Explicitly check this so that the emulated BPF VM tests
can run on s390x, but we avoid test flakes related to endianness.

Updates golang/go#55235.

Change-Id: I9be430dfe7f97503af7a620ed80dcbacb66d73cc
Reviewed-on: https://go-review.googlesource.com/c/net/+/501155
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
  • Loading branch information
mdlayher authored and gopherbot committed Jun 6, 2023
1 parent 10cf388 commit 2796e09
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions bpf/vm_bpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/net/nettest"
"golang.org/x/sys/cpu"
)

// A virtualMachine is a BPF virtual machine which can process an
Expand All @@ -22,18 +23,6 @@ type virtualMachine interface {
Run(in []byte) (int, error)
}

// canUseOSVM indicates if the OS BPF VM is available on this platform.
func canUseOSVM() bool {
// OS BPF VM can only be used on platforms where x/net/ipv4 supports
// attaching a BPF program to a socket.
switch runtime.GOOS {
case "linux":
return true
}

return false
}

// All BPF tests against both the Go VM and OS VM are assumed to
// be used with a UDP socket. As a result, the entire contents
// of a UDP datagram is sent through the BPF program, but only
Expand All @@ -55,11 +44,10 @@ func testVM(t *testing.T, filter []bpf.Instruction) (virtualMachine, func(), err
t: t,
}

// If available, add the OS VM for tests which verify that both the Go
// VM and OS VM have exactly the same output for the same input program
// and packet.
// For linux with a little endian CPU, the Go VM and OS VM have exactly the
// same output for the same input program and packet. Compare both.
done := func() {}
if canUseOSVM() {
if runtime.GOOS == "linux" && !cpu.IsBigEndian {
osVM, osVMDone := testOSVM(t, filter)
done = func() { osVMDone() }
mvm.osVM = osVM
Expand Down

0 comments on commit 2796e09

Please sign in to comment.