-
Notifications
You must be signed in to change notification settings - Fork 48
/
example_hfsc_test.go
170 lines (153 loc) · 4.01 KB
/
example_hfsc_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
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
//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 addHfscClass(class *tc.Class, devID, maj, min uint32, serviceCurve *tc.ServiceCurve) (*tc.Object, error) {
hfsc := tc.Object{
tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: devID,
Handle: core.BuildHandle(maj, min),
Parent: 0x10000,
Info: 0,
},
tc.Attribute{
Kind: "hfsc",
Hfsc: &tc.Hfsc{
Rsc: serviceCurve,
Fsc: serviceCurve,
Usc: serviceCurve,
},
},
}
if err := class.Add(&hfsc); err != nil {
fmt.Fprintf(os.Stderr, "could not assign hfsc class: %v\n", err)
return nil, err
}
return &hfsc, nil
}
func ExampleHfsc() {
var rtnl *rtnetlink.Conn
var err error
tcIface := "tcDev"
if rtnl, err = setupDummyInterface(tcIface); 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)
}
}()
qdisc := tc.Object{
tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: uint32(devID.Index),
Handle: 0x10000,
Parent: tc.HandleRoot,
Info: 0,
},
// tc qdisc add dev tcDev stab linklayer ethernet mtu 1500 root handle 1: hfsc default 3
// http://man7.org/linux/man-pages/man8/tc-stab.8.html
tc.Attribute{
Kind: "hfsc",
HfscQOpt: &tc.HfscQOpt{
DefCls: 3,
},
Stab: &tc.Stab{
Base: &tc.SizeSpec{
CellLog: 0,
SizeLog: 0,
CellAlign: 0,
Overhead: 0,
LinkLayer: 1,
MPU: 0,
MTU: 1500,
TSize: 0,
},
},
},
}
if err := tcnl.Qdisc().Add(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not assign hfsc to %s: %v\n", tcIface, err)
return
}
defer func() {
if err := tcnl.Qdisc().Delete(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not delete htb qdisc of %s: %v\n", tcIface, err)
return
}
}()
class1, err := addHfscClass(tcnl.Class(), uint32(devID.Index), 0x10000, 0x1, &tc.ServiceCurve{M2: 0x1e848})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to add hfsc: %v\n", err)
return
}
defer func() {
if err := tcnl.Class().Delete(class1); err != nil {
fmt.Fprintf(os.Stderr, "could not delete hfsc class of %s: %v\n", tcIface, err)
return
}
}()
class2, err := addHfscClass(tcnl.Class(), uint32(devID.Index), 0x10000, 0x2, &tc.ServiceCurve{M2: 0x1e848})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to add hfsc: %v\n", err)
return
}
defer func() {
if err := tcnl.Class().Delete(class2); err != nil {
fmt.Fprintf(os.Stderr, "could not delete hfsc class of %s: %v\n", tcIface, err)
return
}
}()
class3, err := addHfscClass(tcnl.Class(), uint32(devID.Index), 0x10000, 0x3, &tc.ServiceCurve{M2: 0x1e848})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to add hfsc: %v\n", err)
return
}
defer func() {
if err := tcnl.Class().Delete(class3); err != nil {
fmt.Fprintf(os.Stderr, "could not delete hfsc class of %s: %v\n", tcIface, err)
return
}
}()
classes, err := tcnl.Class().Get(&tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: uint32(devID.Index),
})
if err != nil {
fmt.Fprintf(os.Stderr, "could not get all classes: %v\n", err)
}
for _, class := range classes {
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, class.Kind)
}
}