diff --git a/cmd/run.go b/cmd/run.go index ebc1b26..2a735cf 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -56,7 +56,12 @@ func Run() (err error) { nodeList = append(nodeList, nodes...) } + if param.Global.LanInterface == "" && param.Global.WanInterface == "" { + return fmt.Errorf("LanInterface and WanInterface cannot both be empty") + } + // New ControlPlane. + onlyBindLanInterface := param.Global.WanInterface == "" t, err := control.NewControlPlane( log, nodeList, @@ -65,15 +70,13 @@ func Run() (err error) { param.Global.DnsUpstream, param.Global.CheckUrl, param.Global.CheckInterval, + onlyBindLanInterface, ) if err != nil { return err } // Bind to links. - if param.Global.LanInterface == "" && param.Global.WanInterface == "" { - return fmt.Errorf("LanInterface and WanInterface cannot both be empty") - } if param.Global.LanInterface != "" { if err = t.BindLan(param.Global.LanInterface); err != nil { return fmt.Errorf("BindLan: %v: %w", param.Global.LanInterface, err) diff --git a/component/control/control_plane.go b/component/control/control_plane.go index c29a1b7..4508806 100644 --- a/component/control/control_plane.go +++ b/component/control/control_plane.go @@ -65,6 +65,7 @@ func NewControlPlane( dnsUpstream string, checkUrl string, checkInterval time.Duration, + onlyBindLanInterface bool, ) (c *ControlPlane, err error) { kernelVersion, e := internal.KernelVersion() if e != nil { @@ -80,7 +81,6 @@ func NewControlPlane( // Load pre-compiled programs and maps into the kernel. var bpf bpfObjects - var bpfLan bpfObjectsLan var ProgramOptions ebpf.ProgramOptions if log.IsLevelEnabled(logrus.TraceLevel) { ProgramOptions = ebpf.ProgramOptions{ @@ -88,9 +88,9 @@ func NewControlPlane( } } var obj interface{} = &bpf - if kernelVersion.Less(consts.FtraceFeatureVersion) { + if kernelVersion.Less(consts.FtraceFeatureVersion) || onlyBindLanInterface { // Trick. Replace the beams with rotten timbers. - obj = &bpfLan + obj = &bpfObjectsLan{} } retryLoadBpf: if err = loadBpfObjects(obj, &ebpf.CollectionOptions{ @@ -123,9 +123,9 @@ retryLoadBpf: } return nil, fmt.Errorf("loading objects: %v", err) } - if kernelVersion.Less(consts.FtraceFeatureVersion) { + if _, ok := obj.(*bpfObjects); !ok { // Reverse takeover. - AssignBpfObjects(&bpf, &bpfLan) + AssignBpfObjects(&bpf, obj) } // Write params.