Skip to content

Commit

Permalink
Support STP parameters in bridge
Browse files Browse the repository at this point in the history
Added support for some basic spanning tree protocol configuration
parameters on the bridge device.

* vlan protocol
* stp state
* forward delay
* max age
  • Loading branch information
Daniel Noland committed Apr 2, 2021
1 parent f5de759 commit 3e8eb11
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 1 deletion.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
golang.org/x/sys v0.0.0-20200217220822-9197077df867 h1:JoRuNIf+rpHl+VhScRQQvzbHed86tKkqwPMV34T8myw=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 h1:sIky/MyNRSHTrdxfsiUSS4WIAMvInbeXljJz+jDjeYE=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4 changes: 4 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ type Bridge struct {
AgeingTime *uint32
HelloTime *uint32
VlanFiltering *bool
StpState *uint32
VlanProtocol *string
ForwardDelay *uint32
MaxAge *uint32
}

func (bridge *Bridge) Attrs() *LinkAttrs {
Expand Down
76 changes: 76 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,46 @@ func (h *Handle) BridgeSetVlanFiltering(link Link, on bool) error {
return h.linkModify(bridge, unix.NLM_F_ACK)
}

func BridgeSetVlanProtocol(link Link, protocol string) error {
return pkgHandle.BridgeSetVlanProtocol(link, protocol)
}

func (h *Handle) BridgeSetVlanProtocol(link Link, protocol string) error {
bridge := link.(*Bridge)
bridge.VlanProtocol = &protocol
return h.linkModify(bridge, unix.NLM_F_ACK)
}

func (h *Handle) BridgeSetStpState(link Link, state uint32) error {
bridge := link.(*Bridge)
bridge.StpState = &state
return h.linkModify(bridge, unix.NLM_F_ACK)
}

func BridgeSetStpState(link Link, state uint32) error {
return pkgHandle.BridgeSetStpState(link, state)
}

func (h *Handle) BridgeSetForwardDelay(link Link, delay uint32) error {
bridge := link.(*Bridge)
bridge.ForwardDelay = &delay
return h.linkModify(bridge, unix.NLM_F_ACK)
}

func BridgeSetForwardDelay(link Link, delay uint32) error {
return pkgHandle.BridgeSetForwardDelay(link, delay)
}

func (h *Handle) BridgeSetMaxAge(link Link, maxAge uint32) error {
bridge := link.(*Bridge)
bridge.MaxAge = &maxAge
return h.linkModify(bridge, unix.NLM_F_ACK)
}

func BridgeSetMaxAge(link Link, maxAge uint32) error {
return pkgHandle.BridgeSetMaxAge(link, maxAge)
}

func SetPromiscOn(link Link) error {
return pkgHandle.SetPromiscOn(link)
}
Expand Down Expand Up @@ -2985,6 +3025,23 @@ func addBridgeAttrs(bridge *Bridge, linkInfo *nl.RtAttr) {
if bridge.VlanFiltering != nil {
data.AddRtAttr(nl.IFLA_BR_VLAN_FILTERING, boolToByte(*bridge.VlanFiltering))
}
if bridge.StpState != nil {
data.AddRtAttr(nl.IFLA_BR_STP_STATE, nl.Uint32Attr(*bridge.StpState))
}
if bridge.ForwardDelay != nil {
data.AddRtAttr(nl.IFLA_BR_FORWARD_DELAY, nl.Uint32Attr(*bridge.ForwardDelay))
}
if bridge.MaxAge != nil {
data.AddRtAttr(nl.IFLA_BR_MAX_AGE, nl.Uint32Attr(*bridge.MaxAge))
}
if bridge.VlanProtocol != nil {
switch *bridge.VlanProtocol {
case "802.1Q":
data.AddRtAttr(nl.IFLA_BR_VLAN_PROTOCOL, htons(uint16(unix.ETH_P_8021Q)))
case "802.1ad":
data.AddRtAttr(nl.IFLA_BR_VLAN_PROTOCOL, htons(uint16(unix.ETH_P_8021AD)))
}
}
}

func parseBridgeData(bridge Link, data []syscall.NetlinkRouteAttr) {
Expand All @@ -3003,6 +3060,25 @@ func parseBridgeData(bridge Link, data []syscall.NetlinkRouteAttr) {
case nl.IFLA_BR_VLAN_FILTERING:
vlanFiltering := datum.Value[0] == 1
br.VlanFiltering = &vlanFiltering
case nl.IFLA_BR_VLAN_PROTOCOL:
vlanProtocolCode := native.Uint16(datum.Value[0:2])
switch vlanProtocolCode {
case unix.ETH_P_8021Q:
vlanProtocol := "802.1Q"
br.VlanProtocol = &vlanProtocol
case unix.ETH_P_8021AD:
vlanProtocol := "802.1ad"
br.VlanProtocol = &vlanProtocol
}
case nl.IFLA_BR_STP_STATE:
stpState := native.Uint32(datum.Value[0:4])
br.StpState = &stpState
case nl.IFLA_BR_FORWARD_DELAY:
forwardingDelay := native.Uint32(datum.Value[0:4])
br.ForwardDelay = &forwardingDelay
case nl.IFLA_BR_MAX_AGE:
maxAge := native.Uint32(datum.Value[0:4])
br.MaxAge = &maxAge
}
}
}
Expand Down
Loading

0 comments on commit 3e8eb11

Please sign in to comment.