class_linux.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 = Xmittime(rate, buffer)
  41. if cbuffer == 0 {
  42. cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
  43. }
  44. cbuffer = Xmittime(ceil, cbuffer)
  45. return &HtbClass{
  46. ClassAttrs: attrs,
  47. Rate: rate,
  48. Ceil: ceil,
  49. Buffer: buffer,
  50. Cbuffer: cbuffer,
  51. Level: 0,
  52. Prio: cattrs.Prio,
  53. Quantum: cattrs.Quantum,
  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. if htb.Rate >= uint64(1<<32) {
  160. options.AddRtAttr(nl.TCA_HTB_RATE64, nl.Uint64Attr(htb.Rate))
  161. }
  162. if htb.Ceil >= uint64(1<<32) {
  163. options.AddRtAttr(nl.TCA_HTB_CEIL64, nl.Uint64Attr(htb.Ceil))
  164. }
  165. case "hfsc":
  166. hfsc := class.(*HfscClass)
  167. opt := nl.HfscCopt{}
  168. rm1, rd, rm2 := hfsc.Rsc.Attrs()
  169. opt.Rsc.Set(rm1/8, rd, rm2/8)
  170. fm1, fd, fm2 := hfsc.Fsc.Attrs()
  171. opt.Fsc.Set(fm1/8, fd, fm2/8)
  172. um1, ud, um2 := hfsc.Usc.Attrs()
  173. opt.Usc.Set(um1/8, ud, um2/8)
  174. options.AddRtAttr(nl.TCA_HFSC_RSC, nl.SerializeHfscCurve(&opt.Rsc))
  175. options.AddRtAttr(nl.TCA_HFSC_FSC, nl.SerializeHfscCurve(&opt.Fsc))
  176. options.AddRtAttr(nl.TCA_HFSC_USC, nl.SerializeHfscCurve(&opt.Usc))
  177. }
  178. req.AddData(options)
  179. return nil
  180. }
  181. // ClassList gets a list of classes in the system.
  182. // Equivalent to: `tc class show`.
  183. // Generally returns nothing if link and parent are not specified.
  184. func ClassList(link Link, parent uint32) ([]Class, error) {
  185. return pkgHandle.ClassList(link, parent)
  186. }
  187. // ClassList gets a list of classes in the system.
  188. // Equivalent to: `tc class show`.
  189. // Generally returns nothing if link and parent are not specified.
  190. func (h *Handle) ClassList(link Link, parent uint32) ([]Class, error) {
  191. req := h.newNetlinkRequest(unix.RTM_GETTCLASS, unix.NLM_F_DUMP)
  192. msg := &nl.TcMsg{
  193. Family: nl.FAMILY_ALL,
  194. Parent: parent,
  195. }
  196. if link != nil {
  197. base := link.Attrs()
  198. h.ensureIndex(base)
  199. msg.Ifindex = int32(base.Index)
  200. }
  201. req.AddData(msg)
  202. msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWTCLASS)
  203. if err != nil {
  204. return nil, err
  205. }
  206. var res []Class
  207. for _, m := range msgs {
  208. msg := nl.DeserializeTcMsg(m)
  209. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  210. if err != nil {
  211. return nil, err
  212. }
  213. base := ClassAttrs{
  214. LinkIndex: int(msg.Ifindex),
  215. Handle: msg.Handle,
  216. Parent: msg.Parent,
  217. Statistics: nil,
  218. }
  219. var class Class
  220. classType := ""
  221. for _, attr := range attrs {
  222. switch attr.Attr.Type {
  223. case nl.TCA_KIND:
  224. classType = string(attr.Value[:len(attr.Value)-1])
  225. switch classType {
  226. case "htb":
  227. class = &HtbClass{}
  228. case "hfsc":
  229. class = &HfscClass{}
  230. default:
  231. class = &GenericClass{ClassType: classType}
  232. }
  233. case nl.TCA_OPTIONS:
  234. switch classType {
  235. case "htb":
  236. data, err := nl.ParseRouteAttr(attr.Value)
  237. if err != nil {
  238. return nil, err
  239. }
  240. _, err = parseHtbClassData(class, data)
  241. if err != nil {
  242. return nil, err
  243. }
  244. case "hfsc":
  245. data, err := nl.ParseRouteAttr(attr.Value)
  246. if err != nil {
  247. return nil, err
  248. }
  249. _, err = parseHfscClassData(class, data)
  250. if err != nil {
  251. return nil, err
  252. }
  253. }
  254. // For backward compatibility.
  255. case nl.TCA_STATS:
  256. base.Statistics, err = parseTcStats(attr.Value)
  257. if err != nil {
  258. return nil, err
  259. }
  260. case nl.TCA_STATS2:
  261. base.Statistics, err = parseTcStats2(attr.Value)
  262. if err != nil {
  263. return nil, err
  264. }
  265. }
  266. }
  267. *class.Attrs() = base
  268. res = append(res, class)
  269. }
  270. return res, nil
  271. }
  272. func parseHtbClassData(class Class, data []syscall.NetlinkRouteAttr) (bool, error) {
  273. htb := class.(*HtbClass)
  274. detailed := false
  275. for _, datum := range data {
  276. switch datum.Attr.Type {
  277. case nl.TCA_HTB_PARMS:
  278. opt := nl.DeserializeTcHtbCopt(datum.Value)
  279. htb.Rate = uint64(opt.Rate.Rate)
  280. htb.Ceil = uint64(opt.Ceil.Rate)
  281. htb.Buffer = opt.Buffer
  282. htb.Cbuffer = opt.Cbuffer
  283. htb.Quantum = opt.Quantum
  284. htb.Level = opt.Level
  285. htb.Prio = opt.Prio
  286. case nl.TCA_HTB_RATE64:
  287. htb.Rate = native.Uint64(datum.Value[0:8])
  288. case nl.TCA_HTB_CEIL64:
  289. htb.Ceil = native.Uint64(datum.Value[0:8])
  290. }
  291. }
  292. return detailed, nil
  293. }
  294. func parseHfscClassData(class Class, data []syscall.NetlinkRouteAttr) (bool, error) {
  295. hfsc := class.(*HfscClass)
  296. detailed := false
  297. for _, datum := range data {
  298. m1, d, m2 := nl.DeserializeHfscCurve(datum.Value).Attrs()
  299. switch datum.Attr.Type {
  300. case nl.TCA_HFSC_RSC:
  301. hfsc.Rsc = ServiceCurve{m1: m1 * 8, d: d, m2: m2 * 8}
  302. case nl.TCA_HFSC_FSC:
  303. hfsc.Fsc = ServiceCurve{m1: m1 * 8, d: d, m2: m2 * 8}
  304. case nl.TCA_HFSC_USC:
  305. hfsc.Usc = ServiceCurve{m1: m1 * 8, d: d, m2: m2 * 8}
  306. }
  307. }
  308. return detailed, nil
  309. }
  310. func parseTcStats(data []byte) (*ClassStatistics, error) {
  311. buf := &bytes.Buffer{}
  312. buf.Write(data)
  313. tcStats := &tcStats{}
  314. if err := binary.Read(buf, native, tcStats); err != nil {
  315. return nil, err
  316. }
  317. stats := NewClassStatistics()
  318. stats.Basic.Bytes = tcStats.Bytes
  319. stats.Basic.Packets = tcStats.Packets
  320. stats.Queue.Qlen = tcStats.Qlen
  321. stats.Queue.Backlog = tcStats.Backlog
  322. stats.Queue.Drops = tcStats.Drops
  323. stats.Queue.Overlimits = tcStats.Overlimits
  324. stats.RateEst.Bps = tcStats.Bps
  325. stats.RateEst.Pps = tcStats.Pps
  326. return stats, nil
  327. }
  328. func parseGnetStats(data []byte, gnetStats interface{}) error {
  329. buf := &bytes.Buffer{}
  330. buf.Write(data)
  331. return binary.Read(buf, native, gnetStats)
  332. }
  333. func parseTcStats2(data []byte) (*ClassStatistics, error) {
  334. rtAttrs, err := nl.ParseRouteAttr(data)
  335. if err != nil {
  336. return nil, err
  337. }
  338. stats := NewClassStatistics()
  339. for _, datum := range rtAttrs {
  340. switch datum.Attr.Type {
  341. case nl.TCA_STATS_BASIC:
  342. if err := parseGnetStats(datum.Value, stats.Basic); err != nil {
  343. return nil, fmt.Errorf("Failed to parse ClassStatistics.Basic with: %v\n%s",
  344. err, hex.Dump(datum.Value))
  345. }
  346. case nl.TCA_STATS_QUEUE:
  347. if err := parseGnetStats(datum.Value, stats.Queue); err != nil {
  348. return nil, fmt.Errorf("Failed to parse ClassStatistics.Queue with: %v\n%s",
  349. err, hex.Dump(datum.Value))
  350. }
  351. case nl.TCA_STATS_RATE_EST:
  352. if err := parseGnetStats(datum.Value, stats.RateEst); err != nil {
  353. return nil, fmt.Errorf("Failed to parse ClassStatistics.RateEst with: %v\n%s",
  354. err, hex.Dump(datum.Value))
  355. }
  356. }
  357. }
  358. return stats, nil
  359. }