@@ -44,19 +44,25 @@ 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
63+ POLICY_EVENTS_SCOPE_MAP_DENY = 0
64+ POLICY_EVENTS_SCOPE_MAP_ACCEPT = 1
65+ POLICY_EVENTS_SCOPE_DENY_ARG_VAL = "deny"
6066 BRANCH_ENI_VETH_PREFIX = "vlan"
6167 INTERFACE_COUNT_UNKNOWN = - 1 // Used when caller doesn't know interface count
6268 INTERFACE_COUNT_DEFAULT = 1 // Default single interface
@@ -90,6 +96,10 @@ type pod_state struct {
9096 state uint8
9197}
9298
99+ type policy_scope struct {
100+ scope uint8
101+ }
102+
93103func msSince (start time.Time ) float64 {
94104 return float64 (time .Since (start ) / time .Millisecond )
95105}
@@ -118,9 +128,10 @@ type BPFContext struct {
118128 conntrackMapInfo goebpfmaps.BpfMap
119129}
120130
121- func NewBpfClient (nodeIP string , enablePolicyEventLogs , enableCloudWatchLogs bool ,
131+ func NewBpfClient (nodeIP string , enablePolicyEventLogs bool , policyEventsLogsScope string , enableCloudWatchLogs bool ,
122132 enableIPv6 bool , conntrackTTL int , conntrackTableSize int , networkPolicyMode string , isMultiNICEnabled bool ) (* bpfClient , error ) {
123133 var conntrackMap goebpfmaps.BpfMap
134+ var policyEventsScopeMap goebpfmaps.BpfMap
124135
125136 ebpfClient := & bpfClient {
126137 // Maps PolicyEndpoint resource to it's eBPF context
@@ -147,7 +158,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
147158 ebpfClient .hostMask = ingressBinary , egressBinary , hostMask
148159
149160 bpfBinaries := []string {eventsBinary , ingressBinary , egressBinary , cliBinary }
150- isConntrackMapPresent , isPolicyEventsMapPresent := false , false
161+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
151162 var err error
152163
153164 ebpfClient .bpfSDKClient = goelf .New ()
@@ -182,7 +193,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
182193 var interfaceNametoIngressPinPath map [string ]string
183194 var interfaceNametoEgressPinPath map [string ]string
184195 eventBufferFD := 0
185- isConntrackMapPresent , isPolicyEventsMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
196+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventBufferFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err = ebpfClient .recoverBPFState (ebpfClient .bpfTCClient , ebpfClient .bpfSDKClient , ebpfClient .policyEndpointeBPFContext ,
186197 ebpfClient .globalMaps , ingressUpdateRequired , egressUpdateRequired , eventsUpdateRequired )
187198 if err != nil {
188199 //Log the error and move on
@@ -197,7 +208,7 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
197208 // - Current events binary packaged with network policy agent is different than the one installed
198209 // during the previous installation (or)
199210 // - Either Conntrack Map (or) Events Map is currently missing on the node
200- if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent ) {
211+ if eventsUpdateRequired || (! isConntrackMapPresent || ! isPolicyEventsMapPresent || ! isPolicyEventsScopeMapPresent ) {
201212 log ().Info ("Install the default global maps" )
202213 eventsProbe := EVENTS_BINARY
203214 if enableIPv6 {
@@ -227,6 +238,10 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
227238 if mapName == AWS_EVENTS_MAP {
228239 eventBufferFD = int (mapInfo .MapFD )
229240 }
241+ if mapName == AWS_EVENTS_SCOPE_MAP {
242+ policyEventsScopeMap = mapInfo
243+ isPolicyEventsScopeMapPresent = true
244+ }
230245 }
231246 }
232247
@@ -245,6 +260,36 @@ func NewBpfClient(nodeIP string, enablePolicyEventLogs, enableCloudWatchLogs boo
245260 ebpfClient .conntrackClient = conntrack .NewConntrackClient (conntrackMap , enableIPv6 )
246261 log ().Info ("Initialized Conntrack client" )
247262
263+ //if present update the PolicyEventsScope Map
264+ if isPolicyEventsScopeMapPresent {
265+ recoveredPolicyEventsScopeMap , ok := ebpfClient .globalMaps .Load (POLICY_EVENTS_SCOPE_MAP_PIN_PATH )
266+ if ok {
267+ policyEventsScopeMap = recoveredPolicyEventsScopeMap .(goebpfmaps.BpfMap )
268+ log ().Info ("Derived existing policyEventsScopeMap identifier" )
269+ } else {
270+ log ().Errorf ("Unable to get policyEventsScopeMap post recovery..error: %v" , err )
271+ sdkAPIErr .WithLabelValues ("RecoveryFailed" ).Inc ()
272+ return nil , err
273+ }
274+
275+ key := uint32 (POLICY_EVENTS_SCOPE_MAP_KEY )
276+ scope := uint8 (POLICY_EVENTS_SCOPE_MAP_ACCEPT )
277+
278+ if policyEventsLogsScope == POLICY_EVENTS_SCOPE_DENY_ARG_VAL {
279+ scope = uint8 (POLICY_EVENTS_SCOPE_MAP_DENY )
280+ }
281+
282+ value := policy_scope {scope : scope }
283+ log ().Infof ("Will update Policy Events Scope Map: key=%d value=%v" , key , value )
284+ err := policyEventsScopeMap .CreateUpdateMapEntry (uintptr (unsafe .Pointer (& key )), uintptr (unsafe .Pointer (& value )), 0 )
285+
286+ if err != nil {
287+ log ().Errorf ("Policy Events Scope Map update failed: %v" , err )
288+ sdkAPIErr .WithLabelValues ("updateEbpfMap-policy-events-scope" ).Inc ()
289+ }
290+ log ().Infof ("Updated Policy Events Scope Map: key=%d value=%v" , key , value )
291+ }
292+
248293 if enablePolicyEventLogs {
249294 err = events .ConfigurePolicyEventsLogging (enableCloudWatchLogs , eventBufferFD , enableIPv6 )
250295 if err != nil {
@@ -376,8 +421,8 @@ func checkAndUpdateBPFBinaries(bpfTCClient tc.BpfTc, bpfBinaries []string, hostB
376421}
377422
378423func (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
424+ updateEgressProbe , updateEventsProbe bool ) (bool , bool , bool , int , map [string ]string , map [string ]string , error ) {
425+ isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent := false , false , false
381426 eventsMapFD := 0
382427 var interfaceNametoIngressPinPath = make (map [string ]string )
383428 var interfaceNametoEgressPinPath = make (map [string ]string )
@@ -389,7 +434,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
389434 if err != nil {
390435 log ().Errorf ("failed to recover global maps %v" , err )
391436 sdkAPIErr .WithLabelValues ("RecoverGlobalMaps" ).Inc ()
392- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
437+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
393438 }
394439 log ().Infof ("Total no of global maps recovered count: %d" , len (recoveredGlobalMaps ))
395440 for globalMapName , globalMap := range recoveredGlobalMaps {
@@ -404,6 +449,11 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
404449 eventsMapFD = int (globalMap .MapFD )
405450 log ().Infof ("Policy event Map is already present on the node Recovered FD: %d" , eventsMapFD )
406451 }
452+ if globalMapName == POLICY_EVENTS_SCOPE_MAP_PIN_PATH {
453+ log ().Info ("Policy event scope Map is already present on the node" )
454+ isPolicyEventsScopeMapPresent = true
455+ globalMaps .Store (globalMapName , globalMap )
456+ }
407457 }
408458 }
409459
@@ -472,7 +522,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
472522 if err != nil {
473523 log ().Errorf ("GetAllBpfProgramsAndMaps failed %v" , err )
474524 sdkAPIErr .WithLabelValues ("GetAllBpfProgramsAndMaps" ).Inc ()
475- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
525+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , err
476526 }
477527 log ().Infof ("GetAllBpfProgramsAndMaps returned %d" , len (bpfState ))
478528 progIdToPinPath := make (map [int ]string )
@@ -510,7 +560,7 @@ func (l *bpfClient) recoverBPFState(bpfTCClient tc.BpfTc, eBPFSDKClient goelf.Bp
510560 log ().Info ("Collected all data for reattaching probes" )
511561 }
512562
513- return isConntrackMapPresent , isPolicyEventsMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
563+ return isConntrackMapPresent , isPolicyEventsMapPresent , isPolicyEventsScopeMapPresent , eventsMapFD , interfaceNametoIngressPinPath , interfaceNametoEgressPinPath , nil
514564}
515565
516566func (l * bpfClient ) ReAttachEbpfProbes () error {
0 commit comments