@@ -47,12 +47,15 @@ type FlowTracer struct {
4747 flowsEvictor * sync.Cond
4848 lastEvictionNs uint64
4949 cacheMaxSize int
50+ enableIngress bool
51+ enableEgress bool
5052}
5153
5254// TODO: decouple flowtracer logic from eBPF maps access so we can inject mocks for testing
5355func NewFlowTracer (
5456 sampling , cacheMaxSize , buffersLength int ,
5557 evictionTimeout time.Duration ,
58+ ingress , egress bool ,
5659 namer flow.InterfaceNamer ,
5760) (* FlowTracer , error ) {
5861 if err := rlimit .RemoveMemlock (); err != nil {
@@ -96,6 +99,8 @@ func NewFlowTracer(
9699 flowsEvictor : sync .NewCond (& sync.Mutex {}),
97100 lastEvictionNs : uint64 (monotime .Now ()),
98101 cacheMaxSize : cacheMaxSize ,
102+ enableIngress : ingress ,
103+ enableEgress : egress ,
99104 }, nil
100105}
101106
@@ -129,6 +134,23 @@ func (m *FlowTracer) Register(iface ifaces.Interface) error {
129134 }
130135 m .qdiscs [iface ] = qdisc
131136
137+ if err := m .registerEgress (iface , ipvlan ); err != nil {
138+ return err
139+ }
140+
141+ if err := m .registerIngress (iface , ipvlan ); err != nil {
142+ return err
143+ }
144+
145+ return nil
146+ }
147+
148+ func (m * FlowTracer ) registerEgress (iface ifaces.Interface , ipvlan netlink.Link ) error {
149+ ilog := log .WithField ("iface" , iface )
150+ if ! m .enableEgress {
151+ ilog .Debug ("ignoring egress traffic, according to user configuration" )
152+ return nil
153+ }
132154 // Fetch events on egress
133155 egressAttrs := netlink.FilterAttrs {
134156 LinkIndex : ipvlan .Attrs ().Index ,
@@ -146,15 +168,23 @@ func (m *FlowTracer) Register(iface ifaces.Interface) error {
146168 if err := netlink .FilterDel (egressFilter ); err == nil {
147169 ilog .Warn ("egress filter already existed. Deleted it" )
148170 }
149- if err = netlink .FilterAdd (egressFilter ); err != nil {
171+ if err : = netlink .FilterAdd (egressFilter ); err != nil {
150172 if errors .Is (err , fs .ErrExist ) {
151173 ilog .WithError (err ).Warn ("egress filter already exists. Ignoring" )
152174 } else {
153175 return fmt .Errorf ("failed to create egress filter: %w" , err )
154176 }
155177 }
156178 m .egressFilters [iface ] = egressFilter
179+ return nil
180+ }
157181
182+ func (m * FlowTracer ) registerIngress (iface ifaces.Interface , ipvlan netlink.Link ) error {
183+ ilog := log .WithField ("iface" , iface )
184+ if ! m .enableIngress {
185+ ilog .Debug ("ignoring ingress traffic, according to user configuration" )
186+ return nil
187+ }
158188 // Fetch events on ingress
159189 ingressAttrs := netlink.FilterAttrs {
160190 LinkIndex : ipvlan .Attrs ().Index ,
@@ -172,7 +202,7 @@ func (m *FlowTracer) Register(iface ifaces.Interface) error {
172202 if err := netlink .FilterDel (ingressFilter ); err == nil {
173203 ilog .Warn ("ingress filter already existed. Deleted it" )
174204 }
175- if err = netlink .FilterAdd (ingressFilter ); err != nil {
205+ if err : = netlink .FilterAdd (ingressFilter ); err != nil {
176206 if errors .Is (err , fs .ErrExist ) {
177207 ilog .WithError (err ).Warn ("ingress filter already exists. Ignoring" )
178208 } else {
@@ -376,9 +406,9 @@ func (m *FlowTracer) listenAndForwardRingBuffer(ctx context.Context, forwardFlow
376406// For synchronization purposes, we get/delete a whole snapshot of the flows map.
377407// This way we avoid missing packets that could be updated on the
378408// ebpf side while we process/aggregate them here
379- // Changing this method invaction by BatchLookupAndDelete could improve performance
409+ // Changing this method invocation by BatchLookupAndDelete could improve performance
380410// TODO: detect whether BatchLookupAndDelete is supported (Kernel>=5.6) and use it selectively
381- // Supported Lookup/Delete oprations by kernel: https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md
411+ // Supported Lookup/Delete operations by kernel: https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md
382412func (m * FlowTracer ) lookupAndDeleteFlowsMap () map [flow.RecordKey ][]flow.RecordMetrics {
383413 flowMap := m .objects .AggregatedFlows
384414
0 commit comments