github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/tun/gvisor/open_linux_armamd64.go (about)

     1  //go:build (linux && amd64) || (linux && arm64)
     2  // +build linux,amd64 linux,arm64
     3  
     4  package tun
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/Asutorufa/yuhaiin/pkg/net/netlink"
    10  	vnetlink "github.com/vishvananda/netlink"
    11  	"golang.org/x/sys/unix"
    12  	"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
    13  	gun "gvisor.dev/gvisor/pkg/tcpip/link/tun"
    14  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    15  )
    16  
    17  func init() {
    18  	openFD = func(sc netlink.TunScheme, mtu int) (stack.LinkEndpoint, error) {
    19  		switch sc.Scheme {
    20  		case "fd":
    21  			return fdbased.New(&fdbased.Options{
    22  				FDs:               []int{sc.Fd},
    23  				MTU:               uint32(mtu),
    24  				RXChecksumOffload: true,
    25  			})
    26  		case "tun":
    27  			// fdbased current not support gso, so open new tun direct instead of wireguard
    28  			dev, err := gun.Open(sc.Name)
    29  			if err != nil {
    30  				return nil, fmt.Errorf("create tun failed: %w", err)
    31  			}
    32  
    33  			tunLink, err := vnetlink.LinkByName(sc.Name)
    34  			if err != nil {
    35  				unix.Close(dev)
    36  				return nil, err
    37  			}
    38  
    39  			if err := vnetlink.LinkSetMTU(tunLink, mtu); err != nil {
    40  				unix.Close(dev)
    41  				return nil, err
    42  			}
    43  
    44  			return fdbased.New(&fdbased.Options{
    45  				FDs:                   []int{dev},
    46  				MTU:                   uint32(mtu),
    47  				RXChecksumOffload:     true,
    48  				MaxSyscallHeaderBytes: 0x00,
    49  			})
    50  
    51  		default:
    52  			return nil, fmt.Errorf("invalid tun: %v", sc)
    53  		}
    54  	}
    55  }