Skip to content

Commit

Permalink
Fix older kernel handling for Fentry
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Jul 25, 2024
1 parent c9b6a78 commit d702083
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/netobserv/netobserv-ebpf-agent)](https://goreportcard.com/report/github.com/netobserv/netobserv-ebpf-agent)

The Network Observability eBPF Agent allows collecting and aggregating all the ingress and
egress flows on a Linux host (required a Kernel 4.18+ with eBPF enabled).
egress flows on a Linux host (required a Kernel 5.8+ with eBPF enabled).

* [How to build](#how-to-build)
* [How to configure](#how-to-configure)
Expand Down
26 changes: 17 additions & 9 deletions pkg/ebpf/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
pcaRecordsMap = "packet_record"
tcEgressFilterName = "tc/tc_egress_flow_parse"
tcIngressFilterName = "tc/tc_ingress_flow_parse"
tcpFentryHook = "tcp_rcv_fentry"
)

var log = logrus.WithField("component", "ebpf.FlowFetcher")
Expand Down Expand Up @@ -85,6 +86,7 @@ type FlowFetcherConfig struct {
FilterConfig *FilterConfig
}

// nolint:cyclop
func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {
if err := rlimit.RemoveMemlock(); err != nil {
log.WithError(err).
Expand Down Expand Up @@ -168,16 +170,20 @@ func NewFlowFetcher(cfg *FlowFetcherConfig) (*FlowFetcher, error) {

var rttFentryLink, rttKprobeLink link.Link
if cfg.EnableRTT {
rttFentryLink, err = link.AttachTracing(link.TracingOptions{
Program: objects.BpfPrograms.TcpRcvFentry,
})
if !oldKernel {
rttFentryLink, err = link.AttachTracing(link.TracingOptions{
Program: objects.BpfPrograms.TcpRcvFentry,
})
}
if err != nil {
log.Warningf("failed to attach the BPF program to tcpReceiveFentry: %v fallback to use kprobe", err)
// try to use kprobe for older kernels
rttKprobeLink, err = link.Kprobe("tcp_rcv_established", objects.TcpRcvKprobe, nil)
if err != nil {
return nil, fmt.Errorf("failed to attach the BPF program to tcpReceiveKprobe: %w", err)
}
// Fall through to use kprobe
}
// try to use kprobe for older kernels
rttKprobeLink, err = link.Kprobe("tcp_rcv_established", objects.TcpRcvKprobe, nil)
if err != nil {
log.Warningf("failed to attach the BPF program to tcpReceiveFentry: %v fallback to use kprobe", err)
return nil, fmt.Errorf("failed to attach the BPF program to tcpReceiveKprobe: %w", err)
}
}

Expand Down Expand Up @@ -727,6 +733,8 @@ func kernelSpecificLoadAndAssign(oldKernel bool, spec *ebpf.CollectionSpec) (Bpf
var newObjects NewBpfObjects
// remove pktdrop hook from the spec
delete(spec.Programs, pktDropHook)
// remove fentry hook from the spec
delete(spec.Programs, tcpFentryHook)
newObjects.NewBpfPrograms = NewBpfPrograms{}
if err := spec.LoadAndAssign(&newObjects, nil); err != nil {
var ve *ebpf.VerifierError
Expand All @@ -752,8 +760,8 @@ func kernelSpecificLoadAndAssign(oldKernel bool, spec *ebpf.CollectionSpec) (Bpf
objects.TcIngressPcaParse = newObjects.TcIngressPcaParse
objects.TcxEgressPcaParse = newObjects.TcxEgressPcaParse
objects.TcxIngressPcaParse = newObjects.TcxIngressPcaParse
objects.TcpRcvFentry = newObjects.TCPRcvFentry
objects.TcpRcvKprobe = newObjects.TCPRcvKprobe
objects.TcpRcvFentry = nil
objects.KfreeSkb = nil
} else {
if err := spec.LoadAndAssign(&objects, nil); err != nil {
Expand Down

0 comments on commit d702083

Please sign in to comment.