Skip to content

Commit

Permalink
link_linux: add support for IFLA_MACVLAN_BC_QUEUE_LEN(_USED)
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne Champetier <[email protected]>
  • Loading branch information
champtar authored and aboch committed Aug 15, 2024
1 parent 3b7e16c commit 65a253d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
3 changes: 3 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ type Macvlan struct {

// MACAddrs is only populated for Macvlan SOURCE links
MACAddrs []net.HardwareAddr

BCQueueLen uint32
UsedBCQueueLen uint32
}

func (macvlan *Macvlan) Attrs() *LinkAttrs {
Expand Down
33 changes: 25 additions & 8 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,15 +1733,9 @@ func (h *Handle) linkModify(link Link, flags int) error {
data.AddRtAttr(nl.IFLA_IPVLAN_MODE, nl.Uint16Attr(uint16(link.Mode)))
data.AddRtAttr(nl.IFLA_IPVLAN_FLAG, nl.Uint16Attr(uint16(link.Flag)))
case *Macvlan:
if link.Mode != MACVLAN_MODE_DEFAULT {
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
data.AddRtAttr(nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[link.Mode]))
}
addMacvlanAttrs(link, linkInfo)
case *Macvtap:
if link.Mode != MACVLAN_MODE_DEFAULT {
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
data.AddRtAttr(nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[link.Mode]))
}
addMacvtapAttrs(link, linkInfo)
case *Geneve:
addGeneveAttrs(link, linkInfo)
case *Gretap:
Expand Down Expand Up @@ -2953,11 +2947,30 @@ func parseIPVtapData(link Link, data []syscall.NetlinkRouteAttr) {
}
}

func addMacvtapAttrs(macvtap *Macvtap, linkInfo *nl.RtAttr) {
addMacvlanAttrs(&macvtap.Macvlan, linkInfo)
}

func parseMacvtapData(link Link, data []syscall.NetlinkRouteAttr) {
macv := link.(*Macvtap)
parseMacvlanData(&macv.Macvlan, data)
}

func addMacvlanAttrs(macvlan *Macvlan, linkInfo *nl.RtAttr) {
var data *nl.RtAttr

if macvlan.Mode != MACVLAN_MODE_DEFAULT || macvlan.BCQueueLen > 0 {
data = linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
}

if macvlan.Mode != MACVLAN_MODE_DEFAULT {
data.AddRtAttr(nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[macvlan.Mode]))
}
if macvlan.BCQueueLen > 0 {
data.AddRtAttr(nl.IFLA_MACVLAN_BC_QUEUE_LEN, nl.Uint32Attr(macvlan.BCQueueLen))
}
}

func parseMacvlanData(link Link, data []syscall.NetlinkRouteAttr) {
macv := link.(*Macvlan)
for _, datum := range data {
Expand Down Expand Up @@ -2985,6 +2998,10 @@ func parseMacvlanData(link Link, data []syscall.NetlinkRouteAttr) {
for _, macDatum := range macs {
macv.MACAddrs = append(macv.MACAddrs, net.HardwareAddr(macDatum.Value[0:6]))
}
case nl.IFLA_MACVLAN_BC_QUEUE_LEN:
macv.BCQueueLen = native.Uint32(datum.Value[0:4])
case nl.IFLA_MACVLAN_BC_QUEUE_LEN_USED:
macv.UsedBCQueueLen = native.Uint32(datum.Value[0:4])
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ func testLinkAddDel(t *testing.T, link Link) {
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}

if macv, ok := link.(*Macvtap); ok {
Expand All @@ -211,6 +221,16 @@ func testLinkAddDel(t *testing.T, link Link) {
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}

if _, ok := link.(*Vti); ok {
Expand Down Expand Up @@ -916,6 +936,36 @@ func TestLinkAddDelMacvtap(t *testing.T) {
}
}

func TestLinkMacvBCQueueLen(t *testing.T) {
minKernelRequired(t, 5, 11)

tearDown := setUpNetlinkTest(t)
defer tearDown()

parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}

testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
})

testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
},
})

if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}

func TestNetkitPeerNs(t *testing.T) {
minKernelRequired(t, 6, 7)
tearDown := setUpNetlinkTest(t)
Expand Down
4 changes: 3 additions & 1 deletion nl/link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ const (
IFLA_MACVLAN_MACADDR
IFLA_MACVLAN_MACADDR_DATA
IFLA_MACVLAN_MACADDR_COUNT
IFLA_MACVLAN_MAX = IFLA_MACVLAN_FLAGS
IFLA_MACVLAN_BC_QUEUE_LEN
IFLA_MACVLAN_BC_QUEUE_LEN_USED
IFLA_MACVLAN_MAX = IFLA_MACVLAN_BC_QUEUE_LEN_USED
)

const (
Expand Down

0 comments on commit 65a253d

Please sign in to comment.