optimize: lower kernel version requirement from 5.6 to 5.2

This commit is contained in:
mzz2017
2023-01-28 13:27:54 +08:00
parent 1eaaa4349c
commit 6e3637d018
17 changed files with 962 additions and 14 deletions

View File

@ -6,9 +6,12 @@
package control
import (
"fmt"
"github.com/cilium/ebpf"
"github.com/v2rayA/dae/common"
"github.com/v2rayA/dae/pkg/ebpf_internal"
"net/netip"
"reflect"
)
type _bpfLpmKey struct {
@ -27,7 +30,7 @@ func (o *bpfObjects) newLpmMap(keys []_bpfLpmKey, values []uint32) (m *ebpf.Map,
if err != nil {
return nil, err
}
if _, err = m.BatchUpdate(keys, values, &ebpf.BatchOptions{
if _, err = BatchUpdate(m, keys, values, &ebpf.BatchOptions{
ElemFlags: uint64(ebpf.UpdateAny),
}); err != nil {
return nil, err
@ -50,3 +53,47 @@ func cidrToBpfLpmKey(prefix netip.Prefix) _bpfLpmKey {
Data: common.Ipv6ByteSliceToUint32Array(ip[:]),
}
}
// A utility to convert the values to proper strings.
func int8ToStr(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}
func BatchUpdate(m *ebpf.Map, keys interface{}, values interface{}, opts *ebpf.BatchOptions) (n int, err error) {
var old bool
version, e := internal.KernelVersion()
if e != nil || version.Less(internal.Version{5, 6, 0}) {
old = true
}
if !old {
return m.BatchUpdate(keys, values, opts)
} else {
vKeys := reflect.ValueOf(keys)
if vKeys.Kind() != reflect.Slice {
return 0, fmt.Errorf("keys must be slice")
}
vVals := reflect.ValueOf(values)
if vVals.Kind() != reflect.Slice {
return 0, fmt.Errorf("values must be slice")
}
length := vKeys.Len()
if vVals.Len() != length {
return 0, fmt.Errorf("keys and values must have same length")
}
for i := 0; i < length; i++ {
vKey := vKeys.Index(i)
vVal := vVals.Index(i)
if err = m.Update(vKey.Interface(), vVal.Interface(), ebpf.MapUpdateFlags(opts.ElemFlags)); err != nil {
return i, err
}
}
return vKeys.Len(), nil
}
}