class.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package netlink
  2. import (
  3. "fmt"
  4. )
  5. type Class interface {
  6. Attrs() *ClassAttrs
  7. Type() string
  8. }
  9. // Class represents a netlink class. A filter is associated with a link,
  10. // has a handle and a parent. The root filter of a device should have a
  11. // parent == HANDLE_ROOT.
  12. type ClassAttrs struct {
  13. LinkIndex int
  14. Handle uint32
  15. Parent uint32
  16. Leaf uint32
  17. }
  18. func (q ClassAttrs) String() string {
  19. return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %s}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
  20. }
  21. type HtbClassAttrs struct {
  22. // TODO handle all attributes
  23. Rate uint64
  24. Ceil uint64
  25. Buffer uint32
  26. Cbuffer uint32
  27. Quantum uint32
  28. Level uint32
  29. Prio uint32
  30. }
  31. func (q HtbClassAttrs) String() string {
  32. return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
  33. }
  34. // Htb class
  35. type HtbClass struct {
  36. ClassAttrs
  37. Rate uint64
  38. Ceil uint64
  39. Buffer uint32
  40. Cbuffer uint32
  41. Quantum uint32
  42. Level uint32
  43. Prio uint32
  44. }
  45. func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
  46. mtu := 1600
  47. rate := cattrs.Rate / 8
  48. ceil := cattrs.Ceil / 8
  49. buffer := cattrs.Buffer
  50. cbuffer := cattrs.Cbuffer
  51. if ceil == 0 {
  52. ceil = rate
  53. }
  54. if buffer == 0 {
  55. buffer = uint32(float64(rate)/Hz() + float64(mtu))
  56. }
  57. buffer = uint32(Xmittime(rate, buffer))
  58. if cbuffer == 0 {
  59. cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
  60. }
  61. cbuffer = uint32(Xmittime(ceil, cbuffer))
  62. return &HtbClass{
  63. ClassAttrs: attrs,
  64. Rate: rate,
  65. Ceil: ceil,
  66. Buffer: buffer,
  67. Cbuffer: cbuffer,
  68. Quantum: 10,
  69. Level: 0,
  70. Prio: 0,
  71. }
  72. }
  73. func (q HtbClass) String() string {
  74. return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
  75. }
  76. func (class *HtbClass) Attrs() *ClassAttrs {
  77. return &class.ClassAttrs
  78. }
  79. func (class *HtbClass) Type() string {
  80. return "htb"
  81. }
  82. // GenericClass classes represent types that are not currently understood
  83. // by this netlink library.
  84. type GenericClass struct {
  85. ClassAttrs
  86. ClassType string
  87. }
  88. func (class *GenericClass) Attrs() *ClassAttrs {
  89. return &class.ClassAttrs
  90. }
  91. func (class *GenericClass) Type() string {
  92. return class.ClassType
  93. }