-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlan.go
290 lines (275 loc) · 8.78 KB
/
vlan.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
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
package main
import (
"bytes"
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
// RegisterVLAN adds "vlan", "in", "out" commands to bot
func RegisterVLAN(bot Bot, ifaces *Interfaces) {
ifaces.Update()
v := &vlan{Interfaces: ifaces}
bot.Add("current", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToCurrent(bot, msg, tokens)
})
bot.Add("iface", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToIface(bot, msg, tokens)
})
bot.Add("interface", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToIface(bot, msg, tokens)
})
bot.Add("vlan", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToVLAN(bot, msg, tokens)
})
bot.Add("up", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToIn(bot, msg, tokens)
})
bot.Add("in", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToIn(bot, msg, tokens)
})
bot.Add("down", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToOut(bot, msg, tokens)
})
bot.Add("out", func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.replyToOut(bot, msg, tokens)
})
// Add a shortcut for each interface, except "lo", "ifb"
skipPrefixes := []string{"lo", "ifb"}
for ifaceName := range ifaces.Current {
current, doSkip := ifaceName, false
for _, skip := range skipPrefixes {
if strings.HasPrefix(current, skip) {
doSkip = true
break
}
}
if doSkip {
continue
}
bot.Add(current, func(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return v.selectIface(current, bot, msg, tokens)
})
}
}
// VLAN data
type vlan struct {
Interfaces *Interfaces // Enumeration of interfaces
Device string // Device name for selected VLAN
IFB string // IFB device name for selected vlan
}
// Impairment parameters
type params struct {
delay, jitter int
loss, correlation float64
}
// ReplyToIface selects a particular interface
func (v *vlan) replyToIface(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
if tokens.Remaining() < 1 {
return "Error: must provide the device name (interface <name>)"
}
prefix := tokens.Next()
if prefix == "" {
return "Error: Must provide an interface name"
}
return v.selectIface(prefix, bot, msg, tokens)
}
// SelectIface selects a particular interface
func (v *vlan) selectIface(prefix string, bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
matches := make([]string, 0, 10)
for name := range v.Interfaces.Current {
if strings.HasPrefix(name, prefix) {
// VLAN interfaces are excluded, use the "VLAN" command
if !strings.Contains(name, ".") {
matches = append(matches, name)
}
}
}
if len(matches) <= 0 {
return fmt.Sprintf("Error: Interface %s is not found. Run \"ip\" for more info", prefix)
}
if len(matches) > 1 {
return fmt.Sprintf("Error: Interface %s is ambiguous, matches: %s", prefix, strings.Join(matches, ", "))
}
return v.setDevice(matches[0])
}
// ReplyToVLAN selects a particular VLAN
func (v *vlan) replyToVLAN(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
if tokens.Remaining() < 1 {
return "Error: must provide the VLAN number (vlan <vlan_number>)"
}
vlan, err := strconv.Atoi(tokens.Next())
if err != nil {
return err.Error()
}
if vlan < 1 || vlan > 4094 {
return "Error: VLAN number must be between 1 and 4094"
}
suffix := fmt.Sprintf(".%d", vlan)
found := ""
for name := range v.Interfaces.Current {
if strings.HasSuffix(name, suffix) {
found = name
break
}
}
if found == "" {
return fmt.Sprintf("Error: VLAN %d is not found. Run \"ip\" for more info", vlan)
}
return v.setDevice(found)
}
func (v *vlan) setDevice(device string) string {
v.Device = device
v.IFB = ""
ifb, err := v.getIFB()
if err != nil {
return fmt.Sprintf("Could not get IFB: %s.\n Interface %s will only accept out or down commands.", err.Error(), device)
}
v.IFB = ifb
return fmt.Sprintf("Device %s selected", device)
}
// ReplyToCurrent dumps the current interface
func (v *vlan) replyToCurrent(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
return fmt.Sprintf("Selected device: [%s]. Matching IFB: [%s]", v.Device, v.IFB)
}
// ReplyToIn adds delay in the upstream direction
func (v *vlan) replyToIn(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
if v.IFB == "" {
return "Current VLAN does not have IFB device assigned"
}
params, err := v.getParams(msg, tokens)
if err != nil {
return err.Error()
}
return v.impair(v.IFB, params)
}
// ReplyToOut adds delay in the downstream direction
func (v *vlan) replyToOut(bot Bot, msg *tgbotapi.Message, tokens *Tokens) string {
params, err := v.getParams(msg, tokens)
if err != nil {
return err.Error()
}
return v.impair(v.Device, params)
}
// Get Delay, Jitter, PL and PL correlation from command
func (v *vlan) getParams(msg *tgbotapi.Message, tokens *Tokens) (params, error) {
result := params{}
if v.Device == "" {
return result, errors.New("No device selected. Run \"ip\" for more info")
}
if tokens.Remaining() <= 0 {
return result, errors.New("Error: must at least provide delay (ms). Format: [in|out] <delay_ms> <jitter_ms> <PL %> <correlation %>")
}
msDelay, err := strconv.Atoi(tokens.Next())
if err != nil {
return result, fmt.Errorf("delay is not an int: %s", err.Error())
}
if msDelay < 0 || msDelay > 4094 {
return result, errors.New("Error: Delay must be between 0 and 4094 milliseconds")
}
result.delay = msDelay
if tokens.Remaining() > 0 {
msJitter, err := strconv.Atoi(tokens.Next())
if err != nil {
tokens.Back()
return result, nil
}
if msJitter < 0 || msJitter > 4094 {
return result, errors.New("Error: Jitter must be between 0 and 4094 milliseconds")
}
result.jitter = msJitter
}
if tokens.Remaining() > 0 {
pl, err := strconv.ParseFloat(tokens.Next(), 32)
if err != nil {
tokens.Back()
return result, nil
}
if pl < 0 || pl > 100 {
return result, errors.New("Error: Packet loss must be between 0.0 and 100.0 percent")
}
result.loss = pl
}
if tokens.Remaining() > 0 {
corr, err := strconv.ParseFloat(tokens.Next(), 32)
if err != nil {
tokens.Back()
return result, nil
}
if corr < 0 || corr > 100 {
return result, errors.New("Error: Correlation must be between 0.0 and 100.0 percent")
}
result.correlation = corr
}
return result, nil
}
// Add impairments (delay, jitter, loss...) to an interface
func (v *vlan) impair(iface string, p params) string {
messages := make([]string, 0, 10)
// Remove any qdisc
cmd := exec.Command("tc", "qdisc", "del", "dev", iface, "root")
var outDel bytes.Buffer
cmd.Stdout = &outDel
if err := cmd.Run(); err != nil {
messages = append(messages, fmt.Sprintf("Warn: nothing to clear in interface %s. Proceeding (%s)", iface, err.Error()))
} else {
messages = append(messages, fmt.Sprintf("Cleared interface %s", iface))
}
messages = append(messages, outDel.String())
// Prepare for adding jitter and packet loss
cmdLine := fmt.Sprintf("tc qdisc add dev %s root netem", iface)
doApply := false
if p.delay != 0 {
doApply = true
cmdLine = fmt.Sprintf("%s delay %dms", cmdLine, p.delay)
if p.jitter != 0 {
cmdLine = fmt.Sprintf("%s %dms distribution normal", cmdLine, p.jitter)
}
}
if p.loss != 0 {
doApply = true
cmdLine = fmt.Sprintf("%s loss %f%%", cmdLine, p.loss)
if p.correlation != 0 {
cmdLine = fmt.Sprintf("%s %f%%", cmdLine, p.correlation)
}
}
// If delay != 0, add it
var outAdd bytes.Buffer
var errAdd bytes.Buffer
if doApply {
messages = append(messages, fmt.Sprintf("Policy for interface %s: %dms delay (%dms jitter), %.2f%% PL (%.2f%% correlation)", iface, p.delay, p.jitter, p.loss, p.correlation))
fields := strings.Fields(cmdLine)
cmd = exec.Command(fields[0], fields[1:]...)
cmd.Stdout = &outAdd
cmd.Stderr = &errAdd
if err := cmd.Run(); err != nil {
messages = append(messages, fmt.Sprintf("Error at qdisc add: %+v. The command was: '%s'. stdErr was: %s",
err.Error(),
strings.Join(fields, " "),
errAdd.String()))
}
messages = append(messages, outAdd.String())
}
// Return the output of the qdisc commands
return strings.Join(messages, "\n")
}
// Gets the IFB interface associated to the selected VLAN
func (v *vlan) getIFB() (string, error) {
cmd := exec.Command("tc", "filter", "show", "dev", v.Device, "root")
var outShow bytes.Buffer
cmd.Stdout = &outShow
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error at filter show: %s", err.Error())
}
data := outShow.String()
re := regexp.MustCompile("Egress Redirect to device ifb[0-9]")
match := re.FindString(data)
if match == "" {
return "", fmt.Errorf("Missing IFB device for %s in [%s]", v.Device, data)
}
ifbFields := strings.Fields(match)
return ifbFields[len(ifbFields)-1], nil
}