Skip to content

Commit

Permalink
service,lbmap: Use consistent hashing for LB maps
Browse files Browse the repository at this point in the history
Currently, it's been implemented in very crude way, just as PoC.

Signed-off-by: Martynas Pumputis <m@lambda.lt>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
brb authored and borkmann committed Sep 12, 2020
1 parent 51605b6 commit 2337d98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
27 changes: 26 additions & 1 deletion pkg/maps/lbmap/lbmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maglev"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/u8proto"

Expand All @@ -50,7 +51,7 @@ type LBBPFMap struct{}
// so that the function can remove obsolete ones.
func (*LBBPFMap) UpsertService(
svcID uint16, svcIP net.IP, svcPort uint16,
backendIDs []uint16, prevBackendCount int,
backends map[string]uint16, prevBackendCount int,
ipv6 bool, svcType loadbalancer.SVCType, svcLocal bool,
svcScope uint8, sessionAffinity bool, sessionAffinityTimeoutSec uint32,
checkSourceRange bool) error {
Expand All @@ -68,6 +69,30 @@ func (*LBBPFMap) UpsertService(

slot := 1
svcVal := svcKey.NewValue().(ServiceValue)

var backendIDs []uint16
// TODO(brb) add param "cHash bool" to control the following
if svcType == loadbalancer.SVCTypeNodePort ||
svcType == loadbalancer.SVCTypeExternalIPs ||
svcType == loadbalancer.SVCTypeLoadBalancer {

backendNames := make([]string, 0, len(backends))
for name := range backends {
backendNames = append(backendNames, name)
}
m := uint64(113) // TODO(brb) configure
table := maglev.GetLookupTable(backendNames, m)
backendIDs = make([]uint16, m)
for i, pos := range table {
backendIDs[i] = backends[backendNames[pos]]
}
} else {
backendIDs = make([]uint16, 0, len(backends))
for _, id := range backends {
backendIDs = append(backendIDs, id)
}
}

for _, backendID := range backendIDs {
if backendID == 0 {
return fmt.Errorf("Invalid backend ID 0")
Expand Down
12 changes: 6 additions & 6 deletions pkg/maps/lbmap/lbmap_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ func NewLBMockMap() *LBMockMap {
}

func (m *LBMockMap) UpsertService(id uint16, ip net.IP, port uint16,
backendIDs []uint16, prevCount int, ipv6 bool, svcType lb.SVCType, svcLocal bool,
backends map[string]uint16, prevCount int, ipv6 bool, svcType lb.SVCType, svcLocal bool,
svcScope uint8, sessionAffinity bool, sessionAffinityTimeoutSec uint32, checkLBSrcRange bool) error {

backends := make([]lb.Backend, len(backendIDs))
for i, backendID := range backendIDs {
backendsList := make([]lb.Backend, 0, len(backends))
for name, backendID := range backends {
b, found := m.BackendByID[backendID]
if !found {
return fmt.Errorf("Backend %d not found", id)
return fmt.Errorf("Backend %s (%d) not found", name, id)
}
backends[i] = *b
backendsList = append(backendsList, *b)
}

svc, found := m.ServiceByID[id]
Expand All @@ -60,7 +60,7 @@ func (m *LBMockMap) UpsertService(id uint16, ip net.IP, port uint16,
return fmt.Errorf("Invalid backends count: %d vs %d", prevCount, len(svc.Backends))
}
}
svc.Backends = backends
svc.Backends = backendsList
svc.SessionAffinity = sessionAffinity
svc.SessionAffinityTimeoutSec = sessionAffinityTimeoutSec

Expand Down
12 changes: 6 additions & 6 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ var (

// LBMap is the interface describing methods for manipulating service maps.
type LBMap interface {
UpsertService(uint16, net.IP, uint16, []uint16, int, bool, lb.SVCType, bool,
uint8, bool, uint32, bool) error
UpsertService(uint16, net.IP, uint16, map[string]uint16, int, bool, lb.SVCType,
bool, uint8, bool, uint32, bool) error
DeleteService(lb.L3n4AddrID, int) error
AddBackend(uint16, net.IP, uint16, bool) error
DeleteBackendByID(uint16, bool) error
Expand Down Expand Up @@ -655,15 +655,15 @@ func (s *Service) upsertServiceIntoLBMaps(svc *svcInfo, onlyLocalBackends bool,
}

// Upsert service entries into BPF maps
backendIDs := make([]uint16, len(svc.backends))
for i, b := range svc.backends {
backendIDs[i] = uint16(b.ID)
backends := make(map[string]uint16, len(svc.backends))
for _, b := range svc.backends {
backends[b.String()] = uint16(b.ID)
}

err := s.lbmap.UpsertService(
uint16(svc.frontend.ID), svc.frontend.L3n4Addr.IP,
svc.frontend.L3n4Addr.L4Addr.Port,
backendIDs, prevBackendCount,
backends, prevBackendCount,
ipv6, svc.svcType, onlyLocalBackends,
svc.frontend.L3n4Addr.Scope,
svc.sessionAffinity, svc.sessionAffinityTimeoutSec,
Expand Down

0 comments on commit 2337d98

Please sign in to comment.