@@ -45,13 +45,15 @@ var (
4545 IPv6_HOST_MASK = "/128"
4646 CONNTRACK_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_aws_conntrack_map"
4747 POLICY_EVENTS_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_policy_events"
48+ POLICY_EVENTS_SCOPE_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_policy_events_scope"
4849 CATCH_ALL_PROTOCOL corev1.Protocol = "ANY_IP_PROTOCOL"
4950 POD_VETH_PREFIX = "eni"
5051 POLICIES_APPLIED = 0
5152 DEFAULT_ALLOW = 1
5253 DEFAULT_DENY = 2
5354 POD_STATE_MAP_KEY = 0
5455 CLUSTER_POLICY_POD_STATE_MAP_KEY = 1
56+ POLICY_EVENTS_SCOPE_MAP_KEY = 0
5557 BRANCH_ENI_VETH_PREFIX = "vlan"
5658 INTERFACE_COUNT_UNKNOWN = - 1 // Used when caller doesn't know interface count
5759 INTERFACE_COUNT_DEFAULT = 1 // Default single interface
@@ -85,6 +87,10 @@ type pod_state struct {
8587 state uint8
8688}
8789
90+ type policy_scope struct {
91+ scope uint8
92+ }
93+
8894func msSince (start time.Time ) float64 {
8995 return float64 (time .Since (start ) / time .Millisecond )
9096}
@@ -115,9 +121,10 @@ type BPFContext struct {
115121 conntrackMapInfo goebpfmaps.BpfMap
116122}
117123
118- func NewBpfClient (nodeIP string , enablePolicyEventLogs , enableCloudWatchLogs bool ,
124+ func NewBpfClient (nodeIP string , enablePolicyEventLogs bool , policyEventsLogsScope string , enableCloudWatchLogs bool ,
119125 enableIPv6 bool , conntrackTTL int , conntrackTableSize int , networkPolicyMode string , isMultiNICEnabled bool ) (* bpfClient , error ) {
120126 var conntrackMap goebpfmaps.BpfMap
127+ var policyEventsScopeMap goebpfmaps.BpfMap
121128
122129 ebpfClient := & bpfClient {
123130 // Maps PolicyEndpoint resource to it's eBPF context
@@ -146,7 +153,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
146153 ebpfClient .hostMask = ingressBinary , egressBinary , hostMask
147154
148155 bpfBinaries := []string {eventsBinary , ingressBinary , egressBinary , cliBinary }
149- isConntrackMapPresent , isPolicyEventsMapPresent := false , false
156+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
150157 var err error
151158
152159 ebpfClient .bpfSDKClient = goelf .New ()
@@ -181,7 +188,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
181188 var interfaceNametoIngressPinPath map [string ]string
182189 var interfaceNametoEgressPinPath map [string ]string
183190 eventBufferFD := 0
184- isConntrackMapPresent , isPolicyEventsMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
191+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
185192 ebpfClient .globalMaps , ingressUpdateRequired , egressUpdateRequired , eventsUpdateRequired )
186193 if err != nil {
187194 //Log the error and move on
@@ -196,7 +203,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
196203 // - Current events binary packaged with network policy agent is different than the one installed
197204 // during the previous installation (or)
198205 // - Either Conntrack Map (or) Events Map is currently missing on the node
199- if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent ) {
206+ if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent || ! isPolicyEventsScopeMapPresent ) {
200207 log ().Info ("Install the default global maps" )
201208 eventsProbe := EVENTS_BINARY
202209 if enableIPv6 {
@@ -226,6 +233,10 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
226233 if mapName == AWS_EVENTS_MAP {
227234 eventBufferFD = int (mapInfo .MapFD )
228235 }
236+ if mapName == AWS_EVENTS_SCOPE_MAP {
237+ policyEventsScopeMap = mapInfo
238+ isPolicyEventsScopeMapPresent = true
239+ }
229240 }
230241 }
231242
@@ -244,6 +255,36 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
244255 ebpfClient .conntrackClient = conntrack .NewConntrackClient (conntrackMap , enableIPv6 )
245256 log ().Info ("Initialized Conntrack client" )
246257
258+ //if present update the PolicyEventsScope Map
259+ if isPolicyEventsScopeMapPresent {
260+ recoveredPolicyEventsScopeMap , ok := ebpfClient .globalMaps .Load (POLICY_EVENTS_SCOPE_MAP_PIN_PATH )
261+ if ok {
262+ policyEventsScopeMap = recoveredPolicyEventsScopeMap .(goebpfmaps.BpfMap )
263+ log ().Info ("Derived existing policyEventsScopeMap identifier" )
264+ } else {
265+ log ().Errorf ("Unable to get policyEventsScopeMap post recovery..error: %v" , err )
266+ sdkAPIErr .WithLabelValues ("RecoveryFailed" ).Inc ()
267+ return nil , err
268+ }
269+
270+ key := uint32 (POLICY_EVENTS_SCOPE_MAP_KEY )
271+ scope := uint8 (utils .ACCEPT .Index ())
272+
273+ if policyEventsLogsScope == "DENY" {
274+ scope = uint8 (utils .DENY .Index ())
275+ }
276+
277+ value := policy_scope {scope : scope }
278+ log ().Infof ("Will update Policy Events Scope Map: key=%d value=%v" , key , value )
279+ err := policyEventsScopeMap .CreateUpdateMapEntry (uintptr (unsafe .Pointer (& key )), uintptr (unsafe .Pointer (& value )), 0 )
280+
281+ if err != nil {
282+ log ().Errorf ("Policy Events Scope Map update failed: %v" , err )
283+ sdkAPIErr .WithLabelValues ("updateEbpfMap-policy-events-scope" ).Inc ()
284+ }
285+ log ().Infof ("Updated Policy Events Scope Map: key=%d value=%v" , key , value )
286+ }
287+
247288 if enablePolicyEventLogs {
248289 err = events .ConfigurePolicyEventsLogging (enableCloudWatchLogs , eventBufferFD , enableIPv6 )
249290 if err != nil {
@@ -379,8 +420,8 @@ func checkAndUpdateBPFBinaries(bpfTCClient tc.BpfTc, bpfBinaries []string, hostB
379420}
380421
381422func (l * bpfClient ) recoverBPFState (bpfTCClient tc.BpfTc , eBPFSDKClient goelf.BpfSDKClient , policyEndpointeBPFContext * sync.Map , globalMaps * sync.Map , updateIngressProbe ,
382- updateEgressProbe , updateEventsProbe bool ) (bool , bool , int , map [string ]string , map [string ]string , error ) {
383- isConntrackMapPresent , isPolicyEventsMapPresent := false , false
423+ updateEgressProbe , updateEventsProbe bool ) (bool , bool , bool , int , map [string ]string , map [string ]string , error ) {
424+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
384425 eventsMapFD := 0
385426 var interfaceNametoIngressPinPath = make (map [string ]string )
386427 var interfaceNametoEgressPinPath = make (map [string ]string )
@@ -392,7 +433,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
392433 if err != nil {
393434 log ().Errorf ("failed to recover global maps %v" , err )
394435 sdkAPIErr .WithLabelValues ("RecoverGlobalMaps" ).Inc ()
395- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
436+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
396437 }
397438 log ().Infof ("Total no of global maps recovered count: %d" , len (recoveredGlobalMaps ))
398439 for globalMapName , globalMap := range recoveredGlobalMaps {
@@ -407,6 +448,11 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
407448 eventsMapFD = int (globalMap .MapFD )
408449 log ().Infof ("Policy event Map is already present on the node Recovered FD: %d" , eventsMapFD )
409450 }
451+ if globalMapName == POLICY_EVENTS_SCOPE_MAP_PIN_PATH {
452+ log ().Info ("Policy event scope Map is already present on the node" )
453+ isPolicyEventsScopeMapPresent = true
454+ globalMaps .Store (globalMapName , globalMap )
455+ }
410456 }
411457 }
412458
@@ -505,7 +551,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
505551 if err != nil {
506552 log ().Errorf ("GetAllBpfProgramsAndMaps failed %v" , err )
507553 sdkAPIErr .WithLabelValues ("GetAllBpfProgramsAndMaps" ).Inc ()
508- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
554+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
509555 }
510556 log ().Infof ("GetAllBpfProgramsAndMaps returned %d" , len (bpfState ))
511557 progIdToPinPath := make (map [int ]string )
@@ -543,7 +589,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
543589 log ().Info ("Collected all data for reattaching probes" )
544590 }
545591
546- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
592+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
547593}
548594
549595func (l * bpfClient ) ReAttachEbpfProbes () error {
0 commit comments