From 6183488f89ad745e5151e0e3ad83a385069323d3 Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Mon, 26 Sep 2022 13:56:13 +0100 Subject: [PATCH 1/3] Support hwaddr config option to set bridge MAC --- ovs/vswitch.go | 8 ++++++++ ovs/vswitch_test.go | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ovs/vswitch.go b/ovs/vswitch.go index 181ced2..2d7e7a7 100644 --- a/ovs/vswitch.go +++ b/ovs/vswitch.go @@ -17,6 +17,7 @@ package ovs import ( "encoding/json" "fmt" + "net" "strings" ) @@ -202,6 +203,9 @@ func (v *VSwitchSetService) Bridge(bridge string, options BridgeOptions) error { type BridgeOptions struct { // Protocols specifies the OpenFlow protocols the bridge should use. Protocols []string + + //HWAddr specifies the MAC address to be assigned to the bridge. + HWAddr string } // slice creates a string slice containing any non-zero option values from the @@ -213,6 +217,10 @@ func (o BridgeOptions) slice() []string { s = append(s, fmt.Sprintf("protocols=%s", strings.Join(o.Protocols, ","))) } + if hw, err := net.ParseMAC(o.HWAddr); err == nil { + s = append(s, fmt.Sprintf("other-config:hwaddr=%s", hw.String())) + } + return s } diff --git a/ovs/vswitch_test.go b/ovs/vswitch_test.go index 8298537..94a50d6 100644 --- a/ovs/vswitch_test.go +++ b/ovs/vswitch_test.go @@ -461,7 +461,7 @@ func TestClientVSwitchGetBridgeProtocolsOK(t *testing.T) { } } -func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) { +func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { const bridge = "br0" protocols := []string{ ProtocolOpenFlow10, @@ -471,6 +471,7 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) { ProtocolOpenFlow14, ProtocolOpenFlow15, } + hwaddr := "55:84:a3:2f:d3:20" c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) { if want, got := "ovs-vsctl", cmd; want != got { @@ -484,6 +485,7 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) { "bridge", bridge, fmt.Sprintf("protocols=%s", strings.Join(protocols, ",")), + fmt.Sprintf("other-config:hwaddr=%s", hwaddr), } if want, got := wantArgs, args; !reflect.DeepEqual(want, got) { t.Fatalf("incorrect arguments\n- want: %v\n- got: %v", @@ -495,6 +497,7 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) { err := c.VSwitch.Set.Bridge(bridge, BridgeOptions{ Protocols: protocols, + HWAddr: hwaddr, }) if err != nil { t.Fatalf("unexpected error for Client.VSwitch.Set.Bridge: %v", err) From 1a494c8e962b51c5a3a14a41bdb3173fad58086b Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Mon, 26 Sep 2022 14:24:10 +0100 Subject: [PATCH 2/3] Support bridge STP configuration option --- ovs/vswitch.go | 11 ++++++++++- ovs/vswitch_test.go | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ovs/vswitch.go b/ovs/vswitch.go index 2d7e7a7..b55720e 100644 --- a/ovs/vswitch.go +++ b/ovs/vswitch.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" "net" + "strconv" "strings" ) @@ -204,8 +205,12 @@ type BridgeOptions struct { // Protocols specifies the OpenFlow protocols the bridge should use. Protocols []string - //HWAddr specifies the MAC address to be assigned to the bridge. + // HWAddr specifies the MAC address to be assigned to the bridge. HWAddr string + + // STP defines if the spanning tree protocol is to be enabled on the bridge. A + // nil value here will not change the STP config of the bridge. + STP *bool } // slice creates a string slice containing any non-zero option values from the @@ -221,6 +226,10 @@ func (o BridgeOptions) slice() []string { s = append(s, fmt.Sprintf("other-config:hwaddr=%s", hw.String())) } + if o.STP != nil { + s = append(s, fmt.Sprintf("stp_enable=%s", strconv.FormatBool(*o.STP))) + } + return s } diff --git a/ovs/vswitch_test.go b/ovs/vswitch_test.go index 94a50d6..4393546 100644 --- a/ovs/vswitch_test.go +++ b/ovs/vswitch_test.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "reflect" + "strconv" "strings" "testing" ) @@ -472,6 +473,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { ProtocolOpenFlow15, } hwaddr := "55:84:a3:2f:d3:20" + stp := true c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) { if want, got := "ovs-vsctl", cmd; want != got { @@ -486,6 +488,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { bridge, fmt.Sprintf("protocols=%s", strings.Join(protocols, ",")), fmt.Sprintf("other-config:hwaddr=%s", hwaddr), + fmt.Sprintf("stp_enable=%s", strconv.FormatBool(stp)), } if want, got := wantArgs, args; !reflect.DeepEqual(want, got) { t.Fatalf("incorrect arguments\n- want: %v\n- got: %v", @@ -498,6 +501,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { err := c.VSwitch.Set.Bridge(bridge, BridgeOptions{ Protocols: protocols, HWAddr: hwaddr, + STP: &stp, }) if err != nil { t.Fatalf("unexpected error for Client.VSwitch.Set.Bridge: %v", err) From 64bb3e0cec0eb2b082e3e1694a0c3359961aa73c Mon Sep 17 00:00:00 2001 From: Will Fantom Date: Mon, 26 Sep 2022 14:36:55 +0100 Subject: [PATCH 3/3] Support arbitrary bridge config options --- ovs/vswitch.go | 6 +++++- ovs/vswitch_test.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ovs/vswitch.go b/ovs/vswitch.go index b55720e..2c3ba22 100644 --- a/ovs/vswitch.go +++ b/ovs/vswitch.go @@ -211,6 +211,10 @@ type BridgeOptions struct { // STP defines if the spanning tree protocol is to be enabled on the bridge. A // nil value here will not change the STP config of the bridge. STP *bool + + // Other defines bridge config options not otherwise present in this package + // but available in ovs such as rstp options (e.g. `rstp_enable=false`). + Other []string } // slice creates a string slice containing any non-zero option values from the @@ -230,7 +234,7 @@ func (o BridgeOptions) slice() []string { s = append(s, fmt.Sprintf("stp_enable=%s", strconv.FormatBool(*o.STP))) } - return s + return append(s, o.Other...) } // Interface sets configuration for an interface using the values from an diff --git a/ovs/vswitch_test.go b/ovs/vswitch_test.go index 4393546..0c81d4a 100644 --- a/ovs/vswitch_test.go +++ b/ovs/vswitch_test.go @@ -474,6 +474,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { } hwaddr := "55:84:a3:2f:d3:20" stp := true + other := []string{"rstp_enable=false"} c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) { if want, got := "ovs-vsctl", cmd; want != got { @@ -490,6 +491,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { fmt.Sprintf("other-config:hwaddr=%s", hwaddr), fmt.Sprintf("stp_enable=%s", strconv.FormatBool(stp)), } + wantArgs = append(wantArgs, other...) if want, got := wantArgs, args; !reflect.DeepEqual(want, got) { t.Fatalf("incorrect arguments\n- want: %v\n- got: %v", want, got) @@ -502,6 +504,7 @@ func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) { Protocols: protocols, HWAddr: hwaddr, STP: &stp, + Other: other, }) if err != nil { t.Fatalf("unexpected error for Client.VSwitch.Set.Bridge: %v", err)