Skip to content

Commit

Permalink
fixup! conn, device, tun: implement vectorized I/O on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhited authored Dec 15, 2022
1 parent f385d0c commit 439157b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 25 deletions.
8 changes: 0 additions & 8 deletions endian/big.go

This file was deleted.

8 changes: 0 additions & 8 deletions endian/little.go

This file was deleted.

11 changes: 11 additions & 0 deletions native/doc.go
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions native/endian_big.go
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions native/endian_generic.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions native/endian_little.go
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions native/endian_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 7 additions & 0 deletions native/license
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 9 additions & 9 deletions tun/tcp_offload_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

1 comment on commit 439157b

@zx2c4
Copy link
Member

@zx2c4 zx2c4 commented on 439157b Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss my comment about how I think this package is unnecessary?

#64 (comment)

Please sign in to comment.