From a9441aea2780da8c93da1c73da860219f98438de Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Wed, 3 Mar 2021 10:23:55 -0800 Subject: [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 --- pkg/tcpip/link/fdbased/BUILD | 1 + pkg/tcpip/link/fdbased/endpoint.go | 17 ++++++++--------- pkg/tcpip/link/fdbased/endpoint_test.go | 26 +++++++++++++------------- pkg/tcpip/link/fdbased/mmap.go | 3 +-- pkg/tcpip/link/fdbased/mmap_unsafe.go | 11 +++++------ pkg/tcpip/link/fdbased/packet_dispatchers.go | 15 +++++++-------- 6 files changed, 35 insertions(+), 38 deletions(-) (limited to 'pkg/tcpip/link/fdbased') diff --git a/pkg/tcpip/link/fdbased/BUILD b/pkg/tcpip/link/fdbased/BUILD index ae1394ebf..f042df82e 100644 --- a/pkg/tcpip/link/fdbased/BUILD +++ b/pkg/tcpip/link/fdbased/BUILD @@ -37,5 +37,6 @@ go_test( "//pkg/tcpip/header", "//pkg/tcpip/stack", "@com_github_google_go_cmp//cmp:go_default_library", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/tcpip/link/fdbased/endpoint.go b/pkg/tcpip/link/fdbased/endpoint.go index 0164d851b..72d3f70ac 100644 --- a/pkg/tcpip/link/fdbased/endpoint.go +++ b/pkg/tcpip/link/fdbased/endpoint.go @@ -41,7 +41,6 @@ package fdbased import ( "fmt" - "syscall" "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/binary" @@ -237,8 +236,8 @@ func New(opts *Options) (stack.LinkEndpoint, error) { // Create per channel dispatchers. for i := 0; i < len(e.fds); i++ { fd := e.fds[i] - if err := syscall.SetNonblock(fd, true); err != nil { - return nil, fmt.Errorf("syscall.SetNonblock(%v) failed: %v", fd, err) + if err := unix.SetNonblock(fd, true); err != nil { + return nil, fmt.Errorf("unix.SetNonblock(%v) failed: %v", fd, err) } isSocket, err := isSocketFD(fd) @@ -291,7 +290,7 @@ func createInboundDispatcher(e *endpoint, fd int, isSocket bool) (linkDispatcher // hard to test fragmentation reassembly code in Netstack. const fanoutType = unix.PACKET_FANOUT_HASH fanoutArg := fanoutID | fanoutType<<16 - if err := syscall.SetsockoptInt(fd, syscall.SOL_PACKET, unix.PACKET_FANOUT, fanoutArg); err != nil { + if err := unix.SetsockoptInt(fd, unix.SOL_PACKET, unix.PACKET_FANOUT, fanoutArg); err != nil { return nil, fmt.Errorf("failed to enable PACKET_FANOUT option: %v", err) } } @@ -316,11 +315,11 @@ func createInboundDispatcher(e *endpoint, fd int, isSocket bool) (linkDispatcher } func isSocketFD(fd int) (bool, error) { - var stat syscall.Stat_t - if err := syscall.Fstat(fd, &stat); err != nil { - return false, fmt.Errorf("syscall.Fstat(%v,...) failed: %v", fd, err) + var stat unix.Stat_t + if err := unix.Fstat(fd, &stat); err != nil { + return false, fmt.Errorf("unix.Fstat(%v,...) failed: %v", fd, err) } - return (stat.Mode & syscall.S_IFSOCK) == syscall.S_IFSOCK, nil + return (stat.Mode & unix.S_IFSOCK) == unix.S_IFSOCK, nil } // Attach launches the goroutine that reads packets from the file descriptor and @@ -614,7 +613,7 @@ func (e *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, // NewInjectable creates a new fd-based InjectableEndpoint. func NewInjectable(fd int, mtu uint32, capabilities stack.LinkEndpointCapabilities) *InjectableEndpoint { - syscall.SetNonblock(fd, true) + unix.SetNonblock(fd, true) return &InjectableEndpoint{endpoint: endpoint{ fds: []int{fd}, diff --git a/pkg/tcpip/link/fdbased/endpoint_test.go b/pkg/tcpip/link/fdbased/endpoint_test.go index e82371798..358a030d2 100644 --- a/pkg/tcpip/link/fdbased/endpoint_test.go +++ b/pkg/tcpip/link/fdbased/endpoint_test.go @@ -21,12 +21,12 @@ import ( "fmt" "math/rand" "reflect" - "syscall" "testing" "time" "unsafe" "github.com/google/go-cmp/cmp" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -85,11 +85,11 @@ type context struct { } func newContext(t *testing.T, opt *Options) *context { - firstFDPair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0) + firstFDPair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_SEQPACKET, 0) if err != nil { t.Fatalf("Socketpair failed: %v", err) } - secondFDPair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_SEQPACKET, 0) + secondFDPair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_SEQPACKET, 0) if err != nil { t.Fatalf("Socketpair failed: %v", err) } @@ -121,12 +121,12 @@ func newContext(t *testing.T, opt *Options) *context { func (c *context) cleanup() { for _, fd := range c.readFDs { - syscall.Close(fd) + unix.Close(fd) } <-c.done <-c.done for _, fd := range c.writeFDs { - syscall.Close(fd) + unix.Close(fd) } } @@ -225,7 +225,7 @@ func testWritePacket(t *testing.T, plen int, eth bool, gsoMaxSize uint32, hash u // Read from the corresponding FD, then compare with what we wrote. b = make([]byte, mtu) fd := c.readFDs[hash%uint32(len(c.readFDs))] - n, err := syscall.Read(fd, b) + n, err := unix.Read(fd, b) if err != nil { t.Fatalf("Read failed: %v", err) } @@ -339,7 +339,7 @@ func TestPreserveSrcAddress(t *testing.T) { // Read from the FD, then compare with what we wrote. b := make([]byte, mtu) - n, err := syscall.Read(c.readFDs[0], b) + n, err := unix.Read(c.readFDs[0], b) if err != nil { t.Fatalf("Read failed: %v", err) } @@ -384,7 +384,7 @@ func TestDeliverPacket(t *testing.T) { } // Write packet via the file descriptor. - if _, err := syscall.Write(c.readFDs[0], all); err != nil { + if _, err := unix.Write(c.readFDs[0], all); err != nil { t.Fatalf("Write failed: %v", err) } @@ -481,7 +481,7 @@ func TestIovecBuffer(t *testing.T) { // Make a copy as iovecs points to internal slice. We will need this state // later. - oldIovecs := append([]syscall.Iovec(nil), iovecs...) + oldIovecs := append([]unix.Iovec(nil), iovecs...) // Test the views that get pulled. vv := b.pullViews(c.n) @@ -575,12 +575,12 @@ func TestDispatchPacketFormat(t *testing.T) { } { t.Run(test.name, func(t *testing.T) { // Create a socket pair to send/recv. - fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0) + fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_DGRAM, 0) if err != nil { t.Fatal(err) } - defer syscall.Close(fds[0]) - defer syscall.Close(fds[1]) + defer unix.Close(fds[0]) + defer unix.Close(fds[1]) data := []byte{ // Ethernet header. @@ -590,7 +590,7 @@ func TestDispatchPacketFormat(t *testing.T) { // Mock network header. 40, 41, 42, 43, } - err = syscall.Sendmsg(fds[1], data, nil, nil, 0) + err = unix.Sendmsg(fds[1], data, nil, nil, 0) if err != nil { t.Fatal(err) } diff --git a/pkg/tcpip/link/fdbased/mmap.go b/pkg/tcpip/link/fdbased/mmap.go index a2b63fe6b..5d698a5e9 100644 --- a/pkg/tcpip/link/fdbased/mmap.go +++ b/pkg/tcpip/link/fdbased/mmap.go @@ -19,7 +19,6 @@ package fdbased import ( "encoding/binary" "fmt" - "syscall" "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip" @@ -137,7 +136,7 @@ func (d *packetMMapDispatcher) readMMappedPacket() ([]byte, tcpip.Error) { Events: unix.POLLIN | unix.POLLERR, } if _, errno := rawfile.BlockingPoll(&event, 1, nil); errno != 0 { - if errno == syscall.EINTR { + if errno == unix.EINTR { continue } return nil, rawfile.TranslateErrno(errno) diff --git a/pkg/tcpip/link/fdbased/mmap_unsafe.go b/pkg/tcpip/link/fdbased/mmap_unsafe.go index 3894185ae..1293f68a2 100644 --- a/pkg/tcpip/link/fdbased/mmap_unsafe.go +++ b/pkg/tcpip/link/fdbased/mmap_unsafe.go @@ -19,14 +19,13 @@ package fdbased import ( "fmt" "sync/atomic" - "syscall" "unsafe" "golang.org/x/sys/unix" ) // tPacketHdrlen is the TPACKET_HDRLEN variable defined in . -var tPacketHdrlen = tPacketAlign(unsafe.Sizeof(tPacketHdr{}) + unsafe.Sizeof(syscall.RawSockaddrLinklayer{})) +var tPacketHdrlen = tPacketAlign(unsafe.Sizeof(tPacketHdr{}) + unsafe.Sizeof(unix.RawSockaddrLinklayer{})) // tpStatus returns the frame status field. // The status is concurrently updated by the kernel as a result we must @@ -62,21 +61,21 @@ func newPacketMMapDispatcher(fd int, e *endpoint) (linkDispatcher, error) { tpFrameNR: uint32(tpFrameNR), } // Setup PACKET_RX_RING. - if err := setsockopt(d.fd, syscall.SOL_PACKET, syscall.PACKET_RX_RING, unsafe.Pointer(&tReq), unsafe.Sizeof(tReq)); err != nil { + if err := setsockopt(d.fd, unix.SOL_PACKET, unix.PACKET_RX_RING, unsafe.Pointer(&tReq), unsafe.Sizeof(tReq)); err != nil { return nil, fmt.Errorf("failed to enable PACKET_RX_RING: %v", err) } // Let's mmap the blocks. sz := tpBlockSize * tpBlockNR - buf, err := syscall.Mmap(d.fd, 0, sz, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) + buf, err := unix.Mmap(d.fd, 0, sz, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED) if err != nil { - return nil, fmt.Errorf("syscall.Mmap(...,0, %v, ...) failed = %v", sz, err) + return nil, fmt.Errorf("unix.Mmap(...,0, %v, ...) failed = %v", sz, err) } d.ringBuffer = buf return d, nil } func setsockopt(fd, level, name int, val unsafe.Pointer, vallen uintptr) error { - if _, _, errno := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(fd), uintptr(level), uintptr(name), uintptr(val), vallen, 0); errno != 0 { + if _, _, errno := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(fd), uintptr(level), uintptr(name), uintptr(val), vallen, 0); errno != 0 { return error(errno) } diff --git a/pkg/tcpip/link/fdbased/packet_dispatchers.go b/pkg/tcpip/link/fdbased/packet_dispatchers.go index ecae1ad2d..736871d1c 100644 --- a/pkg/tcpip/link/fdbased/packet_dispatchers.go +++ b/pkg/tcpip/link/fdbased/packet_dispatchers.go @@ -17,8 +17,7 @@ package fdbased import ( - "syscall" - + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -38,7 +37,7 @@ type iovecBuffer struct { // (skipsVnetHdr) then the first iovec points to a buffer for the vnet header // which is stripped before the views are passed up the stack for further // processing. - iovecs []syscall.Iovec + iovecs []unix.Iovec // sizes is an array of buffer sizes for the underlying views. sizes is // immutable. @@ -58,18 +57,18 @@ func newIovecBuffer(sizes []int, skipsVnetHdr bool) *iovecBuffer { if b.skipsVnetHdr { niov++ } - b.iovecs = make([]syscall.Iovec, niov) + b.iovecs = make([]unix.Iovec, niov) return b } -func (b *iovecBuffer) nextIovecs() []syscall.Iovec { +func (b *iovecBuffer) nextIovecs() []unix.Iovec { vnetHdrOff := 0 if b.skipsVnetHdr { var vnetHdr [virtioNetHdrSize]byte // The kernel adds virtioNetHdr before each packet, but // we don't use it, so so we allocate a buffer for it, // add it in iovecs but don't add it in a view. - b.iovecs[0] = syscall.Iovec{ + b.iovecs[0] = unix.Iovec{ Base: &vnetHdr[0], Len: uint64(virtioNetHdrSize), } @@ -81,7 +80,7 @@ func (b *iovecBuffer) nextIovecs() []syscall.Iovec { } v := buffer.NewView(b.sizes[i]) b.views[i] = v - b.iovecs[i+vnetHdrOff] = syscall.Iovec{ + b.iovecs[i+vnetHdrOff] = unix.Iovec{ Base: &v[0], Len: uint64(len(v)), } @@ -200,7 +199,7 @@ type recvMMsgDispatcher struct { // msgHdrs is an array of MMsgHdr objects where each MMsghdr is used to // reference an array of iovecs in the iovecs field defined above. This // array is passed as the parameter to recvmmsg call to retrieve - // potentially more than 1 packet per syscall. + // potentially more than 1 packet per unix. msgHdrs []rawfile.MMsgHdr } -- cgit v1.2.3