@@ -44,19 +44,22 @@ var (
4444 TC_EGRESS_POD_STATE_MAP = "egress_pod_state_map"
4545 AWS_CONNTRACK_MAP = "aws_conntrack_map"
4646 AWS_EVENTS_MAP = "policy_events"
47+ AWS_EVENTS_SCOPE_MAP = "policy_events_scope"
4748 EKS_CLI_BINARY = "aws-eks-na-cli"
4849 EKS_V6_CLI_BINARY = "aws-eks-na-cli-v6"
4950 hostBinaryPath = "/host/opt/cni/bin/"
5051 IPv4_HOST_MASK = "/32"
5152 IPv6_HOST_MASK = "/128"
5253 CONNTRACK_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_aws_conntrack_map"
5354 POLICY_EVENTS_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_policy_events"
55+ POLICY_EVENTS_SCOPE_MAP_PIN_PATH = "/sys/fs/bpf/globals/aws/maps/global_policy_events_scope"
5456 CATCH_ALL_PROTOCOL corev1.Protocol = "ANY_IP_PROTOCOL"
5557 POD_VETH_PREFIX = "eni"
5658 POLICIES_APPLIED = 0
5759 DEFAULT_ALLOW = 1
5860 DEFAULT_DENY = 2
5961 POD_STATE_MAP_KEY = 0
62+ POLICY_EVENTS_SCOPE_MAP_KEY = 0
6063 BRANCH_ENI_VETH_PREFIX = "vlan"
6164 INTERFACE_COUNT_UNKNOWN = - 1 // Used when caller doesn't know interface count
6265 INTERFACE_COUNT_DEFAULT = 1 // Default single interface
@@ -90,6 +93,10 @@ type pod_state struct {
9093 state uint8
9194}
9295
96+ type policy_scope struct {
97+ scope uint8
98+ }
99+
93100func msSince (start time.Time ) float64 {
94101 return float64 (time .Since (start ) / time .Millisecond )
95102}
@@ -118,9 +125,10 @@ type BPFContext struct {
118125 conntrackMapInfo goebpfmaps.BpfMap
119126}
120127
121- func NewBpfClient (nodeIP string , enablePolicyEventLogs , enableCloudWatchLogs bool ,
128+ func NewBpfClient (nodeIP string , enablePolicyEventLogs bool , policyEventsLogsScope string , enableCloudWatchLogs bool ,
122129 enableIPv6 bool , conntrackTTL int , conntrackTableSize int , networkPolicyMode string , isMultiNICEnabled bool ) (* bpfClient , error ) {
123130 var conntrackMap goebpfmaps.BpfMap
131+ var policyEventsScopeMap goebpfmaps.BpfMap
124132
125133 ebpfClient := & bpfClient {
126134 // Maps PolicyEndpoint resource to it's eBPF context
@@ -147,7 +155,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
147155 ebpfClient .hostMask = ingressBinary , egressBinary , hostMask
148156
149157 bpfBinaries := []string {eventsBinary , ingressBinary , egressBinary , cliBinary }
150- isConntrackMapPresent , isPolicyEventsMapPresent := false , false
158+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
151159 var err error
152160
153161 ebpfClient .bpfSDKClient = goelf .New ()
@@ -182,7 +190,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
182190 var interfaceNametoIngressPinPath map [string ]string
183191 var interfaceNametoEgressPinPath map [string ]string
184192 eventBufferFD := 0
185- isConntrackMapPresent , isPolicyEventsMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
193+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
186194 ebpfClient .globalMaps , ingressUpdateRequired , egressUpdateRequired , eventsUpdateRequired )
187195 if err != nil {
188196 //Log the error and move on
@@ -197,7 +205,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
197205 // - Current events binary packaged with network policy agent is different than the one installed
198206 // during the previous installation (or)
199207 // - Either Conntrack Map (or) Events Map is currently missing on the node
200- if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent ) {
208+ if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent || ! isPolicyEventsScopeMapPresent ) {
201209 log ().Info ("Install the default global maps" )
202210 eventsProbe := EVENTS_BINARY
203211 if enableIPv6 {
@@ -227,6 +235,10 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
227235 if mapName == AWS_EVENTS_MAP {
228236 eventBufferFD = int (mapInfo .MapFD )
229237 }
238+ if mapName == AWS_EVENTS_SCOPE_MAP {
239+ policyEventsScopeMap = mapInfo
240+ isPolicyEventsScopeMapPresent = true
241+ }
230242 }
231243 }
232244
@@ -245,6 +257,36 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
245257 ebpfClient .conntrackClient = conntrack .NewConntrackClient (conntrackMap , enableIPv6 )
246258 log ().Info ("Initialized Conntrack client" )
247259
260+ //if present update the PolicyEventsScope Map
261+ if isPolicyEventsScopeMapPresent {
262+ recoveredPolicyEventsScopeMap , ok := ebpfClient .globalMaps .Load (POLICY_EVENTS_SCOPE_MAP_PIN_PATH )
263+ if ok {
264+ policyEventsScopeMap = recoveredPolicyEventsScopeMap .(goebpfmaps.BpfMap )
265+ log ().Info ("Derived existing policyEventsScopeMap identifier" )
266+ } else {
267+ log ().Errorf ("Unable to get policyEventsScopeMap post recovery..error: %v" , err )
268+ sdkAPIErr .WithLabelValues ("RecoveryFailed" ).Inc ()
269+ return nil , err
270+ }
271+
272+ key := uint32 (POLICY_EVENTS_SCOPE_MAP_KEY )
273+ scope := uint8 (utils .ACCEPT .Index ())
274+
275+ if policyEventsLogsScope == "DENY" {
276+ scope = uint8 (utils .DENY .Index ())
277+ }
278+
279+ value := policy_scope {scope : scope }
280+ log ().Infof ("Will update Policy Events Scope Map: key=%d value=%v" , key , value )
281+ err := policyEventsScopeMap .CreateUpdateMapEntry (uintptr (unsafe .Pointer (& key )), uintptr (unsafe .Pointer (& value )), 0 )
282+
283+ if err != nil {
284+ log ().Errorf ("Policy Events Scope Map update failed: %v" , err )
285+ sdkAPIErr .WithLabelValues ("updateEbpfMap-policy-events-scope" ).Inc ()
286+ }
287+ log ().Infof ("Updated Policy Events Scope Map: key=%d value=%v" , key , value )
288+ }
289+
248290 if enablePolicyEventLogs {
249291 err = events .ConfigurePolicyEventsLogging (enableCloudWatchLogs , eventBufferFD , enableIPv6 )
250292 if err != nil {
@@ -376,8 +418,8 @@ func checkAndUpdateBPFBinaries(bpfTCClient tc.BpfTc, bpfBinaries []string, hostB
376418}
377419
378420func (l * bpfClient ) recoverBPFState (bpfTCClient tc.BpfTc , eBPFSDKClient goelf.BpfSDKClient , policyEndpointeBPFContext * sync.Map , globalMaps * sync.Map , updateIngressProbe ,
379- updateEgressProbe , updateEventsProbe bool ) (bool , bool , int , map [string ]string , map [string ]string , error ) {
380- isConntrackMapPresent , isPolicyEventsMapPresent := false , false
421+ updateEgressProbe , updateEventsProbe bool ) (bool , bool , bool , int , map [string ]string , map [string ]string , error ) {
422+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
381423 eventsMapFD := 0
382424 var interfaceNametoIngressPinPath = make (map [string ]string )
383425 var interfaceNametoEgressPinPath = make (map [string ]string )
@@ -389,7 +431,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
389431 if err != nil {
390432 log ().Errorf ("failed to recover global maps %v" , err )
391433 sdkAPIErr .WithLabelValues ("RecoverGlobalMaps" ).Inc ()
392- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
434+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
393435 }
394436 log ().Infof ("Total no of global maps recovered count: %d" , len (recoveredGlobalMaps ))
395437 for globalMapName , globalMap := range recoveredGlobalMaps {
@@ -404,6 +446,11 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
404446 eventsMapFD = int (globalMap .MapFD )
405447 log ().Infof ("Policy event Map is already present on the node Recovered FD: %d" , eventsMapFD )
406448 }
449+ if globalMapName == POLICY_EVENTS_SCOPE_MAP_PIN_PATH {
450+ log ().Info ("Policy event scope Map is already present on the node" )
451+ isPolicyEventsScopeMapPresent = true
452+ globalMaps .Store (globalMapName , globalMap )
453+ }
407454 }
408455 }
409456
@@ -472,7 +519,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
472519 if err != nil {
473520 log ().Errorf ("GetAllBpfProgramsAndMaps failed %v" , err )
474521 sdkAPIErr .WithLabelValues ("GetAllBpfProgramsAndMaps" ).Inc ()
475- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
522+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
476523 }
477524 log ().Infof ("GetAllBpfProgramsAndMaps returned %d" , len (bpfState ))
478525 progIdToPinPath := make (map [int ]string )
@@ -510,7 +557,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
510557 log ().Info ("Collected all data for reattaching probes" )
511558 }
512559
513- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
560+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
514561}
515562
516563func (l * bpfClient ) ReAttachEbpfProbes () error {
0 commit comments