optimize: lower the requirement of bootstrap memory

This commit is contained in:
mzz2017 2023-03-14 13:54:43 +08:00
parent b5846c410f
commit a6c2a077db
5 changed files with 34 additions and 10 deletions

View File

@ -11,6 +11,8 @@ STRIP ?= llvm-strip
CFLAGS := -O2 -Wall -Werror $(CFLAGS)
TARGET ?= bpfel,bpfeb
OUTPUT ?= dae
MAX_MATCH_SET_LEN ?= 64
CFLAGS := -DMAX_MATCH_SET_LEN=$(MAX_MATCH_SET_LEN) $(CFLAGS)
# Get version from .git.
date=$(shell git log -1 --format="%cd" --date=short | sed s/-//g)
@ -25,7 +27,7 @@ endif
.PHONY: clean-ebpf ebpf dae
dae: ebpf
go build -o $(OUTPUT) -trimpath -ldflags "-s -w -X github.com/v2rayA/dae/cmd.Version=$(VERSION)" .
go build -o $(OUTPUT) -trimpath -ldflags "-s -w -X github.com/v2rayA/dae/cmd.Version=$(VERSION) -X github.com/v2rayA/dae/common/consts.MaxMatchSetLen_=$(MAX_MATCH_SET_LEN)" .
clean-ebpf:
@rm -f control/bpf_bpf*.go && \

View File

@ -97,10 +97,24 @@ func (i OutboundIndex) IsReserved() bool {
return !strings.HasPrefix(i.String(), "<index: ")
}
const (
MaxMatchSetLen = 32 * 3
var (
MaxMatchSetLen_ = ""
MaxMatchSetLen = 32 * 2
)
func init() {
if MaxMatchSetLen_ != "" {
i, err := strconv.Atoi(MaxMatchSetLen_)
if err != nil {
panic(err)
}
MaxMatchSetLen = i
}
if MaxMatchSetLen%32 != 0 {
panic("MaxMatchSetLen should be a multiple of 32: " + strconv.Itoa(MaxMatchSetLen))
}
}
type L4ProtoType uint8
const (

View File

@ -116,7 +116,7 @@ func NewControlPlane(
/// Load pre-compiled programs and maps into the kernel.
log.Infof("Loading eBPF programs and maps into the kernel.")
log.Infof("The loading process takes about 512MB free memory, which will be released after loading. Insufficient memory will cause loading failure.")
log.Infof("The loading process takes about 150MB free memory, which will be released after loading. Insufficient memory will cause loading failure.")
//var bpf bpfObjects
var ProgramOptions = ebpf.ProgramOptions{
KernelTypes: nil,
@ -294,7 +294,7 @@ func NewControlPlane(
if err != nil {
return nil, fmt.Errorf("NewRoutingMatcherBuilder: %w", err)
}
if err = builder.BuildKernspace(); err != nil {
if err = builder.BuildKernspace(log); err != nil {
return nil, fmt.Errorf("RoutingMatcherBuilder.BuildKernspace: %w", err)
}
routingMatcher, err := builder.BuildUserspace(core.bpf.LpmArrayMap)

View File

@ -46,7 +46,9 @@
#define MAX_PARAM_LEN 16
#define MAX_INTERFACE_NUM 128
#define MAX_MATCH_SET_LEN (32 * 3)
#ifndef MAX_MATCH_SET_LEN
#define MAX_MATCH_SET_LEN (32 * 2) // Should be sync with common/consts/ebpf.go.
#endif
#define MAX_LPM_SIZE 20480
#define MAX_LPM_NUM (MAX_MATCH_SET_LEN + 8)
#define MAX_DST_MAPPING_NUM (65536 * 2)
@ -546,7 +548,9 @@ handle_ipv6_extensions(const struct __sk_buff *skb, __u32 offset, __u32 hdr,
int ret;
// We only process TCP and UDP traffic.
#pragma unroll
// Unroll can give less instructions but more memory consumption when loading.
// We disable it here to support more poor memory devices.
// #pragma unroll
for (int i = 0; i < IPV6_MAX_EXTENSIONS;
i++, offset += hdr_length, hdr = nexthdr, *ihl += hdr_length / 4) {
if (hdr_length % 4) {
@ -1004,7 +1008,9 @@ routing(const __u32 flag[6], const void *l4hdr, const __be32 saddr[4],
__u32 *p_u32;
__u16 *p_u16;
#pragma unroll
// Unroll can give less instructions but more memory consumption when loading.
// We disable it here to support more poor memory devices.
// #pragma unroll
for (__u32 i = 0; i < MAX_MATCH_SET_LEN; i++) {
__u32 k = i; // Clone to pass code checker.
match_set = bpf_map_lookup_elem(&routing_map, &k);
@ -1526,7 +1532,8 @@ int tproxy_wan_egress(struct __sk_buff *skb) {
return TC_ACT_OK;
}
bool tproxy_response = *tproxy_port == tuples.sport;
// Double check to avoid bind wan and lan to one interface.
// Double check to avoid conflicts when binding wan and lan to the same
// interface.
if (tproxy_response && l4proto == IPPROTO_TCP) {
// If it is a TCP first handshake, it is not a tproxy response.
if (tcph.syn && !tcph.syn) {

View File

@ -279,7 +279,7 @@ func (b *RoutingMatcherBuilder) addFallback(fallbackOutbound config.FunctionOrSt
return nil
}
func (b *RoutingMatcherBuilder) BuildKernspace() (err error) {
func (b *RoutingMatcherBuilder) BuildKernspace(log *logrus.Logger) (err error) {
// Update lpm_array_map.
for i, cidrs := range b.simulatedLpmTries {
var keys []_bpfLpmKey
@ -311,6 +311,7 @@ func (b *RoutingMatcherBuilder) BuildKernspace() (err error) {
}); err != nil {
return fmt.Errorf("BpfMapBatchUpdate: %w", err)
}
log.Infof("Routing match set len: %v/%v", len(b.rules), consts.MaxMatchSetLen)
return nil
}