From 04d369c608f87e72613bae7c12753b4017510dff Mon Sep 17 00:00:00 2001 From: Alejandro Tjaarda <alexdecb@yahoo.es> Date: Fri, 27 Sep 2024 19:00:00 +0000 Subject: [PATCH] switch: improved error logging and added get port number method --- src/switch/pkg/ovs/vsctl.go | 40 +++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/switch/pkg/ovs/vsctl.go b/src/switch/pkg/ovs/vsctl.go index f3f1283..b65c307 100644 --- a/src/switch/pkg/ovs/vsctl.go +++ b/src/switch/pkg/ovs/vsctl.go @@ -2,9 +2,9 @@ package ovs import ( "bytes" - "errors" "fmt" "os/exec" + "strconv" "strings" ) @@ -45,32 +45,34 @@ func NewBridge(bridgeConf Bridge) (Bridge, error) { bridge := Bridge{} - err = exec.Command("ovs-vsctl", "add-br", bridgeConf.Name).Run() + cmd := exec.Command("ovs-vsctl", "add-br", bridgeConf.Name) + output, err := cmd.CombinedOutput() if err != nil { - return bridge, errors.New("could not create brtun interface") + return bridge, fmt.Errorf("could not create %s interface: %v\nOutput: %s", bridgeConf.Name, err, output) } bridge.Name = bridgeConf.Name - err = exec.Command("ip", "link", "set", bridge.Name, "up").Run() - + cmd = exec.Command("ip", "link", "set", bridge.Name, "up") + output, err = cmd.CombinedOutput() if err != nil { - return bridge, errors.New("could not set brtun interface up") + return bridge, fmt.Errorf("could not set %s interface up: %v\nOutput: %s", bridgeConf.Name, err, output) } if bridgeConf.DatapathId != "" { err := exec.Command("ovs-vsctl", "set", "bridge", bridge.Name, fmt.Sprintf("other-config:datapath-id=%s", bridgeConf.DatapathId)).Run() if err != nil { - return bridge, errors.New("could not set custom datapath id") + return bridge, fmt.Errorf("could not set custom datapath id: %v", err) } } protocolString := fmt.Sprintf("protocols=%s", bridgeConf.Protocol) - err = exec.Command("ovs-vsctl", "set", "bridge", "brtun", protocolString).Run() + err = exec.Command("ovs-vsctl", "set", "bridge", bridge.Name, protocolString).Run() if err != nil { - return bridge, errors.New("could not set brtun messaging protocol to OpenFlow13") + return bridge, fmt.Errorf("could not set %s messaging protocol to OpenFlow13: %v", bridgeConf.Name, err) + } bridge.Protocol = bridgeConf.Protocol @@ -78,7 +80,8 @@ func NewBridge(bridgeConf Bridge) (Bridge, error) { err = exec.Command("ovs-vsctl", "set-controller", bridge.Name, bridgeConf.Controller).Run() if err != nil { - return bridge, errors.New("could not connect to controller") + return bridge, fmt.Errorf("could not connect to controller: %v", err) + } bridge.Controller = bridgeConf.Name @@ -146,3 +149,20 @@ func (bridge *Bridge) getPorts() error { return nil } + +func (bridge *Bridge) GetPortNumber(portName string) (int64, error) { + cmd := exec.Command("ovs-vsctl", "get", "Interface", portName, "ofport") + var out bytes.Buffer + cmd.Stdout = &out + if err := cmd.Run(); err != nil { + return 0, fmt.Errorf("failed to get port number for %s: %v", portName, err) + } + + ofportStr := strings.TrimSpace(out.String()) + ofport, err := strconv.ParseInt(ofportStr, 10, 64) + if err != nil { + return 0, fmt.Errorf("failed to parse port number: %v", err) + } + + return ofport, nil +} -- GitLab