optimize: not load CO-RE if possible

This commit is contained in:
mzz2017 2023-02-01 10:59:57 +08:00
parent 18e1eeea80
commit 5cbb1f963c
2 changed files with 11 additions and 8 deletions

View File

@ -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)

View File

@ -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.