class.go 6.9 KB

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