Skip to content

Commit

Permalink
Merge pull request #130 from telekom-mms/feature/remove-nft-updates-f…
Browse files Browse the repository at this point in the history
…rom-excludes

Remove nftables updates from Excludes
  • Loading branch information
hwipl authored Dec 20, 2024
2 parents 30ad7ec + d2eeb2d commit 854d545
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 248 deletions.
90 changes: 25 additions & 65 deletions internal/splitrt/excludes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package splitrt

import (
"context"
"net/netip"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
Expand All @@ -31,9 +29,10 @@ type Excludes struct {
closed chan struct{}
}

// setFilter resets the excludes in netfilter.
func (e *Excludes) setFilter(ctx context.Context) {
log.Debug("SplitRouting resetting excludes in netfilter")
// GetPrefixes returns static and dynamic split excludes as Prefixes.
func (e *Excludes) GetPrefixes() []netip.Prefix {
e.Lock()
defer e.Unlock()

addresses := []netip.Prefix{}
for _, v := range e.s {
Expand All @@ -43,18 +42,18 @@ func (e *Excludes) setFilter(ctx context.Context) {
prefix := netip.PrefixFrom(k, k.BitLen())
addresses = append(addresses, prefix)
}
setExcludes(ctx, addresses)

return addresses
}

// AddStatic adds a static entry to the split excludes.
func (e *Excludes) AddStatic(ctx context.Context, address netip.Prefix) {
func (e *Excludes) AddStatic(address netip.Prefix) bool {
log.WithField("address", address).Debug("SplitRouting adding static exclude")
e.Lock()
defer e.Unlock()

// make sure new prefix in address does not overlap with existing
// prefixes in static excludes
removed := false
for k, v := range e.s {
if !v.Overlaps(address) {
// no overlap
Expand All @@ -63,38 +62,31 @@ func (e *Excludes) AddStatic(ctx context.Context, address netip.Prefix) {
if v.Bits() <= address.Bits() {
// new prefix is already in existing prefix,
// do not add it
return
return false
}

// new prefix contains old prefix, remove old prefix
delete(e.s, k)
removed = true
}

// add new prefix to static excludes
key := address.String()
e.s[key] = address

// add to netfilter
if removed {
// existing entries removed, we need to reset all excludes
e.setFilter(ctx)
return
}
// single new entry, add it
addExclude(ctx, address)
// update netfilter
return true
}

// AddDynamic adds a dynamic entry to the split excludes.
func (e *Excludes) AddDynamic(ctx context.Context, address netip.Prefix, ttl uint32) {
func (e *Excludes) AddDynamic(address netip.Prefix, ttl uint32) bool {
log.WithFields(log.Fields{
"address": address,
"ttl": ttl,
}).Debug("SplitRouting adding dynamic exclude")

if !address.IsSingleIP() {
log.Error("SplitRouting error adding dynamic exclude with multiple IPs")
return
return false
}
a := address.Addr()

Expand All @@ -104,7 +96,7 @@ func (e *Excludes) AddDynamic(ctx context.Context, address netip.Prefix, ttl uin
// make sure new ip address is not in existing static excludes
for _, v := range e.s {
if v.Contains(a) {
return
return false
}
}

Expand All @@ -113,7 +105,7 @@ func (e *Excludes) AddDynamic(ctx context.Context, address netip.Prefix, ttl uin
if old != nil {
old.ttl = ttl
old.updated = true
return
return false
}

// create new entry in dynamic excludes
Expand All @@ -122,21 +114,25 @@ func (e *Excludes) AddDynamic(ctx context.Context, address netip.Prefix, ttl uin
updated: true,
}

// add to netfilter
addExclude(ctx, address)
// update netfilter
return true
}

// RemoveStatic removes a static entry from the split excludes.
func (e *Excludes) RemoveStatic(ctx context.Context, address netip.Prefix) {
func (e *Excludes) RemoveStatic(address netip.Prefix) bool {
e.Lock()
defer e.Unlock()

delete(e.s, address.String())
e.setFilter(ctx)
addr := address.String()
if _, ok := e.s[addr]; !ok {
return false
}
delete(e.s, addr)
return true
}

// cleanup cleans up the dynamic split excludes.
func (e *Excludes) cleanup(ctx context.Context) {
func (e *Excludes) cleanup() bool {
e.Lock()
defer e.Unlock()

Expand All @@ -160,43 +156,7 @@ func (e *Excludes) cleanup(ctx context.Context) {
}

// if entries were changed, reset netfilter
if changed {
e.setFilter(ctx)
}
}

// start starts periodic cleanup of the split excludes.
func (e *Excludes) start() {
defer close(e.closed)

ctx := context.Background()
timer := time.NewTimer(excludesTimer * time.Second)
for {
select {
case <-timer.C:
e.cleanup(ctx)
timer.Reset(excludesTimer * time.Second)

case <-e.done:
if !timer.Stop() {
<-timer.C
}
return
}
}
}

// Start starts periodic cleanup of the split excludes.
func (e *Excludes) Start() {
log.Debug("SplitRouting starting periodic cleanup of excludes")
go e.start()
}

// Stop stops periodic cleanup of the split excludes.
func (e *Excludes) Stop() {
close(e.done)
<-e.closed
log.Debug("SplitRouting stopped periodic cleanup of excludes")
return changed
}

// List returns the list of static and dynamic excludes.
Expand Down
Loading

0 comments on commit 854d545

Please sign in to comment.