Skip to content

Commit

Permalink
OVN EIP gateway: improve lookup speed of IP addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kennelly <mkennell@redhat.com>
  • Loading branch information
martinkennelly committed Dec 9, 2024
1 parent f0f59ba commit 0e0b6ac
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions go-controller/pkg/node/gateway_egressip.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,68 @@ func (e markIPs) containsIP(ip net.IP) bool {
type markIPsCache struct {
mu sync.Mutex
hasSyncOnce bool
cache markIPs
markToIPs markIPs
IPToMark map[string]int
}

func newMarkIPsCache() *markIPsCache {
return &markIPsCache{
mu: sync.Mutex{},
cache: markIPs{
markToIPs: markIPs{
v4: make(map[int]string),
v6: make(map[int]string),
},
IPToMark: map[string]int{},
}
}

func (mic *markIPsCache) IsIPPresent(ip net.IP) bool {
mic.mu.Lock()
defer mic.mu.Unlock()
return mic.cache.containsIP(ip)
if ip == nil {
return false
}
_, isFound := mic.IPToMark[ip.String()]
return isFound
}

func (mic *markIPsCache) insertMarkIP(pktMark util.EgressIPMark, ip net.IP) {
mic.mu.Lock()
mic.cache.insert(pktMark, ip)
mic.mu.Unlock()
defer mic.mu.Unlock()
if ip == nil {
return
}
mic.markToIPs.insert(pktMark, ip)
mic.IPToMark[ip.String()] = pktMark.ToInt()
}

func (mic *markIPsCache) deleteMarkIP(pktMark util.EgressIPMark, ip net.IP) {
mic.mu.Lock()
mic.cache.delete(pktMark, ip)
mic.mu.Unlock()
defer mic.mu.Unlock()
if ip == nil {
return
}
mic.markToIPs.delete(pktMark, ip)
delete(mic.IPToMark, ip.String())
}

func (mic *markIPsCache) replaceAll(markIPs markIPs) {
mic.mu.Lock()
mic.cache = markIPs
mic.markToIPs = markIPs
for mark, ipv4 := range markIPs.v4 {
mic.IPToMark[ipv4] = mark
}
for mark, ipv6 := range markIPs.v6 {
mic.IPToMark[ipv6] = mark
}
mic.mu.Unlock()
}

func (mic *markIPsCache) GetIPv4() map[int]string {
mic.mu.Lock()
defer mic.mu.Unlock()
dupe := make(map[int]string)
for key, value := range mic.cache.v4 {
for key, value := range mic.markToIPs.v4 {
if value == "" {
continue
}
Expand All @@ -131,7 +151,7 @@ func (mic *markIPsCache) GetIPv6() map[int]string {
mic.mu.Lock()
defer mic.mu.Unlock()
dupe := make(map[int]string)
for key, value := range mic.cache.v6 {
for key, value := range mic.markToIPs.v6 {
if value == "" {
continue
}
Expand Down

0 comments on commit 0e0b6ac

Please sign in to comment.