chore: remove selective loading bpf objects

This commit is contained in:
mzz2017 2023-02-27 09:56:00 +08:00
parent 7542884451
commit 8b59492fe5
6 changed files with 5 additions and 121 deletions

View File

@ -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 && \

View File

@ -1,37 +0,0 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2023, v2rayA Organization <team@v2raya.org>
*/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
func main() {
var output string
flag.StringVar(&output, "o", "", "Place the output into <file>.")
flag.Parse()
if output == "" {
fmt.Println("Please provide flag \"-o <file>\"")
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)
}

View File

@ -1,27 +0,0 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2023, v2rayA Organization <team@v2raya.org>
*/
package main
import (
"flag"
"fmt"
"github.com/v2rayA/dae/control"
"os"
)
func main() {
var output string
flag.StringVar(&output, "o", "", "Place the output into <file>.")
flag.Parse()
if output == "" {
fmt.Println("Please provide flag \"-o <file>\"")
os.Exit(1)
}
fmt.Printf("Generating %v\n", output)
control.GenerateObjects(output)
fmt.Printf("Generated %v\n", output)
}

View File

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

View File

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

View File

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