Skip to content

Commit 31af44a

Browse files
ysksuzukijulianwiedmann
authored andcommitted
egressgw: Enable bpf_map_pressure metrics for egress_gw_policy_v4
This commit converts the egress_gw_policy_v4 map implementation from ebpf.Map to bpf.Map package and enables the bpf_map_pressure metrics. Fixes: cilium#23867 Signed-off-by: Yusuke Suzuki <[email protected]>
1 parent 3faa67b commit 31af44a

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

pkg/maps/egressmap/policy.go

+41-24
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"net/netip"
99
"unsafe"
1010

11+
"github.com/cilium/ebpf"
1112
"github.com/spf13/pflag"
1213
"go4.org/netipx"
1314

1415
"github.com/cilium/cilium/pkg/bpf"
1516
"github.com/cilium/cilium/pkg/datapath/linux/config/defines"
16-
"github.com/cilium/cilium/pkg/ebpf"
1717
"github.com/cilium/cilium/pkg/hive"
1818
"github.com/cilium/cilium/pkg/hive/cell"
1919
"github.com/cilium/cilium/pkg/option"
@@ -66,7 +66,7 @@ type PolicyMap interface {
6666

6767
// policyMap is the internal representation of an egress policy map.
6868
type policyMap struct {
69-
m *ebpf.Map
69+
m *bpf.Map
7070
}
7171

7272
func createPolicyMapFromDaemonConfig(in struct {
@@ -102,18 +102,24 @@ func CreatePrivatePolicyMap(lc hive.Lifecycle, cfg PolicyConfig) PolicyMap {
102102
}
103103

104104
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()
113113

114114
lc.Append(hive.Hook{
115115
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)
117123
},
118124
OnStop: func(hive.HookContext) error {
119125
return m.Close()
@@ -124,7 +130,7 @@ func createPolicyMap(lc hive.Lifecycle, cfg PolicyConfig, pinning ebpf.PinType)
124130
}
125131

126132
func OpenPinnedPolicyMap() (PolicyMap, error) {
127-
m, err := ebpf.LoadRegisterMap(PolicyMapName)
133+
m, err := bpf.OpenMap(bpf.MapPath(PolicyMapName), &EgressPolicyKey4{}, &EgressPolicyVal4{})
128134
if err != nil {
129135
return nil, err
130136
}
@@ -156,6 +162,14 @@ func NewEgressPolicyVal4(egressIP, gatewayIP netip.Addr) EgressPolicyVal4 {
156162
return val
157163
}
158164

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+
159173
// Match returns true if the sourceIP and destCIDR parameters match the egress
160174
// policy key.
161175
func (k *EgressPolicyKey4) Match(sourceIP netip.Addr, destCIDR netip.Prefix) bool {
@@ -175,6 +189,9 @@ func (k *EgressPolicyKey4) GetDestCIDR() netip.Prefix {
175189
return netip.PrefixFrom(addr, int(k.PrefixLen-PolicyStaticPrefixBits))
176190
}
177191

192+
// New returns an egress policy value
193+
func (v *EgressPolicyVal4) New() bpf.MapValue { return &EgressPolicyVal4{} }
194+
178195
// Match returns true if the egressIP and gatewayIP parameters match the egress
179196
// policy value.
180197
func (v *EgressPolicyVal4) Match(egressIP, gatewayIP netip.Addr) bool {
@@ -201,11 +218,12 @@ func (v *EgressPolicyVal4) String() string {
201218
// IP, destination CIDR) tuple.
202219
func (m *policyMap) Lookup(sourceIP netip.Addr, destCIDR netip.Prefix) (*EgressPolicyVal4, error) {
203220
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+
}
207225

208-
return &val, err
226+
return val.(*EgressPolicyVal4), err
209227
}
210228

211229
// 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,
214232
key := NewEgressPolicyKey4(sourceIP, destCIDR)
215233
val := NewEgressPolicyVal4(egressIP, gatewayIP)
216234

217-
return m.m.Update(key, val, 0)
235+
return m.m.Update(&key, &val)
218236
}
219237

220238
// Delete deletes the (sourceIP, destCIDR) egress policy entry.
221239
func (m *policyMap) Delete(sourceIP netip.Addr, destCIDR netip.Prefix) error {
222240
key := NewEgressPolicyKey4(sourceIP, destCIDR)
223241

224-
return m.m.Delete(key)
242+
return m.m.Delete(&key)
225243
}
226244

227245
// EgressPolicyIterateCallback represents the signature of the callback function
@@ -232,11 +250,10 @@ type EgressPolicyIterateCallback func(*EgressPolicyKey4, *EgressPolicyVal4)
232250
// IterateWithCallback iterates through all the keys/values of an egress policy
233251
// map, passing each key/value pair to the cb callback.
234252
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)
239256

240-
cb(key, value)
241-
})
257+
cb(key, value)
258+
})
242259
}

pkg/maps/egressmap/policy_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"net/netip"
99
"testing"
1010

11+
"github.com/cilium/ebpf"
1112
"github.com/cilium/ebpf/rlimit"
1213
"github.com/stretchr/testify/assert"
1314

1415
"github.com/cilium/cilium/pkg/bpf"
15-
"github.com/cilium/cilium/pkg/ebpf"
1616
"github.com/cilium/cilium/pkg/hive/hivetest"
1717
"github.com/cilium/cilium/pkg/testutils"
1818
)

0 commit comments

Comments
 (0)