summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/link/rawfile
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2021-01-29 02:16:39 +0000
committergVisor bot <gvisor-bot@google.com>2021-01-29 02:16:39 +0000
commited0a3c924379121fac5e27c683280d1dae3435ad (patch)
tree8c7dfca68a438040c3eeea47aafa2b3865106c11 /pkg/tcpip/link/rawfile
parenta96f13ff5a34ceba128ce91a0f1926004401859a (diff)
parent8d1afb4185789cce7a90e7dc365e4a7afda9a8fc (diff)
Merge release-20210125.0-21-g8d1afb418 (automated)
Diffstat (limited to 'pkg/tcpip/link/rawfile')
-rw-r--r--pkg/tcpip/link/rawfile/errors.go87
-rw-r--r--pkg/tcpip/link/rawfile/rawfile_unsafe.go12
2 files changed, 52 insertions, 47 deletions
diff --git a/pkg/tcpip/link/rawfile/errors.go b/pkg/tcpip/link/rawfile/errors.go
index 604868fd8..406b97709 100644
--- a/pkg/tcpip/link/rawfile/errors.go
+++ b/pkg/tcpip/link/rawfile/errors.go
@@ -17,7 +17,6 @@
package rawfile
import (
- "fmt"
"syscall"
"gvisor.dev/gvisor/pkg/tcpip"
@@ -25,48 +24,54 @@ import (
const maxErrno = 134
-var translations [maxErrno]*tcpip.Error
-
// TranslateErrno translate an errno from the syscall package into a
-// *tcpip.Error.
+// tcpip.Error.
//
// Valid, but unrecognized errnos will be translated to
-// tcpip.ErrInvalidEndpointState (EINVAL).
-func TranslateErrno(e syscall.Errno) *tcpip.Error {
- if e > 0 && e < syscall.Errno(len(translations)) {
- if err := translations[e]; err != nil {
- return err
- }
- }
- return tcpip.ErrInvalidEndpointState
-}
-
-func addTranslation(host syscall.Errno, trans *tcpip.Error) {
- if translations[host] != nil {
- panic(fmt.Sprintf("duplicate translation for host errno %q (%d)", host.Error(), host))
+// *tcpip.ErrInvalidEndpointState (EINVAL).
+func TranslateErrno(e syscall.Errno) tcpip.Error {
+ switch e {
+ case syscall.EEXIST:
+ return &tcpip.ErrDuplicateAddress{}
+ case syscall.ENETUNREACH:
+ return &tcpip.ErrNoRoute{}
+ case syscall.EINVAL:
+ return &tcpip.ErrInvalidEndpointState{}
+ case syscall.EALREADY:
+ return &tcpip.ErrAlreadyConnecting{}
+ case syscall.EISCONN:
+ return &tcpip.ErrAlreadyConnected{}
+ case syscall.EADDRINUSE:
+ return &tcpip.ErrPortInUse{}
+ case syscall.EADDRNOTAVAIL:
+ return &tcpip.ErrBadLocalAddress{}
+ case syscall.EPIPE:
+ return &tcpip.ErrClosedForSend{}
+ case syscall.EWOULDBLOCK:
+ return &tcpip.ErrWouldBlock{}
+ case syscall.ECONNREFUSED:
+ return &tcpip.ErrConnectionRefused{}
+ case syscall.ETIMEDOUT:
+ return &tcpip.ErrTimeout{}
+ case syscall.EINPROGRESS:
+ return &tcpip.ErrConnectStarted{}
+ case syscall.EDESTADDRREQ:
+ return &tcpip.ErrDestinationRequired{}
+ case syscall.ENOTSUP:
+ return &tcpip.ErrNotSupported{}
+ case syscall.ENOTTY:
+ return &tcpip.ErrQueueSizeNotSupported{}
+ case syscall.ENOTCONN:
+ return &tcpip.ErrNotConnected{}
+ case syscall.ECONNRESET:
+ return &tcpip.ErrConnectionReset{}
+ case syscall.ECONNABORTED:
+ return &tcpip.ErrConnectionAborted{}
+ case syscall.EMSGSIZE:
+ return &tcpip.ErrMessageTooLong{}
+ case syscall.ENOBUFS:
+ return &tcpip.ErrNoBufferSpace{}
+ default:
+ return &tcpip.ErrInvalidEndpointState{}
}
- translations[host] = trans
-}
-
-func init() {
- addTranslation(syscall.EEXIST, tcpip.ErrDuplicateAddress)
- addTranslation(syscall.ENETUNREACH, tcpip.ErrNoRoute)
- addTranslation(syscall.EINVAL, tcpip.ErrInvalidEndpointState)
- addTranslation(syscall.EALREADY, tcpip.ErrAlreadyConnecting)
- addTranslation(syscall.EISCONN, tcpip.ErrAlreadyConnected)
- addTranslation(syscall.EADDRINUSE, tcpip.ErrPortInUse)
- addTranslation(syscall.EADDRNOTAVAIL, tcpip.ErrBadLocalAddress)
- addTranslation(syscall.EPIPE, tcpip.ErrClosedForSend)
- addTranslation(syscall.EWOULDBLOCK, tcpip.ErrWouldBlock)
- addTranslation(syscall.ECONNREFUSED, tcpip.ErrConnectionRefused)
- addTranslation(syscall.ETIMEDOUT, tcpip.ErrTimeout)
- addTranslation(syscall.EINPROGRESS, tcpip.ErrConnectStarted)
- addTranslation(syscall.EDESTADDRREQ, tcpip.ErrDestinationRequired)
- addTranslation(syscall.ENOTSUP, tcpip.ErrNotSupported)
- addTranslation(syscall.ENOTTY, tcpip.ErrQueueSizeNotSupported)
- addTranslation(syscall.ENOTCONN, tcpip.ErrNotConnected)
- addTranslation(syscall.ECONNRESET, tcpip.ErrConnectionReset)
- addTranslation(syscall.ECONNABORTED, tcpip.ErrConnectionAborted)
- addTranslation(syscall.EMSGSIZE, tcpip.ErrMessageTooLong)
- addTranslation(syscall.ENOBUFS, tcpip.ErrNoBufferSpace)
}
diff --git a/pkg/tcpip/link/rawfile/rawfile_unsafe.go b/pkg/tcpip/link/rawfile/rawfile_unsafe.go
index f4c32c2da..06f3ee21e 100644
--- a/pkg/tcpip/link/rawfile/rawfile_unsafe.go
+++ b/pkg/tcpip/link/rawfile/rawfile_unsafe.go
@@ -52,7 +52,7 @@ func GetMTU(name string) (uint32, error) {
// NonBlockingWrite writes the given buffer to a file descriptor. It fails if
// partial data is written.
-func NonBlockingWrite(fd int, buf []byte) *tcpip.Error {
+func NonBlockingWrite(fd int, buf []byte) tcpip.Error {
var ptr unsafe.Pointer
if len(buf) > 0 {
ptr = unsafe.Pointer(&buf[0])
@@ -68,7 +68,7 @@ func NonBlockingWrite(fd int, buf []byte) *tcpip.Error {
// NonBlockingWriteIovec writes iovec to a file descriptor in a single syscall.
// It fails if partial data is written.
-func NonBlockingWriteIovec(fd int, iovec []syscall.Iovec) *tcpip.Error {
+func NonBlockingWriteIovec(fd int, iovec []syscall.Iovec) tcpip.Error {
iovecLen := uintptr(len(iovec))
_, _, e := syscall.RawSyscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen)
if e != 0 {
@@ -78,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) {
+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)
if e != 0 {
return 0, TranslateErrno(e)
@@ -97,7 +97,7 @@ type PollEvent struct {
// BlockingRead reads from a file descriptor that is set up as non-blocking. If
// no data is available, it will block in a poll() syscall until the file
// descriptor becomes readable.
-func BlockingRead(fd int, b []byte) (int, *tcpip.Error) {
+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)))
if e == 0 {
@@ -119,7 +119,7 @@ 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 []syscall.Iovec) (int, tcpip.Error) {
for {
n, _, e := syscall.RawSyscall(syscall.SYS_READV, uintptr(fd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
if e == 0 {
@@ -149,7 +149,7 @@ type MMsgHdr struct {
// and stores the received messages in a slice of MMsgHdr structures. If no data
// is available, it will block in a poll() syscall until the file descriptor
// becomes readable.
-func BlockingRecvMMsg(fd int, msgHdrs []MMsgHdr) (int, *tcpip.Error) {
+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)
if e == 0 {