github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/class.go (about) 1 package netlink 2 3 import ( 4 "fmt" 5 ) 6 7 // Class interfaces for all classes 8 type Class interface { 9 Attrs() *ClassAttrs 10 Type() string 11 } 12 13 // Generic networking statistics for netlink users. 14 // This file contains "gnet_" prefixed structs and relevant functions. 15 // See Documentation/networking/getn_stats.txt in Linux source code for more details. 16 17 // GnetStatsBasic Ref: struct gnet_stats_basic { ... } 18 type GnetStatsBasic struct { 19 Bytes uint64 // number of seen bytes 20 Packets uint32 // number of seen packets 21 } 22 23 // GnetStatsRateEst Ref: struct gnet_stats_rate_est { ... } 24 type GnetStatsRateEst struct { 25 Bps uint32 // current byte rate 26 Pps uint32 // current packet rate 27 } 28 29 // GnetStatsRateEst64 Ref: struct gnet_stats_rate_est64 { ... } 30 type GnetStatsRateEst64 struct { 31 Bps uint64 // current byte rate 32 Pps uint64 // current packet rate 33 } 34 35 // GnetStatsQueue Ref: struct gnet_stats_queue { ... } 36 type GnetStatsQueue struct { 37 Qlen uint32 // queue length 38 Backlog uint32 // backlog size of queue 39 Drops uint32 // number of dropped packets 40 Requeues uint32 // number of requues 41 Overlimits uint32 // number of enqueues over the limit 42 } 43 44 // ClassStatistics representation based on generic networking statistics for netlink. 45 // See Documentation/networking/gen_stats.txt in Linux source code for more details. 46 type ClassStatistics struct { 47 Basic *GnetStatsBasic 48 Queue *GnetStatsQueue 49 RateEst *GnetStatsRateEst 50 } 51 52 // NewClassStatistics Construct a ClassStatistics struct which fields are all initialized by 0. 53 func NewClassStatistics() *ClassStatistics { 54 return &ClassStatistics{ 55 Basic: &GnetStatsBasic{}, 56 Queue: &GnetStatsQueue{}, 57 RateEst: &GnetStatsRateEst{}, 58 } 59 } 60 61 // ClassAttrs represents a netlink class. A filter is associated with a link, 62 // has a handle and a parent. The root filter of a device should have a 63 // parent == HANDLE_ROOT. 64 type ClassAttrs struct { 65 LinkIndex int 66 Handle uint32 67 Parent uint32 68 Leaf uint32 69 Statistics *ClassStatistics 70 } 71 72 func (q ClassAttrs) String() string { 73 return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf) 74 } 75 76 // HtbClassAttrs stores the attributes of HTB class 77 type HtbClassAttrs struct { 78 // TODO handle all attributes 79 Rate uint64 80 Ceil uint64 81 Buffer uint32 82 Cbuffer uint32 83 Quantum uint32 84 Level uint32 85 Prio uint32 86 } 87 88 func (q HtbClassAttrs) String() string { 89 return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer) 90 } 91 92 // HtbClass represents an Htb class 93 type HtbClass struct { 94 ClassAttrs 95 Rate uint64 96 Ceil uint64 97 Buffer uint32 98 Cbuffer uint32 99 Quantum uint32 100 Level uint32 101 Prio uint32 102 } 103 104 func (q HtbClass) String() string { 105 return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer) 106 } 107 108 // Attrs returns the class attributes 109 func (q *HtbClass) Attrs() *ClassAttrs { 110 return &q.ClassAttrs 111 } 112 113 // Type return the class type 114 func (q *HtbClass) Type() string { 115 return "htb" 116 } 117 118 // GenericClass classes represent types that are not currently understood 119 // by this netlink library. 120 type GenericClass struct { 121 ClassAttrs 122 ClassType string 123 } 124 125 // Attrs return the class attributes 126 func (class *GenericClass) Attrs() *ClassAttrs { 127 return &class.ClassAttrs 128 } 129 130 // Type return the class type 131 func (class *GenericClass) Type() string { 132 return class.ClassType 133 } 134 135 // ServiceCurve is a nondecreasing function of some time unit, returning the amount of service 136 // (an allowed or allocated amount of bandwidth) at some specific point in time. The purpose of it 137 // should be subconsciously obvious: if a class was allowed to transfer not less than the amount 138 // specified by its service curve, then the service curve is not violated. 139 type ServiceCurve struct { 140 m1 uint32 141 d uint32 142 m2 uint32 143 } 144 145 // Attrs return the parameters of the service curve 146 func (c *ServiceCurve) Attrs() (uint32, uint32, uint32) { 147 return c.m1, c.d, c.m2 148 } 149 150 // Burst returns the burst rate (m1) of the curve 151 func (c *ServiceCurve) Burst() uint32 { 152 return c.m1 153 } 154 155 // Delay return the delay (d) of the curve 156 func (c *ServiceCurve) Delay() uint32 { 157 return c.d 158 } 159 160 // Rate returns the rate (m2) of the curve 161 func (c *ServiceCurve) Rate() uint32 { 162 return c.m2 163 } 164 165 // HfscClass is a representation of the HFSC class 166 type HfscClass struct { 167 ClassAttrs 168 Rsc ServiceCurve 169 Fsc ServiceCurve 170 Usc ServiceCurve 171 } 172 173 // SetUsc sets the USC curve. The bandwidth (m1 and m2) is specified in bits and the delay in 174 // seconds. 175 func (hfsc *HfscClass) SetUsc(m1 uint32, d uint32, m2 uint32) { 176 hfsc.Usc = ServiceCurve{m1: m1, d: d, m2: m2} 177 } 178 179 // SetFsc sets the Fsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in 180 // seconds. 181 func (hfsc *HfscClass) SetFsc(m1 uint32, d uint32, m2 uint32) { 182 hfsc.Fsc = ServiceCurve{m1: m1, d: d, m2: m2} 183 } 184 185 // SetRsc sets the Rsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in 186 // seconds. 187 func (hfsc *HfscClass) SetRsc(m1 uint32, d uint32, m2 uint32) { 188 hfsc.Rsc = ServiceCurve{m1: m1, d: d, m2: m2} 189 } 190 191 // SetSC implements the SC from the `tc` CLI. This function behaves the same as if one would set the 192 // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and 193 // the delay in ms. 194 func (hfsc *HfscClass) SetSC(m1 uint32, d uint32, m2 uint32) { 195 hfsc.SetRsc(m1, d, m2) 196 hfsc.SetFsc(m1, d, m2) 197 } 198 199 // SetUL implements the UL from the `tc` CLI. This function behaves the same as if one would set the 200 // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and 201 // the delay in ms. 202 func (hfsc *HfscClass) SetUL(m1 uint32, d uint32, m2 uint32) { 203 hfsc.SetUsc(m1, d, m2) 204 } 205 206 // SetLS implements the LS from the `tc` CLI. This function behaves the same as if one would set the 207 // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and 208 // the delay in ms. 209 func (hfsc *HfscClass) SetLS(m1 uint32, d uint32, m2 uint32) { 210 hfsc.SetFsc(m1, d, m2) 211 } 212 213 // NewHfscClass returns a new HFSC struct with the set parameters 214 func NewHfscClass(attrs ClassAttrs) *HfscClass { 215 return &HfscClass{ 216 ClassAttrs: attrs, 217 Rsc: ServiceCurve{}, 218 Fsc: ServiceCurve{}, 219 Usc: ServiceCurve{}, 220 } 221 } 222 223 // String() returns a string that contains the information and attributes of the HFSC class 224 func (hfsc *HfscClass) String() string { 225 return fmt.Sprintf( 226 "{%s -- {RSC: {m1=%d d=%d m2=%d}} {FSC: {m1=%d d=%d m2=%d}} {USC: {m1=%d d=%d m2=%d}}}", 227 hfsc.Attrs(), hfsc.Rsc.m1*8, hfsc.Rsc.d, hfsc.Rsc.m2*8, hfsc.Fsc.m1*8, hfsc.Fsc.d, hfsc.Fsc.m2*8, hfsc.Usc.m1*8, hfsc.Usc.d, hfsc.Usc.m2*8, 228 ) 229 } 230 231 // Attrs return the Hfsc parameters 232 func (hfsc *HfscClass) Attrs() *ClassAttrs { 233 return &hfsc.ClassAttrs 234 } 235 236 // Type return the type of the class 237 func (hfsc *HfscClass) Type() string { 238 return "hfsc" 239 }