Skip to content
Snippets Groups Projects
Commit d2ca6ae1 authored by Alex de Cock Buning's avatar Alex de Cock Buning
Browse files

Move source code to ./src:

The source code directory has been moved from ./build to ./src.

This change was made to improve project organization and follow best practices.
parent b9ba9be9
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
[ [
{ {
"name": "<NODE_SWITCH_1>", "name": "test-l2sm-uc3m-polito-1",
"nodeIP": "<IP_SWITCH_1>", "nodeIP": "10.244.0.37",
"neighborNodes": ["<NODE_SWITCH_2>"] "neighborNodes": ["test-l2sm-uc3m-polito-2","test-l2sm-uc3m-polito-3"]
}, },
{ {
"name": "<NODE_SWITCH_2>", "name": "test-l2sm-uc3m-polito-2",
"nodeIP": "<IP_SWITCH_2>", "nodeIP": "10.244.1.64",
"neighborNodes": ["<NODE_SWITCH_1>"] "neighborNodes": ["test-l2sm-uc3m-polito-1","test-l2sm-uc3m-polito-3"]
},
{
"name": "test-l2sm-uc3m-polito-3",
"nodeIP": "10.244.2.33",
"neighborNodes": ["test-l2sm-uc3m-polito-1","test-l2sm-uc3m-polito-2"]
} }
] ]
File moved
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
}
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strings" "strings"
) )
...@@ -23,33 +22,13 @@ type Node struct { ...@@ -23,33 +22,13 @@ type Node struct {
// the second one is the path to the configuration file, in reference to the code. // the second one is the path to the configuration file, in reference to the code.
func main() { func main() {
configDir, vethNumber, nodeName, controllerIP, err := takeArguments() configDir, nodeName, err := takeArguments()
if err != nil { if err != nil {
fmt.Println("Error with the arguments. Error:", err) fmt.Println("Error with the arguments. Error:", err)
return 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
}
err = createVxlans(configDir, nodeName) err = createVxlans(configDir, nodeName)
if err != nil { if err != nil {
...@@ -58,63 +37,21 @@ func main() { ...@@ -58,63 +37,21 @@ func main() {
} }
} }
func takeArguments() (string, int, string, string, error) { func takeArguments() (string, string, error) {
configDir := os.Args[len(os.Args)-1] configDir := os.Args[len(os.Args)-1]
vethNumber := flag.Int("n_veths", 0, "number of pod interfaces that are going to be attached to the switch")
nodeName := flag.String("node_name", "", "name of the node the script is executed in. Required.") nodeName := flag.String("node_name", "", "name of the node the script is executed in. Required.")
controllerIP := flag.String("controller_ip", "", "ip where the SDN controller is listening using the OpenFlow13 protocol. Required")
flag.Parse() flag.Parse()
switch { switch {
case *nodeName == "": case *nodeName == "":
return "", 0, "", "", errors.New("Node name is not defined") return "", "", errors.New("node name is not defined")
case configDir == "": case configDir == "":
return "", 0, "", "", errors.New("Config directory is not defined") return "", "", errors.New("config directory is not defined")
case *controllerIP == "":
return "", 0, "", "", errors.New("Controller IP is not defined")
}
return configDir, *vethNumber, *nodeName, *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("Couldnt set brtun messaing protocol to OpenFlow13")
} }
target := fmt.Sprintf("tcp:%s:6633", controllerIP) return configDir, *nodeName, nil
err = exec.Command("ovs-vsctl", "set-controller", "brtun", target).Run()
if err != nil {
return errors.New("Could not connect to controller")
}
return nil
} }
func createVxlans(configDir, nodeName string) error { func createVxlans(configDir, nodeName string) error {
...@@ -151,16 +88,16 @@ func createVxlans(configDir, nodeName string) error { ...@@ -151,16 +88,16 @@ func createVxlans(configDir, nodeName string) error {
"set", "interface", "set", "interface",
fmt.Sprintf("vxlan%d", vxlanNumber), fmt.Sprintf("vxlan%d", vxlanNumber),
"type=vxlan", "type=vxlan",
fmt.Sprintf("options:key=flow"), "options:key=flow",
fmt.Sprintf("options:remote_ip=%s", neighborIP), fmt.Sprintf("options:remote_ip=%s", neighborIP),
fmt.Sprintf("options:local_ip=%s", nodeIP), fmt.Sprintf("options:local_ip=%s", nodeIP),
"options:dst_port=7000", "options:dst_port=7000",
} }
_, err := exec.Command("ovs-vsctl", commandArgs...).Output() _, err := exec.Command("ovs-vsctl", commandArgs...).Output()
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("Could not create vxlan between node %s and node %s.", node.Name, neighbor)) return fmt.Errorf("could not create vxlan between node %s and node %s", node.Name, neighbor)
} else { } else {
fmt.Println(fmt.Sprintf("Created vxlan between node %s and node %s.", node.Name, neighbor)) fmt.Printf("Created vxlan between node %s and node %s.\n", node.Name, neighbor)
} }
} }
vxlanNumber++ vxlanNumber++
......
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment