fix: ebpf support

This commit is contained in:
gVisor bot 2022-08-22 23:17:41 +08:00
parent 6484ba4d44
commit 48f30b01ef
4 changed files with 41 additions and 2 deletions

View file

@ -1,4 +1,4 @@
//go:build !android && linux //go:build !android
package ebpf package ebpf

View file

@ -119,6 +119,7 @@ type Tun struct {
AutoRoute bool `yaml:"auto-route" json:"auto-route"` AutoRoute bool `yaml:"auto-route" json:"auto-route"`
AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"` AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
TunAddressPrefix netip.Prefix `yaml:"-" json:"-"` TunAddressPrefix netip.Prefix `yaml:"-" json:"-"`
RedirectToTun []string `yaml:"-" json:"-"`
} }
// IPTables config // IPTables config
@ -296,6 +297,10 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
AutoRoute: false, AutoRoute: false,
AutoDetectInterface: false, AutoDetectInterface: false,
}, },
EBpf: EBpf{
RedirectToTun: []string{},
AutoRedir: []string{},
},
IPTables: IPTables{ IPTables: IPTables{
Enable: false, Enable: false,
InboundInterface: "lo", InboundInterface: "lo",
@ -967,6 +972,7 @@ func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) (*Tun, error) {
AutoRoute: rawTun.AutoRoute, AutoRoute: rawTun.AutoRoute,
AutoDetectInterface: rawTun.AutoDetectInterface, AutoDetectInterface: rawTun.AutoDetectInterface,
TunAddressPrefix: tunAddressPrefix, TunAddressPrefix: tunAddressPrefix,
RedirectToTun: rawTun.RedirectToTun,
}, nil }, nil
} }

View file

@ -260,6 +260,7 @@ func loadProxyProvider(proxyProviders map[string]provider.ProxyProvider) {
func updateTun(tun *config.Tun) { func updateTun(tun *config.Tun) {
P.ReCreateTun(tun, tunnel.TCPIn(), tunnel.UDPIn()) P.ReCreateTun(tun, tunnel.TCPIn(), tunnel.UDPIn())
P.ReCreateRedirToTun(tun.RedirectToTun)
} }
func updateSniffer(sniffer *config.Sniffer) { func updateSniffer(sniffer *config.Sniffer) {
@ -332,9 +333,9 @@ func updateGeneral(general *config.General, force bool) {
P.ReCreateHTTP(general.Port, tcpIn) P.ReCreateHTTP(general.Port, tcpIn)
P.ReCreateSocks(general.SocksPort, tcpIn, udpIn) P.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
P.ReCreateRedir(general.RedirPort, tcpIn, udpIn) P.ReCreateRedir(general.RedirPort, tcpIn, udpIn)
P.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn) P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
P.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
} }
func updateUsers(users []auth.AuthUser) { func updateUsers(users []auth.AuthUser) {

View file

@ -43,6 +43,7 @@ var (
tunStackListener ipstack.Stack tunStackListener ipstack.Stack
autoRedirListener *autoredir.Listener autoRedirListener *autoredir.Listener
autoRedirProgram *ebpf.TcEBpfProgram autoRedirProgram *ebpf.TcEBpfProgram
tcProgram *ebpf.TcEBpfProgram
// lock for recreate function // lock for recreate function
socksMux sync.Mutex socksMux sync.Mutex
@ -363,6 +364,37 @@ func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *
lastTunConf = tunConf lastTunConf = tunConf
} }
func ReCreateRedirToTun(ifaceNames []string) {
tcMux.Lock()
defer tcMux.Unlock()
nicArr := ifaceNames
slices.Sort(nicArr)
nicArr = slices.Compact(nicArr)
if tcProgram != nil {
tcProgram.Close()
tcProgram = nil
}
if len(nicArr) == 0 {
return
}
if lastTunConf == nil || !lastTunConf.Enable {
return
}
program, err := ebpf.NewTcEBpfProgram(nicArr, lastTunConf.Device)
if err != nil {
log.Errorln("Attached tc ebpf program error: %v", err)
return
}
tcProgram = program
log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs())
}
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- *inbound.PacketAdapter) { func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- *inbound.PacketAdapter) {
autoRedirMux.Lock() autoRedirMux.Lock()
defer autoRedirMux.Unlock() defer autoRedirMux.Unlock()