forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_fqCodel_test.go
98 lines (87 loc) · 2.22 KB
/
example_fqCodel_test.go
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
//go:build linux
// +build linux
package tc_test
import (
"fmt"
"net"
"os"
"github.com/florianl/go-tc"
"github.com/florianl/go-tc/core"
"github.com/jsimonetti/rtnetlink"
"golang.org/x/sys/unix"
)
func ExampleFqCodel() {
tcIface := "ExampleFQCodel"
rtnl, err := setupDummyInterface(tcIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not setup dummy interface: %v\n", err)
return
}
defer rtnl.Close()
devID, err := net.InterfaceByName(tcIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
return
}
defer func(devID uint32, rtnl *rtnetlink.Conn) {
if err := rtnl.Link.Delete(devID); err != nil {
fmt.Fprintf(os.Stderr, "could not delete interface: %v\n", err)
}
}(uint32(devID.Index), rtnl)
tcnl, err := tc.Open(&tc.Config{})
if err != nil {
fmt.Fprintf(os.Stderr, "could not open rtnetlink socket: %v\n", err)
return
}
defer func() {
if err := tcnl.Close(); err != nil {
fmt.Fprintf(os.Stderr, "could not close rtnetlink socket: %v\n", err)
}
}()
target := uint32(0xbb8)
limit := uint32(0x7d0)
interval := uint32(0x9c40)
ecn := uint32(0x0)
qdisc := tc.Object{
tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: uint32(devID.Index),
Handle: core.BuildHandle(0x1, 0x0),
Parent: tc.HandleRoot,
Info: 0,
},
tc.Attribute{
Kind: "fq_codel",
// http://man7.org/linux/man-pages/man8/tc-fq_codel.8.html
// fq_codel limit 2000 target 3ms interval 40ms noecn
FqCodel: &tc.FqCodel{
Target: &target,
Limit: &limit,
Interval: &interval,
ECN: &ecn,
},
},
}
if err := tcnl.Qdisc().Add(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not assign htb to lo: %v\n", err)
return
}
defer func() {
if err := tcnl.Qdisc().Delete(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not delete htb qdisc of lo: %v\n", err)
return
}
}()
qdiscs, err := tcnl.Qdisc().Get()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get all qdiscs: %v\n", err)
}
for _, qdisc := range qdiscs {
iface, err := net.InterfaceByIndex(int(qdisc.Ifindex))
if err != nil {
fmt.Fprintf(os.Stderr, "could not get interface from id %d: %v", qdisc.Ifindex, err)
return
}
fmt.Printf("%20s\t%s\n", iface.Name, qdisc.Kind)
}
}