@@ -8,12 +8,12 @@ import (
8
8
"net/netip"
9
9
"unsafe"
10
10
11
+ "github.com/cilium/ebpf"
11
12
"github.com/spf13/pflag"
12
13
"go4.org/netipx"
13
14
14
15
"github.com/cilium/cilium/pkg/bpf"
15
16
"github.com/cilium/cilium/pkg/datapath/linux/config/defines"
16
- "github.com/cilium/cilium/pkg/ebpf"
17
17
"github.com/cilium/cilium/pkg/hive"
18
18
"github.com/cilium/cilium/pkg/hive/cell"
19
19
"github.com/cilium/cilium/pkg/option"
@@ -66,7 +66,7 @@ type PolicyMap interface {
66
66
67
67
// policyMap is the internal representation of an egress policy map.
68
68
type policyMap struct {
69
- m * ebpf .Map
69
+ m * bpf .Map
70
70
}
71
71
72
72
func createPolicyMapFromDaemonConfig (in struct {
@@ -102,18 +102,24 @@ func CreatePrivatePolicyMap(lc hive.Lifecycle, cfg PolicyConfig) PolicyMap {
102
102
}
103
103
104
104
func createPolicyMap (lc hive.Lifecycle , cfg PolicyConfig , pinning ebpf.PinType ) * policyMap {
105
- m := ebpf .NewMap (& ebpf. MapSpec {
106
- Name : PolicyMapName ,
107
- Type : ebpf .LPMTrie ,
108
- KeySize : uint32 ( unsafe . Sizeof ( EgressPolicyKey4 {})) ,
109
- ValueSize : uint32 ( unsafe . Sizeof ( EgressPolicyVal4 {})) ,
110
- MaxEntries : uint32 ( cfg .EgressGatewayPolicyMapMax ) ,
111
- Pinning : pinning ,
112
- } )
105
+ m := bpf .NewMap (
106
+ PolicyMapName ,
107
+ ebpf .LPMTrie ,
108
+ & EgressPolicyKey4 {},
109
+ & EgressPolicyVal4 {},
110
+ cfg .EgressGatewayPolicyMapMax ,
111
+ 0 ,
112
+ ). WithPressureMetric ( )
113
113
114
114
lc .Append (hive.Hook {
115
115
OnStart : func (hive.HookContext ) error {
116
- return m .OpenOrCreate ()
116
+ switch pinning {
117
+ case ebpf .PinNone :
118
+ return m .CreateUnpinned ()
119
+ case ebpf .PinByName :
120
+ return m .OpenOrCreate ()
121
+ }
122
+ return fmt .Errorf ("received unexpected pin type: %d" , pinning )
117
123
},
118
124
OnStop : func (hive.HookContext ) error {
119
125
return m .Close ()
@@ -124,7 +130,7 @@ func createPolicyMap(lc hive.Lifecycle, cfg PolicyConfig, pinning ebpf.PinType)
124
130
}
125
131
126
132
func OpenPinnedPolicyMap () (PolicyMap , error ) {
127
- m , err := ebpf . LoadRegisterMap ( PolicyMapName )
133
+ m , err := bpf . OpenMap ( bpf . MapPath ( PolicyMapName ), & EgressPolicyKey4 {}, & EgressPolicyVal4 {} )
128
134
if err != nil {
129
135
return nil , err
130
136
}
@@ -156,6 +162,14 @@ func NewEgressPolicyVal4(egressIP, gatewayIP netip.Addr) EgressPolicyVal4 {
156
162
return val
157
163
}
158
164
165
+ // String returns the string representation of an egress policy key.
166
+ func (k * EgressPolicyKey4 ) String () string {
167
+ return fmt .Sprintf ("%s %s/%d" , k .SourceIP , k .DestCIDR , k .PrefixLen - PolicyStaticPrefixBits )
168
+ }
169
+
170
+ // New returns an egress policy key
171
+ func (k * EgressPolicyKey4 ) New () bpf.MapKey { return & EgressPolicyKey4 {} }
172
+
159
173
// Match returns true if the sourceIP and destCIDR parameters match the egress
160
174
// policy key.
161
175
func (k * EgressPolicyKey4 ) Match (sourceIP netip.Addr , destCIDR netip.Prefix ) bool {
@@ -175,6 +189,9 @@ func (k *EgressPolicyKey4) GetDestCIDR() netip.Prefix {
175
189
return netip .PrefixFrom (addr , int (k .PrefixLen - PolicyStaticPrefixBits ))
176
190
}
177
191
192
+ // New returns an egress policy value
193
+ func (v * EgressPolicyVal4 ) New () bpf.MapValue { return & EgressPolicyVal4 {} }
194
+
178
195
// Match returns true if the egressIP and gatewayIP parameters match the egress
179
196
// policy value.
180
197
func (v * EgressPolicyVal4 ) Match (egressIP , gatewayIP netip.Addr ) bool {
@@ -201,11 +218,12 @@ func (v *EgressPolicyVal4) String() string {
201
218
// IP, destination CIDR) tuple.
202
219
func (m * policyMap ) Lookup (sourceIP netip.Addr , destCIDR netip.Prefix ) (* EgressPolicyVal4 , error ) {
203
220
key := NewEgressPolicyKey4 (sourceIP , destCIDR )
204
- val := EgressPolicyVal4 {}
205
-
206
- err := m .m .Lookup (& key , & val )
221
+ val , err := m .m .Lookup (& key )
222
+ if err != nil {
223
+ return nil , err
224
+ }
207
225
208
- return & val , err
226
+ return val .( * EgressPolicyVal4 ) , err
209
227
}
210
228
211
229
// Update updates the (sourceIP, destCIDR) egress policy entry with the provided
@@ -214,14 +232,14 @@ func (m *policyMap) Update(sourceIP netip.Addr, destCIDR netip.Prefix, egressIP,
214
232
key := NewEgressPolicyKey4 (sourceIP , destCIDR )
215
233
val := NewEgressPolicyVal4 (egressIP , gatewayIP )
216
234
217
- return m .m .Update (key , val , 0 )
235
+ return m .m .Update (& key , & val )
218
236
}
219
237
220
238
// Delete deletes the (sourceIP, destCIDR) egress policy entry.
221
239
func (m * policyMap ) Delete (sourceIP netip.Addr , destCIDR netip.Prefix ) error {
222
240
key := NewEgressPolicyKey4 (sourceIP , destCIDR )
223
241
224
- return m .m .Delete (key )
242
+ return m .m .Delete (& key )
225
243
}
226
244
227
245
// EgressPolicyIterateCallback represents the signature of the callback function
@@ -232,11 +250,10 @@ type EgressPolicyIterateCallback func(*EgressPolicyKey4, *EgressPolicyVal4)
232
250
// IterateWithCallback iterates through all the keys/values of an egress policy
233
251
// map, passing each key/value pair to the cb callback.
234
252
func (m policyMap ) IterateWithCallback (cb EgressPolicyIterateCallback ) error {
235
- return m .m .IterateWithCallback (& EgressPolicyKey4 {}, & EgressPolicyVal4 {},
236
- func (k , v interface {}) {
237
- key := k .(* EgressPolicyKey4 )
238
- value := v .(* EgressPolicyVal4 )
253
+ return m .m .DumpWithCallback (func (k bpf.MapKey , v bpf.MapValue ) {
254
+ key := k .(* EgressPolicyKey4 )
255
+ value := v .(* EgressPolicyVal4 )
239
256
240
- cb (key , value )
241
- })
257
+ cb (key , value )
258
+ })
242
259
}
0 commit comments