@@ -17,7 +17,6 @@ import (
1717 "golang.org/x/sys/unix"
1818 "k8s.io/apimachinery/pkg/util/sets"
1919
20- "github.com/cilium/cilium/pkg/bpf"
2120 cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
2221 "github.com/cilium/cilium/pkg/loadbalancer"
2322 "github.com/cilium/cilium/pkg/maps/lbmap"
@@ -69,7 +68,13 @@ type bpfOps struct {
6968 backendIDAlloc idAllocator
7069 restoredBackendIDs sets.Set [loadbalancer.BackendID ]
7170
72- backendStates map [loadbalancer.L3n4Addr ]backendState
71+ // backendStates maps from backend address to associated state.
72+ // This is used to track which frontends reference a specific backend
73+ // in order to delete orphaned backeds.
74+ backendStates map [loadbalancer.L3n4Addr ]backendState
75+
76+ // backendReferences maps from frontend address to the set of referenced
77+ // backends.
7378 backendReferences map [loadbalancer.L3n4Addr ]sets.Set [loadbalancer.L3n4Addr ]
7479
7580 // nodePortAddrs are the last used NodePort addresses for a given NodePort
@@ -107,6 +112,9 @@ func (ops *bpfOps) start(_ cell.HookContext) error {
107112 // Restore the ID allocations from the BPF maps in order to reuse
108113 // them and thus avoiding traffic disruptions.
109114 err := ops .lbmaps .DumpService (func (key lbmap.ServiceKey , value lbmap.ServiceValue ) {
115+ if key .GetBackendSlot () != 0 {
116+ return
117+ }
110118 id := loadbalancer .ID (value .GetRevNat ())
111119 ops .serviceIDAlloc .addID (svcKeyToAddr (key ), id )
112120 ops .restoredServiceIDs .Insert (id )
@@ -134,10 +142,10 @@ func svcKeyToAddr(svcKey lbmap.ServiceKey) loadbalancer.L3n4Addr {
134142}
135143
136144func beValueToAddr (beValue lbmap.BackendValue ) loadbalancer.L3n4Addr {
137- feIP := beValue .GetAddress ()
138- feAddrCluster := cmtypes .MustAddrClusterFromIP (feIP )
139- feL3n4Addr := loadbalancer .NewL3n4Addr (loadbalancer .TCP /* FIXME */ , feAddrCluster , beValue .GetPort (), 0 )
140- return * feL3n4Addr
145+ beIP := beValue .GetAddress ()
146+ beAddrCluster := cmtypes .MustAddrClusterFromIP (beIP )
147+ beL3n4Addr := loadbalancer .NewL3n4Addr (loadbalancer .TCP /* FIXME */ , beAddrCluster , beValue .GetPort (), 0 )
148+ return * beL3n4Addr
141149}
142150
143151// Delete implements reconciler.Operations.
@@ -233,11 +241,10 @@ func (ops *bpfOps) deleteFrontend(fe *Frontend) error {
233241
234242func (ops * bpfOps ) pruneServiceMaps () error {
235243 toDelete := []lbmap.ServiceKey {}
236- svcCB := func (key bpf.MapKey , value bpf.MapValue ) {
237- svcKey := key .(lbmap.ServiceKey ).ToHost ()
244+ svcCB := func (svcKey lbmap.ServiceKey , _ lbmap.ServiceValue ) {
238245 ac , ok := cmtypes .AddrClusterFromIP (svcKey .GetAddress ())
239246 if ! ok {
240- ops .log .Warn ("Prune: bad address in service key" , "key" , key )
247+ ops .log .Warn ("Prune: bad address in service key" , "key" , svcKey )
241248 return
242249 }
243250 addr := loadbalancer.L3n4Addr {
@@ -246,34 +253,34 @@ func (ops *bpfOps) pruneServiceMaps() error {
246253 Scope : svcKey .GetScope (),
247254 }
248255 if _ , ok := ops .backendReferences [addr ]; ! ok {
249- toDelete = append (toDelete , svcKey )
256+ toDelete = append (toDelete , svcKey . ToNetwork () )
250257 }
251258 }
252- lbmap .Service4MapV2 .DumpWithCallback (svcCB )
253- lbmap .Service6MapV2 .DumpWithCallback (svcCB )
259+ if err := ops .lbmaps .DumpService (svcCB ); err != nil {
260+ ops .log .Warn ("Failed to prune service maps" , "error" , err )
261+ }
254262
255263 for _ , key := range toDelete {
256- if err := key . MapDelete ( ); err != nil {
257- ops .log .Warn ("Failed to delete from service map" , "error" , err )
264+ if err := ops . lbmaps . DeleteService ( key ); err != nil {
265+ ops .log .Warn ("Failed to delete from service map while pruning " , "error" , err )
258266 }
259267 }
260268 return nil
261269}
262270
263271func (ops * bpfOps ) pruneBackendMaps () error {
264272 toDelete := []lbmap.BackendKey {}
265- beCB := func (key bpf.MapKey , value bpf.MapValue ) {
266- beKey := key .(lbmap.BackendKey )
267- beValue := value .(lbmap.BackendValue ).ToHost ()
273+ beCB := func (beKey lbmap.BackendKey , beValue lbmap.BackendValue ) {
268274 if _ , ok := ops .backendStates [beValueToAddr (beValue )]; ! ok {
269275 ops .log .Info ("pruneBackendMaps: deleting" , "id" , beKey .GetID (), "addr" , beValueToAddr (beValue ))
270276 toDelete = append (toDelete , beKey )
271277 }
272278 }
273- lbmap .Backend4MapV3 .DumpWithCallback (beCB )
274- lbmap .Backend6MapV3 .DumpWithCallback (beCB )
279+ if err := ops .lbmaps .DumpBackend (beCB ); err != nil {
280+ ops .log .Warn ("Failed to prune backend maps" , "error" , err )
281+ }
275282 for _ , key := range toDelete {
276- if err := key . Map (). Delete (key ); err != nil {
283+ if err := ops . lbmaps . DeleteBackend (key ); err != nil {
277284 ops .log .Warn ("Failed to delete from backend map" , "error" , err )
278285 }
279286 }
@@ -298,8 +305,8 @@ func (ops *bpfOps) pruneRestoredIDs() error {
298305 }
299306 }
300307
301- ops .restoredServiceIDs . Clear ()
302- ops .restoredBackendIDs . Clear ()
308+ ops .restoredServiceIDs = nil
309+ ops .restoredBackendIDs = nil
303310
304311 return nil
305312}
@@ -348,10 +355,6 @@ func (ops *bpfOps) Update(_ context.Context, _ statedb.ReadTxn, fe *Frontend) er
348355 fe .Type == loadbalancer .SVCTypeHostPort && fe .Address .AddrCluster .IsUnspecified () {
349356 // For NodePort create entries for each node address.
350357 // For HostPort only create them if the address was not specified (HostIP is unset).
351- // TODO: HostPort loopback?
352- // TODO: When the nodeport addresses change trigger a full refresh by marking everything as
353- // pending?
354-
355358 old := sets .New (ops .nodePortAddrs [fe .Address .Port ]... )
356359 for _ , addr := range fe .nodePortAddrs {
357360 if fe .Address .IsIPv6 () != addr .Is6 () {
@@ -414,16 +417,15 @@ func (ops *bpfOps) updateFrontend(fe *Frontend) error {
414417 svc := fe .Service ()
415418 flag := loadbalancer .NewSvcFlag (& loadbalancer.SvcFlagParam {
416419 SvcType : fe .Type ,
420+ SvcNatPolicy : svc .NatPolicy ,
417421 SvcExtLocal : svc .ExtTrafficPolicy == loadbalancer .SVCTrafficPolicyLocal ,
418422 SvcIntLocal : svc .IntTrafficPolicy == loadbalancer .SVCTrafficPolicyLocal ,
419- SvcNatPolicy : svc .NatPolicy ,
420423 SessionAffinity : svc .SessionAffinity ,
421424 IsRoutable : isRoutable ,
425+ CheckSourceRange : len (svc .SourceRanges ) > 0 ,
422426 L7LoadBalancer : svc .L7ProxyPort != 0 ,
423427 LoopbackHostport : svc .LoopbackHostPort ,
424-
425- // TODO:
426- //CheckSourceRange: checkSourceRange,
428+ Quarantined : false ,
427429 })
428430 svcVal .SetFlags (flag .UInt16 ())
429431 svcVal .SetRevNat (int (feID ))
@@ -568,7 +570,7 @@ func (ops *bpfOps) upsertMaster(svcKey lbmap.ServiceKey, svcVal lbmap.ServiceVal
568570func (ops * bpfOps ) cleanupSlots (svcKey lbmap.ServiceKey , oldCount , newCount int ) error {
569571 for i := newCount ; i < oldCount ; i ++ {
570572 svcKey .SetBackendSlot (i + 1 )
571- _ , err := svcKey . Map (). SilentDelete (svcKey .ToNetwork ())
573+ err := ops . lbmaps . DeleteService (svcKey .ToNetwork ())
572574 if err != nil {
573575 return fmt .Errorf ("cleanup service slot %q: %w" , svcKey .String (), err )
574576 }
@@ -604,7 +606,7 @@ func (ops *bpfOps) deleteBackend(ipv6 bool, id loadbalancer.BackendID) error {
604606 } else {
605607 key = lbmap .NewBackend4KeyV3 (id )
606608 }
607- _ , err := key . Map (). SilentDelete (key )
609+ err := ops . lbmaps . DeleteBackend (key )
608610 if err != nil {
609611 return fmt .Errorf ("delete backend %d: %w" , id , err )
610612 }
@@ -622,7 +624,7 @@ func (ops *bpfOps) upsertAffinityMatch(id loadbalancer.ID, beID loadbalancer.Bac
622624 }
623625 var value lbmap.AffinityMatchValue
624626 ops .log .Info ("upsertAffinityMatch" , "key" , key )
625- return lbmap . AffinityMatchMap . Update (key .ToNetwork (), & value )
627+ return ops . lbmaps . UpdateAffinityMatch (key .ToNetwork (), & value )
626628}
627629
628630func (ops * bpfOps ) deleteAffinityMatch (id loadbalancer.ID , beID loadbalancer.BackendID ) error {
@@ -635,8 +637,7 @@ func (ops *bpfOps) deleteAffinityMatch(id loadbalancer.ID, beID loadbalancer.Bac
635637 RevNATID : uint16 (id ),
636638 }
637639 ops .log .Info ("deleteAffinityMatch" , "serviceID" , id , "backendID" , beID )
638- _ , err := lbmap .AffinityMatchMap .SilentDelete (key .ToNetwork ())
639- return err
640+ return ops .lbmaps .DeleteAffinityMatch (key .ToNetwork ())
640641}
641642
642643func (ops * bpfOps ) upsertRevNat (id loadbalancer.ID , svcKey lbmap.ServiceKey , svcVal lbmap.ServiceValue ) error {
0 commit comments