tun: linux: netpoll is broken for tun's epoll

So this mostly reverts the switch to Sysconn for Linux.

Issue: https://github.com/golang/go/issues/30426
This commit is contained in:
Jason A. Donenfeld 2019-02-27 04:10:01 +01:00
parent 971be13e77
commit 88ff67fb6f

View file

@ -19,7 +19,6 @@ import (
"os" "os"
"strconv" "strconv"
"sync" "sync"
"syscall"
"time" "time"
"unsafe" "unsafe"
) )
@ -31,6 +30,8 @@ const (
type nativeTun struct { type nativeTun struct {
tunFile *os.File tunFile *os.File
fd uintptr
fdCancel *rwcancel.RWCancel
index int32 // if index index int32 // if index
name string // name of interface name string // name of interface
errors chan error // async error handling errors chan error // async error handling
@ -51,11 +52,9 @@ func (tun *nativeTun) routineHackListener() {
/* This is needed for the detection to work across network namespaces /* This is needed for the detection to work across network namespaces
* If you are reading this and know a better method, please get in touch. * If you are reading this and know a better method, please get in touch.
*/ */
fd := int(tun.fd)
for { for {
var err error _, err := unix.Write(fd, nil)
tun.operateOnFd(func(fd uintptr) {
_, err = unix.Write(int(fd), nil)
})
switch err { switch err {
case unix.EINVAL: case unix.EINVAL:
tun.events <- TUNEventUp tun.events <- TUNEventUp
@ -257,15 +256,12 @@ func (tun *nativeTun) MTU() (int, error) {
func (tun *nativeTun) Name() (string, error) { func (tun *nativeTun) Name() (string, error) {
var ifr [ifReqSize]byte var ifr [ifReqSize]byte
var errno syscall.Errno _, _, errno := unix.Syscall(
tun.operateOnFd(func(fd uintptr) {
_, _, errno = unix.Syscall(
unix.SYS_IOCTL, unix.SYS_IOCTL,
fd, tun.fd,
uintptr(unix.TUNGETIFF), uintptr(unix.TUNGETIFF),
uintptr(unsafe.Pointer(&ifr[0])), uintptr(unsafe.Pointer(&ifr[0])),
) )
})
if errno != 0 { if errno != 0 {
return "", errors.New("failed to get name of TUN device: " + strconv.FormatInt(int64(errno), 10)) return "", errors.New("failed to get name of TUN device: " + strconv.FormatInt(int64(errno), 10))
} }
@ -306,7 +302,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
return tun.tunFile.Write(buff) return tun.tunFile.Write(buff)
} }
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
select { select {
case err := <-tun.errors: case err := <-tun.errors:
return 0, err return 0, err
@ -324,6 +320,18 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
} }
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
for {
n, err := tun.doRead(buff, offset)
if err == nil || !rwcancel.RetryAfterError(err) {
return n, err
}
if !tun.fdCancel.ReadyRead() {
return 0, errors.New("tun device closed")
}
}
}
func (tun *nativeTun) Events() chan TUNEvent { func (tun *nativeTun) Events() chan TUNEvent {
return tun.events return tun.events
} }
@ -339,20 +347,30 @@ func (tun *nativeTun) Close() error {
close(tun.events) close(tun.events)
} }
err2 := tun.tunFile.Close() err2 := tun.tunFile.Close()
err3 := tun.fdCancel.Cancel()
if err1 != nil { if err1 != nil {
return err1 return err1
} }
if err2 != nil {
return err2 return err2
} }
return err3
}
func CreateTUN(name string, mtu int) (TUNDevice, error) { func CreateTUN(name string, mtu int) (TUNDevice, error) {
tunFile, err := os.OpenFile(cloneDevicePath, os.O_RDWR, 0) nfd, err := unix.Open(cloneDevicePath, os.O_RDWR, 0)
if err != nil {
return nil, err
}
fd := os.NewFile(uintptr(nfd), cloneDevicePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// create new device // create new device
var ifr [ifReqSize]byte var ifr [ifReqSize]byte
var flags uint16 = unix.IFF_TUN // | unix.IFF_NO_PI (disabled for TUN status hack) var flags uint16 = unix.IFF_TUN // | unix.IFF_NO_PI (disabled for TUN status hack)
nameBytes := []byte(name) nameBytes := []byte(name)
@ -362,25 +380,23 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
copy(ifr[:], nameBytes) copy(ifr[:], nameBytes)
*(*uint16)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = flags *(*uint16)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = flags
var errno syscall.Errno _, _, errno := unix.Syscall(
(&nativeTun{tunFile: tunFile}).operateOnFd(func(fd uintptr) {
_, _, errno = unix.Syscall(
unix.SYS_IOCTL, unix.SYS_IOCTL,
fd, fd.Fd(),
uintptr(unix.TUNSETIFF), uintptr(unix.TUNSETIFF),
uintptr(unsafe.Pointer(&ifr[0])), uintptr(unsafe.Pointer(&ifr[0])),
) )
})
if errno != 0 { if errno != 0 {
return nil, errno return nil, errno
} }
return CreateTUNFromFile(tunFile, mtu) return CreateTUNFromFile(fd, mtu)
} }
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
tun := &nativeTun{ tun := &nativeTun{
tunFile: file, tunFile: file,
fd: file.Fd(),
events: make(chan TUNEvent, 5), events: make(chan TUNEvent, 5),
errors: make(chan error, 5), errors: make(chan error, 5),
statusListenersShutdown: make(chan struct{}), statusListenersShutdown: make(chan struct{}),
@ -388,6 +404,11 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
} }
var err error var err error
tun.fdCancel, err = rwcancel.NewRWCancel(int(tun.fd))
if err != nil {
return nil, err
}
_, err = tun.Name() _, err = tun.Name()
if err != nil { if err != nil {
return nil, err return nil, err