From 5bca143dc23e8eecdc6727a2262c68f15b2ae67b Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Wed, 14 Dec 2022 17:02:04 -0800 Subject: [PATCH] fixup! conn, device, tun: implement vectorized I/O on Linux --- endian/big.go | 8 -------- endian/little.go | 8 -------- native/doc.go | 11 +++++++++++ native/endian_big.go | 14 ++++++++++++++ native/endian_generic.go | 31 +++++++++++++++++++++++++++++++ native/endian_little.go | 14 ++++++++++++++ native/endian_test.go | 20 ++++++++++++++++++++ native/license | 7 +++++++ tun/tcp_offload_linux.go | 18 +++++++++--------- 9 files changed, 106 insertions(+), 25 deletions(-) delete mode 100644 endian/big.go delete mode 100644 endian/little.go create mode 100644 native/doc.go create mode 100644 native/endian_big.go create mode 100644 native/endian_generic.go create mode 100644 native/endian_little.go create mode 100644 native/endian_test.go create mode 100644 native/license diff --git a/endian/big.go b/endian/big.go deleted file mode 100644 index b53035d6e..000000000 --- a/endian/big.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build mips || mips64 || ppc64 || s390x - -package endian - -import "encoding/binary" - -// Native is the platform's native byte order. -var Native = binary.LittleEndian diff --git a/endian/little.go b/endian/little.go deleted file mode 100644 index e1090071b..000000000 --- a/endian/little.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build 386 || amd64 || arm || arm64 || mips64le || mipsle || ppc64le || riscv64 || wasm || loong64 - -package endian - -import "encoding/binary" - -// Native is the platform's native byte order. -var Native = binary.LittleEndian diff --git a/native/doc.go b/native/doc.go new file mode 100644 index 000000000..d73d19b70 --- /dev/null +++ b/native/doc.go @@ -0,0 +1,11 @@ +// Package native provides easy access to native byte order. +// +// Usage: use native.Endian where you need the native binary.ByteOrder. +// +// Please think twice before using this package. +// It can break program portability. +// Native byte order is usually not the right answer. +// This package is a drop-in of github.com/josharian/native, with attribution in +// the file 'license'. This package will likely be replaced by something similar +// in the standard library, see https://github.com/golang/go/issues/57237. +package native diff --git a/native/endian_big.go b/native/endian_big.go new file mode 100644 index 000000000..77744fdd4 --- /dev/null +++ b/native/endian_big.go @@ -0,0 +1,14 @@ +//go:build mips || mips64 || ppc64 || s390x +// +build mips mips64 ppc64 s390x + +package native + +import "encoding/binary" + +// Endian is the encoding/binary.ByteOrder implementation for the +// current CPU's native byte order. +var Endian = binary.BigEndian + +// IsBigEndian is whether the current CPU's native byte order is big +// endian. +const IsBigEndian = true diff --git a/native/endian_generic.go b/native/endian_generic.go new file mode 100644 index 000000000..d2a92c3e6 --- /dev/null +++ b/native/endian_generic.go @@ -0,0 +1,31 @@ +//go:build !mips && !mips64 && !ppc64 && !s390x && !amd64 && !386 && !arm && !arm64 && !loong64 && !mipsle && !mips64le && !ppc64le && !riscv64 && !wasm +// +build !mips,!mips64,!ppc64,!s390x,!amd64,!386,!arm,!arm64,!loong64,!mipsle,!mips64le,!ppc64le,!riscv64,!wasm + +// This file is a fallback, so that package native doesn't break +// the instant the Go project adds support for a new architecture. +// + +package native + +import ( + "encoding/binary" + "log" + "runtime" + "unsafe" +) + +var Endian binary.ByteOrder + +var IsBigEndian bool + +func init() { + b := uint16(0xff) // one byte + if *(*byte)(unsafe.Pointer(&b)) == 0 { + Endian = binary.BigEndian + IsBigEndian = true + } else { + Endian = binary.LittleEndian + IsBigEndian = false + } + log.Printf("unrecognized arch %v (%v), please file an issue", runtime.GOARCH, Endian) +} diff --git a/native/endian_little.go b/native/endian_little.go new file mode 100644 index 000000000..5098fec26 --- /dev/null +++ b/native/endian_little.go @@ -0,0 +1,14 @@ +//go:build amd64 || 386 || arm || arm64 || loong64 || mipsle || mips64le || ppc64le || riscv64 || wasm +// +build amd64 386 arm arm64 loong64 mipsle mips64le ppc64le riscv64 wasm + +package native + +import "encoding/binary" + +// Endian is the encoding/binary.ByteOrder implementation for the +// current CPU's native byte order. +var Endian = binary.LittleEndian + +// IsBigEndian is whether the current CPU's native byte order is big +// endian. +const IsBigEndian = false diff --git a/native/endian_test.go b/native/endian_test.go new file mode 100644 index 000000000..fc01a4ec9 --- /dev/null +++ b/native/endian_test.go @@ -0,0 +1,20 @@ +package native_test + +import ( + "encoding/binary" + "testing" + + "golang.zx2c4.com/wireguard/native" +) + +func TestPrintEndianness(t *testing.T) { + t.Logf("native endianness is %v", native.Endian) + + var want binary.ByteOrder = binary.BigEndian + if !native.IsBigEndian { + want = binary.LittleEndian + } + if native.Endian != want { + t.Errorf("IsBigEndian = %v not consistent with native.Endian = %T", native.IsBigEndian, want) + } +} diff --git a/native/license b/native/license new file mode 100644 index 000000000..6e617a9c7 --- /dev/null +++ b/native/license @@ -0,0 +1,7 @@ +Copyright 2020 Josh Bleecher Snyder + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tun/tcp_offload_linux.go b/tun/tcp_offload_linux.go index 4d95b770e..8009d378b 100644 --- a/tun/tcp_offload_linux.go +++ b/tun/tcp_offload_linux.go @@ -8,7 +8,7 @@ import ( "golang.org/x/sys/unix" "golang.zx2c4.com/wireguard/conn" - "golang.zx2c4.com/wireguard/endian" + "golang.zx2c4.com/wireguard/native" ) // virtioNetHdrFlags are defined in the kernel in include/uapi/linux/virtio_net.h. @@ -52,10 +52,10 @@ func (v *virtioNetHdr) decode(b []byte) error { } v.flags = b[0] v.gsoType = b[1] - v.hdrLen = endian.Native.Uint16(b[2:]) - v.gsoSize = endian.Native.Uint16(b[4:]) - v.csumStart = endian.Native.Uint16(b[6:]) - v.csumOffset = endian.Native.Uint16(b[8:]) + v.hdrLen = native.Endian.Uint16(b[2:]) + v.gsoSize = native.Endian.Uint16(b[4:]) + v.csumStart = native.Endian.Uint16(b[6:]) + v.csumOffset = native.Endian.Uint16(b[8:]) return nil } @@ -65,10 +65,10 @@ func (v *virtioNetHdr) encode(b []byte) error { } b[0] = v.flags b[1] = v.gsoType - endian.Native.PutUint16(b[2:], v.hdrLen) - endian.Native.PutUint16(b[4:], v.gsoSize) - endian.Native.PutUint16(b[6:], v.csumStart) - endian.Native.PutUint16(b[8:], v.csumOffset) + native.Endian.PutUint16(b[2:], v.hdrLen) + native.Endian.PutUint16(b[4:], v.gsoSize) + native.Endian.PutUint16(b[6:], v.csumStart) + native.Endian.PutUint16(b[8:], v.csumOffset) return nil }