summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/link/rawfile
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-03-03 10:23:55 -0800
committergVisor bot <gvisor-bot@google.com>2021-03-03 10:25:58 -0800
commita9441aea2780da8c93da1c73da860219f98438de (patch)
tree8b12915756f5bfb926218214cd7bc0b3281605fd /pkg/tcpip/link/rawfile
parentb8a5420f49a2afd622ec08b5019e1bf537f7da82 (diff)
[op] Replace syscall package usage with golang.org/x/sys/unix in pkg/.
The syscall package has been deprecated in favor of golang.org/x/sys. Note that syscall is still used in the following places: - pkg/sentry/socket/hostinet/stack.go: some netlink related functionalities are not yet available in golang.org/x/sys. - syscall.Stat_t is still used in some places because os.FileInfo.Sys() still returns it and not unix.Stat_t. Updates #214 PiperOrigin-RevId: 360701387
Diffstat (limited to 'pkg/tcpip/link/rawfile')
-rw-r--r--pkg/tcpip/link/rawfile/BUILD1
-rw-r--r--pkg/tcpip/link/rawfile/blockingpoll_noyield_unsafe.go7
-rw-r--r--pkg/tcpip/link/rawfile/blockingpoll_yield_unsafe.go5
-rw-r--r--pkg/tcpip/link/rawfile/errors.go45
-rw-r--r--pkg/tcpip/link/rawfile/errors_test.go12
-rw-r--r--pkg/tcpip/link/rawfile/rawfile_unsafe.go33
6 files changed, 52 insertions, 51 deletions
diff --git a/pkg/tcpip/link/rawfile/BUILD b/pkg/tcpip/link/rawfile/BUILD
index e1047da50..4efd7c45e 100644
--- a/pkg/tcpip/link/rawfile/BUILD
+++ b/pkg/tcpip/link/rawfile/BUILD
@@ -28,5 +28,6 @@ go_test(
deps = [
"//pkg/tcpip",
"@com_github_google_go_cmp//cmp:go_default_library",
+ "@org_golang_x_sys//unix:go_default_library",
],
)
diff --git a/pkg/tcpip/link/rawfile/blockingpoll_noyield_unsafe.go b/pkg/tcpip/link/rawfile/blockingpoll_noyield_unsafe.go
index 621ab8d29..2206fe0e6 100644
--- a/pkg/tcpip/link/rawfile/blockingpoll_noyield_unsafe.go
+++ b/pkg/tcpip/link/rawfile/blockingpoll_noyield_unsafe.go
@@ -17,14 +17,15 @@
package rawfile
import (
- "syscall"
"unsafe"
+
+ "golang.org/x/sys/unix"
)
// BlockingPoll is just a stub function that forwards to the ppoll() system call
// on non-amd64 and non-arm64 platforms.
-func BlockingPoll(fds *PollEvent, nfds int, timeout *syscall.Timespec) (int, syscall.Errno) {
- n, _, e := syscall.Syscall6(syscall.SYS_PPOLL, uintptr(unsafe.Pointer(fds)),
+func BlockingPoll(fds *PollEvent, nfds int, timeout *unix.Timespec) (int, unix.Errno) {
+ n, _, e := unix.Syscall6(unix.SYS_PPOLL, uintptr(unsafe.Pointer(fds)),
uintptr(nfds), uintptr(unsafe.Pointer(timeout)), 0, 0, 0)
return int(n), e
diff --git a/pkg/tcpip/link/rawfile/blockingpoll_yield_unsafe.go b/pkg/tcpip/link/rawfile/blockingpoll_yield_unsafe.go
index 7f238102e..5002245a1 100644
--- a/pkg/tcpip/link/rawfile/blockingpoll_yield_unsafe.go
+++ b/pkg/tcpip/link/rawfile/blockingpoll_yield_unsafe.go
@@ -21,8 +21,9 @@
package rawfile
import (
- "syscall"
_ "unsafe" // for go:linkname
+
+ "golang.org/x/sys/unix"
)
// BlockingPoll on amd64/arm64 makes the ppoll() syscall while calling the
@@ -32,7 +33,7 @@ import (
// call.
//
//go:noescape
-func BlockingPoll(fds *PollEvent, nfds int, timeout *syscall.Timespec) (int, syscall.Errno)
+func BlockingPoll(fds *PollEvent, nfds int, timeout *unix.Timespec) (int, unix.Errno)
// Use go:linkname to call into the runtime. As of Go 1.12 this has to
// be done from Go code so that we make an ABIInternal call to an
diff --git a/pkg/tcpip/link/rawfile/errors.go b/pkg/tcpip/link/rawfile/errors.go
index 406b97709..9743e70ea 100644
--- a/pkg/tcpip/link/rawfile/errors.go
+++ b/pkg/tcpip/link/rawfile/errors.go
@@ -17,8 +17,7 @@
package rawfile
import (
- "syscall"
-
+ "golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip"
)
@@ -29,47 +28,47 @@ const maxErrno = 134
//
// Valid, but unrecognized errnos will be translated to
// *tcpip.ErrInvalidEndpointState (EINVAL).
-func TranslateErrno(e syscall.Errno) tcpip.Error {
+func TranslateErrno(e unix.Errno) tcpip.Error {
switch e {
- case syscall.EEXIST:
+ case unix.EEXIST:
return &tcpip.ErrDuplicateAddress{}
- case syscall.ENETUNREACH:
+ case unix.ENETUNREACH:
return &tcpip.ErrNoRoute{}
- case syscall.EINVAL:
+ case unix.EINVAL:
return &tcpip.ErrInvalidEndpointState{}
- case syscall.EALREADY:
+ case unix.EALREADY:
return &tcpip.ErrAlreadyConnecting{}
- case syscall.EISCONN:
+ case unix.EISCONN:
return &tcpip.ErrAlreadyConnected{}
- case syscall.EADDRINUSE:
+ case unix.EADDRINUSE:
return &tcpip.ErrPortInUse{}
- case syscall.EADDRNOTAVAIL:
+ case unix.EADDRNOTAVAIL:
return &tcpip.ErrBadLocalAddress{}
- case syscall.EPIPE:
+ case unix.EPIPE:
return &tcpip.ErrClosedForSend{}
- case syscall.EWOULDBLOCK:
+ case unix.EWOULDBLOCK:
return &tcpip.ErrWouldBlock{}
- case syscall.ECONNREFUSED:
+ case unix.ECONNREFUSED:
return &tcpip.ErrConnectionRefused{}
- case syscall.ETIMEDOUT:
+ case unix.ETIMEDOUT:
return &tcpip.ErrTimeout{}
- case syscall.EINPROGRESS:
+ case unix.EINPROGRESS:
return &tcpip.ErrConnectStarted{}
- case syscall.EDESTADDRREQ:
+ case unix.EDESTADDRREQ:
return &tcpip.ErrDestinationRequired{}
- case syscall.ENOTSUP:
+ case unix.ENOTSUP:
return &tcpip.ErrNotSupported{}
- case syscall.ENOTTY:
+ case unix.ENOTTY:
return &tcpip.ErrQueueSizeNotSupported{}
- case syscall.ENOTCONN:
+ case unix.ENOTCONN:
return &tcpip.ErrNotConnected{}
- case syscall.ECONNRESET:
+ case unix.ECONNRESET:
return &tcpip.ErrConnectionReset{}
- case syscall.ECONNABORTED:
+ case unix.ECONNABORTED:
return &tcpip.ErrConnectionAborted{}
- case syscall.EMSGSIZE:
+ case unix.EMSGSIZE:
return &tcpip.ErrMessageTooLong{}
- case syscall.ENOBUFS:
+ case unix.ENOBUFS:
return &tcpip.ErrNoBufferSpace{}
default:
return &tcpip.ErrInvalidEndpointState{}
diff --git a/pkg/tcpip/link/rawfile/errors_test.go b/pkg/tcpip/link/rawfile/errors_test.go
index 61aea1744..8f4bd60da 100644
--- a/pkg/tcpip/link/rawfile/errors_test.go
+++ b/pkg/tcpip/link/rawfile/errors_test.go
@@ -17,32 +17,32 @@
package rawfile
import (
- "syscall"
"testing"
"github.com/google/go-cmp/cmp"
+ "golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip"
)
func TestTranslateErrno(t *testing.T) {
for _, test := range []struct {
- errno syscall.Errno
+ errno unix.Errno
translated tcpip.Error
}{
{
- errno: syscall.Errno(0),
+ errno: unix.Errno(0),
translated: &tcpip.ErrInvalidEndpointState{},
},
{
- errno: syscall.Errno(maxErrno),
+ errno: unix.Errno(maxErrno),
translated: &tcpip.ErrInvalidEndpointState{},
},
{
- errno: syscall.Errno(514),
+ errno: unix.Errno(514),
translated: &tcpip.ErrInvalidEndpointState{},
},
{
- errno: syscall.EEXIST,
+ errno: unix.EEXIST,
translated: &tcpip.ErrDuplicateAddress{},
},
} {
diff --git a/pkg/tcpip/link/rawfile/rawfile_unsafe.go b/pkg/tcpip/link/rawfile/rawfile_unsafe.go
index 06f3ee21e..ba92aedbc 100644
--- a/pkg/tcpip/link/rawfile/rawfile_unsafe.go
+++ b/pkg/tcpip/link/rawfile/rawfile_unsafe.go
@@ -19,7 +19,6 @@
package rawfile
import (
- "syscall"
"unsafe"
"golang.org/x/sys/unix"
@@ -28,12 +27,12 @@ import (
// GetMTU determines the MTU of a network interface device.
func GetMTU(name string) (uint32, error) {
- fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
+ fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_DGRAM, 0)
if err != nil {
return 0, err
}
- defer syscall.Close(fd)
+ defer unix.Close(fd)
var ifreq struct {
name [16]byte
@@ -42,7 +41,7 @@ func GetMTU(name string) (uint32, error) {
}
copy(ifreq.name[:], name)
- _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifreq)))
+ _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), unix.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifreq)))
if errno != 0 {
return 0, errno
}
@@ -58,7 +57,7 @@ func NonBlockingWrite(fd int, buf []byte) tcpip.Error {
ptr = unsafe.Pointer(&buf[0])
}
- _, _, e := syscall.RawSyscall(syscall.SYS_WRITE, uintptr(fd), uintptr(ptr), uintptr(len(buf)))
+ _, _, e := unix.RawSyscall(unix.SYS_WRITE, uintptr(fd), uintptr(ptr), uintptr(len(buf)))
if e != 0 {
return TranslateErrno(e)
}
@@ -66,11 +65,11 @@ func NonBlockingWrite(fd int, buf []byte) tcpip.Error {
return nil
}
-// NonBlockingWriteIovec writes iovec to a file descriptor in a single syscall.
+// NonBlockingWriteIovec writes iovec to a file descriptor in a single unix.
// It fails if partial data is written.
-func NonBlockingWriteIovec(fd int, iovec []syscall.Iovec) tcpip.Error {
+func NonBlockingWriteIovec(fd int, iovec []unix.Iovec) tcpip.Error {
iovecLen := uintptr(len(iovec))
- _, _, e := syscall.RawSyscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen)
+ _, _, e := unix.RawSyscall(unix.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen)
if e != 0 {
return TranslateErrno(e)
}
@@ -79,7 +78,7 @@ func NonBlockingWriteIovec(fd int, iovec []syscall.Iovec) tcpip.Error {
// NonBlockingSendMMsg sends multiple messages on a socket.
func NonBlockingSendMMsg(fd int, msgHdrs []MMsgHdr) (int, tcpip.Error) {
- n, _, e := syscall.RawSyscall6(unix.SYS_SENDMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), syscall.MSG_DONTWAIT, 0, 0)
+ n, _, e := unix.RawSyscall6(unix.SYS_SENDMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), unix.MSG_DONTWAIT, 0, 0)
if e != 0 {
return 0, TranslateErrno(e)
}
@@ -99,7 +98,7 @@ type PollEvent struct {
// descriptor becomes readable.
func BlockingRead(fd int, b []byte) (int, tcpip.Error) {
for {
- n, _, e := syscall.RawSyscall(syscall.SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
+ n, _, e := unix.RawSyscall(unix.SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
if e == 0 {
return int(n), nil
}
@@ -110,7 +109,7 @@ func BlockingRead(fd int, b []byte) (int, tcpip.Error) {
}
_, e = BlockingPoll(&event, 1, nil)
- if e != 0 && e != syscall.EINTR {
+ if e != 0 && e != unix.EINTR {
return 0, TranslateErrno(e)
}
}
@@ -119,9 +118,9 @@ func BlockingRead(fd int, b []byte) (int, tcpip.Error) {
// BlockingReadv reads from a file descriptor that is set up as non-blocking and
// stores the data in a list of iovecs buffers. If no data is available, it will
// block in a poll() syscall until the file descriptor becomes readable.
-func BlockingReadv(fd int, iovecs []syscall.Iovec) (int, tcpip.Error) {
+func BlockingReadv(fd int, iovecs []unix.Iovec) (int, tcpip.Error) {
for {
- n, _, e := syscall.RawSyscall(syscall.SYS_READV, uintptr(fd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
+ n, _, e := unix.RawSyscall(unix.SYS_READV, uintptr(fd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
if e == 0 {
return int(n), nil
}
@@ -132,7 +131,7 @@ func BlockingReadv(fd int, iovecs []syscall.Iovec) (int, tcpip.Error) {
}
_, e = BlockingPoll(&event, 1, nil)
- if e != 0 && e != syscall.EINTR {
+ if e != 0 && e != unix.EINTR {
return 0, TranslateErrno(e)
}
}
@@ -140,7 +139,7 @@ func BlockingReadv(fd int, iovecs []syscall.Iovec) (int, tcpip.Error) {
// MMsgHdr represents the mmsg_hdr structure required by recvmmsg() on linux.
type MMsgHdr struct {
- Msg syscall.Msghdr
+ Msg unix.Msghdr
Len uint32
_ [4]byte
}
@@ -151,7 +150,7 @@ type MMsgHdr struct {
// becomes readable.
func BlockingRecvMMsg(fd int, msgHdrs []MMsgHdr) (int, tcpip.Error) {
for {
- n, _, e := syscall.RawSyscall6(syscall.SYS_RECVMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), syscall.MSG_DONTWAIT, 0, 0)
+ n, _, e := unix.RawSyscall6(unix.SYS_RECVMMSG, uintptr(fd), uintptr(unsafe.Pointer(&msgHdrs[0])), uintptr(len(msgHdrs)), unix.MSG_DONTWAIT, 0, 0)
if e == 0 {
return int(n), nil
}
@@ -161,7 +160,7 @@ func BlockingRecvMMsg(fd int, msgHdrs []MMsgHdr) (int, tcpip.Error) {
Events: 1, // POLLIN
}
- if _, e := BlockingPoll(&event, 1, nil); e != 0 && e != syscall.EINTR {
+ if _, e := BlockingPoll(&event, 1, nil); e != 0 && e != unix.EINTR {
return 0, TranslateErrno(e)
}
}