1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

feat: set MTU for gVisor tun

This commit is contained in:
AkinoKaede 2023-05-28 15:26:36 +08:00 committed by Shelikhoo
parent f39b046e03
commit e2d2676dfc
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -5,6 +5,9 @@
package tun
import (
"fmt"
"unsafe"
"github.com/v2fly/v2ray-core/v5/app/tun/device"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip/stack"
@ -14,6 +17,10 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/link/tun"
)
const (
ifReqSize = unix.IFNAMSIZ + 64
)
type TUN struct {
stack.LinkEndpoint
@ -36,7 +43,9 @@ func New(options device.Options) (device.Device, error) {
}
t.fd = fd
// TODO: set MTU
if options.MTU > 0 {
setMTU(options.Name, int(options.MTU))
}
mtu, err := rawfile.GetMTU(options.Name)
if err != nil {
@ -66,3 +75,35 @@ func New(options device.Options) (device.Device, error) {
func (t *TUN) Close() error {
return unix.Close(t.fd)
}
// Modified from golang.zx2c4.com/wireguard/tun/tun_linux.go
func setMTU(name string, n int) error {
// open datagram socket
fd, err := unix.Socket(
unix.AF_INET,
unix.SOCK_DGRAM|unix.SOCK_CLOEXEC,
0,
)
if err != nil {
return err
}
defer unix.Close(fd)
// do ioctl call
var ifr [ifReqSize]byte
copy(ifr[:], name)
*(*uint32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = uint32(n)
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(unix.SIOCSIFMTU),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
return fmt.Errorf("failed to set MTU of TUN device: %w", errno)
}
return nil
}