diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-03-03 10:23:55 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-03-03 10:25:58 -0800 |
commit | a9441aea2780da8c93da1c73da860219f98438de (patch) | |
tree | 8b12915756f5bfb926218214cd7bc0b3281605fd /pkg/tcpip/link/sharedmem | |
parent | b8a5420f49a2afd622ec08b5019e1bf537f7da82 (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/sharedmem')
-rw-r--r-- | pkg/tcpip/link/sharedmem/BUILD | 2 | ||||
-rw-r--r-- | pkg/tcpip/link/sharedmem/rx.go | 46 | ||||
-rw-r--r-- | pkg/tcpip/link/sharedmem/sharedmem.go | 4 | ||||
-rw-r--r-- | pkg/tcpip/link/sharedmem/sharedmem_test.go | 42 | ||||
-rw-r--r-- | pkg/tcpip/link/sharedmem/tx.go | 22 |
5 files changed, 59 insertions, 57 deletions
diff --git a/pkg/tcpip/link/sharedmem/BUILD b/pkg/tcpip/link/sharedmem/BUILD index 13243ebbb..4215ee852 100644 --- a/pkg/tcpip/link/sharedmem/BUILD +++ b/pkg/tcpip/link/sharedmem/BUILD @@ -20,6 +20,7 @@ go_library( "//pkg/tcpip/link/rawfile", "//pkg/tcpip/link/sharedmem/queue", "//pkg/tcpip/stack", + "@org_golang_x_sys//unix:go_default_library", ], ) @@ -37,5 +38,6 @@ go_test( "//pkg/tcpip/link/sharedmem/pipe", "//pkg/tcpip/link/sharedmem/queue", "//pkg/tcpip/stack", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/tcpip/link/sharedmem/rx.go b/pkg/tcpip/link/sharedmem/rx.go index eec11e4cb..8e6f3e5e3 100644 --- a/pkg/tcpip/link/sharedmem/rx.go +++ b/pkg/tcpip/link/sharedmem/rx.go @@ -18,8 +18,8 @@ package sharedmem import ( "sync/atomic" - "syscall" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" "gvisor.dev/gvisor/pkg/tcpip/link/sharedmem/queue" ) @@ -46,43 +46,43 @@ func (r *rx) init(mtu uint32, c *QueueConfig) error { rxPipe, err := getBuffer(c.RxPipeFD) if err != nil { - syscall.Munmap(txPipe) + unix.Munmap(txPipe) return err } data, err := getBuffer(c.DataFD) if err != nil { - syscall.Munmap(txPipe) - syscall.Munmap(rxPipe) + unix.Munmap(txPipe) + unix.Munmap(rxPipe) return err } sharedData, err := getBuffer(c.SharedDataFD) if err != nil { - syscall.Munmap(txPipe) - syscall.Munmap(rxPipe) - syscall.Munmap(data) + unix.Munmap(txPipe) + unix.Munmap(rxPipe) + unix.Munmap(data) return err } // Duplicate the eventFD so that caller can close it but we can still // use it. - efd, err := syscall.Dup(c.EventFD) + efd, err := unix.Dup(c.EventFD) if err != nil { - syscall.Munmap(txPipe) - syscall.Munmap(rxPipe) - syscall.Munmap(data) - syscall.Munmap(sharedData) + unix.Munmap(txPipe) + unix.Munmap(rxPipe) + unix.Munmap(data) + unix.Munmap(sharedData) return err } // Set the eventfd as non-blocking. - if err := syscall.SetNonblock(efd, true); err != nil { - syscall.Munmap(txPipe) - syscall.Munmap(rxPipe) - syscall.Munmap(data) - syscall.Munmap(sharedData) - syscall.Close(efd) + if err := unix.SetNonblock(efd, true); err != nil { + unix.Munmap(txPipe) + unix.Munmap(rxPipe) + unix.Munmap(data) + unix.Munmap(sharedData) + unix.Close(efd) return err } @@ -99,12 +99,12 @@ func (r *rx) init(mtu uint32, c *QueueConfig) error { // called if init() has previously succeeded. func (r *rx) cleanup() { a, b := r.q.Bytes() - syscall.Munmap(a) - syscall.Munmap(b) + unix.Munmap(a) + unix.Munmap(b) - syscall.Munmap(r.data) - syscall.Munmap(r.sharedData) - syscall.Close(r.eventFD) + unix.Munmap(r.data) + unix.Munmap(r.sharedData) + unix.Close(r.eventFD) } // postAndReceive posts the provided buffers (if any), and then tries to read diff --git a/pkg/tcpip/link/sharedmem/sharedmem.go b/pkg/tcpip/link/sharedmem/sharedmem.go index 2599bc406..d8d0b16b2 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem.go +++ b/pkg/tcpip/link/sharedmem/sharedmem.go @@ -24,8 +24,8 @@ package sharedmem import ( "sync/atomic" - "syscall" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" @@ -118,7 +118,7 @@ func (e *endpoint) Close() { // Tell dispatch goroutine to stop, then write to the eventfd so that // it wakes up in case it's sleeping. atomic.StoreUint32(&e.stopRequested, 1) - syscall.Write(e.rx.eventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(e.rx.eventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Cleanup the queues inline if the worker hasn't started yet; we also // know it won't start from now on because stopRequested is set to 1. diff --git a/pkg/tcpip/link/sharedmem/sharedmem_test.go b/pkg/tcpip/link/sharedmem/sharedmem_test.go index d480ad656..def47772f 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem_test.go +++ b/pkg/tcpip/link/sharedmem/sharedmem_test.go @@ -22,10 +22,10 @@ import ( "math/rand" "os" "strings" - "syscall" "testing" "time" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/buffer" @@ -72,9 +72,9 @@ func initQueue(t *testing.T, q *queueBuffers, c *QueueConfig) { } func (q *queueBuffers) cleanup() { - syscall.Munmap(q.tx.Bytes()) - syscall.Munmap(q.rx.Bytes()) - syscall.Munmap(q.data) + unix.Munmap(q.tx.Bytes()) + unix.Munmap(q.rx.Bytes()) + unix.Munmap(q.data) } type packetInfo struct { @@ -200,7 +200,7 @@ func createFile(t *testing.T, size int64, initQueue bool) int { t.Fatalf("TempFile failed: %v", err) } defer f.Close() - syscall.Unlink(f.Name()) + unix.Unlink(f.Name()) if initQueue { // Write the "slot-free" flag in the initial queue. @@ -210,13 +210,13 @@ func createFile(t *testing.T, size int64, initQueue bool) int { } } - fd, err := syscall.Dup(int(f.Fd())) + fd, err := unix.Dup(int(f.Fd())) if err != nil { t.Fatalf("Dup failed: %v", err) } - if err := syscall.Ftruncate(fd, size); err != nil { - syscall.Close(fd) + if err := unix.Ftruncate(fd, size); err != nil { + unix.Close(fd) t.Fatalf("Ftruncate failed: %v", err) } @@ -224,11 +224,11 @@ func createFile(t *testing.T, size int64, initQueue bool) int { } func closeFDs(c *QueueConfig) { - syscall.Close(c.DataFD) - syscall.Close(c.EventFD) - syscall.Close(c.TxPipeFD) - syscall.Close(c.RxPipeFD) - syscall.Close(c.SharedDataFD) + unix.Close(c.DataFD) + unix.Close(c.EventFD) + unix.Close(c.TxPipeFD) + unix.Close(c.RxPipeFD) + unix.Close(c.SharedDataFD) } type queueSizes struct { @@ -239,7 +239,7 @@ type queueSizes struct { } func createQueueFDs(t *testing.T, s queueSizes) QueueConfig { - fd, _, err := syscall.RawSyscall(syscall.SYS_EVENTFD2, 0, 0, 0) + fd, _, err := unix.RawSyscall(unix.SYS_EVENTFD2, 0, 0, 0) if err != 0 { t.Fatalf("eventfd failed: %v", error(err)) } @@ -671,7 +671,7 @@ func TestSimpleReceive(t *testing.T) { // Push completion. c.pushRxCompletion(uint32(len(contents)), bufs) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Wait for packet to be received, then check it. c.waitForPackets(1, time.After(5*time.Second), "Timeout waiting for packet") @@ -717,7 +717,7 @@ func TestRxBuffersReposted(t *testing.T) { // Complete the buffer. c.pushRxCompletion(buffers[i].Size, buffers[i:][:1]) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Wait for it to be reposted. bi := queue.DecodeRxBufferHeader(pollPull(t, &c.rxq.tx, timeout, "Timeout waiting for buffer to be reposted")) @@ -733,7 +733,7 @@ func TestRxBuffersReposted(t *testing.T) { // Complete with two buffers. c.pushRxCompletion(2*bufferSize, buffers[2*i:][:2]) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Wait for them to be reposted. for j := 0; j < 2; j++ { @@ -758,7 +758,7 @@ func TestReceivePostingIsFull(t *testing.T) { first := queue.DecodeRxBufferHeader(pollPull(t, &c.rxq.tx, time.After(time.Second), "Timeout waiting for first buffer to be posted")) c.pushRxCompletion(first.Size, []queue.RxBuffer{first}) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Check that packet is received. c.waitForPackets(1, time.After(time.Second), "Timeout waiting for completed packet") @@ -767,7 +767,7 @@ func TestReceivePostingIsFull(t *testing.T) { second := queue.DecodeRxBufferHeader(pollPull(t, &c.rxq.tx, time.After(time.Second), "Timeout waiting for second buffer to be posted")) c.pushRxCompletion(second.Size, []queue.RxBuffer{second}) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Check that no packet is received yet, as the worker is blocked trying // to repost. @@ -780,7 +780,7 @@ func TestReceivePostingIsFull(t *testing.T) { // Flush tx queue, which will allow the first buffer to be reposted, // and the second completion to be pulled. c.rxq.tx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Check that second packet completes. c.waitForPackets(1, time.After(time.Second), "Timeout waiting for second completed packet") @@ -802,7 +802,7 @@ func TestCloseWhileWaitingToPost(t *testing.T) { bi := queue.DecodeRxBufferHeader(pollPull(t, &c.rxq.tx, time.After(time.Second), "Timeout waiting for initial buffer to be posted")) c.pushRxCompletion(bi.Size, []queue.RxBuffer{bi}) c.rxq.rx.Flush() - syscall.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) + unix.Write(c.rxCfg.EventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Wait for packet to be indicated. c.waitForPackets(1, time.After(time.Second), "Timeout waiting for completed packet") diff --git a/pkg/tcpip/link/sharedmem/tx.go b/pkg/tcpip/link/sharedmem/tx.go index 44f421c2d..e3210051f 100644 --- a/pkg/tcpip/link/sharedmem/tx.go +++ b/pkg/tcpip/link/sharedmem/tx.go @@ -16,8 +16,8 @@ package sharedmem import ( "math" - "syscall" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/link/sharedmem/queue" ) @@ -48,14 +48,14 @@ func (t *tx) init(mtu uint32, c *QueueConfig) error { rxPipe, err := getBuffer(c.RxPipeFD) if err != nil { - syscall.Munmap(txPipe) + unix.Munmap(txPipe) return err } data, err := getBuffer(c.DataFD) if err != nil { - syscall.Munmap(txPipe) - syscall.Munmap(rxPipe) + unix.Munmap(txPipe) + unix.Munmap(rxPipe) return err } @@ -72,9 +72,9 @@ func (t *tx) init(mtu uint32, c *QueueConfig) error { // called if init() has previously succeeded. func (t *tx) cleanup() { a, b := t.q.Bytes() - syscall.Munmap(a) - syscall.Munmap(b) - syscall.Munmap(t.data) + unix.Munmap(a) + unix.Munmap(b) + unix.Munmap(t.data) } // transmit sends a packet made of bufs. Returns a boolean that specifies @@ -145,17 +145,17 @@ func (t *tx) transmit(bufs ...buffer.View) bool { // getBuffer returns a memory region mapped to the full contents of the given // file descriptor. func getBuffer(fd int) ([]byte, error) { - var s syscall.Stat_t - if err := syscall.Fstat(fd, &s); err != nil { + var s unix.Stat_t + if err := unix.Fstat(fd, &s); err != nil { return nil, err } // Check that size doesn't overflow an int. if s.Size > int64(^uint(0)>>1) { - return nil, syscall.EDOM + return nil, unix.EDOM } - return syscall.Mmap(fd, 0, int(s.Size), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_FILE) + return unix.Mmap(fd, 0, int(s.Size), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED|unix.MAP_FILE) } // idDescriptor is used by idManager to either point to a tx buffer (in case |