fix: no_gvisor compile failed for target linux

This commit is contained in:
gVisor bot 2022-07-16 19:35:52 +08:00
parent b25ad6fda0
commit c768ae8b54
6 changed files with 70 additions and 35 deletions

View file

@ -1,12 +1,7 @@
package fdbased package fdbased
import ( import (
"fmt"
"github.com/Dreamacro/clash/listener/tun/device" "github.com/Dreamacro/clash/listener/tun/device"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
) )
func open(fd int, mtu uint32) (device.Device, error) { func open(fd int, mtu uint32) (device.Device, error) {
@ -16,17 +11,7 @@ func open(fd int, mtu uint32) (device.Device, error) {
} }
func (f *FD) useEndpoint() error { func (f *FD) useEndpoint() error {
ep, err := fdbased.New(&fdbased.Options{ return f.newLinuxEp()
FDs: []int{f.fd},
MTU: f.mtu,
// TUN only, ignore ethernet header.
EthernetHeader: false,
})
if err != nil {
return fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return nil
} }
func (f *FD) useIOBased() error { func (f *FD) useIOBased() error {
@ -34,23 +19,9 @@ func (f *FD) useIOBased() error {
} }
func (f *FD) Read(packet []byte) (int, error) { func (f *FD) Read(packet []byte) (int, error) {
n, gvErr := rawfile.BlockingRead(f.fd, packet) return f.read(packet)
if gvErr != nil {
return 0, fmt.Errorf("read error: %s", gvErr.String())
}
return n, nil
} }
func (f *FD) Write(packet []byte) (int, error) { func (f *FD) Write(packet []byte) (int, error) {
n := len(packet) return f.write(packet)
if n == 0 {
return 0, nil
}
gvErr := rawfile.NonBlockingWrite(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("write error: %s", gvErr.String())
}
return n, nil
} }

View file

@ -0,0 +1,45 @@
//go:build !no_gvisor && (linux || android)
package fdbased
import (
"fmt"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
)
func (f *FD) newLinuxEp() error {
ep, err := fdbased.New(&fdbased.Options{
FDs: []int{f.fd},
MTU: f.mtu,
// TUN only, ignore ethernet header.
EthernetHeader: false,
})
if err != nil {
return fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return nil
}
func (f *FD) read(packet []byte) (int, error) {
n, gvErr := rawfile.BlockingRead(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("read error: %s", gvErr.String())
}
return n, nil
}
func (f *FD) write(packet []byte) (int, error) {
n := len(packet)
if n == 0 {
return 0, nil
}
gvErr := rawfile.NonBlockingWrite(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("write error: %s", gvErr.String())
}
return n, nil
}

View file

@ -0,0 +1,19 @@
//go:build no_gvisor
package fdbased
import (
"fmt"
)
func (f *FD) newLinuxEp() error {
return fmt.Errorf("unsupported gvisor on the build")
}
func (f *FD) read(packet []byte) (int, error) {
return 0, fmt.Errorf("unsupported gvisor on the build")
}
func (f *FD) write(packet []byte) (int, error) {
return 0, fmt.Errorf("unsupported gvisor on the build")
}

View file

@ -16,7 +16,7 @@ func open(fd int, mtu uint32) (device.Device, error) {
} }
func (f *FD) useEndpoint() error { func (f *FD) useEndpoint() error {
return newEp(f) return f.newEpOther()
} }
func (f *FD) useIOBased() error { func (f *FD) useIOBased() error {

View file

@ -9,7 +9,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/device/iobased" "github.com/Dreamacro/clash/listener/tun/device/iobased"
) )
func newEp(f *FD) error { func (f *FD) newEpOther() error {
ep, err := iobased.New(os.NewFile(uintptr(f.fd), f.Name()), f.mtu, 0) ep, err := iobased.New(os.NewFile(uintptr(f.fd), f.Name()), f.mtu, 0)
if err != nil { if err != nil {
return fmt.Errorf("create endpoint: %w", err) return fmt.Errorf("create endpoint: %w", err)

View file

@ -6,6 +6,6 @@ import (
"fmt" "fmt"
) )
func newEp(f *FD) error { func (f *FD) newEpOther() error {
return fmt.Errorf("unsupported gvisor on the build") return fmt.Errorf("unsupported gvisor on the build")
} }