tc_linux.go 12 KB

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