-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
422 lines (369 loc) · 11.4 KB
/
main.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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
yaml "gopkg.in/yaml.v2"
)
type tcParams struct {
Delay string `yaml:"delay"`
Loss string `yaml:"loss"`
Duplicate string `yaml:"duplicate"`
Rate string `yaml:"rate"`
Corrupt string `yaml:"corrupt"`
Reorder string `yaml:"reorder"`
}
type node struct {
IP string `yaml:"ip"`
CMD string `yaml:"cmd"`
}
type group struct {
Name string `yaml:"name"`
Nodes []node `yaml:"nodes"`
Delay string `yaml:"delay"`
Loss string `yaml:"loss"`
Duplicate string `yaml:"duplicate"`
Rate string `yaml:"rate"`
Corrupt string `yaml:"corrupt"`
Reorder string `yaml:"reorder"`
}
type network struct {
Groups []string `yaml:"groups"`
Delay string `yaml:"delay"`
Loss string `yaml:"loss"`
Duplicate string `yaml:"duplicate"`
Rate string `yaml:"rate"`
Corrupt string `yaml:"corrupt"`
Reorder string `yaml:"reorder"`
}
type root struct {
Group []group `yaml:"group"`
Network []network `yaml:"network"`
}
func keyInArray(key string, array []string) bool {
for _, data := range array {
if data == key {
return true
}
}
return false
}
func paramsCount(str string) int {
var params []string
str = strings.TrimSpace(str)
params = strings.Split(str, " ")
return len(params)
}
func processOneNode(node *node, groupName string, r root, nodemap map[string]bool) []string {
var tcRules []string
//add init rules
tcRules = append(tcRules, "#!/bin/sh\n")
tcRules = append(tcRules, "#"+node.IP)
tcRules = append(tcRules, "tc qdisc del dev eth0 root")
tcRules = append(tcRules, "tc qdisc add dev eth0 root handle 1: htb default 2")
tcRules = append(tcRules, "tc class add dev eth0 parent 1: classid 1:2 htb rate 10gbps")
tcRules = append(tcRules, "tc qdisc add dev eth0 parent 1:2 handle 2: sfq")
tcIndex := 10
//3. build tc tree for this node
for _, group := range r.Group {
if group.Name == groupName { //local group
rule := "tc class add dev eth0 parent 1: classid 1:" + strconv.Itoa(tcIndex) + " htb"
if group.Rate != "" {
rule = rule + " rate " + group.Rate
} else {
rule = rule + " rate 10gbps"
}
tcRules = append(tcRules, rule)
// 3.1 parse other node in same group
rule = "tc qdisc add dev eth0 parent 1:" + strconv.Itoa(tcIndex) + " handle " + strconv.Itoa(tcIndex) + ": netem"
if group.Delay != "" {
if paramsCount(group.Delay) > 1 {
rule = rule + " delay " + group.Delay + " distribution normal"
} else {
rule = rule + " delay " + group.Delay
}
}
if group.Corrupt != "" {
rule = rule + " corrupt " + group.Corrupt
}
if group.Duplicate != "" {
rule = rule + " duplicate " + group.Duplicate
}
if group.Loss != "" {
rule = rule + " loss " + group.Loss
}
if group.Reorder != "" {
rule = rule + " reorder " + group.Reorder
}
tcRules = append(tcRules, rule)
for _, otherNode := range group.Nodes {
if otherNode.IP == node.IP {
continue
}
//find if has pair in node-othernode
if pair, ok := nodemap[node.IP+otherNode.IP]; ok && pair {
continue
}
//3.2 build tc leaf for inner-group
rule = "tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst " + otherNode.IP + " flowid 1:" + strconv.Itoa(tcIndex)
tcRules = append(tcRules, rule)
//set pair in node-othernode
nodemap[node.IP+otherNode.IP] = true
nodemap[otherNode.IP+node.IP] = true
}
} else { //other group
//3.3 parse node in other group
// find network first
for _, network := range r.Network {
if keyInArray(group.Name, network.Groups) && keyInArray(groupName, network.Groups) {
rule := "tc class add dev eth0 parent 1: classid 1:" + strconv.Itoa(tcIndex) + " htb"
if network.Rate != "" {
rule = rule + " rate " + network.Rate
} else {
rule = rule + " rate 10gbps"
}
tcRules = append(tcRules, rule)
rule = "tc qdisc add dev eth0 parent 1:" + strconv.Itoa(tcIndex) + " handle " + strconv.Itoa(tcIndex) + ": netem"
if network.Delay != "" {
if paramsCount(network.Delay) > 1 {
rule = rule + " delay " + network.Delay + " distribution normal"
} else {
rule = rule + " delay " + network.Delay
}
}
if network.Corrupt != "" {
rule = rule + " corrupt " + network.Corrupt
}
if network.Duplicate != "" {
rule = rule + " duplicate " + network.Duplicate
}
if network.Loss != "" {
rule = rule + " loss " + network.Loss
}
if network.Reorder != "" {
rule = rule + " reorder " + network.Reorder
}
tcRules = append(tcRules, rule)
}
}
//3.4 build tc leaf for group-connection
for _, otherNode := range group.Nodes {
//find if has pair in node-othernode
if pair, ok := nodemap[node.IP+otherNode.IP]; ok && pair {
continue
}
rule := "tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst " + otherNode.IP + " flowid 1:" + strconv.Itoa(tcIndex)
tcRules = append(tcRules, rule)
//set pair in node-othernode
nodemap[node.IP+otherNode.IP] = true
nodemap[otherNode.IP+node.IP] = true
}
}
tcIndex = tcIndex + 1
}
tcRules = append(tcRules, "cat /scripts/bashrc >> ~/.bashrc")
tcRules = append(tcRules, ". ~/.bashrc")
tcRules = append(tcRules, node.CMD)
tcRules = append(tcRules, "tail -f /dev/null")
return tcRules
}
func printRules(rules []string) {
fmt.Println()
for _, rule := range rules {
fmt.Println(rule)
}
fmt.Println()
}
func printTcScript(rules []string, node *node, groupName string) {
ip := strings.Split(node.IP, "/")[0]
fmt.Println(ip)
var data []byte
rulestr := strings.Join(rules, "\n")
data = []byte(rulestr + "\n")
// fmt.Println(rulestr)
err := ioutil.WriteFile("scripts/"+groupName+ip+".sh", data, 0777)
if err != nil {
fmt.Println(err)
}
}
func printDockerScript(r root) {
launchFile, err := os.OpenFile(
"scripts/launch.sh",
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0777,
)
if err != nil {
fmt.Println(err)
}
defer launchFile.Close()
cleanFile, err := os.OpenFile(
"scripts/clean.sh",
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0777,
)
if err != nil {
fmt.Println(err)
}
defer cleanFile.Close()
var launchFileData, cleanFileData []string
launchFileData = append(launchFileData, "#!/bin/bash -x\n")
launchFileData = append(launchFileData, "docker network create --subnet=10.250.0.1/16 CovenantSQL_testnet")
cleanFileData = append(cleanFileData, "#!/bin/bash -x\n")
for _, group := range r.Group {
for _, node := range group.Nodes {
ip := strings.Split(node.IP, "/")[0]
launchFileData = append(launchFileData, "echo starting "+group.Name+ip)
launchFileData = append(launchFileData, "docker run -d --net CovenantSQL_testnet --ip "+ip+" -h "+group.Name+ip+
" -v $DIR/scripts:/scripts --cap-add=NET_ADMIN --name "+group.Name+ip+" gnte /scripts/"+group.Name+ip+".sh")
cleanFileData = append(cleanFileData, "docker rm -f "+group.Name+ip)
}
}
cleanFileData = append(cleanFileData, "docker network rm CovenantSQL_testnet")
// run dot convertion
// dot -Tpng graph.gv -o graph.png
launchFileData = append(launchFileData, "docker run --rm -v $DIR/scripts:/scripts gnte dot -Tpng scripts/graph.gv -o scripts/graph.png")
launchFileData = append(launchFileData, "mv -f $DIR/scripts/graph.png $DIR/graph.png")
launchFileByte := []byte(strings.Join(launchFileData, "\n") + "\n")
_, err = launchFile.Write(launchFileByte)
if err != nil {
fmt.Println(err)
}
cleanFileByte := []byte(strings.Join(cleanFileData, "\n") + "\n")
_, err = cleanFile.Write(cleanFileByte)
if err != nil {
fmt.Println(err)
}
}
func printGraphScript(r root) {
gvFile, err := os.OpenFile(
"scripts/graph.gv",
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0666,
)
if err != nil {
fmt.Println(err)
}
defer gvFile.Close()
var gvFileData []string
gvFileData = append(gvFileData, "digraph G {")
gvFileData = append(gvFileData, " compound=true;")
for _, group := range r.Group {
gvFileData = append(gvFileData, " subgraph cluster_"+group.Name+" {")
gvFileData = append(gvFileData, " label = "+group.Name+";")
gvFileData = append(gvFileData, " style = rounded;")
for i := 0; i < len(group.Nodes); i++ {
for j := i + 1; j < len(group.Nodes); j++ {
//"10.2.1.1/16" -> "10.3.1.1/20" [arrowhead=none, arrowtail=none, label="delay\n 100ms ±10ms 30%"];
arrowinfo := " \"" + group.Nodes[i].IP + "\" -> \"" + group.Nodes[j].IP +
"\" [arrowhead=none, arrowtail=none, label=\""
if group.Delay != "" {
arrowinfo = arrowinfo + "delay " + group.Delay + `\n`
}
if group.Corrupt != "" {
arrowinfo = arrowinfo + "corrupt" + group.Corrupt + `\n`
}
if group.Duplicate != "" {
arrowinfo = arrowinfo + "duplicate " + group.Duplicate + `\n`
}
if group.Loss != "" {
arrowinfo = arrowinfo + "loss " + group.Loss + `\n`
}
if group.Reorder != "" {
arrowinfo = arrowinfo + "reorder " + group.Reorder + `\n`
}
if group.Rate != "" {
arrowinfo = arrowinfo + "rate " + group.Rate + `\n`
}
arrowinfo = arrowinfo + "\"];"
gvFileData = append(gvFileData, arrowinfo)
}
}
gvFileData = append(gvFileData, " }")
}
for _, network := range r.Network {
//parse two group pair
for i := 0; i < len(network.Groups); i++ {
for j := i + 1; j < len(network.Groups); j++ {
var groupNodei, groupNodej string
// get group ip
for _, group := range r.Group {
if group.Name == network.Groups[i] {
groupNodei = group.Nodes[0].IP
} else if group.Name == network.Groups[j] {
groupNodej = group.Nodes[0].IP
}
}
arrowinfo := " \"" + groupNodei + "\" -> \"" + groupNodej +
"\"\n [ltail=cluster_" + network.Groups[i] + ", lhead=cluster_" + network.Groups[j] +
", arrowhead=none, arrowtail=none,\n label=\""
if network.Delay != "" {
arrowinfo = arrowinfo + "delay " + network.Delay + `\n`
}
if network.Corrupt != "" {
arrowinfo = arrowinfo + "corrupt" + network.Corrupt + `\n`
}
if network.Duplicate != "" {
arrowinfo = arrowinfo + "duplicate " + network.Duplicate + `\n`
}
if network.Loss != "" {
arrowinfo = arrowinfo + "loss " + network.Loss + `\n`
}
if network.Reorder != "" {
arrowinfo = arrowinfo + "reorder " + network.Reorder + `\n`
}
if network.Rate != "" {
arrowinfo = arrowinfo + "rate " + network.Rate + `\n`
}
arrowinfo = arrowinfo + "\"];"
gvFileData = append(gvFileData, arrowinfo)
}
}
}
gvFileData = append(gvFileData, "}")
gvFileByte := []byte(strings.Join(gvFileData, "\n") + "\n")
_, err = gvFile.Write(gvFileByte)
if err != nil {
fmt.Println(err)
}
}
/*
1. read yaml from file
2. select one node
3. build tc tree for this node
3.1 parse other node in same group
3.2 build tc leaf for inner-group
3.3 parse node in other group
3.4 build tc leaf for group-connection
4. print tc tree
*/
func main() {
r := root{}
//TODO 1. read yaml from specific file
configPath := ""
if len(os.Args) > 1 {
configPath = os.Args[1]
} else {
configPath = "example.yaml"
}
data, err := ioutil.ReadFile(configPath)
if err != nil {
fmt.Println(err)
}
err = yaml.Unmarshal(data, &r)
if err != nil {
fmt.Println(err)
}
nodemap := make(map[string]bool)
//2. select one node
for _, group := range r.Group {
for _, node := range group.Nodes {
tcRules := processOneNode(&node, group.Name, r, nodemap)
//4. print tc tree
printTcScript(tcRules, &node, group.Name)
}
}
printDockerScript(r)
printGraphScript(r)
}