-
Notifications
You must be signed in to change notification settings - Fork 46
/
qdisc.go
188 lines (173 loc) · 4.99 KB
/
qdisc.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package tc
import (
"fmt"
"github.com/florianl/go-tc/internal/unix"
"github.com/mdlayher/netlink"
)
// Qdisc represents the queueing discipline part of traffic control
type Qdisc struct {
Tc
}
// Qdisc allows to read and alter queues
func (tc *Tc) Qdisc() *Qdisc {
return &Qdisc{*tc}
}
// Add creates a new queueing discipline
func (qd *Qdisc) Add(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateQdiscObject(unix.RTM_NEWQDISC, info)
if err != nil {
return err
}
return qd.action(unix.RTM_NEWQDISC, netlink.Create|netlink.Excl, &info.Msg, options)
}
// Replace add/remove a queueing discipline. If the node does not exist yet it is created
func (qd *Qdisc) Replace(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateQdiscObject(unix.RTM_NEWQDISC, info)
if err != nil {
return err
}
return qd.action(unix.RTM_NEWQDISC, netlink.Create|netlink.Replace, &info.Msg, options)
}
// Link performs a replace on an existing queueing discipline
func (qd *Qdisc) Link(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateQdiscObject(unix.RTM_NEWQDISC, info)
if err != nil {
return err
}
return qd.action(unix.RTM_NEWQDISC, netlink.Replace, &info.Msg, options)
}
// Delete removes a queueing discipline
func (qd *Qdisc) Delete(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateQdiscObject(unix.RTM_DELQDISC, info)
if err != nil {
return err
}
return qd.action(unix.RTM_DELQDISC, netlink.HeaderFlags(0), &info.Msg, options)
}
// Change modifies a queueing discipline 'in place'
func (qd *Qdisc) Change(info *Object) error {
if info == nil {
return ErrNoArg
}
options, err := validateQdiscObject(unix.RTM_NEWQDISC, info)
if err != nil {
return err
}
return qd.action(unix.RTM_NEWQDISC, netlink.HeaderFlags(0), &info.Msg, options)
}
// Get fetches all queueing disciplines
func (qd *Qdisc) Get() ([]Object, error) {
return qd.get(unix.RTM_GETQDISC, &Msg{})
}
func validateQdiscObject(action int, info *Object) ([]tcOption, error) {
options := []tcOption{}
if info.Ifindex == 0 {
return options, ErrInvalidDev
}
// TODO: improve logic and check combinations
var data []byte
var err error
switch info.Kind {
case "cake":
data, err = marshalCake(info.Cake)
case "choke":
data, err = marshalChoke(info.Choke)
case "pfifo":
data, err = marshalStruct(info.Pfifo)
case "bfifo":
data, err = marshalStruct(info.Bfifo)
case "tbf":
data, err = marshalTbf(info.Tbf)
case "sfb":
data, err = marshalSfb(info.Sfb)
case "sfq":
data, err = marshalSfq(info.Sfq)
case "red":
data, err = marshalRed(info.Red)
case "qfq":
// qfq is parameterless
// parameters are used in its corresponding class
case "pie":
data, err = marshalPie(info.Pie)
case "mqprio":
data, err = marshalMqPrio(info.MqPrio)
case "hhf":
data, err = marshalHhf(info.Hhf)
case "hfsc":
data, err = marshalHfscQOpt(info.HfscQOpt)
case "fq":
data, err = marshalFq(info.Fq)
case "dsmark":
data, err = marshalDsmark(info.Dsmark)
case "drr":
data, err = marshalDrr(info.Drr)
case "codel":
data, err = marshalCodel(info.Codel)
case "cbq":
data, err = marshalCbq(info.Cbq)
case "atm":
data, err = marshalAtm(info.Atm)
case "fq_codel":
data, err = marshalFqCodel(info.FqCodel)
case "htb":
data, err = marshalHtb(info.Htb)
case "netem":
data, err = marshalNetem(info.Netem)
case "prio":
data, err = marshalPrio(info.Prio)
case "plug":
data, err = marshalPlug(info.Plug)
case "clsact":
// clsact is parameterless
case "ingress":
// ingress is parameterless
default:
return options, fmt.Errorf("%s: %w", info.Kind, ErrNotImplemented)
}
if err != nil {
return options, err
}
if len(data) < 1 && action == unix.RTM_NEWQDISC {
if info.Kind != "clsact" && info.Kind != "ingress" && info.Kind != "qfq" {
return options, ErrNoArg
}
} else {
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaOptions, Data: data})
}
options = append(options, tcOption{Interpretation: vtString, Type: tcaKind, Data: info.Kind})
if (info.Stats != nil || info.XStats != nil || info.Stats2 != nil) && action != unix.RTM_DELQDISC {
return options, ErrNotImplemented
}
if info.EgressBlock != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaEgressBlock, Data: uint32Value(info.EgressBlock)})
}
if info.IngressBlock != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaIngressBlock, Data: uint32Value(info.IngressBlock)})
}
if info.HwOffload != nil {
options = append(options, tcOption{Interpretation: vtUint8, Type: tcaHwOffload, Data: uint8Value(info.HwOffload)})
}
if info.Chain != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaChain, Data: uint32Value(info.Chain)})
}
if info.Stab != nil {
data, err := marshalStab(info.Stab)
if err != nil {
return options, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaStab, Data: data})
}
return options, nil
}