/
affinity.go
200 lines (171 loc) · 6.09 KB
/
affinity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lbmap
import (
"fmt"
"unsafe"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/types"
)
const (
AffinityMatchMapName = "cilium_lb_affinity_match"
Affinity4MapName = "cilium_lb4_affinity"
Affinity6MapName = "cilium_lb6_affinity"
)
var (
// AffinityMatchMap is the BPF map to implement session affinity.
AffinityMatchMap *bpf.Map
Affinity4Map *bpf.Map
Affinity6Map *bpf.Map
)
// initAffinity creates the BPF maps for implementing session affinity.
func initAffinity(params InitParams) {
AffinityMatchMap = bpf.NewMap(
AffinityMatchMapName,
bpf.MapTypeHash,
&AffinityMatchKey{},
int(unsafe.Sizeof(AffinityMatchKey{})),
&AffinityMatchValue{},
int(unsafe.Sizeof(AffinityMatchValue{})),
MaxEntries,
0, 0,
bpf.ConvertKeyValue,
).WithCache().WithPressureMetric()
if params.IPv4 {
Affinity4Map = bpf.NewMap(
Affinity4MapName,
bpf.MapTypeLRUHash,
&Affinity4Key{},
int(unsafe.Sizeof(Affinity4Key{})),
&AffinityValue{},
int(unsafe.Sizeof(AffinityValue{})),
MaxEntries,
0,
0,
bpf.ConvertKeyValue,
)
}
if params.IPv6 {
Affinity6Map = bpf.NewMap(
Affinity6MapName,
bpf.MapTypeLRUHash,
&Affinity6Key{},
int(unsafe.Sizeof(Affinity6Key{})),
&AffinityValue{},
int(unsafe.Sizeof(AffinityValue{})),
MaxEntries,
0,
0,
bpf.ConvertKeyValue,
)
}
}
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type AffinityMatchKey struct {
BackendID uint32 `align:"backend_id"`
RevNATID uint16 `align:"rev_nat_id"`
Pad uint16 `align:"pad"`
}
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
type AffinityMatchValue struct {
Pad uint8 `align:"pad"`
}
// NewAffinityMatchKey creates the AffinityMatch key
func NewAffinityMatchKey(revNATID uint16, backendID uint32) *AffinityMatchKey {
return &AffinityMatchKey{
BackendID: backendID,
RevNATID: revNATID,
}
}
// GetKeyPtr returns the unsafe pointer to the BPF key
func (k *AffinityMatchKey) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// GetValuePtr returns the unsafe pointer to the BPF value
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 {
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 "" }
// NewValue returns a new empty instance of the structure representing the BPF
// map value
func (k *AffinityMatchKey) NewValue() bpf.MapValue { return &AffinityMatchValue{} }
// ToNetwork returns the key in the network byte order
func (k *AffinityMatchKey) ToNetwork() *AffinityMatchKey {
n := *k
// For some reasons rev_nat_index is stored in network byte order in
// the SVC BPF maps
n.RevNATID = byteorder.HostToNetwork(n.RevNATID).(uint16)
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
type Affinity4Key struct {
ClientID uint64 `align:"client_id"`
RevNATID uint16 `align:"rev_nat_id"`
NetNSCookie uint8 `align:"netns_cookie"`
Pad1 uint8 `align:"pad1"`
Pad2 uint32 `align:"pad2"`
}
// Affinity6Key is the Go representation of lb6_affinity_key
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type Affinity6Key struct {
ClientID types.IPv6 `align:"client_id"`
RevNATID uint16 `align:"rev_nat_id"`
NetNSCookie uint8 `align:"netns_cookie"`
Pad1 uint8 `align:"pad1"`
Pad2 uint32 `align:"pad2"`
}
// AffinityValue is the Go representing of lb_affinity_value
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
type AffinityValue struct {
LastUsed uint64 `align:"last_used"`
BackendID uint32 `align:"backend_id"`
Pad uint32 `align:"pad"`
}
// GetKeyPtr returns the unsafe pointer to the BPF key
func (k *Affinity4Key) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// GetKeyPtr returns the unsafe pointer to the BPF key
func (k *Affinity6Key) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// GetValuePtr returns the unsafe pointer to the BPF value
func (v *AffinityValue) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(v) }
// String converts the key into a human readable string format.
func (k *Affinity4Key) String() string {
return fmt.Sprintf("%d %d %d", k.ClientID, k.NetNSCookie, k.RevNATID)
}
// String converts the key into a human readable string format.
func (k *Affinity6Key) String() string {
return fmt.Sprintf("%d %d %d", k.ClientID, k.NetNSCookie, k.RevNATID)
}
// String converts the value into a human readable string format.
func (v *AffinityValue) String() string { return fmt.Sprintf("%d %d", v.BackendID, v.LastUsed) }
// NewValue returns a new empty instance of the structure representing the BPF
// map value.
func (k Affinity4Key) NewValue() bpf.MapValue { return &AffinityValue{} }
// NewValue returns a new empty instance of the structure representing the BPF
// map value.
func (k Affinity6Key) NewValue() bpf.MapValue { return &AffinityValue{} }