tc_linux.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. package nl
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "net"
  7. "unsafe"
  8. "golang.org/x/sys/unix"
  9. )
  10. // LinkLayer
  11. const (
  12. LINKLAYER_UNSPEC = iota
  13. LINKLAYER_ETHERNET
  14. LINKLAYER_ATM
  15. )
  16. // ATM
  17. const (
  18. ATM_CELL_PAYLOAD = 48
  19. ATM_CELL_SIZE = 53
  20. )
  21. const TC_LINKLAYER_MASK = 0x0F
  22. // Police
  23. const (
  24. TCA_POLICE_UNSPEC = iota
  25. TCA_POLICE_TBF
  26. TCA_POLICE_RATE
  27. TCA_POLICE_PEAKRATE
  28. TCA_POLICE_AVRATE
  29. TCA_POLICE_RESULT
  30. TCA_POLICE_MAX = TCA_POLICE_RESULT
  31. )
  32. // Message types
  33. const (
  34. TCA_UNSPEC = iota
  35. TCA_KIND
  36. TCA_OPTIONS
  37. TCA_STATS
  38. TCA_XSTATS
  39. TCA_RATE
  40. TCA_FCNT
  41. TCA_STATS2
  42. TCA_STAB
  43. TCA_PAD
  44. TCA_DUMP_INVISIBLE
  45. TCA_CHAIN
  46. TCA_HW_OFFLOAD
  47. TCA_INGRESS_BLOCK
  48. TCA_EGRESS_BLOCK
  49. TCA_DUMP_FLAGS
  50. TCA_MAX = TCA_DUMP_FLAGS
  51. )
  52. const (
  53. TCA_ACT_TAB = 1
  54. TCAA_MAX = 1
  55. )
  56. const (
  57. TCA_ACT_UNSPEC = iota
  58. TCA_ACT_KIND
  59. TCA_ACT_OPTIONS
  60. TCA_ACT_INDEX
  61. TCA_ACT_STATS
  62. TCA_ACT_PAD
  63. TCA_ACT_COOKIE
  64. TCA_ACT_FLAGS
  65. TCA_ACT_HW_STATS
  66. TCA_ACT_USED_HW_STATS
  67. TCA_ACT_IN_HW_COUNT
  68. TCA_ACT_MAX
  69. )
  70. const (
  71. TCA_ACT_SAMPLE_UNSPEC = iota
  72. TCA_ACT_SAMPLE_TM
  73. TCA_ACT_SAMPLE_PARMS
  74. TCA_ACT_SAMPLE_RATE
  75. TCA_ACT_SAMPLE_TRUNC_SIZE
  76. TCA_ACT_SAMPLE_PSAMPLE_GROUP
  77. TCA_ACT_SAMPLE_PAD
  78. TCA_ACT_SAMPLE_MAX
  79. )
  80. const (
  81. TCA_PRIO_UNSPEC = iota
  82. TCA_PRIO_MQ
  83. TCA_PRIO_MAX = TCA_PRIO_MQ
  84. )
  85. const (
  86. TCA_STATS_UNSPEC = iota
  87. TCA_STATS_BASIC
  88. TCA_STATS_RATE_EST
  89. TCA_STATS_QUEUE
  90. TCA_STATS_APP
  91. TCA_STATS_RATE_EST64
  92. TCA_STATS_PAD
  93. TCA_STATS_BASIC_HW
  94. TCA_STATS_PKT64
  95. TCA_STATS_MAX = TCA_STATS_PKT64
  96. )
  97. const (
  98. SizeofTcMsg = 0x14
  99. SizeofTcActionMsg = 0x04
  100. SizeofTcPrioMap = 0x14
  101. SizeofTcRateSpec = 0x0c
  102. SizeofTcNetemQopt = 0x18
  103. SizeofTcNetemCorr = 0x0c
  104. SizeofTcNetemReorder = 0x08
  105. SizeofTcNetemCorrupt = 0x08
  106. SizeOfTcNetemRate = 0x10
  107. SizeofTcTbfQopt = 2*SizeofTcRateSpec + 0x0c
  108. SizeofTcHtbCopt = 2*SizeofTcRateSpec + 0x14
  109. SizeofTcHtbGlob = 0x14
  110. SizeofTcU32Key = 0x10
  111. SizeofTcU32Sel = 0x10 // without keys
  112. SizeofTcGen = 0x16
  113. SizeofTcConnmark = SizeofTcGen + 0x04
  114. SizeofTcCsum = SizeofTcGen + 0x04
  115. SizeofTcMirred = SizeofTcGen + 0x08
  116. SizeofTcVlan = SizeofTcGen + 0x04
  117. SizeofTcTunnelKey = SizeofTcGen + 0x04
  118. SizeofTcSkbEdit = SizeofTcGen
  119. SizeofTcPolice = 2*SizeofTcRateSpec + 0x20
  120. SizeofTcSfqQopt = 0x0b
  121. SizeofTcSfqRedStats = 0x18
  122. SizeofTcSfqQoptV1 = SizeofTcSfqQopt + SizeofTcSfqRedStats + 0x1c
  123. SizeofUint32Bitfield = 0x8
  124. )
  125. // struct tcmsg {
  126. // unsigned char tcm_family;
  127. // unsigned char tcm__pad1;
  128. // unsigned short tcm__pad2;
  129. // int tcm_ifindex;
  130. // __u32 tcm_handle;
  131. // __u32 tcm_parent;
  132. // __u32 tcm_info;
  133. // };
  134. type TcMsg struct {
  135. Family uint8
  136. Pad [3]byte
  137. Ifindex int32
  138. Handle uint32
  139. Parent uint32
  140. Info uint32
  141. }
  142. func (msg *TcMsg) Len() int {
  143. return SizeofTcMsg
  144. }
  145. func DeserializeTcMsg(b []byte) *TcMsg {
  146. return (*TcMsg)(unsafe.Pointer(&b[0:SizeofTcMsg][0]))
  147. }
  148. func (x *TcMsg) Serialize() []byte {
  149. return (*(*[SizeofTcMsg]byte)(unsafe.Pointer(x)))[:]
  150. }
  151. type Tcf struct {
  152. Install uint64
  153. LastUse uint64
  154. Expires uint64
  155. FirstUse uint64
  156. }
  157. func DeserializeTcf(b []byte) *Tcf {
  158. const size = int(unsafe.Sizeof(Tcf{}))
  159. return (*Tcf)(unsafe.Pointer(&b[0:size][0]))
  160. }
  161. // struct tcamsg {
  162. // unsigned char tca_family;
  163. // unsigned char tca__pad1;
  164. // unsigned short tca__pad2;
  165. // };
  166. type TcActionMsg struct {
  167. Family uint8
  168. Pad [3]byte
  169. }
  170. func (msg *TcActionMsg) Len() int {
  171. return SizeofTcActionMsg
  172. }
  173. func DeserializeTcActionMsg(b []byte) *TcActionMsg {
  174. return (*TcActionMsg)(unsafe.Pointer(&b[0:SizeofTcActionMsg][0]))
  175. }
  176. func (x *TcActionMsg) Serialize() []byte {
  177. return (*(*[SizeofTcActionMsg]byte)(unsafe.Pointer(x)))[:]
  178. }
  179. const (
  180. TC_PRIO_MAX = 15
  181. )
  182. // struct tc_prio_qopt {
  183. // int bands; /* Number of bands */
  184. // __u8 priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
  185. // };
  186. type TcPrioMap struct {
  187. Bands int32
  188. Priomap [TC_PRIO_MAX + 1]uint8
  189. }
  190. func (msg *TcPrioMap) Len() int {
  191. return SizeofTcPrioMap
  192. }
  193. func DeserializeTcPrioMap(b []byte) *TcPrioMap {
  194. return (*TcPrioMap)(unsafe.Pointer(&b[0:SizeofTcPrioMap][0]))
  195. }
  196. func (x *TcPrioMap) Serialize() []byte {
  197. return (*(*[SizeofTcPrioMap]byte)(unsafe.Pointer(x)))[:]
  198. }
  199. const (
  200. TCA_TBF_UNSPEC = iota
  201. TCA_TBF_PARMS
  202. TCA_TBF_RTAB
  203. TCA_TBF_PTAB
  204. TCA_TBF_RATE64
  205. TCA_TBF_PRATE64
  206. TCA_TBF_BURST
  207. TCA_TBF_PBURST
  208. TCA_TBF_MAX = TCA_TBF_PBURST
  209. )
  210. // struct tc_ratespec {
  211. // unsigned char cell_log;
  212. // __u8 linklayer; /* lower 4 bits */
  213. // unsigned short overhead;
  214. // short cell_align;
  215. // unsigned short mpu;
  216. // __u32 rate;
  217. // };
  218. type TcRateSpec struct {
  219. CellLog uint8
  220. Linklayer uint8
  221. Overhead uint16
  222. CellAlign int16
  223. Mpu uint16
  224. Rate uint32
  225. }
  226. func (msg *TcRateSpec) Len() int {
  227. return SizeofTcRateSpec
  228. }
  229. func DeserializeTcRateSpec(b []byte) *TcRateSpec {
  230. return (*TcRateSpec)(unsafe.Pointer(&b[0:SizeofTcRateSpec][0]))
  231. }
  232. func (x *TcRateSpec) Serialize() []byte {
  233. return (*(*[SizeofTcRateSpec]byte)(unsafe.Pointer(x)))[:]
  234. }
  235. /**
  236. * NETEM
  237. */
  238. const (
  239. TCA_NETEM_UNSPEC = iota
  240. TCA_NETEM_CORR
  241. TCA_NETEM_DELAY_DIST
  242. TCA_NETEM_REORDER
  243. TCA_NETEM_CORRUPT
  244. TCA_NETEM_LOSS
  245. TCA_NETEM_RATE
  246. TCA_NETEM_ECN
  247. TCA_NETEM_RATE64
  248. TCA_NETEM_MAX = TCA_NETEM_RATE64
  249. )
  250. // struct tc_netem_qopt {
  251. // __u32 latency; /* added delay (us) */
  252. // __u32 limit; /* fifo limit (packets) */
  253. // __u32 loss; /* random packet loss (0=none ~0=100%) */
  254. // __u32 gap; /* re-ordering gap (0 for none) */
  255. // __u32 duplicate; /* random packet dup (0=none ~0=100%) */
  256. // __u32 jitter; /* random jitter in latency (us) */
  257. // };
  258. type TcNetemQopt struct {
  259. Latency uint32
  260. Limit uint32
  261. Loss uint32
  262. Gap uint32
  263. Duplicate uint32
  264. Jitter uint32
  265. }
  266. func (msg *TcNetemQopt) Len() int {
  267. return SizeofTcNetemQopt
  268. }
  269. func DeserializeTcNetemQopt(b []byte) *TcNetemQopt {
  270. return (*TcNetemQopt)(unsafe.Pointer(&b[0:SizeofTcNetemQopt][0]))
  271. }
  272. func (x *TcNetemQopt) Serialize() []byte {
  273. return (*(*[SizeofTcNetemQopt]byte)(unsafe.Pointer(x)))[:]
  274. }
  275. // struct tc_netem_corr {
  276. // __u32 delay_corr; /* delay correlation */
  277. // __u32 loss_corr; /* packet loss correlation */
  278. // __u32 dup_corr; /* duplicate correlation */
  279. // };
  280. type TcNetemCorr struct {
  281. DelayCorr uint32
  282. LossCorr uint32
  283. DupCorr uint32
  284. }
  285. func (msg *TcNetemCorr) Len() int {
  286. return SizeofTcNetemCorr
  287. }
  288. func DeserializeTcNetemCorr(b []byte) *TcNetemCorr {
  289. return (*TcNetemCorr)(unsafe.Pointer(&b[0:SizeofTcNetemCorr][0]))
  290. }
  291. func (x *TcNetemCorr) Serialize() []byte {
  292. return (*(*[SizeofTcNetemCorr]byte)(unsafe.Pointer(x)))[:]
  293. }
  294. // struct tc_netem_reorder {
  295. // __u32 probability;
  296. // __u32 correlation;
  297. // };
  298. type TcNetemReorder struct {
  299. Probability uint32
  300. Correlation uint32
  301. }
  302. func (msg *TcNetemReorder) Len() int {
  303. return SizeofTcNetemReorder
  304. }
  305. func DeserializeTcNetemReorder(b []byte) *TcNetemReorder {
  306. return (*TcNetemReorder)(unsafe.Pointer(&b[0:SizeofTcNetemReorder][0]))
  307. }
  308. func (x *TcNetemReorder) Serialize() []byte {
  309. return (*(*[SizeofTcNetemReorder]byte)(unsafe.Pointer(x)))[:]
  310. }
  311. // struct tc_netem_corrupt {
  312. // __u32 probability;
  313. // __u32 correlation;
  314. // };
  315. type TcNetemCorrupt struct {
  316. Probability uint32
  317. Correlation uint32
  318. }
  319. func (msg *TcNetemCorrupt) Len() int {
  320. return SizeofTcNetemCorrupt
  321. }
  322. func DeserializeTcNetemCorrupt(b []byte) *TcNetemCorrupt {
  323. return (*TcNetemCorrupt)(unsafe.Pointer(&b[0:SizeofTcNetemCorrupt][0]))
  324. }
  325. func (x *TcNetemCorrupt) Serialize() []byte {
  326. return (*(*[SizeofTcNetemCorrupt]byte)(unsafe.Pointer(x)))[:]
  327. }
  328. // TcNetemRate is a struct that represents the rate of a netem qdisc
  329. type TcNetemRate struct {
  330. Rate uint32
  331. PacketOverhead int32
  332. CellSize uint32
  333. CellOverhead int32
  334. }
  335. func (msg *TcNetemRate) Len() int {
  336. return SizeofTcRateSpec
  337. }
  338. func DeserializeTcNetemRate(b []byte) *TcNetemRate {
  339. return (*TcNetemRate)(unsafe.Pointer(&b[0:SizeofTcRateSpec][0]))
  340. }
  341. func (msg *TcNetemRate) Serialize() []byte {
  342. return (*(*[SizeOfTcNetemRate]byte)(unsafe.Pointer(msg)))[:]
  343. }
  344. // struct tc_tbf_qopt {
  345. // struct tc_ratespec rate;
  346. // struct tc_ratespec peakrate;
  347. // __u32 limit;
  348. // __u32 buffer;
  349. // __u32 mtu;
  350. // };
  351. type TcTbfQopt struct {
  352. Rate TcRateSpec
  353. Peakrate TcRateSpec
  354. Limit uint32
  355. Buffer uint32
  356. Mtu uint32
  357. }
  358. func (msg *TcTbfQopt) Len() int {
  359. return SizeofTcTbfQopt
  360. }
  361. func DeserializeTcTbfQopt(b []byte) *TcTbfQopt {
  362. return (*TcTbfQopt)(unsafe.Pointer(&b[0:SizeofTcTbfQopt][0]))
  363. }
  364. func (x *TcTbfQopt) Serialize() []byte {
  365. return (*(*[SizeofTcTbfQopt]byte)(unsafe.Pointer(x)))[:]
  366. }
  367. const (
  368. TCA_HTB_UNSPEC = iota
  369. TCA_HTB_PARMS
  370. TCA_HTB_INIT
  371. TCA_HTB_CTAB
  372. TCA_HTB_RTAB
  373. TCA_HTB_DIRECT_QLEN
  374. TCA_HTB_RATE64
  375. TCA_HTB_CEIL64
  376. TCA_HTB_MAX = TCA_HTB_CEIL64
  377. )
  378. //struct tc_htb_opt {
  379. // struct tc_ratespec rate;
  380. // struct tc_ratespec ceil;
  381. // __u32 buffer;
  382. // __u32 cbuffer;
  383. // __u32 quantum;
  384. // __u32 level; /* out only */
  385. // __u32 prio;
  386. //};
  387. type TcHtbCopt struct {
  388. Rate TcRateSpec
  389. Ceil TcRateSpec
  390. Buffer uint32
  391. Cbuffer uint32
  392. Quantum uint32
  393. Level uint32
  394. Prio uint32
  395. }
  396. func (msg *TcHtbCopt) Len() int {
  397. return SizeofTcHtbCopt
  398. }
  399. func DeserializeTcHtbCopt(b []byte) *TcHtbCopt {
  400. return (*TcHtbCopt)(unsafe.Pointer(&b[0:SizeofTcHtbCopt][0]))
  401. }
  402. func (x *TcHtbCopt) Serialize() []byte {
  403. return (*(*[SizeofTcHtbCopt]byte)(unsafe.Pointer(x)))[:]
  404. }
  405. type TcHtbGlob struct {
  406. Version uint32
  407. Rate2Quantum uint32
  408. Defcls uint32
  409. Debug uint32
  410. DirectPkts uint32
  411. }
  412. func (msg *TcHtbGlob) Len() int {
  413. return SizeofTcHtbGlob
  414. }
  415. func DeserializeTcHtbGlob(b []byte) *TcHtbGlob {
  416. return (*TcHtbGlob)(unsafe.Pointer(&b[0:SizeofTcHtbGlob][0]))
  417. }
  418. func (x *TcHtbGlob) Serialize() []byte {
  419. return (*(*[SizeofTcHtbGlob]byte)(unsafe.Pointer(x)))[:]
  420. }
  421. // HFSC
  422. type Curve struct {
  423. m1 uint32
  424. d uint32
  425. m2 uint32
  426. }
  427. type HfscCopt struct {
  428. Rsc Curve
  429. Fsc Curve
  430. Usc Curve
  431. }
  432. func (c *Curve) Attrs() (uint32, uint32, uint32) {
  433. return c.m1, c.d, c.m2
  434. }
  435. func (c *Curve) Set(m1 uint32, d uint32, m2 uint32) {
  436. c.m1 = m1
  437. c.d = d
  438. c.m2 = m2
  439. }
  440. func DeserializeHfscCurve(b []byte) *Curve {
  441. return &Curve{
  442. m1: binary.LittleEndian.Uint32(b[0:4]),
  443. d: binary.LittleEndian.Uint32(b[4:8]),
  444. m2: binary.LittleEndian.Uint32(b[8:12]),
  445. }
  446. }
  447. func SerializeHfscCurve(c *Curve) (b []byte) {
  448. t := make([]byte, binary.MaxVarintLen32)
  449. binary.LittleEndian.PutUint32(t, c.m1)
  450. b = append(b, t[:4]...)
  451. binary.LittleEndian.PutUint32(t, c.d)
  452. b = append(b, t[:4]...)
  453. binary.LittleEndian.PutUint32(t, c.m2)
  454. b = append(b, t[:4]...)
  455. return b
  456. }
  457. type TcHfscOpt struct {
  458. Defcls uint16
  459. }
  460. func (x *TcHfscOpt) Serialize() []byte {
  461. return (*(*[2]byte)(unsafe.Pointer(x)))[:]
  462. }
  463. const (
  464. TCA_U32_UNSPEC = iota
  465. TCA_U32_CLASSID
  466. TCA_U32_HASH
  467. TCA_U32_LINK
  468. TCA_U32_DIVISOR
  469. TCA_U32_SEL
  470. TCA_U32_POLICE
  471. TCA_U32_ACT
  472. TCA_U32_INDEV
  473. TCA_U32_PCNT
  474. TCA_U32_MARK
  475. TCA_U32_MAX = TCA_U32_MARK
  476. )
  477. // struct tc_u32_key {
  478. // __be32 mask;
  479. // __be32 val;
  480. // int off;
  481. // int offmask;
  482. // };
  483. type TcU32Key struct {
  484. Mask uint32 // big endian
  485. Val uint32 // big endian
  486. Off int32
  487. OffMask int32
  488. }
  489. func (msg *TcU32Key) Len() int {
  490. return SizeofTcU32Key
  491. }
  492. func DeserializeTcU32Key(b []byte) *TcU32Key {
  493. return (*TcU32Key)(unsafe.Pointer(&b[0:SizeofTcU32Key][0]))
  494. }
  495. func (x *TcU32Key) Serialize() []byte {
  496. return (*(*[SizeofTcU32Key]byte)(unsafe.Pointer(x)))[:]
  497. }
  498. // struct tc_u32_sel {
  499. // unsigned char flags;
  500. // unsigned char offshift;
  501. // unsigned char nkeys;
  502. //
  503. // __be16 offmask;
  504. // __u16 off;
  505. // short offoff;
  506. //
  507. // short hoff;
  508. // __be32 hmask;
  509. // struct tc_u32_key keys[0];
  510. // };
  511. const (
  512. TC_U32_TERMINAL = 1 << iota
  513. TC_U32_OFFSET = 1 << iota
  514. TC_U32_VAROFFSET = 1 << iota
  515. TC_U32_EAT = 1 << iota
  516. )
  517. type TcU32Sel struct {
  518. Flags uint8
  519. Offshift uint8
  520. Nkeys uint8
  521. Pad uint8
  522. Offmask uint16 // big endian
  523. Off uint16
  524. Offoff int16
  525. Hoff int16
  526. Hmask uint32 // big endian
  527. Keys []TcU32Key
  528. }
  529. func (msg *TcU32Sel) Len() int {
  530. return SizeofTcU32Sel + int(msg.Nkeys)*SizeofTcU32Key
  531. }
  532. func DeserializeTcU32Sel(b []byte) *TcU32Sel {
  533. x := &TcU32Sel{}
  534. copy((*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:], b)
  535. next := SizeofTcU32Sel
  536. var i uint8
  537. for i = 0; i < x.Nkeys; i++ {
  538. x.Keys = append(x.Keys, *DeserializeTcU32Key(b[next:]))
  539. next += SizeofTcU32Key
  540. }
  541. return x
  542. }
  543. func (x *TcU32Sel) Serialize() []byte {
  544. // This can't just unsafe.cast because it must iterate through keys.
  545. buf := make([]byte, x.Len())
  546. copy(buf, (*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:])
  547. next := SizeofTcU32Sel
  548. for _, key := range x.Keys {
  549. keyBuf := key.Serialize()
  550. copy(buf[next:], keyBuf)
  551. next += SizeofTcU32Key
  552. }
  553. return buf
  554. }
  555. type TcGen struct {
  556. Index uint32
  557. Capab uint32
  558. Action int32
  559. Refcnt int32
  560. Bindcnt int32
  561. }
  562. func (msg *TcGen) Len() int {
  563. return SizeofTcGen
  564. }
  565. func DeserializeTcGen(b []byte) *TcGen {
  566. return (*TcGen)(unsafe.Pointer(&b[0:SizeofTcGen][0]))
  567. }
  568. func (x *TcGen) Serialize() []byte {
  569. return (*(*[SizeofTcGen]byte)(unsafe.Pointer(x)))[:]
  570. }
  571. // #define tc_gen \
  572. // __u32 index; \
  573. // __u32 capab; \
  574. // int action; \
  575. // int refcnt; \
  576. // int bindcnt
  577. const (
  578. TCA_ACT_GACT = 5
  579. )
  580. const (
  581. TCA_GACT_UNSPEC = iota
  582. TCA_GACT_TM
  583. TCA_GACT_PARMS
  584. TCA_GACT_PROB
  585. TCA_GACT_MAX = TCA_GACT_PROB
  586. )
  587. type TcGact TcGen
  588. const (
  589. TCA_ACT_BPF = 13
  590. )
  591. const (
  592. TCA_ACT_BPF_UNSPEC = iota
  593. TCA_ACT_BPF_TM
  594. TCA_ACT_BPF_PARMS
  595. TCA_ACT_BPF_OPS_LEN
  596. TCA_ACT_BPF_OPS
  597. TCA_ACT_BPF_FD
  598. TCA_ACT_BPF_NAME
  599. TCA_ACT_BPF_MAX = TCA_ACT_BPF_NAME
  600. )
  601. const (
  602. TCA_BPF_FLAG_ACT_DIRECT uint32 = 1 << iota
  603. )
  604. const (
  605. TCA_BPF_UNSPEC = iota
  606. TCA_BPF_ACT
  607. TCA_BPF_POLICE
  608. TCA_BPF_CLASSID
  609. TCA_BPF_OPS_LEN
  610. TCA_BPF_OPS
  611. TCA_BPF_FD
  612. TCA_BPF_NAME
  613. TCA_BPF_FLAGS
  614. TCA_BPF_FLAGS_GEN
  615. TCA_BPF_TAG
  616. TCA_BPF_ID
  617. TCA_BPF_MAX = TCA_BPF_ID
  618. )
  619. type TcBpf TcGen
  620. const (
  621. TCA_ACT_CONNMARK = 14
  622. )
  623. const (
  624. TCA_CONNMARK_UNSPEC = iota
  625. TCA_CONNMARK_PARMS
  626. TCA_CONNMARK_TM
  627. TCA_CONNMARK_MAX = TCA_CONNMARK_TM
  628. )
  629. // struct tc_connmark {
  630. // tc_gen;
  631. // __u16 zone;
  632. // };
  633. type TcConnmark struct {
  634. TcGen
  635. Zone uint16
  636. }
  637. func (msg *TcConnmark) Len() int {
  638. return SizeofTcConnmark
  639. }
  640. func DeserializeTcConnmark(b []byte) *TcConnmark {
  641. return (*TcConnmark)(unsafe.Pointer(&b[0:SizeofTcConnmark][0]))
  642. }
  643. func (x *TcConnmark) Serialize() []byte {
  644. return (*(*[SizeofTcConnmark]byte)(unsafe.Pointer(x)))[:]
  645. }
  646. const (
  647. TCA_CSUM_UNSPEC = iota
  648. TCA_CSUM_PARMS
  649. TCA_CSUM_TM
  650. TCA_CSUM_PAD
  651. TCA_CSUM_MAX = TCA_CSUM_PAD
  652. )
  653. // struct tc_csum {
  654. // tc_gen;
  655. // __u32 update_flags;
  656. // }
  657. type TcCsum struct {
  658. TcGen
  659. UpdateFlags uint32
  660. }
  661. func (msg *TcCsum) Len() int {
  662. return SizeofTcCsum
  663. }
  664. func DeserializeTcCsum(b []byte) *TcCsum {
  665. return (*TcCsum)(unsafe.Pointer(&b[0:SizeofTcCsum][0]))
  666. }
  667. func (x *TcCsum) Serialize() []byte {
  668. return (*(*[SizeofTcCsum]byte)(unsafe.Pointer(x)))[:]
  669. }
  670. const (
  671. TCA_ACT_MIRRED = 8
  672. )
  673. const (
  674. TCA_MIRRED_UNSPEC = iota
  675. TCA_MIRRED_TM
  676. TCA_MIRRED_PARMS
  677. TCA_MIRRED_MAX = TCA_MIRRED_PARMS
  678. )
  679. // struct tc_mirred {
  680. // tc_gen;
  681. // int eaction; /* one of IN/EGRESS_MIRROR/REDIR */
  682. // __u32 ifindex; /* ifindex of egress port */
  683. // };
  684. type TcMirred struct {
  685. TcGen
  686. Eaction int32
  687. Ifindex uint32
  688. }
  689. func (msg *TcMirred) Len() int {
  690. return SizeofTcMirred
  691. }
  692. func DeserializeTcMirred(b []byte) *TcMirred {
  693. return (*TcMirred)(unsafe.Pointer(&b[0:SizeofTcMirred][0]))
  694. }
  695. func (x *TcMirred) Serialize() []byte {
  696. return (*(*[SizeofTcMirred]byte)(unsafe.Pointer(x)))[:]
  697. }
  698. const (
  699. TCA_VLAN_UNSPEC = iota
  700. TCA_VLAN_TM
  701. TCA_VLAN_PARMS
  702. TCA_VLAN_PUSH_VLAN_ID
  703. TCA_VLAN_PUSH_VLAN_PROTOCOL
  704. TCA_VLAN_PAD
  705. TCA_VLAN_PUSH_VLAN_PRIORITY
  706. TCA_VLAN_PUSH_ETH_DST
  707. TCA_VLAN_PUSH_ETH_SRC
  708. TCA_VLAN_MAX
  709. )
  710. //struct tc_vlan {
  711. // tc_gen;
  712. // int v_action;
  713. //};
  714. type TcVlan struct {
  715. TcGen
  716. Action int32
  717. }
  718. func (msg *TcVlan) Len() int {
  719. return SizeofTcVlan
  720. }
  721. func DeserializeTcVlan(b []byte) *TcVlan {
  722. return (*TcVlan)(unsafe.Pointer(&b[0:SizeofTcVlan][0]))
  723. }
  724. func (x *TcVlan) Serialize() []byte {
  725. return (*(*[SizeofTcVlan]byte)(unsafe.Pointer(x)))[:]
  726. }
  727. const (
  728. TCA_TUNNEL_KEY_UNSPEC = iota
  729. TCA_TUNNEL_KEY_TM
  730. TCA_TUNNEL_KEY_PARMS
  731. TCA_TUNNEL_KEY_ENC_IPV4_SRC
  732. TCA_TUNNEL_KEY_ENC_IPV4_DST
  733. TCA_TUNNEL_KEY_ENC_IPV6_SRC
  734. TCA_TUNNEL_KEY_ENC_IPV6_DST
  735. TCA_TUNNEL_KEY_ENC_KEY_ID
  736. TCA_TUNNEL_KEY_PAD
  737. TCA_TUNNEL_KEY_ENC_DST_PORT
  738. TCA_TUNNEL_KEY_NO_CSUM
  739. TCA_TUNNEL_KEY_ENC_OPTS
  740. TCA_TUNNEL_KEY_ENC_TOS
  741. TCA_TUNNEL_KEY_ENC_TTL
  742. TCA_TUNNEL_KEY_MAX
  743. )
  744. type TcTunnelKey struct {
  745. TcGen
  746. Action int32
  747. }
  748. func (x *TcTunnelKey) Len() int {
  749. return SizeofTcTunnelKey
  750. }
  751. func DeserializeTunnelKey(b []byte) *TcTunnelKey {
  752. return (*TcTunnelKey)(unsafe.Pointer(&b[0:SizeofTcTunnelKey][0]))
  753. }
  754. func (x *TcTunnelKey) Serialize() []byte {
  755. return (*(*[SizeofTcTunnelKey]byte)(unsafe.Pointer(x)))[:]
  756. }
  757. const (
  758. TCA_SKBEDIT_UNSPEC = iota
  759. TCA_SKBEDIT_TM
  760. TCA_SKBEDIT_PARMS
  761. TCA_SKBEDIT_PRIORITY
  762. TCA_SKBEDIT_QUEUE_MAPPING
  763. TCA_SKBEDIT_MARK
  764. TCA_SKBEDIT_PAD
  765. TCA_SKBEDIT_PTYPE
  766. TCA_SKBEDIT_MASK
  767. TCA_SKBEDIT_MAX
  768. )
  769. type TcSkbEdit struct {
  770. TcGen
  771. }
  772. func (x *TcSkbEdit) Len() int {
  773. return SizeofTcSkbEdit
  774. }
  775. func DeserializeSkbEdit(b []byte) *TcSkbEdit {
  776. return (*TcSkbEdit)(unsafe.Pointer(&b[0:SizeofTcSkbEdit][0]))
  777. }
  778. func (x *TcSkbEdit) Serialize() []byte {
  779. return (*(*[SizeofTcSkbEdit]byte)(unsafe.Pointer(x)))[:]
  780. }
  781. // struct tc_police {
  782. // __u32 index;
  783. // int action;
  784. // __u32 limit;
  785. // __u32 burst;
  786. // __u32 mtu;
  787. // struct tc_ratespec rate;
  788. // struct tc_ratespec peakrate;
  789. // int refcnt;
  790. // int bindcnt;
  791. // __u32 capab;
  792. // };
  793. type TcPolice struct {
  794. Index uint32
  795. Action int32
  796. Limit uint32
  797. Burst uint32
  798. Mtu uint32
  799. Rate TcRateSpec
  800. PeakRate TcRateSpec
  801. Refcnt int32
  802. Bindcnt int32
  803. Capab uint32
  804. }
  805. func (msg *TcPolice) Len() int {
  806. return SizeofTcPolice
  807. }
  808. func DeserializeTcPolice(b []byte) *TcPolice {
  809. return (*TcPolice)(unsafe.Pointer(&b[0:SizeofTcPolice][0]))
  810. }
  811. func (x *TcPolice) Serialize() []byte {
  812. return (*(*[SizeofTcPolice]byte)(unsafe.Pointer(x)))[:]
  813. }
  814. const (
  815. TCA_FW_UNSPEC = iota
  816. TCA_FW_CLASSID
  817. TCA_FW_POLICE
  818. TCA_FW_INDEV
  819. TCA_FW_ACT
  820. TCA_FW_MASK
  821. TCA_FW_MAX = TCA_FW_MASK
  822. )
  823. const (
  824. TCA_MATCHALL_UNSPEC = iota
  825. TCA_MATCHALL_CLASSID
  826. TCA_MATCHALL_ACT
  827. TCA_MATCHALL_FLAGS
  828. )
  829. const (
  830. TCA_FQ_UNSPEC = iota
  831. TCA_FQ_PLIMIT // limit of total number of packets in queue
  832. TCA_FQ_FLOW_PLIMIT // limit of packets per flow
  833. TCA_FQ_QUANTUM // RR quantum
  834. TCA_FQ_INITIAL_QUANTUM // RR quantum for new flow
  835. TCA_FQ_RATE_ENABLE // enable/disable rate limiting
  836. TCA_FQ_FLOW_DEFAULT_RATE // obsolete do not use
  837. TCA_FQ_FLOW_MAX_RATE // per flow max rate
  838. TCA_FQ_BUCKETS_LOG // log2(number of buckets)
  839. TCA_FQ_FLOW_REFILL_DELAY // flow credit refill delay in usec
  840. TCA_FQ_ORPHAN_MASK // mask applied to orphaned skb hashes
  841. TCA_FQ_LOW_RATE_THRESHOLD // per packet delay under this rate
  842. TCA_FQ_CE_THRESHOLD // DCTCP-like CE-marking threshold
  843. TCA_FQ_TIMER_SLACK // timer slack
  844. TCA_FQ_HORIZON // time horizon in us
  845. TCA_FQ_HORIZON_DROP // drop packets beyond horizon, or cap their EDT
  846. )
  847. const (
  848. TCA_FQ_CODEL_UNSPEC = iota
  849. TCA_FQ_CODEL_TARGET
  850. TCA_FQ_CODEL_LIMIT
  851. TCA_FQ_CODEL_INTERVAL
  852. TCA_FQ_CODEL_ECN
  853. TCA_FQ_CODEL_FLOWS
  854. TCA_FQ_CODEL_QUANTUM
  855. TCA_FQ_CODEL_CE_THRESHOLD
  856. TCA_FQ_CODEL_DROP_BATCH_SIZE
  857. TCA_FQ_CODEL_MEMORY_LIMIT
  858. )
  859. const (
  860. TCA_HFSC_UNSPEC = iota
  861. TCA_HFSC_RSC
  862. TCA_HFSC_FSC
  863. TCA_HFSC_USC
  864. )
  865. const (
  866. TCA_FLOWER_UNSPEC = iota
  867. TCA_FLOWER_CLASSID
  868. TCA_FLOWER_INDEV
  869. TCA_FLOWER_ACT
  870. TCA_FLOWER_KEY_ETH_DST /* ETH_ALEN */
  871. TCA_FLOWER_KEY_ETH_DST_MASK /* ETH_ALEN */
  872. TCA_FLOWER_KEY_ETH_SRC /* ETH_ALEN */
  873. TCA_FLOWER_KEY_ETH_SRC_MASK /* ETH_ALEN */
  874. TCA_FLOWER_KEY_ETH_TYPE /* be16 */
  875. TCA_FLOWER_KEY_IP_PROTO /* u8 */
  876. TCA_FLOWER_KEY_IPV4_SRC /* be32 */
  877. TCA_FLOWER_KEY_IPV4_SRC_MASK /* be32 */
  878. TCA_FLOWER_KEY_IPV4_DST /* be32 */
  879. TCA_FLOWER_KEY_IPV4_DST_MASK /* be32 */
  880. TCA_FLOWER_KEY_IPV6_SRC /* struct in6_addr */
  881. TCA_FLOWER_KEY_IPV6_SRC_MASK /* struct in6_addr */
  882. TCA_FLOWER_KEY_IPV6_DST /* struct in6_addr */
  883. TCA_FLOWER_KEY_IPV6_DST_MASK /* struct in6_addr */
  884. TCA_FLOWER_KEY_TCP_SRC /* be16 */
  885. TCA_FLOWER_KEY_TCP_DST /* be16 */
  886. TCA_FLOWER_KEY_UDP_SRC /* be16 */
  887. TCA_FLOWER_KEY_UDP_DST /* be16 */
  888. TCA_FLOWER_FLAGS
  889. TCA_FLOWER_KEY_VLAN_ID /* be16 */
  890. TCA_FLOWER_KEY_VLAN_PRIO /* u8 */
  891. TCA_FLOWER_KEY_VLAN_ETH_TYPE /* be16 */
  892. TCA_FLOWER_KEY_ENC_KEY_ID /* be32 */
  893. TCA_FLOWER_KEY_ENC_IPV4_SRC /* be32 */
  894. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK /* be32 */
  895. TCA_FLOWER_KEY_ENC_IPV4_DST /* be32 */
  896. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK /* be32 */
  897. TCA_FLOWER_KEY_ENC_IPV6_SRC /* struct in6_addr */
  898. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK /* struct in6_addr */
  899. TCA_FLOWER_KEY_ENC_IPV6_DST /* struct in6_addr */
  900. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK /* struct in6_addr */
  901. TCA_FLOWER_KEY_TCP_SRC_MASK /* be16 */
  902. TCA_FLOWER_KEY_TCP_DST_MASK /* be16 */
  903. TCA_FLOWER_KEY_UDP_SRC_MASK /* be16 */
  904. TCA_FLOWER_KEY_UDP_DST_MASK /* be16 */
  905. TCA_FLOWER_KEY_SCTP_SRC_MASK /* be16 */
  906. TCA_FLOWER_KEY_SCTP_DST_MASK /* be16 */
  907. TCA_FLOWER_KEY_SCTP_SRC /* be16 */
  908. TCA_FLOWER_KEY_SCTP_DST /* be16 */
  909. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT /* be16 */
  910. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK /* be16 */
  911. TCA_FLOWER_KEY_ENC_UDP_DST_PORT /* be16 */
  912. TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK /* be16 */
  913. TCA_FLOWER_KEY_FLAGS /* be32 */
  914. TCA_FLOWER_KEY_FLAGS_MASK /* be32 */
  915. TCA_FLOWER_KEY_ICMPV4_CODE /* u8 */
  916. TCA_FLOWER_KEY_ICMPV4_CODE_MASK /* u8 */
  917. TCA_FLOWER_KEY_ICMPV4_TYPE /* u8 */
  918. TCA_FLOWER_KEY_ICMPV4_TYPE_MASK /* u8 */
  919. TCA_FLOWER_KEY_ICMPV6_CODE /* u8 */
  920. TCA_FLOWER_KEY_ICMPV6_CODE_MASK /* u8 */
  921. TCA_FLOWER_KEY_ICMPV6_TYPE /* u8 */
  922. TCA_FLOWER_KEY_ICMPV6_TYPE_MASK /* u8 */
  923. TCA_FLOWER_KEY_ARP_SIP /* be32 */
  924. TCA_FLOWER_KEY_ARP_SIP_MASK /* be32 */
  925. TCA_FLOWER_KEY_ARP_TIP /* be32 */
  926. TCA_FLOWER_KEY_ARP_TIP_MASK /* be32 */
  927. TCA_FLOWER_KEY_ARP_OP /* u8 */
  928. TCA_FLOWER_KEY_ARP_OP_MASK /* u8 */
  929. TCA_FLOWER_KEY_ARP_SHA /* ETH_ALEN */
  930. TCA_FLOWER_KEY_ARP_SHA_MASK /* ETH_ALEN */
  931. TCA_FLOWER_KEY_ARP_THA /* ETH_ALEN */
  932. TCA_FLOWER_KEY_ARP_THA_MASK /* ETH_ALEN */
  933. TCA_FLOWER_KEY_MPLS_TTL /* u8 - 8 bits */
  934. TCA_FLOWER_KEY_MPLS_BOS /* u8 - 1 bit */
  935. TCA_FLOWER_KEY_MPLS_TC /* u8 - 3 bits */
  936. TCA_FLOWER_KEY_MPLS_LABEL /* be32 - 20 bits */
  937. TCA_FLOWER_KEY_TCP_FLAGS /* be16 */
  938. TCA_FLOWER_KEY_TCP_FLAGS_MASK /* be16 */
  939. TCA_FLOWER_KEY_IP_TOS /* u8 */
  940. TCA_FLOWER_KEY_IP_TOS_MASK /* u8 */
  941. TCA_FLOWER_KEY_IP_TTL /* u8 */
  942. TCA_FLOWER_KEY_IP_TTL_MASK /* u8 */
  943. TCA_FLOWER_KEY_CVLAN_ID /* be16 */
  944. TCA_FLOWER_KEY_CVLAN_PRIO /* u8 */
  945. TCA_FLOWER_KEY_CVLAN_ETH_TYPE /* be16 */
  946. TCA_FLOWER_KEY_ENC_IP_TOS /* u8 */
  947. TCA_FLOWER_KEY_ENC_IP_TOS_MASK /* u8 */
  948. TCA_FLOWER_KEY_ENC_IP_TTL /* u8 */
  949. TCA_FLOWER_KEY_ENC_IP_TTL_MASK /* u8 */
  950. TCA_FLOWER_KEY_ENC_OPTS
  951. TCA_FLOWER_KEY_ENC_OPTS_MASK
  952. TCA_FLOWER_IN_HW_COUNT
  953. TCA_FLOWER_KEY_PORT_SRC_MIN /* be16 */
  954. TCA_FLOWER_KEY_PORT_SRC_MAX /* be16 */
  955. TCA_FLOWER_KEY_PORT_DST_MIN /* be16 */
  956. TCA_FLOWER_KEY_PORT_DST_MAX /* be16 */
  957. __TCA_FLOWER_MAX
  958. )
  959. const TCA_CLS_FLAGS_SKIP_HW = 1 << 0 /* don't offload filter to HW */
  960. const TCA_CLS_FLAGS_SKIP_SW = 1 << 1 /* don't use filter in SW */
  961. // struct tc_sfq_qopt {
  962. // unsigned quantum; /* Bytes per round allocated to flow */
  963. // int perturb_period; /* Period of hash perturbation */
  964. // __u32 limit; /* Maximal packets in queue */
  965. // unsigned divisor; /* Hash divisor */
  966. // unsigned flows; /* Maximal number of flows */
  967. // };
  968. type TcSfqQopt struct {
  969. Quantum uint32
  970. Perturb int32
  971. Limit uint32
  972. Divisor uint32
  973. Flows uint32
  974. }
  975. func (x *TcSfqQopt) Len() int {
  976. return SizeofTcSfqQopt
  977. }
  978. func DeserializeTcSfqQopt(b []byte) *TcSfqQopt {
  979. return (*TcSfqQopt)(unsafe.Pointer(&b[0:SizeofTcSfqQopt][0]))
  980. }
  981. func (x *TcSfqQopt) Serialize() []byte {
  982. return (*(*[SizeofTcSfqQopt]byte)(unsafe.Pointer(x)))[:]
  983. }
  984. // struct tc_sfqred_stats {
  985. // __u32 prob_drop; /* Early drops, below max threshold */
  986. // __u32 forced_drop; /* Early drops, after max threshold */
  987. // __u32 prob_mark; /* Marked packets, below max threshold */
  988. // __u32 forced_mark; /* Marked packets, after max threshold */
  989. // __u32 prob_mark_head; /* Marked packets, below max threshold */
  990. // __u32 forced_mark_head;/* Marked packets, after max threshold */
  991. // };
  992. type TcSfqRedStats struct {
  993. ProbDrop uint32
  994. ForcedDrop uint32
  995. ProbMark uint32
  996. ForcedMark uint32
  997. ProbMarkHead uint32
  998. ForcedMarkHead uint32
  999. }
  1000. func (x *TcSfqRedStats) Len() int {
  1001. return SizeofTcSfqRedStats
  1002. }
  1003. func DeserializeTcSfqRedStats(b []byte) *TcSfqRedStats {
  1004. return (*TcSfqRedStats)(unsafe.Pointer(&b[0:SizeofTcSfqRedStats][0]))
  1005. }
  1006. func (x *TcSfqRedStats) Serialize() []byte {
  1007. return (*(*[SizeofTcSfqRedStats]byte)(unsafe.Pointer(x)))[:]
  1008. }
  1009. // struct tc_sfq_qopt_v1 {
  1010. // struct tc_sfq_qopt v0;
  1011. // unsigned int depth; /* max number of packets per flow */
  1012. // unsigned int headdrop;
  1013. //
  1014. // /* SFQRED parameters */
  1015. //
  1016. // __u32 limit; /* HARD maximal flow queue length (bytes) */
  1017. // __u32 qth_min; /* Min average length threshold (bytes) */
  1018. // __u32 qth_max; /* Max average length threshold (bytes) */
  1019. // unsigned char Wlog; /* log(W) */
  1020. // unsigned char Plog; /* log(P_max/(qth_max-qth_min)) */
  1021. // unsigned char Scell_log; /* cell size for idle damping */
  1022. // unsigned char flags;
  1023. // __u32 max_P; /* probability, high resolution */
  1024. //
  1025. // /* SFQRED stats */
  1026. //
  1027. // struct tc_sfqred_stats stats;
  1028. // };
  1029. type TcSfqQoptV1 struct {
  1030. TcSfqQopt
  1031. Depth uint32
  1032. HeadDrop uint32
  1033. Limit uint32
  1034. QthMin uint32
  1035. QthMax uint32
  1036. Wlog byte
  1037. Plog byte
  1038. ScellLog byte
  1039. Flags byte
  1040. MaxP uint32
  1041. TcSfqRedStats
  1042. }
  1043. func (x *TcSfqQoptV1) Len() int {
  1044. return SizeofTcSfqQoptV1
  1045. }
  1046. func DeserializeTcSfqQoptV1(b []byte) *TcSfqQoptV1 {
  1047. return (*TcSfqQoptV1)(unsafe.Pointer(&b[0:SizeofTcSfqQoptV1][0]))
  1048. }
  1049. func (x *TcSfqQoptV1) Serialize() []byte {
  1050. return (*(*[SizeofTcSfqQoptV1]byte)(unsafe.Pointer(x)))[:]
  1051. }
  1052. // IPProto represents Flower ip_proto attribute
  1053. type IPProto uint8
  1054. const (
  1055. IPPROTO_TCP IPProto = unix.IPPROTO_TCP
  1056. IPPROTO_UDP IPProto = unix.IPPROTO_UDP
  1057. IPPROTO_SCTP IPProto = unix.IPPROTO_SCTP
  1058. IPPROTO_ICMP IPProto = unix.IPPROTO_ICMP
  1059. IPPROTO_ICMPV6 IPProto = unix.IPPROTO_ICMPV6
  1060. )
  1061. func (i IPProto) Serialize() []byte {
  1062. arr := make([]byte, 1)
  1063. arr[0] = byte(i)
  1064. return arr
  1065. }
  1066. func (i IPProto) String() string {
  1067. switch i {
  1068. case IPPROTO_TCP:
  1069. return "tcp"
  1070. case IPPROTO_UDP:
  1071. return "udp"
  1072. case IPPROTO_SCTP:
  1073. return "sctp"
  1074. case IPPROTO_ICMP:
  1075. return "icmp"
  1076. case IPPROTO_ICMPV6:
  1077. return "icmpv6"
  1078. }
  1079. return fmt.Sprintf("%d", i)
  1080. }
  1081. const (
  1082. MaxOffs = 128
  1083. SizeOfPeditSel = 24
  1084. SizeOfPeditKey = 24
  1085. TCA_PEDIT_KEY_EX_HTYPE = 1
  1086. TCA_PEDIT_KEY_EX_CMD = 2
  1087. )
  1088. const (
  1089. TCA_PEDIT_UNSPEC = iota
  1090. TCA_PEDIT_TM
  1091. TCA_PEDIT_PARMS
  1092. TCA_PEDIT_PAD
  1093. TCA_PEDIT_PARMS_EX
  1094. TCA_PEDIT_KEYS_EX
  1095. TCA_PEDIT_KEY_EX
  1096. )
  1097. // /* TCA_PEDIT_KEY_EX_HDR_TYPE_NETWROK is a special case for legacy users. It
  1098. // - means no specific header type - offset is relative to the network layer
  1099. // */
  1100. type PeditHeaderType uint16
  1101. const (
  1102. TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK = iota
  1103. TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
  1104. TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
  1105. TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1106. TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
  1107. TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
  1108. __PEDIT_HDR_TYPE_MAX
  1109. )
  1110. type PeditCmd uint16
  1111. const (
  1112. TCA_PEDIT_KEY_EX_CMD_SET = 0
  1113. TCA_PEDIT_KEY_EX_CMD_ADD = 1
  1114. )
  1115. type TcPeditSel struct {
  1116. TcGen
  1117. NKeys uint8
  1118. Flags uint8
  1119. }
  1120. func DeserializeTcPeditKey(b []byte) *TcPeditKey {
  1121. return (*TcPeditKey)(unsafe.Pointer(&b[0:SizeOfPeditKey][0]))
  1122. }
  1123. func DeserializeTcPedit(b []byte) (*TcPeditSel, []TcPeditKey) {
  1124. x := &TcPeditSel{}
  1125. copy((*(*[SizeOfPeditSel]byte)(unsafe.Pointer(x)))[:SizeOfPeditSel], b)
  1126. var keys []TcPeditKey
  1127. next := SizeOfPeditKey
  1128. var i uint8
  1129. for i = 0; i < x.NKeys; i++ {
  1130. keys = append(keys, *DeserializeTcPeditKey(b[next:]))
  1131. next += SizeOfPeditKey
  1132. }
  1133. return x, keys
  1134. }
  1135. type TcPeditKey struct {
  1136. Mask uint32
  1137. Val uint32
  1138. Off uint32
  1139. At uint32
  1140. OffMask uint32
  1141. Shift uint32
  1142. }
  1143. type TcPeditKeyEx struct {
  1144. HeaderType PeditHeaderType
  1145. Cmd PeditCmd
  1146. }
  1147. type TcPedit struct {
  1148. Sel TcPeditSel
  1149. Keys []TcPeditKey
  1150. KeysEx []TcPeditKeyEx
  1151. Extend uint8
  1152. }
  1153. func (p *TcPedit) Encode(parent *RtAttr) {
  1154. parent.AddRtAttr(TCA_ACT_KIND, ZeroTerminated("pedit"))
  1155. actOpts := parent.AddRtAttr(TCA_ACT_OPTIONS, nil)
  1156. bbuf := bytes.NewBuffer(make([]byte, 0, int(unsafe.Sizeof(p.Sel)+unsafe.Sizeof(p.Keys))))
  1157. bbuf.Write((*(*[SizeOfPeditSel]byte)(unsafe.Pointer(&p.Sel)))[:])
  1158. for i := uint8(0); i < p.Sel.NKeys; i++ {
  1159. bbuf.Write((*(*[SizeOfPeditKey]byte)(unsafe.Pointer(&p.Keys[i])))[:])
  1160. }
  1161. actOpts.AddRtAttr(TCA_PEDIT_PARMS_EX, bbuf.Bytes())
  1162. exAttrs := actOpts.AddRtAttr(int(TCA_PEDIT_KEYS_EX|NLA_F_NESTED), nil)
  1163. for i := uint8(0); i < p.Sel.NKeys; i++ {
  1164. keyAttr := exAttrs.AddRtAttr(int(TCA_PEDIT_KEY_EX|NLA_F_NESTED), nil)
  1165. htypeBuf := make([]byte, 2)
  1166. cmdBuf := make([]byte, 2)
  1167. NativeEndian().PutUint16(htypeBuf, uint16(p.KeysEx[i].HeaderType))
  1168. NativeEndian().PutUint16(cmdBuf, uint16(p.KeysEx[i].Cmd))
  1169. keyAttr.AddRtAttr(TCA_PEDIT_KEY_EX_HTYPE, htypeBuf)
  1170. keyAttr.AddRtAttr(TCA_PEDIT_KEY_EX_CMD, cmdBuf)
  1171. }
  1172. }
  1173. func (p *TcPedit) SetEthDst(mac net.HardwareAddr) {
  1174. u32 := NativeEndian().Uint32(mac)
  1175. u16 := NativeEndian().Uint16(mac[4:])
  1176. tKey := TcPeditKey{}
  1177. tKeyEx := TcPeditKeyEx{}
  1178. tKey.Val = u32
  1179. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
  1180. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1181. p.Keys = append(p.Keys, tKey)
  1182. p.KeysEx = append(p.KeysEx, tKeyEx)
  1183. p.Sel.NKeys++
  1184. tKey = TcPeditKey{}
  1185. tKeyEx = TcPeditKeyEx{}
  1186. tKey.Val = uint32(u16)
  1187. tKey.Mask = 0xffff0000
  1188. tKey.Off = 4
  1189. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
  1190. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1191. p.Keys = append(p.Keys, tKey)
  1192. p.KeysEx = append(p.KeysEx, tKeyEx)
  1193. p.Sel.NKeys++
  1194. }
  1195. func (p *TcPedit) SetEthSrc(mac net.HardwareAddr) {
  1196. u16 := NativeEndian().Uint16(mac)
  1197. u32 := NativeEndian().Uint32(mac[2:])
  1198. tKey := TcPeditKey{}
  1199. tKeyEx := TcPeditKeyEx{}
  1200. tKey.Val = uint32(u16) << 16
  1201. tKey.Mask = 0x0000ffff
  1202. tKey.Off = 4
  1203. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
  1204. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1205. p.Keys = append(p.Keys, tKey)
  1206. p.KeysEx = append(p.KeysEx, tKeyEx)
  1207. p.Sel.NKeys++
  1208. tKey = TcPeditKey{}
  1209. tKeyEx = TcPeditKeyEx{}
  1210. tKey.Val = u32
  1211. tKey.Mask = 0
  1212. tKey.Off = 8
  1213. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
  1214. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1215. p.Keys = append(p.Keys, tKey)
  1216. p.KeysEx = append(p.KeysEx, tKeyEx)
  1217. p.Sel.NKeys++
  1218. }
  1219. func (p *TcPedit) SetIPv6Src(ip6 net.IP) {
  1220. u32 := NativeEndian().Uint32(ip6[:4])
  1221. tKey := TcPeditKey{}
  1222. tKeyEx := TcPeditKeyEx{}
  1223. tKey.Val = u32
  1224. tKey.Off = 8
  1225. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1226. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1227. p.Keys = append(p.Keys, tKey)
  1228. p.KeysEx = append(p.KeysEx, tKeyEx)
  1229. p.Sel.NKeys++
  1230. u32 = NativeEndian().Uint32(ip6[4:8])
  1231. tKey = TcPeditKey{}
  1232. tKeyEx = TcPeditKeyEx{}
  1233. tKey.Val = u32
  1234. tKey.Off = 12
  1235. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1236. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1237. p.Keys = append(p.Keys, tKey)
  1238. p.KeysEx = append(p.KeysEx, tKeyEx)
  1239. p.Sel.NKeys++
  1240. u32 = NativeEndian().Uint32(ip6[8:12])
  1241. tKey = TcPeditKey{}
  1242. tKeyEx = TcPeditKeyEx{}
  1243. tKey.Val = u32
  1244. tKey.Off = 16
  1245. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1246. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1247. p.Keys = append(p.Keys, tKey)
  1248. p.KeysEx = append(p.KeysEx, tKeyEx)
  1249. p.Sel.NKeys++
  1250. u32 = NativeEndian().Uint32(ip6[12:16])
  1251. tKey = TcPeditKey{}
  1252. tKeyEx = TcPeditKeyEx{}
  1253. tKey.Val = u32
  1254. tKey.Off = 20
  1255. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1256. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1257. p.Keys = append(p.Keys, tKey)
  1258. p.KeysEx = append(p.KeysEx, tKeyEx)
  1259. p.Sel.NKeys++
  1260. }
  1261. func (p *TcPedit) SetDstIP(ip net.IP) {
  1262. if ip.To4() != nil {
  1263. p.SetIPv4Dst(ip)
  1264. } else {
  1265. p.SetIPv6Dst(ip)
  1266. }
  1267. }
  1268. func (p *TcPedit) SetSrcIP(ip net.IP) {
  1269. if ip.To4() != nil {
  1270. p.SetIPv4Src(ip)
  1271. } else {
  1272. p.SetIPv6Src(ip)
  1273. }
  1274. }
  1275. func (p *TcPedit) SetIPv6Dst(ip6 net.IP) {
  1276. u32 := NativeEndian().Uint32(ip6[:4])
  1277. tKey := TcPeditKey{}
  1278. tKeyEx := TcPeditKeyEx{}
  1279. tKey.Val = u32
  1280. tKey.Off = 24
  1281. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1282. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1283. p.Keys = append(p.Keys, tKey)
  1284. p.KeysEx = append(p.KeysEx, tKeyEx)
  1285. p.Sel.NKeys++
  1286. u32 = NativeEndian().Uint32(ip6[4:8])
  1287. tKey = TcPeditKey{}
  1288. tKeyEx = TcPeditKeyEx{}
  1289. tKey.Val = u32
  1290. tKey.Off = 28
  1291. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1292. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1293. p.Keys = append(p.Keys, tKey)
  1294. p.KeysEx = append(p.KeysEx, tKeyEx)
  1295. p.Sel.NKeys++
  1296. u32 = NativeEndian().Uint32(ip6[8:12])
  1297. tKey = TcPeditKey{}
  1298. tKeyEx = TcPeditKeyEx{}
  1299. tKey.Val = u32
  1300. tKey.Off = 32
  1301. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1302. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1303. p.Keys = append(p.Keys, tKey)
  1304. p.KeysEx = append(p.KeysEx, tKeyEx)
  1305. p.Sel.NKeys++
  1306. u32 = NativeEndian().Uint32(ip6[12:16])
  1307. tKey = TcPeditKey{}
  1308. tKeyEx = TcPeditKeyEx{}
  1309. tKey.Val = u32
  1310. tKey.Off = 36
  1311. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
  1312. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1313. p.Keys = append(p.Keys, tKey)
  1314. p.KeysEx = append(p.KeysEx, tKeyEx)
  1315. p.Sel.NKeys++
  1316. }
  1317. func (p *TcPedit) SetIPv4Src(ip net.IP) {
  1318. u32 := NativeEndian().Uint32(ip.To4())
  1319. tKey := TcPeditKey{}
  1320. tKeyEx := TcPeditKeyEx{}
  1321. tKey.Val = u32
  1322. tKey.Off = 12
  1323. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
  1324. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1325. p.Keys = append(p.Keys, tKey)
  1326. p.KeysEx = append(p.KeysEx, tKeyEx)
  1327. p.Sel.NKeys++
  1328. }
  1329. func (p *TcPedit) SetIPv4Dst(ip net.IP) {
  1330. u32 := NativeEndian().Uint32(ip.To4())
  1331. tKey := TcPeditKey{}
  1332. tKeyEx := TcPeditKeyEx{}
  1333. tKey.Val = u32
  1334. tKey.Off = 16
  1335. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
  1336. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1337. p.Keys = append(p.Keys, tKey)
  1338. p.KeysEx = append(p.KeysEx, tKeyEx)
  1339. p.Sel.NKeys++
  1340. }
  1341. // SetDstPort only tcp and udp are supported to set port
  1342. func (p *TcPedit) SetDstPort(dstPort uint16, protocol uint8) {
  1343. tKey := TcPeditKey{}
  1344. tKeyEx := TcPeditKeyEx{}
  1345. switch protocol {
  1346. case unix.IPPROTO_TCP:
  1347. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
  1348. case unix.IPPROTO_UDP:
  1349. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
  1350. default:
  1351. return
  1352. }
  1353. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1354. tKey.Val = uint32(Swap16(dstPort)) << 16
  1355. tKey.Mask = 0x0000ffff
  1356. p.Keys = append(p.Keys, tKey)
  1357. p.KeysEx = append(p.KeysEx, tKeyEx)
  1358. p.Sel.NKeys++
  1359. }
  1360. // SetSrcPort only tcp and udp are supported to set port
  1361. func (p *TcPedit) SetSrcPort(srcPort uint16, protocol uint8) {
  1362. tKey := TcPeditKey{}
  1363. tKeyEx := TcPeditKeyEx{}
  1364. switch protocol {
  1365. case unix.IPPROTO_TCP:
  1366. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
  1367. case unix.IPPROTO_UDP:
  1368. tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
  1369. default:
  1370. return
  1371. }
  1372. tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET
  1373. tKey.Val = uint32(Swap16(srcPort))
  1374. tKey.Mask = 0xffff0000
  1375. p.Keys = append(p.Keys, tKey)
  1376. p.KeysEx = append(p.KeysEx, tKeyEx)
  1377. p.Sel.NKeys++
  1378. }