Skip to content

Commit 439157b

Browse files
authored
fixup! conn, device, tun: implement vectorized I/O on Linux
1 parent f385d0c commit 439157b

File tree

9 files changed

+106
-25
lines changed

9 files changed

+106
-25
lines changed

Diff for: endian/big.go

-8
This file was deleted.

Diff for: endian/little.go

-8
This file was deleted.

Diff for: native/doc.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Package native provides easy access to native byte order.
2+
//
3+
// Usage: use native.Endian where you need the native binary.ByteOrder.
4+
//
5+
// Please think twice before using this package.
6+
// It can break program portability.
7+
// Native byte order is usually not the right answer.
8+
// This package is a drop-in of github.com/josharian/native, with attribution in
9+
// the file 'license'. This package will likely be replaced by something similar
10+
// in the standard library, see https://github.com/golang/go/issues/57237.
11+
package native

Diff for: native/endian_big.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build mips || mips64 || ppc64 || s390x
2+
// +build mips mips64 ppc64 s390x
3+
4+
package native
5+
6+
import "encoding/binary"
7+
8+
// Endian is the encoding/binary.ByteOrder implementation for the
9+
// current CPU's native byte order.
10+
var Endian = binary.BigEndian
11+
12+
// IsBigEndian is whether the current CPU's native byte order is big
13+
// endian.
14+
const IsBigEndian = true

Diff for: native/endian_generic.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build !mips && !mips64 && !ppc64 && !s390x && !amd64 && !386 && !arm && !arm64 && !loong64 && !mipsle && !mips64le && !ppc64le && !riscv64 && !wasm
2+
// +build !mips,!mips64,!ppc64,!s390x,!amd64,!386,!arm,!arm64,!loong64,!mipsle,!mips64le,!ppc64le,!riscv64,!wasm
3+
4+
// This file is a fallback, so that package native doesn't break
5+
// the instant the Go project adds support for a new architecture.
6+
//
7+
8+
package native
9+
10+
import (
11+
"encoding/binary"
12+
"log"
13+
"runtime"
14+
"unsafe"
15+
)
16+
17+
var Endian binary.ByteOrder
18+
19+
var IsBigEndian bool
20+
21+
func init() {
22+
b := uint16(0xff) // one byte
23+
if *(*byte)(unsafe.Pointer(&b)) == 0 {
24+
Endian = binary.BigEndian
25+
IsBigEndian = true
26+
} else {
27+
Endian = binary.LittleEndian
28+
IsBigEndian = false
29+
}
30+
log.Printf("unrecognized arch %v (%v), please file an issue", runtime.GOARCH, Endian)
31+
}

Diff for: native/endian_little.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build amd64 || 386 || arm || arm64 || loong64 || mipsle || mips64le || ppc64le || riscv64 || wasm
2+
// +build amd64 386 arm arm64 loong64 mipsle mips64le ppc64le riscv64 wasm
3+
4+
package native
5+
6+
import "encoding/binary"
7+
8+
// Endian is the encoding/binary.ByteOrder implementation for the
9+
// current CPU's native byte order.
10+
var Endian = binary.LittleEndian
11+
12+
// IsBigEndian is whether the current CPU's native byte order is big
13+
// endian.
14+
const IsBigEndian = false

Diff for: native/endian_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package native_test
2+
3+
import (
4+
"encoding/binary"
5+
"testing"
6+
7+
"golang.zx2c4.com/wireguard/native"
8+
)
9+
10+
func TestPrintEndianness(t *testing.T) {
11+
t.Logf("native endianness is %v", native.Endian)
12+
13+
var want binary.ByteOrder = binary.BigEndian
14+
if !native.IsBigEndian {
15+
want = binary.LittleEndian
16+
}
17+
if native.Endian != want {
18+
t.Errorf("IsBigEndian = %v not consistent with native.Endian = %T", native.IsBigEndian, want)
19+
}
20+
}

Diff for: native/license

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Josh Bleecher Snyder
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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 for: tun/tcp_offload_linux.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"golang.org/x/sys/unix"
1010
"golang.zx2c4.com/wireguard/conn"
11-
"golang.zx2c4.com/wireguard/endian"
11+
"golang.zx2c4.com/wireguard/native"
1212
)
1313

1414
// virtioNetHdrFlags are defined in the kernel in include/uapi/linux/virtio_net.h.
@@ -52,10 +52,10 @@ func (v *virtioNetHdr) decode(b []byte) error {
5252
}
5353
v.flags = b[0]
5454
v.gsoType = b[1]
55-
v.hdrLen = endian.Native.Uint16(b[2:])
56-
v.gsoSize = endian.Native.Uint16(b[4:])
57-
v.csumStart = endian.Native.Uint16(b[6:])
58-
v.csumOffset = endian.Native.Uint16(b[8:])
55+
v.hdrLen = native.Endian.Uint16(b[2:])
56+
v.gsoSize = native.Endian.Uint16(b[4:])
57+
v.csumStart = native.Endian.Uint16(b[6:])
58+
v.csumOffset = native.Endian.Uint16(b[8:])
5959
return nil
6060
}
6161

@@ -65,10 +65,10 @@ func (v *virtioNetHdr) encode(b []byte) error {
6565
}
6666
b[0] = v.flags
6767
b[1] = v.gsoType
68-
endian.Native.PutUint16(b[2:], v.hdrLen)
69-
endian.Native.PutUint16(b[4:], v.gsoSize)
70-
endian.Native.PutUint16(b[6:], v.csumStart)
71-
endian.Native.PutUint16(b[8:], v.csumOffset)
68+
native.Endian.PutUint16(b[2:], v.hdrLen)
69+
native.Endian.PutUint16(b[4:], v.gsoSize)
70+
native.Endian.PutUint16(b[6:], v.csumStart)
71+
native.Endian.PutUint16(b[8:], v.csumOffset)
7272
return nil
7373
}
7474

0 commit comments

Comments
 (0)