Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lbmap: Correct issue that port info display error #13244

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 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)
brb marked this conversation as resolved.
Show resolved Hide resolved
svcVal := value.(lbmap.ServiceValue)
svcVal := value.(lbmap.ServiceValue).ToHost()
svc := svcKey.String()
svcKey = svcKey.ToHost()
revNATID := svcVal.GetRevNat()
backendID := svcVal.GetBackendID()
flags := loadbalancer.ServiceFlags(svcVal.GetFlags())
Expand Down
23 changes: 13 additions & 10 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 Expand Up @@ -103,7 +96,10 @@ func (k *AffinityMatchKey) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k)
func (v *AffinityMatchValue) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(v) }

// String converts the key into a human readable string format
func (k *AffinityMatchKey) String() string { return fmt.Sprintf("%d %d", k.BackendID, k.RevNATID) }
func (k *AffinityMatchKey) String() string {
brb marked this conversation as resolved.
Show resolved Hide resolved
kHost := k.ToHost()
return fmt.Sprintf("%d %d", kHost.BackendID, kHost.RevNATID)
}

// String converts the value into a human readable string format
func (v *AffinityMatchValue) String() string { return "" }
Expand All @@ -121,6 +117,13 @@ func (k *AffinityMatchKey) ToNetwork() *AffinityMatchKey {
return &n
}

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

// Affinity4Key is the Go representation of lb4_affinity_key
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
Expand Down
84 changes: 51 additions & 33 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 All @@ -107,7 +86,7 @@ func NewRevNat4Key(value uint16) *RevNat4Key {
func (k *RevNat4Key) Map() *bpf.Map { return RevNat4Map }
func (k *RevNat4Key) NewValue() bpf.MapValue { return &RevNat4Value{} }
func (k *RevNat4Key) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
func (k *RevNat4Key) String() string { return fmt.Sprintf("%d", k.Key) }
func (k *RevNat4Key) String() string { return fmt.Sprintf("%d", k.ToHost().(*RevNat4Key).Key) }
func (k *RevNat4Key) GetKey() uint16 { return k.Key }

// ToNetwork converts RevNat4Key to network byte order.
Expand All @@ -117,6 +96,13 @@ func (k *RevNat4Key) ToNetwork() RevNatKey {
return &n
}

// ToHost converts RevNat4Key to host byte order.
func (k *RevNat4Key) ToHost() RevNatKey {
h := *k
h.Key = byteorder.NetworkToHost(h.Key).(uint16)
return &h
}

// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
type RevNat4Value struct {
Expand All @@ -133,8 +119,16 @@ func (v *RevNat4Value) ToNetwork() RevNatValue {
return &n
}

// ToHost converts RevNat4Value to host byte order.
func (k *RevNat4Value) ToHost() RevNatValue {
h := *k
h.Port = byteorder.NetworkToHost(h.Port).(uint16)
return &h
}

func (v *RevNat4Value) String() string {
return net.JoinHostPort(v.Address.String(), fmt.Sprintf("%d", v.Port))
vHost := v.ToHost().(*RevNat4Value)
return net.JoinHostPort(vHost.Address.String(), fmt.Sprintf("%d", vHost.Port))
}

type pad2uint8 [2]uint8
Expand Down Expand Up @@ -171,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 Expand Up @@ -204,6 +199,13 @@ func (k *Service4Key) ToNetwork() ServiceKey {
return &n
}

// ToHost converts Service4Key to host byte order.
func (k *Service4Key) ToHost() ServiceKey {
h := *k
h.Port = byteorder.NetworkToHost(h.Port).(uint16)
return &h
}

type pad3uint8 [3]uint8

// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
Expand All @@ -225,7 +227,8 @@ type Service4Value struct {
}

func (s *Service4Value) String() string {
return fmt.Sprintf("%d (%d) [FLAGS: 0x%x]", s.BackendID, s.RevNat, s.Flags)
sHost := s.ToHost().(*Service4Value)
return fmt.Sprintf("%d (%d) [FLAGS: 0x%x]", sHost.BackendID, sHost.RevNat, sHost.Flags)
}

func (s *Service4Value) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(s) }
Expand Down Expand Up @@ -263,6 +266,13 @@ func (s *Service4Value) ToNetwork() ServiceValue {
return &n
}

// ToHost converts Service4Value to host byte order.
func (s *Service4Value) ToHost() ServiceValue {
h := *s
h.RevNat = byteorder.NetworkToHost(h.RevNat).(uint16)
return &h
}

// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type Backend4Key struct {
Expand Down Expand Up @@ -306,7 +316,8 @@ func NewBackend4Value(ip net.IP, port uint16, proto u8proto.U8proto) (*Backend4V
}

func (v *Backend4Value) String() string {
return fmt.Sprintf("%s://%s:%d", v.Proto, v.Address, v.Port)
vHost := v.ToHost().(*Backend4Value)
return fmt.Sprintf("%s://%s:%d", vHost.Proto, vHost.Address, vHost.Port)
}

func (v *Backend4Value) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(v) }
Expand All @@ -320,6 +331,13 @@ func (v *Backend4Value) ToNetwork() BackendValue {
return &n
}

// ToHost converts Backend4Value to host byte order.
func (v *Backend4Value) ToHost() BackendValue {
h := *v
h.Port = byteorder.NetworkToHost(h.Port).(uint16)
return &h
}

type Backend4 struct {
Key *Backend4Key
Value *Backend4Value
Expand Down
Loading