mirror of
https://github.com/netbirdio/netbird.git
synced 2025-02-16 18:21:24 +01:00
Move ebpf code to its own package to avoid crash issues in Android (#1033)
* Move ebpf code to its own package to avoid crash issues in Android Older versions of android crashes because of the bytecode files Even when they aren't loaded as it was our case * move c file to own folder * fix lint
This commit is contained in:
parent
7794b744f8
commit
24713fbe59
@ -2,7 +2,7 @@
|
||||
//go:build arm64be || armbe || mips || mips64 || mips64p32 || ppc64 || s390 || s390x || sparc || sparc64
|
||||
// +build arm64be armbe mips mips64 mips64p32 ppc64 s390 s390x sparc sparc64
|
||||
|
||||
package wgproxy
|
||||
package ebpf
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -2,7 +2,7 @@
|
||||
//go:build 386 || amd64 || amd64p32 || arm || arm64 || mips64le || mips64p32le || mipsle || ppc64le || riscv64
|
||||
// +build 386 amd64 amd64p32 arm arm64 mips64le mips64p32le mipsle ppc64le riscv64
|
||||
|
||||
package wgproxy
|
||||
package ebpf
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,6 +1,6 @@
|
||||
//go:build linux && !android
|
||||
|
||||
package wgproxy
|
||||
package ebpf
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
@ -15,17 +15,20 @@ const (
|
||||
mapKeyWgPort uint32 = 1
|
||||
)
|
||||
|
||||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang-14 bpf bpf/portreplace.c --
|
||||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang-14 bpf src/portreplace.c --
|
||||
|
||||
type eBPF struct {
|
||||
// EBPF is a wrapper for eBPF program
|
||||
type EBPF struct {
|
||||
link link.Link
|
||||
}
|
||||
|
||||
func newEBPF() *eBPF {
|
||||
return &eBPF{}
|
||||
// NewEBPF create new EBPF instance
|
||||
func NewEBPF() *EBPF {
|
||||
return &EBPF{}
|
||||
}
|
||||
|
||||
func (l *eBPF) load(proxyPort, wgPort int) error {
|
||||
// Load load ebpf program
|
||||
func (l *EBPF) Load(proxyPort, wgPort int) error {
|
||||
// it required for Docker
|
||||
err := rlimit.RemoveMemlock()
|
||||
if err != nil {
|
||||
@ -72,7 +75,8 @@ func (l *eBPF) load(proxyPort, wgPort int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *eBPF) free() error {
|
||||
// Free free ebpf program
|
||||
func (l *EBPF) Free() error {
|
||||
if l.link != nil {
|
||||
return l.link.Close()
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
//go:build linux
|
||||
|
||||
package wgproxy
|
||||
package ebpf
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_newEBPF(t *testing.T) {
|
||||
ebpf := newEBPF()
|
||||
err := ebpf.load(1234, 51892)
|
||||
ebpf := NewEBPF()
|
||||
err := ebpf.Load(1234, 51892)
|
||||
defer func() {
|
||||
_ = ebpf.free()
|
||||
_ = ebpf.Free()
|
||||
}()
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
@ -14,11 +14,13 @@ import (
|
||||
"github.com/google/gopacket/layers"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
ebpf2 "github.com/netbirdio/netbird/client/internal/wgproxy/ebpf"
|
||||
)
|
||||
|
||||
// WGEBPFProxy definition for proxy with eBPF support
|
||||
// WGEBPFProxy definition for proxy with EBPF support
|
||||
type WGEBPFProxy struct {
|
||||
ebpf *eBPF
|
||||
ebpf *ebpf2.EBPF
|
||||
lastUsedPort uint16
|
||||
localWGListenPort int
|
||||
|
||||
@ -34,7 +36,7 @@ func NewWGEBPFProxy(wgPort int) *WGEBPFProxy {
|
||||
log.Debugf("instantiate ebpf proxy")
|
||||
wgProxy := &WGEBPFProxy{
|
||||
localWGListenPort: wgPort,
|
||||
ebpf: newEBPF(),
|
||||
ebpf: ebpf2.NewEBPF(),
|
||||
lastUsedPort: 0,
|
||||
turnConnStore: make(map[uint16]net.Conn),
|
||||
}
|
||||
@ -54,7 +56,7 @@ func (p *WGEBPFProxy) Listen() error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = p.ebpf.load(wgPorxyPort, p.localWGListenPort)
|
||||
err = p.ebpf.Load(wgPorxyPort, p.localWGListenPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -107,7 +109,7 @@ func (p *WGEBPFProxy) Free() error {
|
||||
err1 = p.conn.Close()
|
||||
}
|
||||
|
||||
err2 = p.ebpf.free()
|
||||
err2 = p.ebpf.Free()
|
||||
if p.rawConn != nil {
|
||||
err3 = p.rawConn.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user