@@ -15,20 +15,24 @@ import (
1515 "github.com/netobserv/netobserv-ebpf-agent/pkg/flow"
1616 "github.com/sirupsen/logrus"
1717 "github.com/vishvananda/netlink"
18+ "golang.org/x/sys/unix"
1819)
1920
2021// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
2122//go:generate bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../../bpf/flows.c -- -I../../bpf/headers
2223
2324const (
2425 qdiscType = "clsact"
26+ // constants defined in flows.c as "volatile const"
27+ constSampling = "sampling"
2528)
2629
2730var log = logrus .WithField ("component" , "ebpf.FlowTracer" )
2831
2932// FlowTracer reads and forwards the Flows from the Transmission Control, for a given interface.
3033type FlowTracer struct {
3134 interfaceName string
35+ sampling uint32
3236 objects bpfObjects
3337 qdisc * netlink.GenericQdisc
3438 egressFilter * netlink.BpfFilter
@@ -37,10 +41,11 @@ type FlowTracer struct {
3741}
3842
3943// NewFlowTracer fo a given interface type
40- func NewFlowTracer (iface string ) * FlowTracer {
44+ func NewFlowTracer (iface string , sampling uint32 ) * FlowTracer {
4145 log .WithField ("iface" , iface ).Debug ("Instantiating flow tracer" )
4246 return & FlowTracer {
4347 interfaceName : iface ,
48+ sampling : sampling ,
4449 }
4550}
4651
@@ -53,9 +58,18 @@ func (m *FlowTracer) Register() error {
5358 if err := rlimit .RemoveMemlock (); err != nil {
5459 return fmt .Errorf ("removing mem lock: %w" , err )
5560 }
56- // Load pre-compiled programs and maps into the kernel.
57- if err := loadBpfObjects (& m .objects , nil ); err != nil {
58- return fmt .Errorf ("loading objects: %w" , err )
61+ // Load pre-compiled programs and maps into the kernel, and rewrites the configuration
62+ spec , err := loadBpf ()
63+ if err != nil {
64+ return fmt .Errorf ("loading BPF data: %w" , err )
65+ }
66+ if err := spec .RewriteConstants (map [string ]interface {}{
67+ constSampling : m .sampling ,
68+ }); err != nil {
69+ return fmt .Errorf ("rewriting BPF constants definition: %w" , err )
70+ }
71+ if err := spec .LoadAndAssign (& m .objects , nil ); err != nil {
72+ return fmt .Errorf ("loading and assigning BPF objects: %w" , err )
5973 }
6074 ipvlan , err := netlink .LinkByName (m .interfaceName )
6175 if err != nil {
@@ -110,7 +124,7 @@ func (m *FlowTracer) Register() error {
110124 LinkIndex : ipvlan .Attrs ().Index ,
111125 Parent : netlink .HANDLE_MIN_INGRESS ,
112126 Handle : netlink .MakeHandle (0 , 1 ),
113- Protocol : 3 ,
127+ Protocol : unix . ETH_P_ALL ,
114128 Priority : 1 ,
115129 }
116130 m .ingressFilter = & netlink.BpfFilter {
0 commit comments