1package netlink 2 3import ( 4 "fmt" 5) 6 7// Class interfaces for all classes 8type 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 { ... } 18type 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 { ... } 24type GnetStatsRateEst struct { 25 Bps uint32 // current byte rate 26 Pps uint32 // current packet rate 27} 28 29// GnetStatsRateEst64 Ref: struct gnet_stats_rate_est64 { ... } 30type GnetStatsRateEst64 struct { 31 Bps uint64 // current byte rate 32 Pps uint64 // current packet rate 33} 34 35// GnetStatsQueue Ref: struct gnet_stats_queue { ... } 36type 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. 46type 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. 53func 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. 64type ClassAttrs struct { 65 LinkIndex int 66 Handle uint32 67 Parent uint32 68 Leaf uint32 69 Statistics *ClassStatistics 70} 71 72func (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 77type 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 88func (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 93type 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 104func (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 109func (q *HtbClass) Attrs() *ClassAttrs { 110 return &q.ClassAttrs 111} 112 113// Type return the class type 114func (q *HtbClass) Type() string { 115 return "htb" 116} 117 118// GenericClass classes represent types that are not currently understood 119// by this netlink library. 120type GenericClass struct { 121 ClassAttrs 122 ClassType string 123} 124 125// Attrs return the class attributes 126func (class *GenericClass) Attrs() *ClassAttrs { 127 return &class.ClassAttrs 128} 129 130// Type return the class type 131func (class *GenericClass) Type() string { 132 return class.ClassType 133} 134 135// ServiceCurve is the way the HFSC curve are represented 136type ServiceCurve struct { 137 m1 uint32 138 d uint32 139 m2 uint32 140} 141 142// Attrs return the parameters of the service curve 143func (c *ServiceCurve) Attrs() (uint32, uint32, uint32) { 144 return c.m1, c.d, c.m2 145} 146 147// HfscClass is a representation of the HFSC class 148type HfscClass struct { 149 ClassAttrs 150 Rsc ServiceCurve 151 Fsc ServiceCurve 152 Usc ServiceCurve 153} 154 155// SetUsc sets the Usc curve 156func (hfsc *HfscClass) SetUsc(m1 uint32, d uint32, m2 uint32) { 157 hfsc.Usc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 158} 159 160// SetFsc sets the Fsc curve 161func (hfsc *HfscClass) SetFsc(m1 uint32, d uint32, m2 uint32) { 162 hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 163} 164 165// SetRsc sets the Rsc curve 166func (hfsc *HfscClass) SetRsc(m1 uint32, d uint32, m2 uint32) { 167 hfsc.Rsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 168} 169 170// SetSC implements the SC from the tc CLI 171func (hfsc *HfscClass) SetSC(m1 uint32, d uint32, m2 uint32) { 172 hfsc.Rsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 173 hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 174} 175 176// SetUL implements the UL from the tc CLI 177func (hfsc *HfscClass) SetUL(m1 uint32, d uint32, m2 uint32) { 178 hfsc.Usc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 179} 180 181// SetLS implements the LS from the tc CLI 182func (hfsc *HfscClass) SetLS(m1 uint32, d uint32, m2 uint32) { 183 hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8} 184} 185 186// NewHfscClass returns a new HFSC struct with the set parameters 187func NewHfscClass(attrs ClassAttrs) *HfscClass { 188 return &HfscClass{ 189 ClassAttrs: attrs, 190 Rsc: ServiceCurve{}, 191 Fsc: ServiceCurve{}, 192 Usc: ServiceCurve{}, 193 } 194} 195 196func (hfsc *HfscClass) String() string { 197 return fmt.Sprintf( 198 "{%s -- {RSC: {m1=%d d=%d m2=%d}} {FSC: {m1=%d d=%d m2=%d}} {USC: {m1=%d d=%d m2=%d}}}", 199 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, 200 ) 201} 202 203// Attrs return the Hfsc parameters 204func (hfsc *HfscClass) Attrs() *ClassAttrs { 205 return &hfsc.ClassAttrs 206} 207 208// Type return the type of the class 209func (hfsc *HfscClass) Type() string { 210 return "hfsc" 211} 212