mirror of
https://github.com/daeuniverse/dae.git
synced 2024-12-23 01:34:42 +07:00
chore: remove selective loading bpf objects
This commit is contained in:
parent
7542884451
commit
8b59492fe5
12
Makefile
12
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 && \
|
||||
|
@ -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)
|
||||
}
|
@ -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)
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user