File: tc_linux.go

package info (click to toggle)
golang-github-vishvananda-netlink 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,648 kB
  • sloc: makefile: 25
file content (1665 lines) | stat: -rw-r--r-- 35,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
package nl

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"net"
	"unsafe"

	"golang.org/x/sys/unix"
)

// LinkLayer
const (
	LINKLAYER_UNSPEC = iota
	LINKLAYER_ETHERNET
	LINKLAYER_ATM
)

// ATM
const (
	ATM_CELL_PAYLOAD = 48
	ATM_CELL_SIZE    = 53
)

const TC_LINKLAYER_MASK = 0x0F

// Police
const (
	TCA_POLICE_UNSPEC = iota
	TCA_POLICE_TBF
	TCA_POLICE_RATE
	TCA_POLICE_PEAKRATE
	TCA_POLICE_AVRATE
	TCA_POLICE_RESULT
	TCA_POLICE_MAX = TCA_POLICE_RESULT
)

// Message types
const (
	TCA_UNSPEC = iota
	TCA_KIND
	TCA_OPTIONS
	TCA_STATS
	TCA_XSTATS
	TCA_RATE
	TCA_FCNT
	TCA_STATS2
	TCA_STAB
	TCA_PAD
	TCA_DUMP_INVISIBLE
	TCA_CHAIN
	TCA_HW_OFFLOAD
	TCA_INGRESS_BLOCK
	TCA_EGRESS_BLOCK
	TCA_DUMP_FLAGS
	TCA_MAX = TCA_DUMP_FLAGS
)

const (
	TCA_ACT_TAB = 1
	TCAA_MAX    = 1
)

const (
	TCA_ACT_UNSPEC = iota
	TCA_ACT_KIND
	TCA_ACT_OPTIONS
	TCA_ACT_INDEX
	TCA_ACT_STATS
	TCA_ACT_PAD
	TCA_ACT_COOKIE
	TCA_ACT_FLAGS
	TCA_ACT_HW_STATS
	TCA_ACT_USED_HW_STATS
	TCA_ACT_IN_HW_COUNT
	TCA_ACT_MAX
)

const (
	TCA_ACT_SAMPLE_UNSPEC = iota
	TCA_ACT_SAMPLE_TM
	TCA_ACT_SAMPLE_PARMS
	TCA_ACT_SAMPLE_RATE
	TCA_ACT_SAMPLE_TRUNC_SIZE
	TCA_ACT_SAMPLE_PSAMPLE_GROUP
	TCA_ACT_SAMPLE_PAD
	TCA_ACT_SAMPLE_MAX
)

const (
	TCA_PRIO_UNSPEC = iota
	TCA_PRIO_MQ
	TCA_PRIO_MAX = TCA_PRIO_MQ
)

const (
	TCA_STATS_UNSPEC = iota
	TCA_STATS_BASIC
	TCA_STATS_RATE_EST
	TCA_STATS_QUEUE
	TCA_STATS_APP
	TCA_STATS_RATE_EST64
	TCA_STATS_PAD
	TCA_STATS_BASIC_HW
	TCA_STATS_PKT64
	TCA_STATS_MAX = TCA_STATS_PKT64
)

const (
	SizeofTcMsg          = 0x14
	SizeofTcActionMsg    = 0x04
	SizeofTcPrioMap      = 0x14
	SizeofTcRateSpec     = 0x0c
	SizeofTcNetemQopt    = 0x18
	SizeofTcNetemCorr    = 0x0c
	SizeofTcNetemReorder = 0x08
	SizeofTcNetemCorrupt = 0x08
	SizeOfTcNetemRate    = 0x10
	SizeofTcTbfQopt      = 2*SizeofTcRateSpec + 0x0c
	SizeofTcHtbCopt      = 2*SizeofTcRateSpec + 0x14
	SizeofTcHtbGlob      = 0x14
	SizeofTcU32Key       = 0x10
	SizeofTcU32Sel       = 0x10 // without keys
	SizeofTcGen          = 0x16
	SizeofTcConnmark     = SizeofTcGen + 0x04
	SizeofTcCsum         = SizeofTcGen + 0x04
	SizeofTcMirred       = SizeofTcGen + 0x08
	SizeofTcVlan         = SizeofTcGen + 0x04
	SizeofTcTunnelKey    = SizeofTcGen + 0x04
	SizeofTcSkbEdit      = SizeofTcGen
	SizeofTcPolice       = 2*SizeofTcRateSpec + 0x20
	SizeofTcSfqQopt      = 0x0b
	SizeofTcSfqRedStats  = 0x18
	SizeofTcSfqQoptV1    = SizeofTcSfqQopt + SizeofTcSfqRedStats + 0x1c
	SizeofUint32Bitfield = 0x8
)

// struct tcmsg {
//   unsigned char tcm_family;
//   unsigned char tcm__pad1;
//   unsigned short  tcm__pad2;
//   int   tcm_ifindex;
//   __u32   tcm_handle;
//   __u32   tcm_parent;
//   __u32   tcm_info;
// };

type TcMsg struct {
	Family  uint8
	Pad     [3]byte
	Ifindex int32
	Handle  uint32
	Parent  uint32
	Info    uint32
}

func (msg *TcMsg) Len() int {
	return SizeofTcMsg
}

func DeserializeTcMsg(b []byte) *TcMsg {
	return (*TcMsg)(unsafe.Pointer(&b[0:SizeofTcMsg][0]))
}

func (x *TcMsg) Serialize() []byte {
	return (*(*[SizeofTcMsg]byte)(unsafe.Pointer(x)))[:]
}

type Tcf struct {
	Install  uint64
	LastUse  uint64
	Expires  uint64
	FirstUse uint64
}

func DeserializeTcf(b []byte) *Tcf {
	const size = int(unsafe.Sizeof(Tcf{}))
	return (*Tcf)(unsafe.Pointer(&b[0:size][0]))
}

// struct tcamsg {
//   unsigned char tca_family;
//   unsigned char tca__pad1;
//   unsigned short  tca__pad2;
// };

type TcActionMsg struct {
	Family uint8
	Pad    [3]byte
}

func (msg *TcActionMsg) Len() int {
	return SizeofTcActionMsg
}

func DeserializeTcActionMsg(b []byte) *TcActionMsg {
	return (*TcActionMsg)(unsafe.Pointer(&b[0:SizeofTcActionMsg][0]))
}

func (x *TcActionMsg) Serialize() []byte {
	return (*(*[SizeofTcActionMsg]byte)(unsafe.Pointer(x)))[:]
}

const (
	TC_PRIO_MAX = 15
)

// struct tc_prio_qopt {
// 	int bands;      /* Number of bands */
// 	__u8  priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
// };

type TcPrioMap struct {
	Bands   int32
	Priomap [TC_PRIO_MAX + 1]uint8
}

func (msg *TcPrioMap) Len() int {
	return SizeofTcPrioMap
}

func DeserializeTcPrioMap(b []byte) *TcPrioMap {
	return (*TcPrioMap)(unsafe.Pointer(&b[0:SizeofTcPrioMap][0]))
}

func (x *TcPrioMap) Serialize() []byte {
	return (*(*[SizeofTcPrioMap]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_TBF_UNSPEC = iota
	TCA_TBF_PARMS
	TCA_TBF_RTAB
	TCA_TBF_PTAB
	TCA_TBF_RATE64
	TCA_TBF_PRATE64
	TCA_TBF_BURST
	TCA_TBF_PBURST
	TCA_TBF_MAX = TCA_TBF_PBURST
)

// struct tc_ratespec {
//   unsigned char cell_log;
//   __u8    linklayer; /* lower 4 bits */
//   unsigned short  overhead;
//   short   cell_align;
//   unsigned short  mpu;
//   __u32   rate;
// };

type TcRateSpec struct {
	CellLog   uint8
	Linklayer uint8
	Overhead  uint16
	CellAlign int16
	Mpu       uint16
	Rate      uint32
}

func (msg *TcRateSpec) Len() int {
	return SizeofTcRateSpec
}

func DeserializeTcRateSpec(b []byte) *TcRateSpec {
	return (*TcRateSpec)(unsafe.Pointer(&b[0:SizeofTcRateSpec][0]))
}

func (x *TcRateSpec) Serialize() []byte {
	return (*(*[SizeofTcRateSpec]byte)(unsafe.Pointer(x)))[:]
}

/**
* NETEM
 */

const (
	TCA_NETEM_UNSPEC = iota
	TCA_NETEM_CORR
	TCA_NETEM_DELAY_DIST
	TCA_NETEM_REORDER
	TCA_NETEM_CORRUPT
	TCA_NETEM_LOSS
	TCA_NETEM_RATE
	TCA_NETEM_ECN
	TCA_NETEM_RATE64
	TCA_NETEM_MAX = TCA_NETEM_RATE64
)

// struct tc_netem_qopt {
//	__u32	latency;	/* added delay (us) */
//	__u32   limit;		/* fifo limit (packets) */
//	__u32	loss;		/* random packet loss (0=none ~0=100%) */
//	__u32	gap;		/* re-ordering gap (0 for none) */
//	__u32   duplicate;	/* random packet dup  (0=none ~0=100%) */
// 	__u32	jitter;		/* random jitter in latency (us) */
// };

type TcNetemQopt struct {
	Latency   uint32
	Limit     uint32
	Loss      uint32
	Gap       uint32
	Duplicate uint32
	Jitter    uint32
}

func (msg *TcNetemQopt) Len() int {
	return SizeofTcNetemQopt
}

func DeserializeTcNetemQopt(b []byte) *TcNetemQopt {
	return (*TcNetemQopt)(unsafe.Pointer(&b[0:SizeofTcNetemQopt][0]))
}

func (x *TcNetemQopt) Serialize() []byte {
	return (*(*[SizeofTcNetemQopt]byte)(unsafe.Pointer(x)))[:]
}

// struct tc_netem_corr {
//  __u32   delay_corr; /* delay correlation */
//  __u32   loss_corr;  /* packet loss correlation */
//  __u32   dup_corr;   /* duplicate correlation  */
// };

type TcNetemCorr struct {
	DelayCorr uint32
	LossCorr  uint32
	DupCorr   uint32
}

func (msg *TcNetemCorr) Len() int {
	return SizeofTcNetemCorr
}

func DeserializeTcNetemCorr(b []byte) *TcNetemCorr {
	return (*TcNetemCorr)(unsafe.Pointer(&b[0:SizeofTcNetemCorr][0]))
}

func (x *TcNetemCorr) Serialize() []byte {
	return (*(*[SizeofTcNetemCorr]byte)(unsafe.Pointer(x)))[:]
}

// struct tc_netem_reorder {
//  __u32   probability;
//  __u32   correlation;
// };

type TcNetemReorder struct {
	Probability uint32
	Correlation uint32
}

func (msg *TcNetemReorder) Len() int {
	return SizeofTcNetemReorder
}

func DeserializeTcNetemReorder(b []byte) *TcNetemReorder {
	return (*TcNetemReorder)(unsafe.Pointer(&b[0:SizeofTcNetemReorder][0]))
}

func (x *TcNetemReorder) Serialize() []byte {
	return (*(*[SizeofTcNetemReorder]byte)(unsafe.Pointer(x)))[:]
}

// struct tc_netem_corrupt {
//  __u32   probability;
//  __u32   correlation;
// };

type TcNetemCorrupt struct {
	Probability uint32
	Correlation uint32
}

func (msg *TcNetemCorrupt) Len() int {
	return SizeofTcNetemCorrupt
}

func DeserializeTcNetemCorrupt(b []byte) *TcNetemCorrupt {
	return (*TcNetemCorrupt)(unsafe.Pointer(&b[0:SizeofTcNetemCorrupt][0]))
}

func (x *TcNetemCorrupt) Serialize() []byte {
	return (*(*[SizeofTcNetemCorrupt]byte)(unsafe.Pointer(x)))[:]
}

// TcNetemRate is a struct that represents the rate of a netem qdisc
type TcNetemRate struct {
	Rate           uint32
	PacketOverhead int32
	CellSize       uint32
	CellOverhead   int32
}

func (msg *TcNetemRate) Len() int {
	return SizeofTcRateSpec
}

func DeserializeTcNetemRate(b []byte) *TcNetemRate {
	return (*TcNetemRate)(unsafe.Pointer(&b[0:SizeofTcRateSpec][0]))
}

func (msg *TcNetemRate) Serialize() []byte {
	return (*(*[SizeOfTcNetemRate]byte)(unsafe.Pointer(msg)))[:]
}

// struct tc_tbf_qopt {
//   struct tc_ratespec rate;
//   struct tc_ratespec peakrate;
//   __u32   limit;
//   __u32   buffer;
//   __u32   mtu;
// };

type TcTbfQopt struct {
	Rate     TcRateSpec
	Peakrate TcRateSpec
	Limit    uint32
	Buffer   uint32
	Mtu      uint32
}

func (msg *TcTbfQopt) Len() int {
	return SizeofTcTbfQopt
}

func DeserializeTcTbfQopt(b []byte) *TcTbfQopt {
	return (*TcTbfQopt)(unsafe.Pointer(&b[0:SizeofTcTbfQopt][0]))
}

func (x *TcTbfQopt) Serialize() []byte {
	return (*(*[SizeofTcTbfQopt]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_HTB_UNSPEC = iota
	TCA_HTB_PARMS
	TCA_HTB_INIT
	TCA_HTB_CTAB
	TCA_HTB_RTAB
	TCA_HTB_DIRECT_QLEN
	TCA_HTB_RATE64
	TCA_HTB_CEIL64
	TCA_HTB_MAX = TCA_HTB_CEIL64
)

//struct tc_htb_opt {
//	struct tc_ratespec	rate;
//	struct tc_ratespec	ceil;
//	__u32	buffer;
//	__u32	cbuffer;
//	__u32	quantum;
//	__u32	level;		/* out only */
//	__u32	prio;
//};

type TcHtbCopt struct {
	Rate    TcRateSpec
	Ceil    TcRateSpec
	Buffer  uint32
	Cbuffer uint32
	Quantum uint32
	Level   uint32
	Prio    uint32
}

func (msg *TcHtbCopt) Len() int {
	return SizeofTcHtbCopt
}

func DeserializeTcHtbCopt(b []byte) *TcHtbCopt {
	return (*TcHtbCopt)(unsafe.Pointer(&b[0:SizeofTcHtbCopt][0]))
}

func (x *TcHtbCopt) Serialize() []byte {
	return (*(*[SizeofTcHtbCopt]byte)(unsafe.Pointer(x)))[:]
}

type TcHtbGlob struct {
	Version      uint32
	Rate2Quantum uint32
	Defcls       uint32
	Debug        uint32
	DirectPkts   uint32
}

func (msg *TcHtbGlob) Len() int {
	return SizeofTcHtbGlob
}

func DeserializeTcHtbGlob(b []byte) *TcHtbGlob {
	return (*TcHtbGlob)(unsafe.Pointer(&b[0:SizeofTcHtbGlob][0]))
}

func (x *TcHtbGlob) Serialize() []byte {
	return (*(*[SizeofTcHtbGlob]byte)(unsafe.Pointer(x)))[:]
}

// HFSC

type Curve struct {
	m1 uint32
	d  uint32
	m2 uint32
}

type HfscCopt struct {
	Rsc Curve
	Fsc Curve
	Usc Curve
}

func (c *Curve) Attrs() (uint32, uint32, uint32) {
	return c.m1, c.d, c.m2
}

func (c *Curve) Set(m1 uint32, d uint32, m2 uint32) {
	c.m1 = m1
	c.d = d
	c.m2 = m2
}

func DeserializeHfscCurve(b []byte) *Curve {
	return &Curve{
		m1: binary.LittleEndian.Uint32(b[0:4]),
		d:  binary.LittleEndian.Uint32(b[4:8]),
		m2: binary.LittleEndian.Uint32(b[8:12]),
	}
}

func SerializeHfscCurve(c *Curve) (b []byte) {
	t := make([]byte, binary.MaxVarintLen32)
	binary.LittleEndian.PutUint32(t, c.m1)
	b = append(b, t[:4]...)
	binary.LittleEndian.PutUint32(t, c.d)
	b = append(b, t[:4]...)
	binary.LittleEndian.PutUint32(t, c.m2)
	b = append(b, t[:4]...)
	return b
}

type TcHfscOpt struct {
	Defcls uint16
}

func (x *TcHfscOpt) Serialize() []byte {
	return (*(*[2]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_U32_UNSPEC = iota
	TCA_U32_CLASSID
	TCA_U32_HASH
	TCA_U32_LINK
	TCA_U32_DIVISOR
	TCA_U32_SEL
	TCA_U32_POLICE
	TCA_U32_ACT
	TCA_U32_INDEV
	TCA_U32_PCNT
	TCA_U32_MARK
	TCA_U32_MAX = TCA_U32_MARK
)

// struct tc_u32_key {
//   __be32    mask;
//   __be32    val;
//   int   off;
//   int   offmask;
// };

type TcU32Key struct {
	Mask    uint32 // big endian
	Val     uint32 // big endian
	Off     int32
	OffMask int32
}

func (msg *TcU32Key) Len() int {
	return SizeofTcU32Key
}

func DeserializeTcU32Key(b []byte) *TcU32Key {
	return (*TcU32Key)(unsafe.Pointer(&b[0:SizeofTcU32Key][0]))
}

func (x *TcU32Key) Serialize() []byte {
	return (*(*[SizeofTcU32Key]byte)(unsafe.Pointer(x)))[:]
}

// struct tc_u32_sel {
//   unsigned char   flags;
//   unsigned char   offshift;
//   unsigned char   nkeys;
//
//   __be16      offmask;
//   __u16     off;
//   short     offoff;
//
//   short     hoff;
//   __be32      hmask;
//   struct tc_u32_key keys[0];
// };

const (
	TC_U32_TERMINAL  = 1 << iota
	TC_U32_OFFSET    = 1 << iota
	TC_U32_VAROFFSET = 1 << iota
	TC_U32_EAT       = 1 << iota
)

type TcU32Sel struct {
	Flags    uint8
	Offshift uint8
	Nkeys    uint8
	Pad      uint8
	Offmask  uint16 // big endian
	Off      uint16
	Offoff   int16
	Hoff     int16
	Hmask    uint32 // big endian
	Keys     []TcU32Key
}

func (msg *TcU32Sel) Len() int {
	return SizeofTcU32Sel + int(msg.Nkeys)*SizeofTcU32Key
}

func DeserializeTcU32Sel(b []byte) *TcU32Sel {
	x := &TcU32Sel{}
	copy((*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:], b)
	next := SizeofTcU32Sel
	var i uint8
	for i = 0; i < x.Nkeys; i++ {
		x.Keys = append(x.Keys, *DeserializeTcU32Key(b[next:]))
		next += SizeofTcU32Key
	}
	return x
}

func (x *TcU32Sel) Serialize() []byte {
	// This can't just unsafe.cast because it must iterate through keys.
	buf := make([]byte, x.Len())
	copy(buf, (*(*[SizeofTcU32Sel]byte)(unsafe.Pointer(x)))[:])
	next := SizeofTcU32Sel
	for _, key := range x.Keys {
		keyBuf := key.Serialize()
		copy(buf[next:], keyBuf)
		next += SizeofTcU32Key
	}
	return buf
}

type TcGen struct {
	Index   uint32
	Capab   uint32
	Action  int32
	Refcnt  int32
	Bindcnt int32
}

func (msg *TcGen) Len() int {
	return SizeofTcGen
}

func DeserializeTcGen(b []byte) *TcGen {
	return (*TcGen)(unsafe.Pointer(&b[0:SizeofTcGen][0]))
}

func (x *TcGen) Serialize() []byte {
	return (*(*[SizeofTcGen]byte)(unsafe.Pointer(x)))[:]
}

// #define tc_gen \
//   __u32                 index; \
//   __u32                 capab; \
//   int                   action; \
//   int                   refcnt; \
//   int                   bindcnt

const (
	TCA_ACT_GACT = 5
)

const (
	TCA_GACT_UNSPEC = iota
	TCA_GACT_TM
	TCA_GACT_PARMS
	TCA_GACT_PROB
	TCA_GACT_MAX = TCA_GACT_PROB
)

type TcGact TcGen

const (
	TCA_ACT_BPF = 13
)

const (
	TCA_ACT_BPF_UNSPEC = iota
	TCA_ACT_BPF_TM
	TCA_ACT_BPF_PARMS
	TCA_ACT_BPF_OPS_LEN
	TCA_ACT_BPF_OPS
	TCA_ACT_BPF_FD
	TCA_ACT_BPF_NAME
	TCA_ACT_BPF_MAX = TCA_ACT_BPF_NAME
)

const (
	TCA_BPF_FLAG_ACT_DIRECT uint32 = 1 << iota
)

const (
	TCA_BPF_UNSPEC = iota
	TCA_BPF_ACT
	TCA_BPF_POLICE
	TCA_BPF_CLASSID
	TCA_BPF_OPS_LEN
	TCA_BPF_OPS
	TCA_BPF_FD
	TCA_BPF_NAME
	TCA_BPF_FLAGS
	TCA_BPF_FLAGS_GEN
	TCA_BPF_TAG
	TCA_BPF_ID
	TCA_BPF_MAX = TCA_BPF_ID
)

type TcBpf TcGen

const (
	TCA_ACT_CONNMARK = 14
)

const (
	TCA_CONNMARK_UNSPEC = iota
	TCA_CONNMARK_PARMS
	TCA_CONNMARK_TM
	TCA_CONNMARK_MAX = TCA_CONNMARK_TM
)

// struct tc_connmark {
//   tc_gen;
//   __u16 zone;
// };

type TcConnmark struct {
	TcGen
	Zone uint16
}

func (msg *TcConnmark) Len() int {
	return SizeofTcConnmark
}

func DeserializeTcConnmark(b []byte) *TcConnmark {
	return (*TcConnmark)(unsafe.Pointer(&b[0:SizeofTcConnmark][0]))
}

func (x *TcConnmark) Serialize() []byte {
	return (*(*[SizeofTcConnmark]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_CSUM_UNSPEC = iota
	TCA_CSUM_PARMS
	TCA_CSUM_TM
	TCA_CSUM_PAD
	TCA_CSUM_MAX = TCA_CSUM_PAD
)

// struct tc_csum {
//   tc_gen;
//   __u32 update_flags;
// }

type TcCsum struct {
	TcGen
	UpdateFlags uint32
}

func (msg *TcCsum) Len() int {
	return SizeofTcCsum
}

func DeserializeTcCsum(b []byte) *TcCsum {
	return (*TcCsum)(unsafe.Pointer(&b[0:SizeofTcCsum][0]))
}

func (x *TcCsum) Serialize() []byte {
	return (*(*[SizeofTcCsum]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_ACT_MIRRED = 8
)

const (
	TCA_MIRRED_UNSPEC = iota
	TCA_MIRRED_TM
	TCA_MIRRED_PARMS
	TCA_MIRRED_MAX = TCA_MIRRED_PARMS
)

// struct tc_mirred {
// 	tc_gen;
// 	int                     eaction;   /* one of IN/EGRESS_MIRROR/REDIR */
// 	__u32                   ifindex;  /* ifindex of egress port */
// };

type TcMirred struct {
	TcGen
	Eaction int32
	Ifindex uint32
}

func (msg *TcMirred) Len() int {
	return SizeofTcMirred
}

func DeserializeTcMirred(b []byte) *TcMirred {
	return (*TcMirred)(unsafe.Pointer(&b[0:SizeofTcMirred][0]))
}

func (x *TcMirred) Serialize() []byte {
	return (*(*[SizeofTcMirred]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_VLAN_UNSPEC = iota
	TCA_VLAN_TM
	TCA_VLAN_PARMS
	TCA_VLAN_PUSH_VLAN_ID
	TCA_VLAN_PUSH_VLAN_PROTOCOL
	TCA_VLAN_PAD
	TCA_VLAN_PUSH_VLAN_PRIORITY
	TCA_VLAN_PUSH_ETH_DST
	TCA_VLAN_PUSH_ETH_SRC
	TCA_VLAN_MAX
)

//struct tc_vlan {
//	tc_gen;
//	int v_action;
//};

type TcVlan struct {
	TcGen
	Action int32
}

func (msg *TcVlan) Len() int {
	return SizeofTcVlan
}

func DeserializeTcVlan(b []byte) *TcVlan {
	return (*TcVlan)(unsafe.Pointer(&b[0:SizeofTcVlan][0]))
}

func (x *TcVlan) Serialize() []byte {
	return (*(*[SizeofTcVlan]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_TUNNEL_KEY_UNSPEC = iota
	TCA_TUNNEL_KEY_TM
	TCA_TUNNEL_KEY_PARMS
	TCA_TUNNEL_KEY_ENC_IPV4_SRC
	TCA_TUNNEL_KEY_ENC_IPV4_DST
	TCA_TUNNEL_KEY_ENC_IPV6_SRC
	TCA_TUNNEL_KEY_ENC_IPV6_DST
	TCA_TUNNEL_KEY_ENC_KEY_ID
	TCA_TUNNEL_KEY_PAD
	TCA_TUNNEL_KEY_ENC_DST_PORT
	TCA_TUNNEL_KEY_NO_CSUM
	TCA_TUNNEL_KEY_ENC_OPTS
	TCA_TUNNEL_KEY_ENC_TOS
	TCA_TUNNEL_KEY_ENC_TTL
	TCA_TUNNEL_KEY_MAX
)

type TcTunnelKey struct {
	TcGen
	Action int32
}

func (x *TcTunnelKey) Len() int {
	return SizeofTcTunnelKey
}

func DeserializeTunnelKey(b []byte) *TcTunnelKey {
	return (*TcTunnelKey)(unsafe.Pointer(&b[0:SizeofTcTunnelKey][0]))
}

func (x *TcTunnelKey) Serialize() []byte {
	return (*(*[SizeofTcTunnelKey]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_SKBEDIT_UNSPEC = iota
	TCA_SKBEDIT_TM
	TCA_SKBEDIT_PARMS
	TCA_SKBEDIT_PRIORITY
	TCA_SKBEDIT_QUEUE_MAPPING
	TCA_SKBEDIT_MARK
	TCA_SKBEDIT_PAD
	TCA_SKBEDIT_PTYPE
	TCA_SKBEDIT_MASK
	TCA_SKBEDIT_MAX
)

type TcSkbEdit struct {
	TcGen
}

func (x *TcSkbEdit) Len() int {
	return SizeofTcSkbEdit
}

func DeserializeSkbEdit(b []byte) *TcSkbEdit {
	return (*TcSkbEdit)(unsafe.Pointer(&b[0:SizeofTcSkbEdit][0]))
}

func (x *TcSkbEdit) Serialize() []byte {
	return (*(*[SizeofTcSkbEdit]byte)(unsafe.Pointer(x)))[:]
}

// struct tc_police {
// 	__u32			index;
// 	int			action;
// 	__u32			limit;
// 	__u32			burst;
// 	__u32			mtu;
// 	struct tc_ratespec	rate;
// 	struct tc_ratespec	peakrate;
// 	int				refcnt;
// 	int				bindcnt;
// 	__u32			capab;
// };

type TcPolice struct {
	Index    uint32
	Action   int32
	Limit    uint32
	Burst    uint32
	Mtu      uint32
	Rate     TcRateSpec
	PeakRate TcRateSpec
	Refcnt   int32
	Bindcnt  int32
	Capab    uint32
}

func (msg *TcPolice) Len() int {
	return SizeofTcPolice
}

func DeserializeTcPolice(b []byte) *TcPolice {
	return (*TcPolice)(unsafe.Pointer(&b[0:SizeofTcPolice][0]))
}

func (x *TcPolice) Serialize() []byte {
	return (*(*[SizeofTcPolice]byte)(unsafe.Pointer(x)))[:]
}

const (
	TCA_FW_UNSPEC = iota
	TCA_FW_CLASSID
	TCA_FW_POLICE
	TCA_FW_INDEV
	TCA_FW_ACT
	TCA_FW_MASK
	TCA_FW_MAX = TCA_FW_MASK
)

const (
	TCA_MATCHALL_UNSPEC = iota
	TCA_MATCHALL_CLASSID
	TCA_MATCHALL_ACT
	TCA_MATCHALL_FLAGS
)

const (
	TCA_FQ_UNSPEC             = iota
	TCA_FQ_PLIMIT             // limit of total number of packets in queue
	TCA_FQ_FLOW_PLIMIT        // limit of packets per flow
	TCA_FQ_QUANTUM            // RR quantum
	TCA_FQ_INITIAL_QUANTUM    // RR quantum for new flow
	TCA_FQ_RATE_ENABLE        // enable/disable rate limiting
	TCA_FQ_FLOW_DEFAULT_RATE  // obsolete do not use
	TCA_FQ_FLOW_MAX_RATE      // per flow max rate
	TCA_FQ_BUCKETS_LOG        // log2(number of buckets)
	TCA_FQ_FLOW_REFILL_DELAY  // flow credit refill delay in usec
	TCA_FQ_ORPHAN_MASK        // mask applied to orphaned skb hashes
	TCA_FQ_LOW_RATE_THRESHOLD // per packet delay under this rate
	TCA_FQ_CE_THRESHOLD       // DCTCP-like CE-marking threshold
	TCA_FQ_TIMER_SLACK        // timer slack
	TCA_FQ_HORIZON            // time horizon in us
	TCA_FQ_HORIZON_DROP       // drop packets beyond horizon, or cap their EDT
)

const (
	TCA_FQ_CODEL_UNSPEC = iota
	TCA_FQ_CODEL_TARGET
	TCA_FQ_CODEL_LIMIT
	TCA_FQ_CODEL_INTERVAL
	TCA_FQ_CODEL_ECN
	TCA_FQ_CODEL_FLOWS
	TCA_FQ_CODEL_QUANTUM
	TCA_FQ_CODEL_CE_THRESHOLD
	TCA_FQ_CODEL_DROP_BATCH_SIZE
	TCA_FQ_CODEL_MEMORY_LIMIT
)

const (
	TCA_HFSC_UNSPEC = iota
	TCA_HFSC_RSC
	TCA_HFSC_FSC
	TCA_HFSC_USC
)

const (
	TCA_FLOWER_UNSPEC = iota
	TCA_FLOWER_CLASSID
	TCA_FLOWER_INDEV
	TCA_FLOWER_ACT
	TCA_FLOWER_KEY_ETH_DST       /* ETH_ALEN */
	TCA_FLOWER_KEY_ETH_DST_MASK  /* ETH_ALEN */
	TCA_FLOWER_KEY_ETH_SRC       /* ETH_ALEN */
	TCA_FLOWER_KEY_ETH_SRC_MASK  /* ETH_ALEN */
	TCA_FLOWER_KEY_ETH_TYPE      /* be16 */
	TCA_FLOWER_KEY_IP_PROTO      /* u8 */
	TCA_FLOWER_KEY_IPV4_SRC      /* be32 */
	TCA_FLOWER_KEY_IPV4_SRC_MASK /* be32 */
	TCA_FLOWER_KEY_IPV4_DST      /* be32 */
	TCA_FLOWER_KEY_IPV4_DST_MASK /* be32 */
	TCA_FLOWER_KEY_IPV6_SRC      /* struct in6_addr */
	TCA_FLOWER_KEY_IPV6_SRC_MASK /* struct in6_addr */
	TCA_FLOWER_KEY_IPV6_DST      /* struct in6_addr */
	TCA_FLOWER_KEY_IPV6_DST_MASK /* struct in6_addr */
	TCA_FLOWER_KEY_TCP_SRC       /* be16 */
	TCA_FLOWER_KEY_TCP_DST       /* be16 */
	TCA_FLOWER_KEY_UDP_SRC       /* be16 */
	TCA_FLOWER_KEY_UDP_DST       /* be16 */

	TCA_FLOWER_FLAGS
	TCA_FLOWER_KEY_VLAN_ID       /* be16 */
	TCA_FLOWER_KEY_VLAN_PRIO     /* u8   */
	TCA_FLOWER_KEY_VLAN_ETH_TYPE /* be16 */

	TCA_FLOWER_KEY_ENC_KEY_ID        /* be32 */
	TCA_FLOWER_KEY_ENC_IPV4_SRC      /* be32 */
	TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK /* be32 */
	TCA_FLOWER_KEY_ENC_IPV4_DST      /* be32 */
	TCA_FLOWER_KEY_ENC_IPV4_DST_MASK /* be32 */
	TCA_FLOWER_KEY_ENC_IPV6_SRC      /* struct in6_addr */
	TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK /* struct in6_addr */
	TCA_FLOWER_KEY_ENC_IPV6_DST      /* struct in6_addr */
	TCA_FLOWER_KEY_ENC_IPV6_DST_MASK /* struct in6_addr */

	TCA_FLOWER_KEY_TCP_SRC_MASK  /* be16 */
	TCA_FLOWER_KEY_TCP_DST_MASK  /* be16 */
	TCA_FLOWER_KEY_UDP_SRC_MASK  /* be16 */
	TCA_FLOWER_KEY_UDP_DST_MASK  /* be16 */
	TCA_FLOWER_KEY_SCTP_SRC_MASK /* be16 */
	TCA_FLOWER_KEY_SCTP_DST_MASK /* be16 */

	TCA_FLOWER_KEY_SCTP_SRC /* be16 */
	TCA_FLOWER_KEY_SCTP_DST /* be16 */

	TCA_FLOWER_KEY_ENC_UDP_SRC_PORT      /* be16 */
	TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK /* be16 */
	TCA_FLOWER_KEY_ENC_UDP_DST_PORT      /* be16 */
	TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK /* be16 */

	TCA_FLOWER_KEY_FLAGS      /* be32 */
	TCA_FLOWER_KEY_FLAGS_MASK /* be32 */

	TCA_FLOWER_KEY_ICMPV4_CODE      /* u8 */
	TCA_FLOWER_KEY_ICMPV4_CODE_MASK /* u8 */
	TCA_FLOWER_KEY_ICMPV4_TYPE      /* u8 */
	TCA_FLOWER_KEY_ICMPV4_TYPE_MASK /* u8 */
	TCA_FLOWER_KEY_ICMPV6_CODE      /* u8 */
	TCA_FLOWER_KEY_ICMPV6_CODE_MASK /* u8 */
	TCA_FLOWER_KEY_ICMPV6_TYPE      /* u8 */
	TCA_FLOWER_KEY_ICMPV6_TYPE_MASK /* u8 */

	TCA_FLOWER_KEY_ARP_SIP      /* be32 */
	TCA_FLOWER_KEY_ARP_SIP_MASK /* be32 */
	TCA_FLOWER_KEY_ARP_TIP      /* be32 */
	TCA_FLOWER_KEY_ARP_TIP_MASK /* be32 */
	TCA_FLOWER_KEY_ARP_OP       /* u8 */
	TCA_FLOWER_KEY_ARP_OP_MASK  /* u8 */
	TCA_FLOWER_KEY_ARP_SHA      /* ETH_ALEN */
	TCA_FLOWER_KEY_ARP_SHA_MASK /* ETH_ALEN */
	TCA_FLOWER_KEY_ARP_THA      /* ETH_ALEN */
	TCA_FLOWER_KEY_ARP_THA_MASK /* ETH_ALEN */

	TCA_FLOWER_KEY_MPLS_TTL   /* u8 - 8 bits */
	TCA_FLOWER_KEY_MPLS_BOS   /* u8 - 1 bit */
	TCA_FLOWER_KEY_MPLS_TC    /* u8 - 3 bits */
	TCA_FLOWER_KEY_MPLS_LABEL /* be32 - 20 bits */

	TCA_FLOWER_KEY_TCP_FLAGS      /* be16 */
	TCA_FLOWER_KEY_TCP_FLAGS_MASK /* be16 */

	TCA_FLOWER_KEY_IP_TOS      /* u8 */
	TCA_FLOWER_KEY_IP_TOS_MASK /* u8 */
	TCA_FLOWER_KEY_IP_TTL      /* u8 */
	TCA_FLOWER_KEY_IP_TTL_MASK /* u8 */

	TCA_FLOWER_KEY_CVLAN_ID       /* be16 */
	TCA_FLOWER_KEY_CVLAN_PRIO     /* u8   */
	TCA_FLOWER_KEY_CVLAN_ETH_TYPE /* be16 */

	TCA_FLOWER_KEY_ENC_IP_TOS      /* u8 */
	TCA_FLOWER_KEY_ENC_IP_TOS_MASK /* u8 */
	TCA_FLOWER_KEY_ENC_IP_TTL      /* u8 */
	TCA_FLOWER_KEY_ENC_IP_TTL_MASK /* u8 */

	TCA_FLOWER_KEY_ENC_OPTS
	TCA_FLOWER_KEY_ENC_OPTS_MASK

	TCA_FLOWER_IN_HW_COUNT

	TCA_FLOWER_KEY_PORT_SRC_MIN /* be16 */
	TCA_FLOWER_KEY_PORT_SRC_MAX /* be16 */
	TCA_FLOWER_KEY_PORT_DST_MIN /* be16 */
	TCA_FLOWER_KEY_PORT_DST_MAX /* be16 */

	__TCA_FLOWER_MAX
)

const TCA_CLS_FLAGS_SKIP_HW = 1 << 0 /* don't offload filter to HW */
const TCA_CLS_FLAGS_SKIP_SW = 1 << 1 /* don't use filter in SW */

// struct tc_sfq_qopt {
// 	unsigned	quantum;	/* Bytes per round allocated to flow */
// 	int		perturb_period;	/* Period of hash perturbation */
// 	__u32		limit;		/* Maximal packets in queue */
// 	unsigned	divisor;	/* Hash divisor  */
// 	unsigned	flows;		/* Maximal number of flows  */
// };

type TcSfqQopt struct {
	Quantum uint32
	Perturb int32
	Limit   uint32
	Divisor uint32
	Flows   uint32
}

func (x *TcSfqQopt) Len() int {
	return SizeofTcSfqQopt
}

func DeserializeTcSfqQopt(b []byte) *TcSfqQopt {
	return (*TcSfqQopt)(unsafe.Pointer(&b[0:SizeofTcSfqQopt][0]))
}

func (x *TcSfqQopt) Serialize() []byte {
	return (*(*[SizeofTcSfqQopt]byte)(unsafe.Pointer(x)))[:]
}

//	struct tc_sfqred_stats {
//		__u32           prob_drop;      /* Early drops, below max threshold */
//		__u32           forced_drop;	/* Early drops, after max threshold */
//		__u32           prob_mark;      /* Marked packets, below max threshold */
//		__u32           forced_mark;    /* Marked packets, after max threshold */
//		__u32           prob_mark_head; /* Marked packets, below max threshold */
//		__u32           forced_mark_head;/* Marked packets, after max threshold */
//	};
type TcSfqRedStats struct {
	ProbDrop       uint32
	ForcedDrop     uint32
	ProbMark       uint32
	ForcedMark     uint32
	ProbMarkHead   uint32
	ForcedMarkHead uint32
}

func (x *TcSfqRedStats) Len() int {
	return SizeofTcSfqRedStats
}

func DeserializeTcSfqRedStats(b []byte) *TcSfqRedStats {
	return (*TcSfqRedStats)(unsafe.Pointer(&b[0:SizeofTcSfqRedStats][0]))
}

func (x *TcSfqRedStats) Serialize() []byte {
	return (*(*[SizeofTcSfqRedStats]byte)(unsafe.Pointer(x)))[:]
}

//	struct tc_sfq_qopt_v1 {
//		struct tc_sfq_qopt v0;
//		unsigned int	depth;		/* max number of packets per flow */
//		unsigned int	headdrop;
//
// /* SFQRED parameters */
//
//	__u32		limit;		/* HARD maximal flow queue length (bytes) */
//	__u32		qth_min;	/* Min average length threshold (bytes) */
//	__u32		qth_max;	/* Max average length threshold (bytes) */
//	unsigned char   Wlog;		/* log(W)		*/
//	unsigned char   Plog;		/* log(P_max/(qth_max-qth_min))	*/
//	unsigned char   Scell_log;	/* cell size for idle damping */
//	unsigned char	flags;
//	__u32		max_P;		/* probability, high resolution */
//
// /* SFQRED stats */
//
//		struct tc_sfqred_stats stats;
//	};
type TcSfqQoptV1 struct {
	TcSfqQopt
	Depth    uint32
	HeadDrop uint32
	Limit    uint32
	QthMin   uint32
	QthMax   uint32
	Wlog     byte
	Plog     byte
	ScellLog byte
	Flags    byte
	MaxP     uint32
	TcSfqRedStats
}

func (x *TcSfqQoptV1) Len() int {
	return SizeofTcSfqQoptV1
}

func DeserializeTcSfqQoptV1(b []byte) *TcSfqQoptV1 {
	return (*TcSfqQoptV1)(unsafe.Pointer(&b[0:SizeofTcSfqQoptV1][0]))
}

func (x *TcSfqQoptV1) Serialize() []byte {
	return (*(*[SizeofTcSfqQoptV1]byte)(unsafe.Pointer(x)))[:]
}

// IPProto represents Flower ip_proto attribute
type IPProto uint8

const (
	IPPROTO_TCP    IPProto = unix.IPPROTO_TCP
	IPPROTO_UDP    IPProto = unix.IPPROTO_UDP
	IPPROTO_SCTP   IPProto = unix.IPPROTO_SCTP
	IPPROTO_ICMP   IPProto = unix.IPPROTO_ICMP
	IPPROTO_ICMPV6 IPProto = unix.IPPROTO_ICMPV6
)

func (i IPProto) Serialize() []byte {
	arr := make([]byte, 1)
	arr[0] = byte(i)
	return arr
}

func (i IPProto) String() string {
	switch i {
	case IPPROTO_TCP:
		return "tcp"
	case IPPROTO_UDP:
		return "udp"
	case IPPROTO_SCTP:
		return "sctp"
	case IPPROTO_ICMP:
		return "icmp"
	case IPPROTO_ICMPV6:
		return "icmpv6"
	}
	return fmt.Sprintf("%d", i)
}

const (
	MaxOffs        = 128
	SizeOfPeditSel = 24
	SizeOfPeditKey = 24

	TCA_PEDIT_KEY_EX_HTYPE = 1
	TCA_PEDIT_KEY_EX_CMD   = 2
)

const (
	TCA_PEDIT_UNSPEC = iota
	TCA_PEDIT_TM
	TCA_PEDIT_PARMS
	TCA_PEDIT_PAD
	TCA_PEDIT_PARMS_EX
	TCA_PEDIT_KEYS_EX
	TCA_PEDIT_KEY_EX
)

// /* TCA_PEDIT_KEY_EX_HDR_TYPE_NETWROK is a special case for legacy users. It
//   - means no specific header type - offset is relative to the network layer
//     */
type PeditHeaderType uint16

const (
	TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK = iota
	TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
	TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
	TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
	TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
	__PEDIT_HDR_TYPE_MAX
)

type PeditCmd uint16

const (
	TCA_PEDIT_KEY_EX_CMD_SET = 0
	TCA_PEDIT_KEY_EX_CMD_ADD = 1
)

type TcPeditSel struct {
	TcGen
	NKeys uint8
	Flags uint8
}

func DeserializeTcPeditKey(b []byte) *TcPeditKey {
	return (*TcPeditKey)(unsafe.Pointer(&b[0:SizeOfPeditKey][0]))
}

func DeserializeTcPedit(b []byte) (*TcPeditSel, []TcPeditKey) {
	x := &TcPeditSel{}
	copy((*(*[SizeOfPeditSel]byte)(unsafe.Pointer(x)))[:SizeOfPeditSel], b)

	var keys []TcPeditKey

	next := SizeOfPeditKey
	var i uint8
	for i = 0; i < x.NKeys; i++ {
		keys = append(keys, *DeserializeTcPeditKey(b[next:]))
		next += SizeOfPeditKey
	}

	return x, keys
}

type TcPeditKey struct {
	Mask    uint32
	Val     uint32
	Off     uint32
	At      uint32
	OffMask uint32
	Shift   uint32
}

type TcPeditKeyEx struct {
	HeaderType PeditHeaderType
	Cmd        PeditCmd
}

type TcPedit struct {
	Sel    TcPeditSel
	Keys   []TcPeditKey
	KeysEx []TcPeditKeyEx
	Extend uint8
}

func (p *TcPedit) Encode(parent *RtAttr) {
	parent.AddRtAttr(TCA_ACT_KIND, ZeroTerminated("pedit"))
	actOpts := parent.AddRtAttr(TCA_ACT_OPTIONS, nil)

	bbuf := bytes.NewBuffer(make([]byte, 0, int(unsafe.Sizeof(p.Sel)+unsafe.Sizeof(p.Keys))))

	bbuf.Write((*(*[SizeOfPeditSel]byte)(unsafe.Pointer(&p.Sel)))[:])

	for i := uint8(0); i < p.Sel.NKeys; i++ {
		bbuf.Write((*(*[SizeOfPeditKey]byte)(unsafe.Pointer(&p.Keys[i])))[:])
	}
	actOpts.AddRtAttr(TCA_PEDIT_PARMS_EX, bbuf.Bytes())

	exAttrs := actOpts.AddRtAttr(int(TCA_PEDIT_KEYS_EX|NLA_F_NESTED), nil)
	for i := uint8(0); i < p.Sel.NKeys; i++ {
		keyAttr := exAttrs.AddRtAttr(int(TCA_PEDIT_KEY_EX|NLA_F_NESTED), nil)

		htypeBuf := make([]byte, 2)
		cmdBuf := make([]byte, 2)

		NativeEndian().PutUint16(htypeBuf, uint16(p.KeysEx[i].HeaderType))
		NativeEndian().PutUint16(cmdBuf, uint16(p.KeysEx[i].Cmd))

		keyAttr.AddRtAttr(TCA_PEDIT_KEY_EX_HTYPE, htypeBuf)
		keyAttr.AddRtAttr(TCA_PEDIT_KEY_EX_CMD, cmdBuf)
	}
}

func (p *TcPedit) SetEthDst(mac net.HardwareAddr) {
	u32 := NativeEndian().Uint32(mac)
	u16 := NativeEndian().Uint16(mac[4:])

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = u32

	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++

	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = uint32(u16)
	tKey.Mask = 0xffff0000
	tKey.Off = 4
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++
}

func (p *TcPedit) SetEthSrc(mac net.HardwareAddr) {
	u16 := NativeEndian().Uint16(mac)
	u32 := NativeEndian().Uint32(mac[2:])

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = uint32(u16) << 16
	tKey.Mask = 0x0000ffff
	tKey.Off = 4

	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++

	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Mask = 0
	tKey.Off = 8

	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++
}

func (p *TcPedit) SetIPv6Src(ip6 net.IP) {
	u32 := NativeEndian().Uint32(ip6[:4])

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 8
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[4:8])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 12
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[8:12])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 16
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[12:16])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 20
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++
}

func (p *TcPedit) SetDstIP(ip net.IP) {
	if ip.To4() != nil {
		p.SetIPv4Dst(ip)
	} else {
		p.SetIPv6Dst(ip)
	}
}

func (p *TcPedit) SetSrcIP(ip net.IP) {
	if ip.To4() != nil {
		p.SetIPv4Src(ip)
	} else {
		p.SetIPv6Src(ip)
	}
}

func (p *TcPedit) SetIPv6Dst(ip6 net.IP) {
	u32 := NativeEndian().Uint32(ip6[:4])

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 24
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[4:8])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 28
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[8:12])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 32
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++

	u32 = NativeEndian().Uint32(ip6[12:16])
	tKey = TcPeditKey{}
	tKeyEx = TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 36
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)

	p.Sel.NKeys++
}

func (p *TcPedit) SetIPv4Src(ip net.IP) {
	u32 := NativeEndian().Uint32(ip.To4())

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 12
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++
}

func (p *TcPedit) SetIPv4Dst(ip net.IP) {
	u32 := NativeEndian().Uint32(ip.To4())

	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	tKey.Val = u32
	tKey.Off = 16
	tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4
	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++
}

// SetDstPort only tcp and udp are supported to set port
func (p *TcPedit) SetDstPort(dstPort uint16, protocol uint8) {
	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	switch protocol {
	case unix.IPPROTO_TCP:
		tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
	case unix.IPPROTO_UDP:
		tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
	default:
		return
	}

	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	tKey.Val = uint32(Swap16(dstPort)) << 16
	tKey.Mask = 0x0000ffff
	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++
}

// SetSrcPort only tcp and udp are supported to set port
func (p *TcPedit) SetSrcPort(srcPort uint16, protocol uint8) {
	tKey := TcPeditKey{}
	tKeyEx := TcPeditKeyEx{}

	switch protocol {
	case unix.IPPROTO_TCP:
		tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP
	case unix.IPPROTO_UDP:
		tKeyEx.HeaderType = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP
	default:
		return
	}

	tKeyEx.Cmd = TCA_PEDIT_KEY_EX_CMD_SET

	tKey.Val = uint32(Swap16(srcPort))
	tKey.Mask = 0xffff0000
	p.Keys = append(p.Keys, tKey)
	p.KeysEx = append(p.KeysEx, tKeyEx)
	p.Sel.NKeys++
}