Skip to content

Commit

Permalink
add group_fwd_mask support for bridge and bridge port
Browse files Browse the repository at this point in the history
  • Loading branch information
hujun-open authored and aboch committed Jul 3, 2024
1 parent 154a911 commit b7b7ca8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
35 changes: 35 additions & 0 deletions bridge_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,38 @@ func TestBridgeVlan(t *testing.T) {
}
}
}

func TestBridgeGroupFwdMask(t *testing.T) {
minKernelRequired(t, 4, 15) //minimal release for per-port group_fwd_mask
tearDown := setUpNetlinkTest(t)
defer tearDown()
if err := remountSysfs(); err != nil {
t.Fatal(err)
}
bridgeName := "foo"
var mask uint16 = 0xfff0
bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}, GroupFwdMask: &mask}
if err := LinkAdd(bridge); err != nil {
t.Fatal(err)
}
brlink, err := LinkByName(bridgeName)
if err != nil {
t.Fatal(err)
}
if *(brlink.(*Bridge).GroupFwdMask) != mask {
t.Fatalf("created bridge has group_fwd_mask value %x, different from expected %x",
*(brlink.(*Bridge).GroupFwdMask), mask)
}
dummyName := "dm1"
dummy := &Dummy{LinkAttrs: LinkAttrs{Name: dummyName, MasterIndex: brlink.Attrs().Index}}
if err := LinkAdd(dummy); err != nil {
t.Fatal(err)
}
dmLink, err := LinkByName(dummyName)
if err != nil {
t.Fatal(err)
}
if err = LinkSetBRSlaveGroupFwdMask(dmLink, mask); err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ type Bridge struct {
HelloTime *uint32
VlanFiltering *bool
VlanDefaultPVID *uint16
GroupFwdMask *uint16
}

func (bridge *Bridge) Attrs() *LinkAttrs {
Expand Down
23 changes: 21 additions & 2 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,16 @@ func (h *Handle) LinkSetGuard(link Link, mode bool) error {
return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_GUARD)
}

// LinkSetBRSlaveGroupFwdMask set the group_fwd_mask of a bridge slave interface
func LinkSetBRSlaveGroupFwdMask(link Link, mask uint16) error {
return pkgHandle.LinkSetBRSlaveGroupFwdMask(link, mask)
}

// LinkSetBRSlaveGroupFwdMask set the group_fwd_mask of a bridge slave interface
func (h *Handle) LinkSetBRSlaveGroupFwdMask(link Link, mask uint16) error {
return h.setProtinfoAttrRawVal(link, nl.Uint16Attr(mask), nl.IFLA_BRPORT_GROUP_FWD_MASK)
}

func LinkSetFastLeave(link Link, mode bool) error {
return pkgHandle.LinkSetFastLeave(link, mode)
}
Expand Down Expand Up @@ -2558,7 +2568,7 @@ func (h *Handle) LinkSetBrNeighSuppress(link Link, mode bool) error {
return h.setProtinfoAttr(link, mode, nl.IFLA_BRPORT_NEIGH_SUPPRESS)
}

func (h *Handle) setProtinfoAttr(link Link, mode bool, attr int) error {
func (h *Handle) setProtinfoAttrRawVal(link Link, val []byte, attr int) error {
base := link.Attrs()
h.ensureIndex(base)
req := h.newNetlinkRequest(unix.RTM_SETLINK, unix.NLM_F_ACK)
Expand All @@ -2568,14 +2578,17 @@ func (h *Handle) setProtinfoAttr(link Link, mode bool, attr int) error {
req.AddData(msg)

br := nl.NewRtAttr(unix.IFLA_PROTINFO|unix.NLA_F_NESTED, nil)
br.AddRtAttr(attr, boolToByte(mode))
br.AddRtAttr(attr, val)
req.AddData(br)
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
if err != nil {
return err
}
return nil
}
func (h *Handle) setProtinfoAttr(link Link, mode bool, attr int) error {
return h.setProtinfoAttrRawVal(link, boolToByte(mode), attr)
}

// LinkSetTxQLen sets the transaction queue length for the link.
// Equivalent to: `ip link set $link txqlen $qlen`
Expand Down Expand Up @@ -3541,6 +3554,9 @@ func addBridgeAttrs(bridge *Bridge, linkInfo *nl.RtAttr) {
if bridge.VlanDefaultPVID != nil {
data.AddRtAttr(nl.IFLA_BR_VLAN_DEFAULT_PVID, nl.Uint16Attr(*bridge.VlanDefaultPVID))
}
if bridge.GroupFwdMask != nil {
data.AddRtAttr(nl.IFLA_BR_GROUP_FWD_MASK, nl.Uint16Attr(*bridge.GroupFwdMask))
}
}

func parseBridgeData(bridge Link, data []syscall.NetlinkRouteAttr) {
Expand All @@ -3562,6 +3578,9 @@ func parseBridgeData(bridge Link, data []syscall.NetlinkRouteAttr) {
case nl.IFLA_BR_VLAN_DEFAULT_PVID:
vlanDefaultPVID := native.Uint16(datum.Value[0:2])
br.VlanDefaultPVID = &vlanDefaultPVID
case nl.IFLA_BR_GROUP_FWD_MASK:
mask := native.Uint16(datum.Value[0:2])
br.GroupFwdMask = &mask
}
}
}
Expand Down

0 comments on commit b7b7ca8

Please sign in to comment.