From 8b59492fe54ba2c8b0a9a213f8d4a21285cd62ec Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Mon, 27 Feb 2023 09:56:00 +0800 Subject: [PATCH] chore: remove selective loading bpf objects --- Makefile | 12 +---- .../generate_bpf_objects/dummy/dummy.go | 37 ---------------- .../generate_bpf_objects.go | 27 ------------ control/bpf_utils.go | 44 +------------------ control/control.go | 4 -- control/control_plane.go | 2 +- 6 files changed, 5 insertions(+), 121 deletions(-) delete mode 100644 cmd/internal/generate_bpf_objects/dummy/dummy.go delete mode 100644 cmd/internal/generate_bpf_objects/generate_bpf_objects.go diff --git a/Makefile b/Makefile index 25b3609..5f04b86 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ else VERSION ?= unstable-$(date).r$(count).$(commit) endif -.PHONY: clean-ebpf bpf_objects ebpf dae +.PHONY: clean-ebpf ebpf dae dae: ebpf go build -o $(OUTPUT) -trimpath -ldflags "-s -w -X github.com/v2rayA/dae/cmd.Version=$(VERSION)" . @@ -31,20 +31,12 @@ clean-ebpf: @rm -f control/bpf_bpf*.go && \ rm -f control/bpf_bpf*.o -bpf_objects: - @unset GOOS && \ - unset GOARCH && \ - unset GOARM && \ - if [ ! -f control/bpf_objects_wan_lan.go ]; then \ - go run github.com/v2rayA/dae/cmd/internal/generate_bpf_objects/dummy -o control/bpf_objects_wan_lan.go; \ - fi - # $BPF_CLANG is used in go:generate invocations. ebpf: export BPF_CLANG := $(CLANG) ebpf: export BPF_STRIP := $(STRIP) ebpf: export BPF_CFLAGS := $(CFLAGS) ebpf: export BPF_TARGET := $(TARGET) -ebpf: clean-ebpf bpf_objects +ebpf: clean-ebpf @unset GOOS && \ unset GOARCH && \ unset GOARM && \ diff --git a/cmd/internal/generate_bpf_objects/dummy/dummy.go b/cmd/internal/generate_bpf_objects/dummy/dummy.go deleted file mode 100644 index a216156..0000000 --- a/cmd/internal/generate_bpf_objects/dummy/dummy.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * SPDX-License-Identifier: AGPL-3.0-only - * Copyright (c) 2022-2023, v2rayA Organization - */ - -package main - -import ( - "flag" - "fmt" - "os" - "path/filepath" -) - -func main() { - var output string - flag.StringVar(&output, "o", "", "Place the output into .") - flag.Parse() - - if output == "" { - fmt.Println("Please provide flag \"-o \"") - os.Exit(1) - } - output, err := filepath.Abs(output) - if err != nil { - fmt.Printf("Failed to get absolute path of \"%v\": %v", output, err) - os.Exit(1) - } - // Trick: write a dummy bpfObjectsLan{} and bpfObjectsWan{} before call control package. - if err := os.WriteFile(output, []byte(`package control -type bpfObjectsLan struct{} -type bpfObjectsWan struct{}`), 0644); err != nil { - fmt.Printf("Failed to write \"%v\": %v", output, err) - os.Exit(1) - } - fmt.Printf("Generated dummy %v\n", output) -} diff --git a/cmd/internal/generate_bpf_objects/generate_bpf_objects.go b/cmd/internal/generate_bpf_objects/generate_bpf_objects.go deleted file mode 100644 index 5064724..0000000 --- a/cmd/internal/generate_bpf_objects/generate_bpf_objects.go +++ /dev/null @@ -1,27 +0,0 @@ -/* - * SPDX-License-Identifier: AGPL-3.0-only - * Copyright (c) 2022-2023, v2rayA Organization - */ - -package main - -import ( - "flag" - "fmt" - "github.com/v2rayA/dae/control" - "os" -) - -func main() { - var output string - flag.StringVar(&output, "o", "", "Place the output into .") - flag.Parse() - - if output == "" { - fmt.Println("Please provide flag \"-o \"") - os.Exit(1) - } - fmt.Printf("Generating %v\n", output) - control.GenerateObjects(output) - fmt.Printf("Generated %v\n", output) -} diff --git a/control/bpf_utils.go b/control/bpf_utils.go index 91336c3..f7c9f3e 100644 --- a/control/bpf_utils.go +++ b/control/bpf_utils.go @@ -141,32 +141,6 @@ func BpfMapBatchUpdate(m *ebpf.Map, keys interface{}, values interface{}, opts * return vKeys.Len(), nil } -func AssignBpfObjects(to *bpfObjects, from interface{}) { - vTo := reflect.Indirect(reflect.ValueOf(to)) - vFrom := reflect.Indirect(reflect.ValueOf(from)) - tFrom := vFrom.Type() - // programs - for i := 0; i < vFrom.NumField(); i++ { - fieldFrom := vFrom.Field(i) - structFieldFrom := tFrom.Field(i) - if structFieldFrom.Type != reflect.TypeOf(&ebpf.Program{}) { - continue - } - fieldTo := vTo.FieldByName(structFieldFrom.Name) - fieldTo.Set(fieldFrom) - } - - // bpfMaps - vFrom = vFrom.FieldByName("bpfMaps") - tFrom = vFrom.Type() - for i := 0; i < vFrom.NumField(); i++ { - fieldFrom := vFrom.Field(i) - structFieldFrom := tFrom.Field(i) - fieldTo := vTo.FieldByName(structFieldFrom.Name) - fieldTo.Set(fieldFrom) - } -} - // detectCgroupPath returns the first-found mount point of type cgroup2 // and stores it in the cgroupPath global variable. // Copied from https://github.com/cilium/ebpf/blob/v0.10.0/examples/cgroup_skb/main.go @@ -208,23 +182,13 @@ type loadBpfOptions struct { BindWan bool } -func selectivelyLoadBpfObjects( +func fullLoadBpfObjects( log *logrus.Logger, bpf *bpfObjects, opts *loadBpfOptions, ) (err error) { - // Trick. Replace the beams with rotten timbers to reduce the loading. - var obj interface{} = bpf // Bind to both LAN and WAN. - if opts.BindLan && !opts.BindWan { - // Only bind LAN. - obj = &bpfObjectsLan{} - } else if !opts.BindLan && opts.BindWan { - // Only bind to WAN. - // Trick. Replace the beams with rotten timbers. - obj = &bpfObjectsWan{} - } retryLoadBpf: - if err = loadBpfObjects(obj, opts.CollectionOptions); err != nil { + if err = loadBpfObjects(bpf, opts.CollectionOptions); err != nil { if errors.Is(err, ebpf.ErrMapIncompatible) { // Map property is incompatible. Remove the old map and try again. prefix := "use pinned map " @@ -252,9 +216,5 @@ retryLoadBpf: } return err } - if _, ok := obj.(*bpfObjects); !ok { - // Reverse takeover. - AssignBpfObjects(bpf, obj) - } return nil } diff --git a/control/control.go b/control/control.go index bf631cf..c6467e9 100644 --- a/control/control.go +++ b/control/control.go @@ -8,7 +8,3 @@ package control // $BPF_CLANG, $BPF_STRIP, $BPF_CFLAGS, $BPF_TARGET are set by the Makefile. //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -strip $BPF_STRIP -cflags $BPF_CFLAGS -target $BPF_TARGET bpf kern/tproxy.c -- -I./headers -// Separate bpfObjectsLan and bpfObjectsWan from bpfObjects. -//go:generate go clean -cache -//go:generate go run github.com/v2rayA/dae/cmd/internal/generate_bpf_objects -o bpf_objects_wan_lan.go -//go:generate go fmt bpf_objects_wan_lan.go diff --git a/control/control_plane.go b/control/control_plane.go index 1cf4926..4056499 100644 --- a/control/control_plane.go +++ b/control/control_plane.go @@ -116,7 +116,7 @@ func NewControlPlane( }, Programs: ProgramOptions, } - if err = selectivelyLoadBpfObjects(log, &bpf, &loadBpfOptions{ + if err = fullLoadBpfObjects(log, &bpf, &loadBpfOptions{ PinPath: pinPath, CollectionOptions: collectionOpts, BindLan: len(global.LanInterface) > 0,