Skip to content

Commit

Permalink
vsock: factor out newConn function on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Aug 6, 2018
1 parent e73e6ad commit ce2ff06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
29 changes: 18 additions & 11 deletions conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func (c *conn) Read(b []byte) (n int, err error) { return c.file.Read(b) }
func (c *conn) Write(b []byte) (n int, err error) { return c.file.Write(b) }
func (c *conn) Close() error { return c.file.Close() }

// newConn creates a conn using an fd with the specified file name, local, and
// remote addresses.
func newConn(cfd fd, file string, local, remote *Addr) (*conn, error) {
// Enable integration with runtime network poller for timeout support
// in Go 1.11+.
if err := cfd.SetNonblock(true); err != nil {
return nil, err
}

return &conn{
file: cfd.NewFile(file),
localAddr: local,
remoteAddr: remote,
}, nil
}

// dialStream is the entry point for DialStream on Linux.
func dialStream(cid, port uint32) (net.Conn, error) {
fd, err := unix.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0)
Expand Down Expand Up @@ -81,15 +97,6 @@ func dialStreamLinux(cfd fd, cid, port uint32) (net.Conn, error) {
Port: port,
}

// Enable integration with runtime network poller for timeout support
// in Go 1.11+.
if err := cfd.SetNonblock(true); err != nil {
return nil, err
}

return &conn{
file: cfd.NewFile(remoteAddr.fileName()),
localAddr: localAddr,
remoteAddr: remoteAddr,
}, nil
// File name is the name of the local socket.
return newConn(cfd, localAddr.fileName(), localAddr, remoteAddr)
}
12 changes: 1 addition & 11 deletions listener_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@ func (l *listener) Accept() (net.Conn, error) {
Port: savm.Port,
}

// Enable integration with runtime network poller for timeout support
// in Go 1.11+.
if err := cfd.SetNonblock(true); err != nil {
return nil, err
}

return &conn{
file: cfd.NewFile(l.addr.fileName()),
localAddr: l.addr,
remoteAddr: remoteAddr,
}, nil
return newConn(cfd, l.addr.fileName(), l.addr, remoteAddr)
}

// listenStream is the entry point for ListenStream on Linux.
Expand Down

0 comments on commit ce2ff06

Please sign in to comment.