From 0be8668835fb5668ece3a1b63f965ced6b312908 Mon Sep 17 00:00:00 2001 From: Alejandro Tjaarda <alexdecb@yahoo.es> Date: Sun, 29 Sep 2024 03:58:42 +0000 Subject: [PATCH] switches: bug fix with bridge names Autogenerated bridge names would sometimes not work at all, just because the names weren't compliant. Made a digest function to make unique bridge names but compliant with the linux interfaces api --- src/switch/cmd/ned-server/main.go | 20 +++++++++----------- src/switch/pkg/utils/utils.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/switch/pkg/utils/utils.go diff --git a/src/switch/cmd/ned-server/main.go b/src/switch/cmd/ned-server/main.go index 490ac0f..8ebb070 100644 --- a/src/switch/cmd/ned-server/main.go +++ b/src/switch/cmd/ned-server/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "errors" "flag" "fmt" "log" @@ -18,6 +17,11 @@ import ( "l2sm.local/ovs-switch/internal/inits" "l2sm.local/ovs-switch/pkg/nedpb" "l2sm.local/ovs-switch/pkg/ovs" + "l2sm.local/ovs-switch/pkg/utils" +) + +const ( + DEFAULT_CONFIG_PATH = "/etc/l2sm" ) // server is used to implement nedpb.VxlanServiceServer @@ -45,7 +49,8 @@ func main() { fmt.Println("Initializing switch, connected to controller: ", settings.ControllerIP) - bridge, err := inits.InitializeSwitch(settings.NedName, settings.ControllerIP) + nedBridgeName, _ := utils.GenerateBridgeName(settings.NedName) + bridge, err := inits.InitializeSwitch(nedBridgeName, settings.ControllerIP) if err != nil { log.Fatalf("error initializing ned: %v", err) } @@ -158,17 +163,10 @@ func AddInterfaceToBridge(bridgeName string) (string, error) { func takeArguments() (string, string, error) { - configDir := flag.String("config_dir", "", "directory where the ned settings are specified. Required") - neighborsDir := flag.String("neighbors_dir", "", "directory where the ned's neighbors are specified. Required") + configDir := flag.String("config_dir", fmt.Sprintf("%s/config.json", DEFAULT_CONFIG_PATH), "directory where the ned settings are specified. Required") + neighborsDir := flag.String("neighbors_dir", fmt.Sprintf("%s/neighbors.json", DEFAULT_CONFIG_PATH), "directory where the ned's neighbors are specified. Required") flag.Parse() - switch { - case *configDir == "": - return "", "", errors.New("config directory is not defined") - case *neighborsDir == "": - return "", "", errors.New("provider name is not defined") - } - return *configDir, *neighborsDir, nil } diff --git a/src/switch/pkg/utils/utils.go b/src/switch/pkg/utils/utils.go new file mode 100644 index 0000000..f897b0e --- /dev/null +++ b/src/switch/pkg/utils/utils.go @@ -0,0 +1,24 @@ +package utils + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" +) + +func GenerateBridgeName(uniqueIdentifier string) (string, error) { + // Create a SHA-256 hash of the input string + hash := sha256.New() + _, err := hash.Write([]byte(uniqueIdentifier)) + if err != nil { + return "", err + } + // Get the full hashed value in hex format + fullHash := hex.EncodeToString(hash.Sum(nil)) + + // Truncate to the first 5 characters of the hash + digestedName := fullHash[:5] + + // Return the formatted bridge name + return fmt.Sprintf("br-%s", digestedName), nil +} -- GitLab