github.com/cilium/cilium@v1.16.2/pkg/maps/bwmap/bwmap.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package bwmap 5 6 import ( 7 "fmt" 8 9 "github.com/cilium/hive/cell" 10 11 "github.com/cilium/cilium/pkg/bpf" 12 "github.com/cilium/cilium/pkg/datapath/types" 13 "github.com/cilium/cilium/pkg/ebpf" 14 "github.com/cilium/cilium/pkg/maps/lxcmap" 15 "github.com/cilium/cilium/pkg/time" 16 ) 17 18 const ( 19 MapName = "cilium_throttle" 20 // Flow aggregate is per Pod, so same size as Endpoint map. 21 MapSize = lxcmap.MaxEntries 22 23 // DefaultDropHorizon represents maximum allowed departure 24 // time delta in future. Given applications can set SO_TXTIME 25 // from user space this is a limit to prevent buggy applications 26 // to fill the FQ qdisc. 27 DefaultDropHorizon = 2 * time.Second 28 ) 29 30 type EdtId struct { 31 Id uint64 `align:"id"` 32 } 33 34 func (k *EdtId) String() string { return fmt.Sprintf("%d", int(k.Id)) } 35 func (k *EdtId) New() bpf.MapKey { return &EdtId{} } 36 37 type EdtInfo struct { 38 Bps uint64 `align:"bps"` 39 TimeLast uint64 `align:"t_last"` 40 TimeHorizonDrop uint64 `align:"t_horizon_drop"` 41 Pad [4]uint64 `align:"pad"` 42 } 43 44 func (v *EdtInfo) String() string { return fmt.Sprintf("%d", int(v.Bps)) } 45 func (v *EdtInfo) New() bpf.MapValue { return &EdtInfo{} } 46 47 type throttleMap struct { 48 *bpf.Map 49 } 50 51 // ThrottleMap constructs the cilium_throttle map. Direct use of this 52 // outside of this package is solely for cilium-dbg. 53 func ThrottleMap() *bpf.Map { 54 return bpf.NewMap( 55 MapName, 56 ebpf.Hash, 57 &EdtId{}, 58 &EdtInfo{}, 59 MapSize, 60 bpf.BPF_F_NO_PREALLOC, 61 ) 62 } 63 64 func newThrottleMap(cfg types.BandwidthConfig, lc cell.Lifecycle) (out bpf.MapOut[throttleMap]) { 65 m := throttleMap{ThrottleMap()} 66 if cfg.EnableBandwidthManager { 67 // Only open the map if bandwidth manager is enabled. 68 lc.Append(cell.Hook{ 69 OnStart: func(cell.HookContext) error { 70 return m.OpenOrCreate() 71 }, 72 OnStop: func(cell.HookContext) error { 73 return m.Close() 74 }, 75 }) 76 } 77 return bpf.NewMapOut(m) 78 }