class_linux.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package netlink
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "syscall"
  9. "github.com/vishvananda/netlink/nl"
  10. "golang.org/x/sys/unix"
  11. )
  12. // Internal tc_stats representation in Go struct.
  13. // This is for internal uses only to deserialize the payload of rtattr.
  14. // After the deserialization, this should be converted into the canonical stats
  15. // struct, ClassStatistics, in case of statistics of a class.
  16. // Ref: struct tc_stats { ... }
  17. type tcStats struct {
  18. Bytes uint64 // Number of enqueued bytes
  19. Packets uint32 // Number of enqueued packets
  20. Drops uint32 // Packets dropped because of lack of resources
  21. Overlimits uint32 // Number of throttle events when this flow goes out of allocated bandwidth
  22. Bps uint32 // Current flow byte rate
  23. Pps uint32 // Current flow packet rate
  24. Qlen uint32
  25. Backlog uint32
  26. }
  27. // NewHtbClass NOTE: function is in here because it uses other linux functions
  28. func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
  29. mtu := 1600
  30. rate := cattrs.Rate / 8
  31. ceil := cattrs.Ceil / 8
  32. buffer := cattrs.Buffer
  33. cbuffer := cattrs.Cbuffer
  34. if ceil == 0 {
  35. ceil = rate
  36. }
  37. if buffer == 0 {
  38. buffer = uint32(float64(rate)/Hz() + float64(mtu))
  39. }
  40. buffer = uint32(Xmittime(rate, buffer))
  41. if cbuffer == 0 {
  42. cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
  43. }
  44. cbuffer = uint32(Xmittime(ceil, cbuffer))
  45. return &HtbClass{
  46. ClassAttrs: attrs,
  47. Rate: rate,
  48. Ceil: ceil,
  49. Buffer: buffer,
  50. Cbuffer: cbuffer,
  51. Quantum: 10,
  52. Level: 0,
  53. Prio: 0,
  54. }
  55. }
  56. // ClassDel will delete a class from the system.
  57. // Equivalent to: `tc class del $class`
  58. func ClassDel(class Class) error {
  59. return pkgHandle.ClassDel(class)
  60. }
  61. // ClassDel will delete a class from the system.
  62. // Equivalent to: `tc class del $class`
  63. func (h *Handle) ClassDel(class Class) error {
  64. return h.classModify(unix.RTM_DELTCLASS, 0, class)
  65. }
  66. // ClassChange will change a class in place
  67. // Equivalent to: `tc class change $class`
  68. // The parent and handle MUST NOT be changed.
  69. func ClassChange(class Class) error {
  70. return pkgHandle.ClassChange(class)
  71. }
  72. // ClassChange will change a class in place
  73. // Equivalent to: `tc class change $class`
  74. // The parent and handle MUST NOT be changed.
  75. func (h *Handle) ClassChange(class Class) error {
  76. return h.classModify(unix.RTM_NEWTCLASS, 0, class)
  77. }
  78. // ClassReplace will replace a class to the system.
  79. // quivalent to: `tc class replace $class`
  80. // The handle MAY be changed.
  81. // If a class already exist with this parent/handle pair, the class is changed.
  82. // If a class does not already exist with this parent/handle, a new class is created.
  83. func ClassReplace(class Class) error {
  84. return pkgHandle.ClassReplace(class)
  85. }
  86. // ClassReplace will replace a class to the system.
  87. // quivalent to: `tc class replace $class`
  88. // The handle MAY be changed.
  89. // If a class already exist with this parent/handle pair, the class is changed.
  90. // If a class does not already exist with this parent/handle, a new class is created.
  91. func (h *Handle) ClassReplace(class Class) error {
  92. return h.classModify(unix.RTM_NEWTCLASS, unix.NLM_F_CREATE, class)
  93. }
  94. // ClassAdd will add a class to the system.
  95. // Equivalent to: `tc class add $class`
  96. func ClassAdd(class Class) error {
  97. return pkgHandle.ClassAdd(class)
  98. }
  99. // ClassAdd will add a class to the system.
  100. // Equivalent to: `tc class add $class`
  101. func (h *Handle) ClassAdd(class Class) error {
  102. return h.classModify(
  103. unix.RTM_NEWTCLASS,
  104. unix.NLM_F_CREATE|unix.NLM_F_EXCL,
  105. class,
  106. )
  107. }
  108. func (h *Handle) classModify(cmd, flags int, class Class) error {
  109. req := h.newNetlinkRequest(cmd, flags|unix.NLM_F_ACK)
  110. base := class.Attrs()
  111. msg := &nl.TcMsg{
  112. Family: nl.FAMILY_ALL,
  113. Ifindex: int32(base.LinkIndex),
  114. Handle: base.Handle,
  115. Parent: base.Parent,
  116. }
  117. req.AddData(msg)
  118. if cmd != unix.RTM_DELTCLASS {
  119. if err := classPayload(req, class); err != nil {
  120. return err
  121. }
  122. }
  123. _, err := req.Execute(unix.NETLINK_ROUTE, 0)
  124. return err
  125. }
  126. func classPayload(req *nl.NetlinkRequest, class Class) error {
  127. req.AddData(nl.NewRtAttr(nl.TCA_KIND, nl.ZeroTerminated(class.Type())))
  128. options := nl.NewRtAttr(nl.TCA_OPTIONS, nil)
  129. switch class.Type() {
  130. case "htb":
  131. htb := class.(*HtbClass)
  132. opt := nl.TcHtbCopt{}
  133. opt.Buffer = htb.Buffer
  134. opt.Cbuffer = htb.Cbuffer
  135. opt.Quantum = htb.Quantum
  136. opt.Level = htb.Level
  137. opt.Prio = htb.Prio
  138. // TODO: Handle Debug properly. For now default to 0
  139. /* Calculate {R,C}Tab and set Rate and Ceil */
  140. cellLog := -1
  141. ccellLog := -1
  142. linklayer := nl.LINKLAYER_ETHERNET
  143. mtu := 1600
  144. var rtab [256]uint32
  145. var ctab [256]uint32
  146. tcrate := nl.TcRateSpec{Rate: uint32(htb.Rate)}
  147. if CalcRtable(&tcrate, rtab[:], cellLog, uint32(mtu), linklayer) < 0 {
  148. return errors.New("HTB: failed to calculate rate table")
  149. }
  150. opt.Rate = tcrate
  151. tcceil := nl.TcRateSpec{Rate: uint32(htb.Ceil)}
  152. if CalcRtable(&tcceil, ctab[:], ccellLog, uint32(mtu), linklayer) < 0 {
  153. return errors.New("HTB: failed to calculate ceil rate table")
  154. }
  155. opt.Ceil = tcceil
  156. options.AddRtAttr(nl.TCA_HTB_PARMS, opt.Serialize())
  157. options.AddRtAttr(nl.TCA_HTB_RTAB, SerializeRtab(rtab))
  158. options.AddRtAttr(nl.TCA_HTB_CTAB, SerializeRtab(ctab))
  159. case "hfsc":
  160. hfsc := class.(*HfscClass)
  161. opt := nl.HfscCopt{}
  162. opt.Rsc.Set(hfsc.Rsc.Attrs())
  163. opt.Fsc.Set(hfsc.Fsc.Attrs())
  164. opt.Usc.Set(hfsc.Usc.Attrs())
  165. options.AddRtAttr(nl.TCA_HFSC_RSC, nl.SerializeHfscCurve(&opt.Rsc))
  166. options.AddRtAttr(nl.TCA_HFSC_FSC, nl.SerializeHfscCurve(&opt.Fsc))
  167. options.AddRtAttr(nl.TCA_HFSC_USC, nl.SerializeHfscCurve(&opt.Usc))
  168. }
  169. req.AddData(options)
  170. return nil
  171. }
  172. // ClassList gets a list of classes in the system.
  173. // Equivalent to: `tc class show`.
  174. // Generally returns nothing if link and parent are not specified.
  175. func ClassList(link Link, parent uint32) ([]Class, error) {
  176. return pkgHandle.ClassList(link, parent)
  177. }
  178. // ClassList gets a list of classes in the system.
  179. // Equivalent to: `tc class show`.
  180. // Generally returns nothing if link and parent are not specified.
  181. func (h *Handle) ClassList(link Link, parent uint32) ([]Class, error) {
  182. req := h.newNetlinkRequest(unix.RTM_GETTCLASS, unix.NLM_F_DUMP)
  183. msg := &nl.TcMsg{
  184. Family: nl.FAMILY_ALL,
  185. Parent: parent,
  186. }
  187. if link != nil {
  188. base := link.Attrs()
  189. h.ensureIndex(base)
  190. msg.Ifindex = int32(base.Index)
  191. }
  192. req.AddData(msg)
  193. msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWTCLASS)
  194. if err != nil {
  195. return nil, err
  196. }
  197. var res []Class
  198. for _, m := range msgs {
  199. msg := nl.DeserializeTcMsg(m)
  200. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  201. if err != nil {
  202. return nil, err
  203. }
  204. base := ClassAttrs{
  205. LinkIndex: int(msg.Ifindex),
  206. Handle: msg.Handle,
  207. Parent: msg.Parent,
  208. Statistics: nil,
  209. }
  210. var class Class
  211. classType := ""
  212. for _, attr := range attrs {
  213. switch attr.Attr.Type {
  214. case nl.TCA_KIND:
  215. classType = string(attr.Value[:len(attr.Value)-1])
  216. switch classType {
  217. case "htb":
  218. class = &HtbClass{}
  219. case "hfsc":
  220. class = &HfscClass{}
  221. default:
  222. class = &GenericClass{ClassType: classType}
  223. }
  224. case nl.TCA_OPTIONS:
  225. switch classType {
  226. case "htb":
  227. data, err := nl.ParseRouteAttr(attr.Value)
  228. if err != nil {
  229. return nil, err
  230. }
  231. _, err = parseHtbClassData(class, data)
  232. if err != nil {
  233. return nil, err
  234. }
  235. case "hfsc":
  236. data, err := nl.ParseRouteAttr(attr.Value)
  237. if err != nil {
  238. return nil, err
  239. }
  240. _, err = parseHfscClassData(class, data)
  241. if err != nil {
  242. return nil, err
  243. }
  244. }
  245. // For backward compatibility.
  246. case nl.TCA_STATS:
  247. base.Statistics, err = parseTcStats(attr.Value)
  248. if err != nil {
  249. return nil, err
  250. }
  251. case nl.TCA_STATS2:
  252. base.Statistics, err = parseTcStats2(attr.Value)
  253. if err != nil {
  254. return nil, err
  255. }
  256. }
  257. }
  258. *class.Attrs() = base
  259. res = append(res, class)
  260. }
  261. return res, nil
  262. }
  263. func parseHtbClassData(class Class, data []syscall.NetlinkRouteAttr) (bool, error) {
  264. htb := class.(*HtbClass)
  265. detailed := false
  266. for _, datum := range data {
  267. switch datum.Attr.Type {
  268. case nl.TCA_HTB_PARMS:
  269. opt := nl.DeserializeTcHtbCopt(datum.Value)
  270. htb.Rate = uint64(opt.Rate.Rate)
  271. htb.Ceil = uint64(opt.Ceil.Rate)
  272. htb.Buffer = opt.Buffer
  273. htb.Cbuffer = opt.Cbuffer
  274. htb.Quantum = opt.Quantum
  275. htb.Level = opt.Level
  276. htb.Prio = opt.Prio
  277. }
  278. }
  279. return detailed, nil
  280. }
  281. func parseHfscClassData(class Class, data []syscall.NetlinkRouteAttr) (bool, error) {
  282. hfsc := class.(*HfscClass)
  283. detailed := false
  284. for _, datum := range data {
  285. m1, d, m2 := nl.DeserializeHfscCurve(datum.Value).Attrs()
  286. switch datum.Attr.Type {
  287. case nl.TCA_HFSC_RSC:
  288. hfsc.Rsc = ServiceCurve{m1: m1, d: d, m2: m2}
  289. case nl.TCA_HFSC_FSC:
  290. hfsc.Fsc = ServiceCurve{m1: m1, d: d, m2: m2}
  291. case nl.TCA_HFSC_USC:
  292. hfsc.Usc = ServiceCurve{m1: m1, d: d, m2: m2}
  293. }
  294. }
  295. return detailed, nil
  296. }
  297. func parseTcStats(data []byte) (*ClassStatistics, error) {
  298. buf := &bytes.Buffer{}
  299. buf.Write(data)
  300. native := nl.NativeEndian()
  301. tcStats := &tcStats{}
  302. if err := binary.Read(buf, native, tcStats); err != nil {
  303. return nil, err
  304. }
  305. stats := NewClassStatistics()
  306. stats.Basic.Bytes = tcStats.Bytes
  307. stats.Basic.Packets = tcStats.Packets
  308. stats.Queue.Qlen = tcStats.Qlen
  309. stats.Queue.Backlog = tcStats.Backlog
  310. stats.Queue.Drops = tcStats.Drops
  311. stats.Queue.Overlimits = tcStats.Overlimits
  312. stats.RateEst.Bps = tcStats.Bps
  313. stats.RateEst.Pps = tcStats.Pps
  314. return stats, nil
  315. }
  316. func parseGnetStats(data []byte, gnetStats interface{}) error {
  317. buf := &bytes.Buffer{}
  318. buf.Write(data)
  319. native := nl.NativeEndian()
  320. return binary.Read(buf, native, gnetStats)
  321. }
  322. func parseTcStats2(data []byte) (*ClassStatistics, error) {
  323. rtAttrs, err := nl.ParseRouteAttr(data)
  324. if err != nil {
  325. return nil, err
  326. }
  327. stats := NewClassStatistics()
  328. for _, datum := range rtAttrs {
  329. switch datum.Attr.Type {
  330. case nl.TCA_STATS_BASIC:
  331. if err := parseGnetStats(datum.Value, stats.Basic); err != nil {
  332. return nil, fmt.Errorf("Failed to parse ClassStatistics.Basic with: %v\n%s",
  333. err, hex.Dump(datum.Value))
  334. }
  335. case nl.TCA_STATS_QUEUE:
  336. if err := parseGnetStats(datum.Value, stats.Queue); err != nil {
  337. return nil, fmt.Errorf("Failed to parse ClassStatistics.Queue with: %v\n%s",
  338. err, hex.Dump(datum.Value))
  339. }
  340. case nl.TCA_STATS_RATE_EST:
  341. if err := parseGnetStats(datum.Value, stats.RateEst); err != nil {
  342. return nil, fmt.Errorf("Failed to parse ClassStatistics.RateEst with: %v\n%s",
  343. err, hex.Dump(datum.Value))
  344. }
  345. }
  346. }
  347. return stats, nil
  348. }