Skip to content
Snippets Groups Projects
Commit 11c2db2d authored by Alejandro Tjaarda's avatar Alejandro Tjaarda
Browse files

switch: fixed bug with the bridge name that didnt comply with ip rules

parent 6cad92a4
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
...@@ -49,8 +49,7 @@ func main() { ...@@ -49,8 +49,7 @@ func main() {
fmt.Println("Initializing switch, connected to controller: ", settings.ControllerIP) fmt.Println("Initializing switch, connected to controller: ", settings.ControllerIP)
nedBridgeName, _ := utils.GenerateBridgeName(settings.NedName) bridge, err := inits.InitializeSwitch(settings.NedName, settings.ControllerIP)
bridge, err := inits.InitializeSwitch(nedBridgeName, settings.ControllerIP)
if err != nil { if err != nil {
log.Fatalf("error initializing ned: %v", err) log.Fatalf("error initializing ned: %v", err)
} }
...@@ -112,6 +111,12 @@ func (s *server) AttachInterface(ctx context.Context, req *nedpb.AttachInterface ...@@ -112,6 +111,12 @@ func (s *server) AttachInterface(ctx context.Context, req *nedpb.AttachInterface
// Create a new interface and attach it to the bridge // Create a new interface and attach it to the bridge
newPort, err := AddInterfaceToBridge(interfaceName) newPort, err := AddInterfaceToBridge(interfaceName)
if err != nil {
return nil, fmt.Errorf("failed to create interface: %v", err)
}
err = s.Bridge.AddPort(newPort)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to add interface to bridge: %v", err) return nil, fmt.Errorf("failed to add interface to bridge: %v", err)
} }
...@@ -137,14 +142,13 @@ func (s *server) AttachInterface(ctx context.Context, req *nedpb.AttachInterface ...@@ -137,14 +142,13 @@ func (s *server) AttachInterface(ctx context.Context, req *nedpb.AttachInterface
func AddInterfaceToBridge(bridgeName string) (string, error) { func AddInterfaceToBridge(bridgeName string) (string, error) {
// Generate unique interface names // Generate unique interface names
timestamp := time.Now().UnixNano() timestamp := time.Now().UnixNano()
vethName := fmt.Sprintf("veth%d", timestamp) vethName, _ := utils.GenerateInterfaceName("veth", fmt.Sprintf("%s%d", bridgeName, timestamp))
peerName := fmt.Sprintf("vpeer%d", timestamp) peerName, _ := utils.GenerateInterfaceName("vpeer", fmt.Sprintf("%s%d", bridgeName, timestamp))
// Create the veth pair // Create the veth pair
if err := exec.Command("ip", "link", "add", vethName, "type", "veth", "peer", "name", peerName).Run(); err != nil { if err := exec.Command("ip", "link", "add", vethName, "type", "veth", "peer", "name", peerName).Run(); err != nil {
return "", fmt.Errorf("failed to create veth pair: %v", err) return "", fmt.Errorf("failed to create veth pair: %v", err)
} }
// Set both interfaces up // Set both interfaces up
if err := exec.Command("ip", "link", "set", vethName, "up").Run(); err != nil { if err := exec.Command("ip", "link", "set", vethName, "up").Run(); err != nil {
return "", fmt.Errorf("failed to set %s up: %v", vethName, err) return "", fmt.Errorf("failed to set %s up: %v", vethName, err)
...@@ -153,12 +157,11 @@ func AddInterfaceToBridge(bridgeName string) (string, error) { ...@@ -153,12 +157,11 @@ func AddInterfaceToBridge(bridgeName string) (string, error) {
return "", fmt.Errorf("failed to set %s up: %v", peerName, err) return "", fmt.Errorf("failed to set %s up: %v", peerName, err)
} }
// Add one end to the Linux bridge if err := exec.Command("ip", "link", "set", peerName, "master", bridgeName).Run(); err != nil {
if err := exec.Command("ip", "link", "set", vethName, "master", bridgeName).Run(); err != nil { return "", fmt.Errorf("failed to add %s to bridge %s: %v", peerName, bridgeName, err)
return "", fmt.Errorf("failed to add %s to bridge %s: %v", vethName, bridgeName, err)
} }
return peerName, nil return vethName, nil
} }
func takeArguments() (string, string, error) { func takeArguments() (string, string, error) {
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"fmt" "fmt"
) )
func GenerateBridgeName(uniqueIdentifier string) (string, error) { func GenerateInterfaceName(prefix, uniqueIdentifier string) (string, error) {
// Create a SHA-256 hash of the input string // Create a SHA-256 hash of the input string
hash := sha256.New() hash := sha256.New()
_, err := hash.Write([]byte(uniqueIdentifier)) _, err := hash.Write([]byte(uniqueIdentifier))
...@@ -20,5 +20,5 @@ func GenerateBridgeName(uniqueIdentifier string) (string, error) { ...@@ -20,5 +20,5 @@ func GenerateBridgeName(uniqueIdentifier string) (string, error) {
digestedName := fullHash[:5] digestedName := fullHash[:5]
// Return the formatted bridge name // Return the formatted bridge name
return fmt.Sprintf("br-%s", digestedName), nil return fmt.Sprintf("%s%s", prefix, digestedName), nil
} }
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