conn, device, tun: implement vectorized I/O plumbing
Accept packet vectors for reading and writing in the tun.Device and conn.Bind interfaces, so that the internal plumbing between these interfaces now passes a vector of packets. Vectors move untouched between these interfaces, i.e. if 128 packets are received from conn.Bind.Read(), 128 packets are passed to tun.Device.Write(). There is no internal buffering. Currently, existing implementations are only adjusted to have vectors of length one. Subsequent patches will improve that. Also, as a related fixup, use the unix and windows packages rather than the syscall package when possible. Co-authored-by: James Tucker <james@tailscale.com> Signed-off-by: James Tucker <james@tailscale.com> Signed-off-by: Jordan Whited <jordan@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
committed by
Jason A. Donenfeld
parent
21636207a6
commit
3bb8fec7e4
@@ -323,12 +323,13 @@ func (tun *NativeTun) nameSlow() (string, error) {
|
||||
return unix.ByteSliceToString(ifr[:]), nil
|
||||
}
|
||||
|
||||
func (tun *NativeTun) Write(buf []byte, offset int) (int, error) {
|
||||
func (tun *NativeTun) Write(buffs [][]byte, offset int) (n int, err error) {
|
||||
var buf []byte
|
||||
if tun.nopi {
|
||||
buf = buf[offset:]
|
||||
buf = buffs[0][offset:]
|
||||
} else {
|
||||
// reserve space for header
|
||||
buf = buf[offset-4:]
|
||||
buf = buffs[0][offset-4:]
|
||||
|
||||
// add packet information header
|
||||
buf[0] = 0x00
|
||||
@@ -342,34 +343,36 @@ func (tun *NativeTun) Write(buf []byte, offset int) (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
n, err := tun.tunFile.Write(buf)
|
||||
_, err = tun.tunFile.Write(buf)
|
||||
if errors.Is(err, syscall.EBADFD) {
|
||||
err = os.ErrClosed
|
||||
} else if err == nil {
|
||||
n = 1
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (tun *NativeTun) Flush() error {
|
||||
// TODO: can flushing be implemented by buffering and using sendmmsg?
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tun *NativeTun) Read(buf []byte, offset int) (n int, err error) {
|
||||
func (tun *NativeTun) Read(buffs [][]byte, sizes []int, offset int) (n int, err error) {
|
||||
select {
|
||||
case err = <-tun.errors:
|
||||
default:
|
||||
if tun.nopi {
|
||||
n, err = tun.tunFile.Read(buf[offset:])
|
||||
sizes[0], err = tun.tunFile.Read(buffs[0][offset:])
|
||||
if err == nil {
|
||||
n = 1
|
||||
}
|
||||
} else {
|
||||
buff := buf[offset-4:]
|
||||
n, err = tun.tunFile.Read(buff[:])
|
||||
buff := buffs[0][offset-4:]
|
||||
sizes[0], err = tun.tunFile.Read(buff[:])
|
||||
if errors.Is(err, syscall.EBADFD) {
|
||||
err = os.ErrClosed
|
||||
} else if err == nil {
|
||||
n = 1
|
||||
}
|
||||
if n < 4 {
|
||||
n = 0
|
||||
if sizes[0] < 4 {
|
||||
sizes[0] = 0
|
||||
} else {
|
||||
n -= 4
|
||||
sizes[0] -= 4
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,6 +402,10 @@ func (tun *NativeTun) Close() error {
|
||||
return err2
|
||||
}
|
||||
|
||||
func (tun *NativeTun) BatchSize() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func CreateTUN(name string, mtu int) (Device, error) {
|
||||
nfd, err := unix.Open(cloneDevicePath, unix.O_RDWR|unix.O_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user