Skip to content

Commit

Permalink
lbmap: Optimize lbmap byte order related code
Browse files Browse the repository at this point in the history
Gets rid of ToNetwork() in DumpParser().

Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
  • Loading branch information
Jianlin-lv authored and tklauser committed Oct 19, 2020
1 parent c5b709b commit 682f682
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 91 deletions.
7 changes: 4 additions & 3 deletions cilium/cmd/bpf_lb_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func dumpSVC(serviceList map[string][]string) {

parseBackendEntry := func(key bpf.MapKey, value bpf.MapValue) {
id := key.(lbmap.BackendKey).GetID()
backendMap[id] = value.DeepCopyMapValue().(lbmap.BackendValue)
backendMap[id] = value.DeepCopyMapValue().(lbmap.BackendValue).ToHost()
}
if err := lbmap.Backend4Map.DumpWithCallbackIfExists(parseBackendEntry); err != nil {
Fatalf("Unable to dump IPv4 backends table: %s", err)
Expand All @@ -65,8 +65,9 @@ func dumpSVC(serviceList map[string][]string) {
var entry string

svcKey := key.(lbmap.ServiceKey)
svcVal := value.(lbmap.ServiceValue)
svc := svcKey.ToNetwork().String()
svcVal := value.(lbmap.ServiceValue).ToHost()
svc := svcKey.String()
svcKey = svcKey.ToHost()
revNATID := svcVal.GetRevNat()
backendID := svcVal.GetBackendID()
flags := loadbalancer.ServiceFlags(svcVal.GetFlags())
Expand Down
11 changes: 2 additions & 9 deletions pkg/maps/lbmap/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,8 @@ var (
int(unsafe.Sizeof(AffinityMatchValue{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
aKey, aVal := mapKey.(*AffinityMatchKey), mapValue.(*AffinityMatchValue)

if _, _, err := bpf.ConvertKeyValue(key, value, aKey, aVal); err != nil {
return nil, nil, err
}

return aKey.ToNetwork(), aVal, nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
Affinity4Map = bpf.NewMap(
Affinity4MapName,
bpf.MapTypeLRUHash,
Expand Down
38 changes: 9 additions & 29 deletions pkg/maps/lbmap/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,8 @@ var (
int(unsafe.Sizeof(Service4Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
svcKey, svcVal := mapKey.(*Service4Key), mapValue.(*Service4Value)

if _, _, err := bpf.ConvertKeyValue(key, value, svcKey, svcVal); err != nil {
return nil, nil, err
}

return svcKey.ToNetwork(), svcVal.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
Backend4Map = bpf.NewMap("cilium_lb4_backends",
bpf.MapTypeHash,
&Backend4Key{},
Expand All @@ -66,15 +59,8 @@ var (
int(unsafe.Sizeof(Backend4Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
backendVal := mapValue.(*Backend4Value)

if _, _, err := bpf.ConvertKeyValue(key, value, mapKey, backendVal); err != nil {
return nil, nil, err
}

return mapKey, backendVal.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
RevNat4Map = bpf.NewMap("cilium_lb4_reverse_nat",
bpf.MapTypeHash,
&RevNat4Key{},
Expand All @@ -83,15 +69,8 @@ var (
int(unsafe.Sizeof(RevNat4Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
revKey, revNat := mapKey.(*RevNat4Key), mapValue.(*RevNat4Value)

if _, _, err := bpf.ConvertKeyValue(key, value, revKey, revNat); err != nil {
return nil, nil, err
}

return revKey.ToNetwork(), revNat.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
)

// +k8s:deepcopy-gen=true
Expand Down Expand Up @@ -186,8 +165,9 @@ func NewService4Key(ip net.IP, port uint16, proto u8proto.U8proto, scope uint8,
}

func (k *Service4Key) String() string {
addr := net.JoinHostPort(k.Address.String(), fmt.Sprintf("%d", k.Port))
if k.Scope == loadbalancer.ScopeInternal {
kHost := k.ToHost().(*Service4Key)
addr := net.JoinHostPort(kHost.Address.String(), fmt.Sprintf("%d", kHost.Port))
if kHost.Scope == loadbalancer.ScopeInternal {
addr += "/i"
}
return addr
Expand Down
40 changes: 10 additions & 30 deletions pkg/maps/lbmap/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,8 @@ var (
int(unsafe.Sizeof(Service6Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
svcKey, svcVal := mapKey.(*Service6Key), mapValue.(*Service6Value)

if _, _, err := bpf.ConvertKeyValue(key, value, svcKey, svcVal); err != nil {
return nil, nil, err
}

return svcKey.ToNetwork(), svcVal.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
Backend6Map = bpf.NewMap("cilium_lb6_backends",
bpf.MapTypeHash,
&Backend6Key{},
Expand All @@ -66,15 +59,8 @@ var (
int(unsafe.Sizeof(Backend6Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
backendVal := mapValue.(*Backend6Value)

if _, _, err := bpf.ConvertKeyValue(key, value, mapKey, backendVal); err != nil {
return nil, nil, err
}

return mapKey, backendVal.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
// RevNat6Map represents the BPF map for reverse NAT in IPv6 load balancer
RevNat6Map = bpf.NewMap("cilium_lb6_reverse_nat",
bpf.MapTypeHash,
Expand All @@ -84,15 +70,8 @@ var (
int(unsafe.Sizeof(RevNat6Value{})),
MaxEntries,
0, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
revKey, revNat := mapKey.(*RevNat6Key), mapValue.(*RevNat6Value)

if _, _, err := bpf.ConvertKeyValue(key, value, revKey, revNat); err != nil {
return nil, nil, err
}

return revKey.ToNetwork(), revNat.ToNetwork(), nil
}).WithCache()
bpf.ConvertKeyValue,
).WithCache()
)

// +k8s:deepcopy-gen=true
Expand Down Expand Up @@ -179,10 +158,11 @@ func NewService6Key(ip net.IP, port uint16, proto u8proto.U8proto, scope uint8,
}

func (k *Service6Key) String() string {
if k.Scope == loadbalancer.ScopeInternal {
return fmt.Sprintf("[%s]:%d/i", k.Address, k.Port)
kHost := k.ToHost().(*Service6Key)
if kHost.Scope == loadbalancer.ScopeInternal {
return fmt.Sprintf("[%s]:%d/i", kHost.Address, kHost.Port)
} else {
return fmt.Sprintf("[%s]:%d", k.Address, k.Port)
return fmt.Sprintf("[%s]:%d", kHost.Address, kHost.Port)
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/maps/lbmap/lbmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (*LBBPFMap) DumpAffinityMatches() (BackendIDByServiceIDSet, error) {
matches := BackendIDByServiceIDSet{}

parse := func(key bpf.MapKey, value bpf.MapValue) {
matchKey := key.DeepCopyMapKey().(*AffinityMatchKey)
matchKey := key.DeepCopyMapKey().(*AffinityMatchKey).ToHost()
svcID := matchKey.RevNATID
backendID := uint16(matchKey.BackendID) // currently backend_id is u16

Expand All @@ -295,7 +295,7 @@ func (*LBBPFMap) DumpAffinityMatches() (BackendIDByServiceIDSet, error) {
func (*LBBPFMap) DumpSourceRanges(ipv6 bool) (SourceRangeSetByServiceID, error) {
ret := SourceRangeSetByServiceID{}
parser := func(key bpf.MapKey, value bpf.MapValue) {
k := key.(SourceRangeKey)
k := key.(SourceRangeKey).ToHost()
revNATID := k.GetRevNATID()
if _, found := ret[revNATID]; !found {
ret[revNATID] = []*cidr.CIDR{}
Expand Down Expand Up @@ -370,13 +370,13 @@ func (*LBBPFMap) DumpServiceMaps() ([]*loadbalancer.SVC, []error) {

parseBackendEntries := func(key bpf.MapKey, value bpf.MapValue) {
backendKey := key.(BackendKey)
backendValue := value.DeepCopyMapValue().(BackendValue)
backendValue := value.DeepCopyMapValue().(BackendValue).ToHost()
backendValueMap[backendKey.GetID()] = backendValue
}

parseSVCEntries := func(key bpf.MapKey, value bpf.MapValue) {
svcKey := key.DeepCopyMapKey().(ServiceKey)
svcValue := value.DeepCopyMapValue().(ServiceValue)
svcKey := key.DeepCopyMapKey().(ServiceKey).ToHost()
svcValue := value.DeepCopyMapValue().(ServiceValue).ToHost()

fe := svcFrontend(svcKey, svcValue)

Expand Down Expand Up @@ -452,7 +452,7 @@ func (*LBBPFMap) DumpBackendMaps() ([]*loadbalancer.Backend, error) {
// No need to deep copy the key because we are using the ID which
// is a value.
backendKey := key.(BackendKey)
backendValue := value.DeepCopyMapValue().(BackendValue)
backendValue := value.DeepCopyMapValue().(BackendValue).ToHost()
backendValueMap[backendKey.GetID()] = backendValue
}

Expand Down
38 changes: 24 additions & 14 deletions pkg/maps/lbmap/source_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const (
type SourceRangeKey interface {
GetCIDR() *cidr.CIDR
GetRevNATID() uint16

// Convert fields to network byte order.
ToNetwork() SourceRangeKey

// ToHost converts fields to host byte order.
ToHost() SourceRangeKey
}

// +k8s:deepcopy-gen=true
Expand All @@ -56,6 +62,14 @@ func (k *SourceRangeKey4) ToNetwork() *SourceRangeKey4 {
n.RevNATID = byteorder.HostToNetwork(n.RevNATID).(uint16)
return &n
}

// ToHost returns the key in the host byte order
func (k *SourceRangeKey4) ToHost() *SourceRangeKey4 {
h := *k
h.RevNATID = byteorder.NetworkToHost(h.RevNATID).(uint16)
return &h
}

func (k *SourceRangeKey4) GetCIDR() *cidr.CIDR {
var (
c net.IPNet
Expand Down Expand Up @@ -89,6 +103,14 @@ func (k *SourceRangeKey6) ToNetwork() *SourceRangeKey6 {
n.RevNATID = byteorder.HostToNetwork(n.RevNATID).(uint16)
return &n
}

// ToHost returns the key in the host byte order
func (k *SourceRangeKey6) ToHost() *SourceRangeKey6 {
h := *k
h.RevNATID = byteorder.NetworkToHost(h.RevNATID).(uint16)
return &h
}

func (k *SourceRangeKey6) GetCIDR() *cidr.CIDR {
var (
c net.IPNet
Expand Down Expand Up @@ -119,13 +141,7 @@ var SourceRange4Map = bpf.NewMap(
&SourceRangeValue{}, int(unsafe.Sizeof(SourceRangeValue{})),
MaxEntries,
bpf.BPF_F_NO_PREALLOC, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
sKey, sVal := mapKey.(*SourceRangeKey4), mapValue.(*SourceRangeValue)
if _, _, err := bpf.ConvertKeyValue(key, value, sKey, sVal); err != nil {
return nil, nil, err
}
return sKey.ToNetwork(), sVal, nil
},
bpf.ConvertKeyValue,
).WithCache()

var SourceRange6Map = bpf.NewMap(
Expand All @@ -135,13 +151,7 @@ var SourceRange6Map = bpf.NewMap(
&SourceRangeValue{}, int(unsafe.Sizeof(SourceRangeValue{})),
MaxEntries,
bpf.BPF_F_NO_PREALLOC, 0,
func(key []byte, value []byte, mapKey bpf.MapKey, mapValue bpf.MapValue) (bpf.MapKey, bpf.MapValue, error) {
sKey, sVal := mapKey.(*SourceRangeKey6), mapValue.(*SourceRangeValue)
if _, _, err := bpf.ConvertKeyValue(key, value, sKey, sVal); err != nil {
return nil, nil, err
}
return sKey.ToNetwork(), sVal, nil
},
bpf.ConvertKeyValue,
).WithCache()

func srcRangeKey(cidr *cidr.CIDR, revNATID uint16, ipv6 bool) bpf.MapKey {
Expand Down

0 comments on commit 682f682

Please sign in to comment.