diff --git a/pkg/beyla/config.go b/pkg/beyla/config.go index 95bd92610..cb55fba08 100644 --- a/pkg/beyla/config.go +++ b/pkg/beyla/config.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "log/slog" - "os" "time" "github.com/caarlos0/env/v9" @@ -47,8 +46,6 @@ var DefaultConfig = Config{ EBPF: config.EPPFTracer{ BatchLength: 100, BatchTimeout: time.Second, - BpfBaseDir: "/var/run/beyla", - BpfPath: fmt.Sprintf("beyla-%d", os.Getpid()), HTTPRequestTimeout: 30 * time.Second, }, Grafana: otel.GrafanaConfig{ diff --git a/pkg/beyla/config_test.go b/pkg/beyla/config_test.go index e8462f325..edf78d06a 100644 --- a/pkg/beyla/config_test.go +++ b/pkg/beyla/config_test.go @@ -113,8 +113,6 @@ network: EBPF: config.EPPFTracer{ BatchLength: 100, BatchTimeout: time.Second, - BpfBaseDir: "/var/run/beyla", - BpfPath: DefaultConfig.EBPF.BpfPath, HTTPRequestTimeout: 30 * time.Second, }, Grafana: otel.GrafanaConfig{ diff --git a/pkg/config/ebpf_tracer.go b/pkg/config/ebpf_tracer.go index f20484f27..c542dd353 100644 --- a/pkg/config/ebpf_tracer.go +++ b/pkg/config/ebpf_tracer.go @@ -19,14 +19,6 @@ type EPPFTracer struct { // reach the BatchLength size BatchTimeout time.Duration `yaml:"batch_timeout" env:"BEYLA_BPF_BATCH_TIMEOUT"` - // BpfBaseDir specifies the base directory where the BPF pinned maps will be mounted. - // By default, it will be /var/run/beyla - BpfBaseDir string `yaml:"bpf_fs_base_dir" env:"BEYLA_BPF_FS_BASE_DIR"` - - // BpfPath specifies the path in the base directory where the BPF pinned maps will be mounted. - // By default, it will be beyla-. - BpfPath string `yaml:"bpf_fs_path" env:"BEYLA_BPF_FS_PATH"` - // If enabled, the kprobes based HTTP request tracking will start tracking the request // headers to process any 'Traceparent' fields. TrackRequestHeaders bool `yaml:"track_request_headers" env:"BEYLA_BPF_TRACK_REQUEST_HEADERS"` diff --git a/pkg/internal/appolly/appolly.go b/pkg/internal/appolly/appolly.go index 0915d675d..e8c9ca081 100644 --- a/pkg/internal/appolly/appolly.go +++ b/pkg/internal/appolly/appolly.go @@ -9,7 +9,6 @@ import ( "github.com/grafana/beyla/pkg/beyla" "github.com/grafana/beyla/pkg/internal/discover" - "github.com/grafana/beyla/pkg/internal/ebpf" "github.com/grafana/beyla/pkg/internal/pipe" "github.com/grafana/beyla/pkg/internal/pipe/global" "github.com/grafana/beyla/pkg/internal/request" @@ -109,7 +108,6 @@ func (i *Instrumenter) ReadAndForward() error { bp.Run(i.ctx) log.Info("exiting auto-instrumenter") - discover.UnmountBPFFS(ebpf.BuildPinPath(i.config), log) return nil } diff --git a/pkg/internal/discover/attacher.go b/pkg/internal/discover/attacher.go index 8e26accd4..7e117af3f 100644 --- a/pkg/internal/discover/attacher.go +++ b/pkg/internal/discover/attacher.go @@ -26,7 +26,6 @@ type TraceAttacher struct { DiscoveredTracers chan *ebpf.Instrumentable DeleteTracers chan *ebpf.Instrumentable Metrics imetrics.Reporter - pinPath string beylaPID int // processInstances keeps track of the instances of each process. This will help making sure @@ -56,7 +55,6 @@ func (ta *TraceAttacher) attacherLoop() (pipe.FinalFunc[[]Event[ebpf.Instrumenta ta.existingTracers = map[uint64]*ebpf.ProcessTracer{} ta.processInstances = maps.MultiCounter[uint64]{} ta.beylaPID = os.Getpid() - ta.pinPath = ebpf.BuildPinPath(ta.Cfg) if err := ta.init(); err != nil { ta.log.Error("cant start process tracer. Stopping it", "error", err) diff --git a/pkg/internal/discover/attacher_linux.go b/pkg/internal/discover/attacher_linux.go index 5ea03124a..c2d158564 100644 --- a/pkg/internal/discover/attacher_linux.go +++ b/pkg/internal/discover/attacher_linux.go @@ -1,96 +1,17 @@ package discover import ( - "context" "fmt" - "log/slog" - "os" - "time" "github.com/cilium/ebpf/rlimit" - "golang.org/x/sys/unix" - - "github.com/grafana/beyla/pkg/internal/helpers" ) func (ta *TraceAttacher) close() { - ta.unmountBpfPinPath() -} - -func (ta *TraceAttacher) mountBpfPinPath() error { - ta.log.Debug("mounting BPF map pinning", "path", ta.pinPath) - if _, err := os.Stat(ta.pinPath); err != nil { - if !os.IsNotExist(err) { - return fmt.Errorf("accessing %s stat: %w", ta.pinPath, err) - } - ta.log.Debug("BPF map pinning path does not exist. Creating before mounting") - if err := os.MkdirAll(ta.pinPath, 0700); err != nil { - return fmt.Errorf("creating directory %s: %w", ta.pinPath, err) - } - } - - return ta.bpfMount(ta.pinPath) -} - -func UnmountBPFFS(pinPath string, log *slog.Logger) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - done := make(chan struct{}) - - go func() { - if err := unix.Unmount(pinPath, unix.MNT_FORCE|unix.MNT_DETACH); err != nil { - log.Debug("can't unmount pinned root. Try unmounting and removing it manually", "error", err) - } - log.Debug("unmounted bpf file system") - if err := os.RemoveAll(pinPath); err != nil { - log.Warn("can't remove pinned root. Try removing it manually", "error", err) - } else { - log.Debug("removed pin path") - } - close(done) - }() - - select { - case <-done: - log.Debug("Unmount BPF finished") - case <-ctx.Done(): - log.Debug("Timed out while waiting for BPF unmount to finish") - } -} - -func (ta *TraceAttacher) unmountBpfPinPath() { - UnmountBPFFS(ta.pinPath, ta.log) -} - -func (ta *TraceAttacher) bpfMount(pinPath string) error { - mounted, bpffsInstance, err := IsMountFS(FilesystemTypeBPFFS, pinPath) - if err != nil { - return err - } - if !mounted { - caps, err := helpers.GetCurrentProcCapabilities() - - if err == nil && !caps.Has(unix.CAP_SYS_ADMIN) { - return fmt.Errorf("beyla requires CAP_SYS_ADMIN in order to mount %s", pinPath) - } - - return unix.Mount(pinPath, pinPath, "bpf", 0, "") - } - if !bpffsInstance { - return fmt.Errorf("mount in the custom directory %s has a different filesystem than BPFFS", pinPath) - } - ta.log.Info(fmt.Sprintf("Detected mounted BPF filesystem at %v", pinPath)) - - return nil } func (ta *TraceAttacher) init() error { if err := rlimit.RemoveMemlock(); err != nil { return fmt.Errorf("removing memory lock: %w", err) } - if err := ta.mountBpfPinPath(); err != nil { - return fmt.Errorf("can't mount BPF filesystem: %w", err) - } return nil } diff --git a/pkg/internal/discover/attacher_linux_privileged_test.go b/pkg/internal/discover/attacher_linux_privileged_test.go deleted file mode 100644 index e04ac293f..000000000 --- a/pkg/internal/discover/attacher_linux_privileged_test.go +++ /dev/null @@ -1,55 +0,0 @@ -//go:build linux - -package discover - -import ( - "log/slog" - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestMountBpfPinPath(t *testing.T) { - if os.Getenv(privilegedEnv) == "" { - t.Skipf("Set %s to run this test", privilegedEnv) - } - tmpDir := "path" - ta := &TraceAttacher{ - log: slog.With("component", "discover.TraceAttacher"), - pinPath: tmpDir, - } - - // Nothing there to start the test - mounted, matched, err := IsMountFS(FilesystemTypeBPFFS, tmpDir) - assert.NoError(t, err) - assert.False(t, mounted) - assert.False(t, matched) - - err = ta.mountBpfPinPath() - assert.NoError(t, err) - - // Check that it is mounted - mounted, matched, err = IsMountFS(FilesystemTypeBPFFS, tmpDir) - assert.NoError(t, err) - assert.True(t, mounted) - assert.True(t, matched) - - // Ensure mounting the same path twice does not fail - err = ta.mountBpfPinPath() - assert.NoError(t, err) - - // Check that it is mounted - mounted, matched, err = IsMountFS(FilesystemTypeBPFFS, tmpDir) - assert.NoError(t, err) - assert.True(t, mounted) - assert.True(t, matched) - - ta.unmountBpfPinPath() - - // Check that it is cleaned up - mounted, matched, err = IsMountFS(FilesystemTypeBPFFS, tmpDir) - assert.NoError(t, err) - assert.False(t, mounted) - assert.False(t, matched) -} diff --git a/pkg/internal/discover/attacher_nolinux.go b/pkg/internal/discover/attacher_nolinux.go index b6e23fe1a..fd0e5927f 100644 --- a/pkg/internal/discover/attacher_nolinux.go +++ b/pkg/internal/discover/attacher_nolinux.go @@ -9,5 +9,3 @@ func (ta *TraceAttacher) init() error { } func (ta *TraceAttacher) close() {} - -func UnmountBPFFS(_ string, _ *slog.Logger) {} diff --git a/pkg/internal/discover/mountinfo_linux.go b/pkg/internal/discover/mountinfo_linux.go index 9544ce6f2..260e61116 100644 --- a/pkg/internal/discover/mountinfo_linux.go +++ b/pkg/internal/discover/mountinfo_linux.go @@ -17,7 +17,6 @@ import ( const ( // FilesystemType superblock magic numbers for filesystems, // to be used for IsMountFS. - FilesystemTypeBPFFS = unix.BPF_FS_MAGIC FilesystemTypeCgroup2 = unix.CGROUP2_SUPER_MAGIC ) diff --git a/pkg/internal/discover/mountinfo_linux_test.go b/pkg/internal/discover/mountinfo_linux_test.go index 246b0eae6..716827b37 100644 --- a/pkg/internal/discover/mountinfo_linux_test.go +++ b/pkg/internal/discover/mountinfo_linux_test.go @@ -23,13 +23,4 @@ func TestIsMountFS(t *testing.T) { assert.NoError(t, err) assert.True(t, mounted) assert.True(t, matched) - - mounted, matched, err = IsMountFS(FilesystemTypeBPFFS, "/sys/fs/bpf") - assert.NoError(t, err) - // We can't expect /sys/fs/bpf is mounted, so only check fstype - // if it is mounted. IOW, if /sys/fs/bpf is a mount point, - // we expect it to be bpffs. - if mounted { - assert.True(t, matched) - } } diff --git a/pkg/internal/discover/watcher_proc.go b/pkg/internal/discover/watcher_proc.go index 33750c9bd..6cb185d97 100644 --- a/pkg/internal/discover/watcher_proc.go +++ b/pkg/internal/discover/watcher_proc.go @@ -327,10 +327,10 @@ func fetchProcessPorts(scanPorts bool) (map[PID]processAttrs, error) { func loadBPFWatcher(cfg *beyla.Config, events chan<- watcher.Event) error { wt := watcher.New(cfg, events) - return ebpf.RunUtilityTracer(wt, ebpf.BuildPinPath(cfg)) + return ebpf.RunUtilityTracer(wt) } func loadBPFLogger(cfg *beyla.Config) error { wt := logger.New(cfg) - return ebpf.RunUtilityTracer(wt, ebpf.BuildPinPath(cfg)) + return ebpf.RunUtilityTracer(wt) } diff --git a/pkg/internal/ebpf/tracer.go b/pkg/internal/ebpf/tracer.go index 1b2ddedaa..e35c2df1b 100644 --- a/pkg/internal/ebpf/tracer.go +++ b/pkg/internal/ebpf/tracer.go @@ -111,7 +111,6 @@ const ( type ProcessTracer struct { log *slog.Logger //nolint:unused Programs []Tracer - PinPath string SystemWide bool Type ProcessTracerType diff --git a/pkg/internal/ebpf/tracer_darwin.go b/pkg/internal/ebpf/tracer_darwin.go index 9fdf31ed4..f92cc4070 100644 --- a/pkg/internal/ebpf/tracer_darwin.go +++ b/pkg/internal/ebpf/tracer_darwin.go @@ -25,10 +25,6 @@ func (pt *ProcessTracer) Init() error { return nil } -func BuildPinPath(_ *beyla.Config) string { - return "" -} - func (pt *ProcessTracer) NewExecutable(_ *Instrumentable) error { return nil } @@ -39,6 +35,6 @@ func (pt *ProcessTracer) NewExecutableInstance(_ *link.Executable, _ *Instrument func (pt *ProcessTracer) UnlinkExecutable(_ *exec.FileInfo) {} -func RunUtilityTracer(_ UtilityTracer, _ string) error { +func RunUtilityTracer(_ UtilityTracer) error { return nil } diff --git a/pkg/internal/ebpf/tracer_linux.go b/pkg/internal/ebpf/tracer_linux.go index e23096391..da566c87a 100644 --- a/pkg/internal/ebpf/tracer_linux.go +++ b/pkg/internal/ebpf/tracer_linux.go @@ -7,7 +7,6 @@ import ( "io" "log/slog" "os" - "path" "reflect" "strings" "sync" @@ -70,7 +69,6 @@ func resolveInternalMaps(spec *ebpf.CollectionSpec) (*ebpf.CollectionOptions, er func NewProcessTracer(cfg *beyla.Config, tracerType ProcessTracerType, programs []Tracer) *ProcessTracer { return &ProcessTracer{ Programs: programs, - PinPath: BuildPinPath(cfg), SystemWide: cfg.Discovery.SystemWide, Type: tracerType, Instrumentables: map[uint64]*instrumenter{}, @@ -92,13 +90,6 @@ func (pt *ProcessTracer) Run(ctx context.Context, out chan<- []request.Span) { }() } -// BuildPinPath pinpath must be unique for a given executable group -// it will be: -// - current beyla PID -func BuildPinPath(cfg *beyla.Config) string { - return path.Join(cfg.EBPF.BpfBaseDir, cfg.EBPF.BpfPath) -} - func (pt *ProcessTracer) loadSpec(p Tracer) (*ebpf.CollectionSpec, error) { spec, err := p.Load() if err != nil { @@ -121,7 +112,7 @@ func (pt *ProcessTracer) loadTracers() error { for _, p := range pt.Programs { plog := log.With("program", reflect.TypeOf(p)) - plog.Debug("loading eBPF program", "PinPath", pt.PinPath, "type", pt.Type) + plog.Debug("loading eBPF program", "type", pt.Type) spec, err := pt.loadSpec(p) if err != nil { return err @@ -132,7 +123,6 @@ func (pt *ProcessTracer) loadTracers() error { return err } - collOpts.Maps = ebpf.MapOptions{PinPath: pt.PinPath} collOpts.Programs = ebpf.ProgramOptions{LogSize: 640 * 1024} if err := spec.LoadAndAssign(p.BpfObjects(), collOpts); err != nil { @@ -151,7 +141,6 @@ func (pt *ProcessTracer) loadTracers() error { return err } - collOpts.Maps = ebpf.MapOptions{PinPath: pt.PinPath} collOpts.Programs = ebpf.ProgramOptions{LogSize: 640 * 1024} err = spec.LoadAndAssign(p.BpfObjects(), collOpts) @@ -266,7 +255,7 @@ func printVerifierErrorInfo(err error) { } } -func RunUtilityTracer(p UtilityTracer, pinPath string) error { +func RunUtilityTracer(p UtilityTracer) error { i := instrumenter{} plog := ptlog() plog.Debug("loading independent eBPF program") @@ -280,8 +269,6 @@ func RunUtilityTracer(p UtilityTracer, pinPath string) error { return err } - collOpts.Maps = ebpf.MapOptions{PinPath: pinPath} - if err := spec.LoadAndAssign(p.BpfObjects(), collOpts); err != nil { printVerifierErrorInfo(err) return fmt.Errorf("loading and assigning BPF objects: %w", err)