Skip to content

Better support for arbitrary bridge config options #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion ovs/vswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package ovs
import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
)

Expand Down Expand Up @@ -202,6 +204,17 @@ 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

// 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
Expand All @@ -213,7 +226,15 @@ func (o BridgeOptions) slice() []string {
s = append(s, fmt.Sprintf("protocols=%s", strings.Join(o.Protocols, ",")))
}

return s
if hw, err := net.ParseMAC(o.HWAddr); err == nil {
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 append(s, o.Other...)
}

// Interface sets configuration for an interface using the values from an
Expand Down
12 changes: 11 additions & 1 deletion ovs/vswitch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
)
Expand Down Expand Up @@ -461,7 +462,7 @@ func TestClientVSwitchGetBridgeProtocolsOK(t *testing.T) {
}
}

func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) {
func TestClientVSwitchSetBridgeOptionsOK(t *testing.T) {
const bridge = "br0"
protocols := []string{
ProtocolOpenFlow10,
Expand All @@ -471,6 +472,9 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) {
ProtocolOpenFlow14,
ProtocolOpenFlow15,
}
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 {
Expand All @@ -484,7 +488,10 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) {
"bridge",
bridge,
fmt.Sprintf("protocols=%s", strings.Join(protocols, ",")),
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)
Expand All @@ -495,6 +502,9 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) {

err := c.VSwitch.Set.Bridge(bridge, BridgeOptions{
Protocols: protocols,
HWAddr: hwaddr,
STP: &stp,
Other: other,
})
if err != nil {
t.Fatalf("unexpected error for Client.VSwitch.Set.Bridge: %v", err)
Expand Down