github.com/cilium/cilium@v1.16.2/pkg/bpf/bpf.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package bpf 5 6 import ( 7 "sync/atomic" 8 9 "github.com/cilium/ebpf" 10 11 "github.com/cilium/cilium/pkg/logging" 12 "github.com/cilium/cilium/pkg/logging/logfields" 13 ) 14 15 var ( 16 log = logging.DefaultLogger.WithField(logfields.LogSubsys, "bpf") 17 18 preAllocateMapSetting uint32 = BPF_F_NO_PREALLOC 19 ) 20 21 const ( 22 // Flags for BPF_MAP_CREATE. Must match values from linux/bpf.h 23 BPF_F_NO_PREALLOC = 1 << 0 24 ) 25 26 // EnableMapPreAllocation enables BPF map pre-allocation on map types that 27 // support it. This does not take effect on existing map although some map 28 // types could be recreated later when objCheck() runs. 29 func EnableMapPreAllocation() { 30 atomic.StoreUint32(&preAllocateMapSetting, 0) 31 } 32 33 // DisableMapPreAllocation disables BPF map pre-allocation as a default 34 // setting. Some map types enforces pre-alloc strategy so this does not 35 // take effect in that case. Also note that this does not take effect on 36 // existing map although could be recreated later when objCheck() runs. 37 func DisableMapPreAllocation() { 38 atomic.StoreUint32(&preAllocateMapSetting, BPF_F_NO_PREALLOC) 39 } 40 41 // GetPreAllocateMapFlags returns the map flags for map which use conditional 42 // pre-allocation. 43 func GetPreAllocateMapFlags(t ebpf.MapType) uint32 { 44 switch t { 45 // LPM Tries don't support preallocation. 46 case ebpf.LPMTrie: 47 return BPF_F_NO_PREALLOC 48 // Support disabling preallocation for these map types. 49 case ebpf.Hash, ebpf.PerCPUHash, ebpf.HashOfMaps: 50 return atomic.LoadUint32(&preAllocateMapSetting) 51 } 52 53 return 0 54 }