Newer
Older
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
package main
import (
"errors"
"flag"
"fmt"
"os/exec"
"regexp"
)
// Script that takes two required arguments:
// the first one is the name in the cluster of the node where the script is running
// the second one is the path to the configuration file, in reference to the code.
func main() {
vethNumber, controllerIP, err := takeArguments()
if err != nil {
fmt.Println("Error with the arguments. Error:", err)
return
}
fmt.Println("Initializing switch, connected to controller: ", controllerIP)
err = initializeSwitch(controllerIP)
if err != nil {
fmt.Println("Could not initialize switch. Error:", err)
return
}
fmt.Println("Switch initialized and connected to the controller.")
// Set all virtual interfaces up, and connect them to the tunnel bridge:
for i := 1; i <= vethNumber; i++ {
veth := fmt.Sprintf("net%d", i)
cmd := exec.Command("ip", "link", "set", veth, "up") // i.e: ip link set veth1 up
if err := cmd.Run(); err != nil {
fmt.Println("Error:", err)
}
exec.Command("ovs-vsctl", "add-port", "brtun", veth).Run() // i.e: ovs-vsctl add-port brtun veth1
}
}
func takeArguments() (int, string, error) {
vethNumber := flag.Int("n_veths", 0, "number of pod interfaces that are going to be attached to the switch")
controllerIP := flag.String("controller_ip", "", "ip where the SDN controller is listening using the OpenFlow13 protocol. Required")
flag.Parse()
switch {
case *controllerIP == "":
return 0, "", errors.New("controller IP is not defined")
}
return *vethNumber, *controllerIP, nil
}
func initializeSwitch(controllerIP string) error {
re := regexp.MustCompile(`\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b`)
if !re.MatchString(controllerIP) {
out, _ := exec.Command("host", controllerIP).Output()
controllerIP = re.FindString(string(out))
}
var err error
err = exec.Command("ovs-vsctl", "add-br", "brtun").Run()
if err != nil {
return errors.New("could not create brtun interface")
}
err = exec.Command("ip", "link", "set", "brtun", "up").Run()
if err != nil {
return errors.New("could not set brtun interface up")
}
err = exec.Command("ovs-vsctl", "set", "bridge", "brtun", "protocols=OpenFlow13").Run()
if err != nil {
return errors.New("could not set brtun messaing protocol to OpenFlow13")
}
target := fmt.Sprintf("tcp:%s:6633", controllerIP)
err = exec.Command("ovs-vsctl", "set-controller", "brtun", target).Run()
if err != nil {
return errors.New("could not connect to controller")
}
return nil
}