class.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 representaion based on generic networking statisticsfor 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. }
  43. // NewClassStatistics Construct a ClassStatistics struct which fields are all initialized by 0.
  44. func NewClassStatistics() *ClassStatistics {
  45. return &ClassStatistics{
  46. Basic: &GnetStatsBasic{},
  47. Queue: &GnetStatsQueue{},
  48. RateEst: &GnetStatsRateEst{},
  49. }
  50. }
  51. // ClassAttrs represents a netlink class. A filter is associated with a link,
  52. // has a handle and a parent. The root filter of a device should have a
  53. // parent == HANDLE_ROOT.
  54. type ClassAttrs struct {
  55. LinkIndex int
  56. Handle uint32
  57. Parent uint32
  58. Leaf uint32
  59. Statistics *ClassStatistics
  60. }
  61. func (q ClassAttrs) String() string {
  62. return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
  63. }
  64. // HtbClassAttrs stores the attributes of HTB class
  65. type HtbClassAttrs struct {
  66. // TODO handle all attributes
  67. Rate uint64
  68. Ceil uint64
  69. Buffer uint32
  70. Cbuffer uint32
  71. Quantum uint32
  72. Level uint32
  73. Prio uint32
  74. }
  75. func (q HtbClassAttrs) String() string {
  76. return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
  77. }
  78. // HtbClass represents an Htb class
  79. type HtbClass struct {
  80. ClassAttrs
  81. Rate uint64
  82. Ceil uint64
  83. Buffer uint32
  84. Cbuffer uint32
  85. Quantum uint32
  86. Level uint32
  87. Prio uint32
  88. }
  89. func (q HtbClass) String() string {
  90. return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
  91. }
  92. // Attrs returns the class attributes
  93. func (q *HtbClass) Attrs() *ClassAttrs {
  94. return &q.ClassAttrs
  95. }
  96. // Type return the class type
  97. func (q *HtbClass) Type() string {
  98. return "htb"
  99. }
  100. // GenericClass classes represent types that are not currently understood
  101. // by this netlink library.
  102. type GenericClass struct {
  103. ClassAttrs
  104. ClassType string
  105. }
  106. // Attrs return the class attributes
  107. func (class *GenericClass) Attrs() *ClassAttrs {
  108. return &class.ClassAttrs
  109. }
  110. // Type retrun the class type
  111. func (class *GenericClass) Type() string {
  112. return class.ClassType
  113. }
  114. // ServiceCurve is the way the HFSC curve are represented
  115. type ServiceCurve struct {
  116. m1 uint32
  117. d uint32
  118. m2 uint32
  119. }
  120. // Attrs return the parameters of the service curve
  121. func (c *ServiceCurve) Attrs() (uint32, uint32, uint32) {
  122. return c.m1, c.d, c.m2
  123. }
  124. // HfscClass is a representation of the HFSC class
  125. type HfscClass struct {
  126. ClassAttrs
  127. Rsc ServiceCurve
  128. Fsc ServiceCurve
  129. Usc ServiceCurve
  130. }
  131. // SetUsc sets the Usc curve
  132. func (hfsc *HfscClass) SetUsc(m1 uint32, d uint32, m2 uint32) {
  133. hfsc.Usc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  134. }
  135. // SetFsc sets the Fsc curve
  136. func (hfsc *HfscClass) SetFsc(m1 uint32, d uint32, m2 uint32) {
  137. hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  138. }
  139. // SetRsc sets the Rsc curve
  140. func (hfsc *HfscClass) SetRsc(m1 uint32, d uint32, m2 uint32) {
  141. hfsc.Rsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  142. }
  143. // SetSC implements the SC from the tc CLI
  144. func (hfsc *HfscClass) SetSC(m1 uint32, d uint32, m2 uint32) {
  145. hfsc.Rsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  146. hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  147. }
  148. // SetUL implements the UL from the tc CLI
  149. func (hfsc *HfscClass) SetUL(m1 uint32, d uint32, m2 uint32) {
  150. hfsc.Usc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  151. }
  152. // SetLS implemtens the LS from the tc CLI
  153. func (hfsc *HfscClass) SetLS(m1 uint32, d uint32, m2 uint32) {
  154. hfsc.Fsc = ServiceCurve{m1: m1 / 8, d: d, m2: m2 / 8}
  155. }
  156. // NewHfscClass returns a new HFSC struct with the set parameters
  157. func NewHfscClass(attrs ClassAttrs) *HfscClass {
  158. return &HfscClass{
  159. ClassAttrs: attrs,
  160. Rsc: ServiceCurve{},
  161. Fsc: ServiceCurve{},
  162. Usc: ServiceCurve{},
  163. }
  164. }
  165. func (hfsc *HfscClass) String() string {
  166. return fmt.Sprintf(
  167. "{%s -- {RSC: {m1=%d d=%d m2=%d}} {FSC: {m1=%d d=%d m2=%d}} {USC: {m1=%d d=%d m2=%d}}}",
  168. 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,
  169. )
  170. }
  171. // Attrs return the Hfsc parameters
  172. func (hfsc *HfscClass) Attrs() *ClassAttrs {
  173. return &hfsc.ClassAttrs
  174. }
  175. // Type return the type of the class
  176. func (hfsc *HfscClass) Type() string {
  177. return "hfsc"
  178. }