tc_linux.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package nl
  2. import (
  3. "unsafe"
  4. )
  5. // LinkLayer
  6. const (
  7. LINKLAYER_UNSPEC = iota
  8. LINKLAYER_ETHERNET
  9. LINKLAYER_ATM
  10. )
  11. // ATM
  12. const (
  13. ATM_CELL_PAYLOAD = 48
  14. ATM_CELL_SIZE = 53
  15. )
  16. const TC_LINKLAYER_MASK = 0x0F
  17. // Police
  18. const (
  19. TCA_POLICE_UNSPEC = iota
  20. TCA_POLICE_TBF
  21. TCA_POLICE_RATE
  22. TCA_POLICE_PEAKRATE
  23. TCA_POLICE_AVRATE
  24. TCA_POLICE_RESULT
  25. TCA_POLICE_MAX = TCA_POLICE_RESULT
  26. )
  27. // Message types
  28. const (
  29. TCA_UNSPEC = iota
  30. TCA_KIND
  31. TCA_OPTIONS
  32. TCA_STATS
  33. TCA_XSTATS
  34. TCA_RATE
  35. TCA_FCNT
  36. TCA_STATS2
  37. TCA_STAB
  38. TCA_MAX = TCA_STAB
  39. )
  40. const (
  41. TCA_ACT_TAB = 1
  42. TCAA_MAX = 1
  43. )
  44. const (
  45. TCA_PRIO_UNSPEC = iota
  46. TCA_PRIO_MQ
  47. TCA_PRIO_MAX = TCA_PRIO_MQ
  48. )
  49. const (
  50. SizeofTcMsg = 0x14
  51. SizeofTcActionMsg = 0x04
  52. SizeofTcPrioMap = 0x14
  53. SizeofTcRateSpec = 0x0c
  54. SizeofTcNetemQopt = 0x18
  55. SizeofTcNetemCorr = 0x0c
  56. SizeofTcNetemReorder = 0x08
  57. SizeofTcNetemCorrupt = 0x08
  58. SizeofTcTbfQopt = 2*SizeofTcRateSpec + 0x0c
  59. SizeofTcHtbCopt = 2*SizeofTcRateSpec + 0x14
  60. SizeofTcHtbGlob = 0x14
  61. SizeofTcU32Key = 0x10
  62. SizeofTcU32Sel = 0x10 // without keys
  63. SizeofTcMirred = 0x1c
  64. SizeofTcPolice = 2*SizeofTcRateSpec + 0x20
  65. )
  66. // struct tcmsg {
  67. // unsigned char tcm_family;
  68. // unsigned char tcm__pad1;
  69. // unsigned short tcm__pad2;
  70. // int tcm_ifindex;
  71. // __u32 tcm_handle;
  72. // __u32 tcm_parent;
  73. // __u32 tcm_info;
  74. // };
  75. type TcMsg struct {
  76. Family uint8
  77. Pad [3]byte
  78. Ifindex int32
  79. Handle uint32
  80. Parent uint32
  81. Info uint32
  82. }
  83. func (msg *TcMsg) Len() int {
  84. return SizeofTcMsg
  85. }
  86. func DeserializeTcMsg(b []byte) *TcMsg {
  87. return (*TcMsg)(unsafe.Pointer(&b[0:SizeofTcMsg][0]))
  88. }
  89. func (x *TcMsg) Serialize() []byte {
  90. return (*(*[SizeofTcMsg]byte)(unsafe.Pointer(x)))[:]
  91. }
  92. // struct tcamsg {
  93. // unsigned char tca_family;
  94. // unsigned char tca__pad1;
  95. // unsigned short tca__pad2;
  96. // };
  97. type TcActionMsg struct {
  98. Family uint8
  99. Pad [3]byte
  100. }
  101. func (msg *TcActionMsg) Len() int {
  102. return SizeofTcActionMsg
  103. }
  104. func DeserializeTcActionMsg(b []byte) *TcActionMsg {
  105. return (*TcActionMsg)(unsafe.Pointer(&b[0:SizeofTcActionMsg][0]))
  106. }
  107. func (x *TcActionMsg) Serialize() []byte {
  108. return (*(*[SizeofTcActionMsg]byte)(unsafe.Pointer(x)))[:]
  109. }
  110. const (
  111. TC_PRIO_MAX = 15
  112. )
  113. // struct tc_prio_qopt {
  114. // int bands; /* Number of bands */
  115. // __u8 priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
  116. // };
  117. type TcPrioMap struct {
  118. Bands int32
  119. Priomap [TC_PRIO_MAX + 1]uint8
  120. }
  121. func (msg *TcPrioMap) Len() int {
  122. return SizeofTcPrioMap
  123. }
  124. func DeserializeTcPrioMap(b []byte) *TcPrioMap {
  125. return (*TcPrioMap)(unsafe.Pointer(&b[0:SizeofTcPrioMap][0]))
  126. }
  127. func (x *TcPrioMap) Serialize() []byte {
  128. return (*(*[SizeofTcPrioMap]byte)(unsafe.Pointer(x)))[:]
  129. }
  130. const (
  131. TCA_TBF_UNSPEC = iota
  132. TCA_TBF_PARMS
  133. TCA_TBF_RTAB
  134. TCA_TBF_PTAB
  135. TCA_TBF_RATE64
  136. TCA_TBF_PRATE64
  137. TCA_TBF_BURST
  138. TCA_TBF_PBURST
  139. TCA_TBF_MAX = TCA_TBF_PBURST
  140. )
  141. // struct tc_ratespec {
  142. // unsigned char cell_log;
  143. // __u8 linklayer; /* lower 4 bits */
  144. // unsigned short overhead;
  145. // short cell_align;
  146. // unsigned short mpu;
  147. // __u32 rate;
  148. // };
  149. type TcRateSpec struct {
  150. CellLog uint8
  151. Linklayer uint8
  152. Overhead uint16
  153. CellAlign int16
  154. Mpu uint16
  155. Rate uint32
  156. }
  157. func (msg *TcRateSpec) Len() int {
  158. return SizeofTcRateSpec
  159. }
  160. func DeserializeTcRateSpec(b []byte) *TcRateSpec {
  161. return (*TcRateSpec)(unsafe.Pointer(&b[0:SizeofTcRateSpec][0]))
  162. }
  163. func (x *TcRateSpec) Serialize() []byte {
  164. return (*(*[SizeofTcRateSpec]byte)(unsafe.Pointer(x)))[:]
  165. }
  166. /**
  167. * NETEM
  168. */
  169. const (
  170. TCA_NETEM_UNSPEC = iota
  171. TCA_NETEM_CORR
  172. TCA_NETEM_DELAY_DIST
  173. TCA_NETEM_REORDER
  174. TCA_NETEM_CORRUPT
  175. TCA_NETEM_LOSS
  176. TCA_NETEM_RATE
  177. TCA_NETEM_ECN
  178. TCA_NETEM_RATE64
  179. TCA_NETEM_MAX = TCA_NETEM_RATE64
  180. )
  181. // struct tc_netem_qopt {
  182. // __u32 latency; /* added delay (us) */
  183. // __u32 limit; /* fifo limit (packets) */
  184. // __u32 loss; /* random packet loss (0=none ~0=100%) */
  185. // __u32 gap; /* re-ordering gap (0 for none) */
  186. // __u32 duplicate; /* random packet dup (0=none ~0=100%) */
  187. // __u32 jitter; /* random jitter in latency (us) */
  188. // };
  189. type TcNetemQopt struct {
  190. Latency uint32
  191. Limit uint32
  192. Loss uint32
  193. Gap uint32
  194. Duplicate uint32
  195. Jitter uint32
  196. }
  197. func (msg *TcNetemQopt) Len() int {
  198. return SizeofTcNetemQopt
  199. }
  200. func DeserializeTcNetemQopt(b []byte) *TcNetemQopt {
  201. return (*TcNetemQopt)(unsafe.Pointer(&b[0:SizeofTcNetemQopt][0]))
  202. }
  203. func (x *TcNetemQopt) Serialize() []byte {
  204. return (*(*[SizeofTcNetemQopt]byte)(unsafe.Pointer(x)))[:]
  205. }
  206. // struct tc_netem_corr {
  207. // __u32 delay_corr; /* delay correlation */
  208. // __u32 loss_corr; /* packet loss correlation */
  209. // __u32 dup_corr; /* duplicate correlation */
  210. // };
  211. type TcNetemCorr struct {
  212. DelayCorr uint32
  213. LossCorr uint32
  214. DupCorr uint32
  215. }
  216. func (msg *TcNetemCorr) Len() int {
  217. return SizeofTcNetemCorr
  218. }
  219. func DeserializeTcNetemCorr(b []byte) *TcNetemCorr {
  220. return (*TcNetemCorr)(unsafe.Pointer(&b[0:SizeofTcNetemCorr][0]))
  221. }
  222. func (x *TcNetemCorr) Serialize() []byte {
  223. return (*(*[SizeofTcNetemCorr]byte)(unsafe.Pointer(x)))[:]
  224. }
  225. // struct tc_netem_reorder {
  226. // __u32 probability;
  227. // __u32 correlation;
  228. // };
  229. type TcNetemReorder struct {
  230. Probability uint32
  231. Correlation uint32
  232. }
  233. func (msg *TcNetemReorder) Len() int {
  234. return SizeofTcNetemReorder
  235. }
  236. func DeserializeTcNetemReorder(b []byte) *TcNetemReorder {
  237. return (*TcNetemReorder)(unsafe.Pointer(&b[0:SizeofTcNetemReorder][0]))
  238. }
  239. func (x *TcNetemReorder) Serialize() []byte {
  240. return (*(*[SizeofTcNetemReorder]byte)(unsafe.Pointer(x)))[:]
  241. }
  242. // struct tc_netem_corrupt {
  243. // __u32 probability;
  244. // __u32 correlation;
  245. // };
  246. type TcNetemCorrupt struct {
  247. Probability uint32
  248. Correlation uint32
  249. }
  250. func (msg *TcNetemCorrupt) Len() int {
  251. return SizeofTcNetemCorrupt
  252. }
  253. func DeserializeTcNetemCorrupt(b []byte) *TcNetemCorrupt {
  254. return (*TcNetemCorrupt)(unsafe.Pointer(&b[0:SizeofTcNetemCorrupt][0]))
  255. }
  256. func (x *TcNetemCorrupt) Serialize() []byte {
  257. return (*(*[SizeofTcNetemCorrupt]byte)(unsafe.Pointer(x)))[:]
  258. }
  259. // struct tc_tbf_qopt {
  260. // struct tc_ratespec rate;
  261. // struct tc_ratespec peakrate;
  262. // __u32 limit;
  263. // __u32 buffer;
  264. // __u32 mtu;
  265. // };
  266. type TcTbfQopt struct {
  267. Rate TcRateSpec
  268. Peakrate TcRateSpec
  269. Limit uint32
  270. Buffer uint32
  271. Mtu uint32
  272. }
  273. func (msg *TcTbfQopt) Len() int {
  274. return SizeofTcTbfQopt
  275. }
  276. func DeserializeTcTbfQopt(b []byte) *TcTbfQopt {
  277. return (*TcTbfQopt)(unsafe.Pointer(&b[0:SizeofTcTbfQopt][0]))
  278. }
  279. func (x *TcTbfQopt) Serialize() []byte {
  280. return (*(*[SizeofTcTbfQopt]byte)(unsafe.Pointer(x)))[:]
  281. }
  282. const (
  283. TCA_HTB_UNSPEC = iota
  284. TCA_HTB_PARMS
  285. TCA_HTB_INIT
  286. TCA_HTB_CTAB
  287. TCA_HTB_RTAB
  288. TCA_HTB_DIRECT_QLEN
  289. TCA_HTB_RATE64
  290. TCA_HTB_CEIL64
  291. TCA_HTB_MAX = TCA_HTB_CEIL64
  292. )
  293. //struct tc_htb_opt {
  294. // struct tc_ratespec rate;
  295. // struct tc_ratespec ceil;
  296. // __u32 buffer;
  297. // __u32 cbuffer;
  298. // __u32 quantum;
  299. // __u32 level; /* out only */
  300. // __u32 prio;
  301. //};
  302. type TcHtbCopt struct {
  303. Rate TcRateSpec
  304. Ceil TcRateSpec
  305. Buffer uint32
  306. Cbuffer uint32
  307. Quantum uint32
  308. Level uint32
  309. Prio uint32
  310. }
  311. func (msg *TcHtbCopt) Len() int {
  312. return SizeofTcHtbCopt
  313. }
  314. func DeserializeTcHtbCopt(b []byte) *TcHtbCopt {
  315. return (*TcHtbCopt)(unsafe.Pointer(&b[0:SizeofTcHtbCopt][0]))
  316. }
  317. func (x *TcHtbCopt) Serialize() []byte {
  318. return (*(*[SizeofTcHtbCopt]byte)(unsafe.Pointer(x)))[:]
  319. }
  320. type TcHtbGlob struct {
  321. Version uint32
  322. Rate2Quantum uint32
  323. Defcls uint32
  324. Debug uint32
  325. DirectPkts uint32
  326. }
  327. func (msg *TcHtbGlob) Len() int {
  328. return SizeofTcHtbGlob
  329. }
  330. func DeserializeTcHtbGlob(b []byte) *TcHtbGlob {
  331. return (*TcHtbGlob)(unsafe.Pointer(&b[0:SizeofTcHtbGlob][0]))
  332. }
  333. func (x *TcHtbGlob) Serialize() []byte {
  334. return (*(*[SizeofTcHtbGlob]byte)(unsafe.Pointer(x)))[:]
  335. }
  336. const (
  337. TCA_U32_UNSPEC = iota
  338. TCA_U32_CLASSID
  339. TCA_U32_HASH
  340. TCA_U32_LINK
  341. TCA_U32_DIVISOR
  342. TCA_U32_SEL
  343. TCA_U32_POLICE
  344. TCA_U32_ACT
  345. TCA_U32_INDEV
  346. TCA_U32_PCNT
  347. TCA_U32_MARK
  348. TCA_U32_MAX = TCA_U32_MARK
  349. )
  350. // struct tc_u32_key {
  351. // __be32 mask;
  352. // __be32 val;
  353. // int off;
  354. // int offmask;
  355. // };
  356. type TcU32Key struct {
  357. Mask uint32 // big endian
  358. Val uint32 // big endian
  359. Off int32
  360. OffMask int32
  361. }
  362. func (msg *TcU32Key) Len() int {
  363. return SizeofTcU32Key
  364. }
  365. func DeserializeTcU32Key(b []byte) *TcU32Key {
  366. return (*TcU32Key)(unsafe.Pointer(&b[0:SizeofTcU32Key][0]))
  367. }
  368. func (x *TcU32Key) Serialize() []byte {
  369. return (*(*[SizeofTcU32Key]byte)(unsafe.Pointer(x)))[:]
  370. }
  371. // struct tc_u32_sel {
  372. // unsigned char flags;
  373. // unsigned char offshift;
  374. // unsigned char nkeys;
  375. //
  376. // __be16 offmask;
  377. // __u16 off;
  378. // short offoff;
  379. //
  380. // short hoff;
  381. // __be32 hmask;
  382. // struct tc_u32_key keys[0];
  383. // };
  384. const (
  385. TC_U32_TERMINAL = 1 << iota
  386. TC_U32_OFFSET = 1 << iota
  387. TC_U32_VAROFFSET = 1 << iota
  388. TC_U32_EAT = 1 << iota
  389. )
  390. type TcU32Sel struct {
  391. Flags uint8
  392. Offshift uint8
  393. Nkeys uint8
  394. Pad uint8
  395. Offmask uint16 // big endian
  396. Off uint16
  397. Offoff int16
  398. Hoff int16
  399. Hmask uint32 // big endian
  400. Keys []TcU32Key
  401. }
  402. func (msg *TcU32Sel) Len() int {
  403. return SizeofTcU32Sel + int(msg.Nkeys)*SizeofTcU32Key
  404. }
  405. func DeserializeTcU32Sel(b []byte) *TcU32Sel {
  406. x := &TcU32Sel{}
  407. copy((*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:], b)
  408. next := SizeofTcU32Sel
  409. var i uint8
  410. for i = 0; i < x.Nkeys; i++ {
  411. x.Keys = append(x.Keys, *DeserializeTcU32Key(b[next:]))
  412. next += SizeofTcU32Key
  413. }
  414. return x
  415. }
  416. func (x *TcU32Sel) Serialize() []byte {
  417. // This can't just unsafe.cast because it must iterate through keys.
  418. buf := make([]byte, x.Len())
  419. copy(buf, (*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:])
  420. next := SizeofTcU32Sel
  421. for _, key := range x.Keys {
  422. keyBuf := key.Serialize()
  423. copy(buf[next:], keyBuf)
  424. next += SizeofTcU32Key
  425. }
  426. return buf
  427. }
  428. const (
  429. TCA_ACT_MIRRED = 8
  430. )
  431. const (
  432. TCA_MIRRED_UNSPEC = iota
  433. TCA_MIRRED_TM
  434. TCA_MIRRED_PARMS
  435. TCA_MIRRED_MAX = TCA_MIRRED_PARMS
  436. )
  437. const (
  438. TCA_EGRESS_REDIR = 1 /* packet redirect to EGRESS*/
  439. TCA_EGRESS_MIRROR = 2 /* mirror packet to EGRESS */
  440. TCA_INGRESS_REDIR = 3 /* packet redirect to INGRESS*/
  441. TCA_INGRESS_MIRROR = 4 /* mirror packet to INGRESS */
  442. )
  443. const (
  444. TC_ACT_UNSPEC = int32(-1)
  445. TC_ACT_OK = 0
  446. TC_ACT_RECLASSIFY = 1
  447. TC_ACT_SHOT = 2
  448. TC_ACT_PIPE = 3
  449. TC_ACT_STOLEN = 4
  450. TC_ACT_QUEUED = 5
  451. TC_ACT_REPEAT = 6
  452. TC_ACT_JUMP = 0x10000000
  453. )
  454. // #define tc_gen \
  455. // __u32 index; \
  456. // __u32 capab; \
  457. // int action; \
  458. // int refcnt; \
  459. // int bindcnt
  460. // struct tc_mirred {
  461. // tc_gen;
  462. // int eaction; /* one of IN/EGRESS_MIRROR/REDIR */
  463. // __u32 ifindex; /* ifindex of egress port */
  464. // };
  465. type TcMirred struct {
  466. Index uint32
  467. Capab uint32
  468. Action int32
  469. Refcnt int32
  470. Bindcnt int32
  471. Eaction int32
  472. Ifindex uint32
  473. }
  474. func (msg *TcMirred) Len() int {
  475. return SizeofTcMirred
  476. }
  477. func DeserializeTcMirred(b []byte) *TcMirred {
  478. return (*TcMirred)(unsafe.Pointer(&b[0:SizeofTcMirred][0]))
  479. }
  480. func (x *TcMirred) Serialize() []byte {
  481. return (*(*[SizeofTcMirred]byte)(unsafe.Pointer(x)))[:]
  482. }
  483. const (
  484. TC_POLICE_UNSPEC = TC_ACT_UNSPEC
  485. TC_POLICE_OK = TC_ACT_OK
  486. TC_POLICE_RECLASSIFY = TC_ACT_RECLASSIFY
  487. TC_POLICE_SHOT = TC_ACT_SHOT
  488. TC_POLICE_PIPE = TC_ACT_PIPE
  489. )
  490. // struct tc_police {
  491. // __u32 index;
  492. // int action;
  493. // __u32 limit;
  494. // __u32 burst;
  495. // __u32 mtu;
  496. // struct tc_ratespec rate;
  497. // struct tc_ratespec peakrate;
  498. // int refcnt;
  499. // int bindcnt;
  500. // __u32 capab;
  501. // };
  502. type TcPolice struct {
  503. Index uint32
  504. Action int32
  505. Limit uint32
  506. Burst uint32
  507. Mtu uint32
  508. Rate TcRateSpec
  509. PeakRate TcRateSpec
  510. Refcnt int32
  511. Bindcnt int32
  512. Capab uint32
  513. }
  514. func (msg *TcPolice) Len() int {
  515. return SizeofTcPolice
  516. }
  517. func DeserializeTcPolice(b []byte) *TcPolice {
  518. return (*TcPolice)(unsafe.Pointer(&b[0:SizeofTcPolice][0]))
  519. }
  520. func (x *TcPolice) Serialize() []byte {
  521. return (*(*[SizeofTcPolice]byte)(unsafe.Pointer(x)))[:]
  522. }
  523. const (
  524. TCA_FW_UNSPEC = iota
  525. TCA_FW_CLASSID
  526. TCA_FW_POLICE
  527. TCA_FW_INDEV
  528. TCA_FW_ACT
  529. TCA_FW_MASK
  530. TCA_FW_MAX = TCA_FW_MASK
  531. )