github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/bandwidth/cell.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 //go:build linux 5 6 // NOTE: We can only build on linux because we import bwmap which in turn imports pkg/ebpf and pkg/bpf 7 // which throw build errors when building on non-linux platforms. 8 9 package bandwidth 10 11 import ( 12 "log/slog" 13 14 "github.com/cilium/hive/cell" 15 "github.com/cilium/statedb" 16 "github.com/cilium/statedb/reconciler" 17 18 "github.com/cilium/cilium/pkg/datapath/linux/config/defines" 19 "github.com/cilium/cilium/pkg/datapath/linux/sysctl" 20 "github.com/cilium/cilium/pkg/datapath/tables" 21 "github.com/cilium/cilium/pkg/datapath/types" 22 "github.com/cilium/cilium/pkg/maps/bwmap" 23 "github.com/cilium/cilium/pkg/option" 24 ) 25 26 var Cell = cell.Module( 27 "bandwidth-manager", 28 "Linux Bandwidth Manager for EDT-based pacing", 29 30 cell.Config(types.DefaultBandwidthConfig), 31 cell.Provide(newBandwidthManager), 32 33 cell.ProvidePrivate( 34 tables.NewBandwidthQDiscTable, // RWTable[*BandwidthQDisc] 35 ), 36 cell.Invoke(registerReconciler), 37 ) 38 39 type registerParams struct { 40 cell.In 41 42 Log *slog.Logger 43 Table statedb.RWTable[*tables.BandwidthQDisc] 44 BWM types.BandwidthManager 45 Config types.BandwidthConfig 46 DeriveParams statedb.DeriveParams[*tables.Device, *tables.BandwidthQDisc] 47 ReconcilerParams reconciler.Params 48 } 49 50 func registerReconciler(p registerParams) error { 51 if !p.Config.EnableBandwidthManager { 52 return nil 53 } 54 55 // Start deriving Table[*BandwidthQDisc] from Table[*Device] 56 statedb.Derive("derive-desired-qdiscs", deviceToBandwidthQDisc)( 57 p.DeriveParams, 58 ) 59 60 _, err := reconciler.Register( 61 p.ReconcilerParams, 62 p.Table, 63 64 (*tables.BandwidthQDisc).Clone, 65 (*tables.BandwidthQDisc).SetStatus, 66 (*tables.BandwidthQDisc).GetStatus, 67 newOps(p.Log, p.BWM), 68 nil, 69 ) 70 return err 71 } 72 73 func newBandwidthManager(lc cell.Lifecycle, p bandwidthManagerParams) (types.BandwidthManager, defines.NodeFnOut) { 74 m := &manager{params: p} 75 76 if !option.Config.DryMode { 77 lc.Append(m) 78 } 79 80 return m, defines.NewNodeFnOut(m.defines) 81 } 82 83 func (m *manager) Start(cell.HookContext) error { 84 err := m.probe() 85 if err != nil { 86 return err 87 } else if !m.enabled { 88 return nil 89 } 90 91 return m.init() 92 } 93 94 func (*manager) Stop(cell.HookContext) error { 95 return nil 96 } 97 98 type bandwidthManagerParams struct { 99 cell.In 100 101 Log *slog.Logger 102 Config types.BandwidthConfig 103 DaemonConfig *option.DaemonConfig 104 Sysctl sysctl.Sysctl 105 DB *statedb.DB 106 EdtTable statedb.RWTable[bwmap.Edt] 107 } 108 109 func deviceToBandwidthQDisc(device *tables.Device, deleted bool) (*tables.BandwidthQDisc, statedb.DeriveResult) { 110 if deleted || !device.Selected { 111 return &tables.BandwidthQDisc{ 112 LinkIndex: device.Index, 113 LinkName: device.Name, 114 }, statedb.DeriveDelete 115 } 116 return &tables.BandwidthQDisc{ 117 LinkIndex: device.Index, 118 LinkName: device.Name, 119 FqHorizon: FqDefaultHorizon, 120 FqBuckets: FqDefaultBuckets, 121 Status: reconciler.StatusPending(), 122 }, statedb.DeriveInsert 123 }