Skip to content

Commit

Permalink
Add support for IFLA_NETKIT_SCRUB and IFLA_NETKIT_PEER_SCRUB
Browse files Browse the repository at this point in the history
  • Loading branch information
jrife authored and aboch committed Oct 22, 2024
1 parent 084abd9 commit 976bd8d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
34 changes: 24 additions & 10 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ const (
NETKIT_POLICY_BLACKHOLE NetkitPolicy = 2
)

type NetkitScrub int

const (
NETKIT_SCRUB_NONE NetkitScrub = 0
NETKIT_SCRUB_DEFAULT NetkitScrub = 1
)

func (n *Netkit) IsPrimary() bool {
return n.isPrimary
}
Expand All @@ -391,6 +398,9 @@ type Netkit struct {
Mode NetkitMode
Policy NetkitPolicy
PeerPolicy NetkitPolicy
Scrub NetkitScrub
PeerScrub NetkitScrub
supportsScrub bool
isPrimary bool
peerLinkAttrs LinkAttrs
}
Expand All @@ -403,6 +413,10 @@ func (n *Netkit) Type() string {
return "netkit"
}

func (n *Netkit) SupportsScrub() bool {
return n.supportsScrub
}

// Veth devices must specify PeerName on create
type Veth struct {
LinkAttrs
Expand Down Expand Up @@ -761,19 +775,19 @@ const (
)

var bondXmitHashPolicyToString = map[BondXmitHashPolicy]string{
BOND_XMIT_HASH_POLICY_LAYER2: "layer2",
BOND_XMIT_HASH_POLICY_LAYER3_4: "layer3+4",
BOND_XMIT_HASH_POLICY_LAYER2_3: "layer2+3",
BOND_XMIT_HASH_POLICY_ENCAP2_3: "encap2+3",
BOND_XMIT_HASH_POLICY_ENCAP3_4: "encap3+4",
BOND_XMIT_HASH_POLICY_LAYER2: "layer2",
BOND_XMIT_HASH_POLICY_LAYER3_4: "layer3+4",
BOND_XMIT_HASH_POLICY_LAYER2_3: "layer2+3",
BOND_XMIT_HASH_POLICY_ENCAP2_3: "encap2+3",
BOND_XMIT_HASH_POLICY_ENCAP3_4: "encap3+4",
BOND_XMIT_HASH_POLICY_VLAN_SRCMAC: "vlan+srcmac",
}
var StringToBondXmitHashPolicyMap = map[string]BondXmitHashPolicy{
"layer2": BOND_XMIT_HASH_POLICY_LAYER2,
"layer3+4": BOND_XMIT_HASH_POLICY_LAYER3_4,
"layer2+3": BOND_XMIT_HASH_POLICY_LAYER2_3,
"encap2+3": BOND_XMIT_HASH_POLICY_ENCAP2_3,
"encap3+4": BOND_XMIT_HASH_POLICY_ENCAP3_4,
"layer2": BOND_XMIT_HASH_POLICY_LAYER2,
"layer3+4": BOND_XMIT_HASH_POLICY_LAYER3_4,
"layer2+3": BOND_XMIT_HASH_POLICY_LAYER2_3,
"encap2+3": BOND_XMIT_HASH_POLICY_ENCAP2_3,
"encap3+4": BOND_XMIT_HASH_POLICY_ENCAP3_4,
"vlan+srcmac": BOND_XMIT_HASH_POLICY_VLAN_SRCMAC,
}

Expand Down
8 changes: 8 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,8 @@ func addNetkitAttrs(nk *Netkit, linkInfo *nl.RtAttr, flag int) error {
data.AddRtAttr(nl.IFLA_NETKIT_MODE, nl.Uint32Attr(uint32(nk.Mode)))
data.AddRtAttr(nl.IFLA_NETKIT_POLICY, nl.Uint32Attr(uint32(nk.Policy)))
data.AddRtAttr(nl.IFLA_NETKIT_PEER_POLICY, nl.Uint32Attr(uint32(nk.PeerPolicy)))
data.AddRtAttr(nl.IFLA_NETKIT_SCRUB, nl.Uint32Attr(uint32(nk.Scrub)))
data.AddRtAttr(nl.IFLA_NETKIT_PEER_SCRUB, nl.Uint32Attr(uint32(nk.PeerScrub)))

if (flag & unix.NLM_F_EXCL) == 0 {
// Modifying peer link attributes will not take effect
Expand Down Expand Up @@ -2736,6 +2738,12 @@ func parseNetkitData(link Link, data []syscall.NetlinkRouteAttr) {
netkit.Policy = NetkitPolicy(native.Uint32(datum.Value[0:4]))
case nl.IFLA_NETKIT_PEER_POLICY:
netkit.PeerPolicy = NetkitPolicy(native.Uint32(datum.Value[0:4]))
case nl.IFLA_NETKIT_SCRUB:
netkit.supportsScrub = true
netkit.Scrub = NetkitScrub(native.Uint32(datum.Value[0:4]))
case nl.IFLA_NETKIT_PEER_SCRUB:
netkit.supportsScrub = true
netkit.PeerScrub = NetkitScrub(native.Uint32(datum.Value[0:4]))
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func testLinkAddDel(t *testing.T, link Link) {
if resultPrimary.Mode != inputPrimary.Mode {
t.Fatalf("Mode is %d, should be %d", int(resultPrimary.Mode), int(inputPrimary.Mode))
}
if resultPrimary.SupportsScrub() && resultPrimary.Scrub != inputPrimary.Scrub {
t.Fatalf("Scrub is %d, should be %d", int(resultPrimary.Scrub), int(inputPrimary.Scrub))
}
if resultPrimary.SupportsScrub() && resultPrimary.PeerScrub != inputPrimary.PeerScrub {
t.Fatalf("Peer Scrub is %d, should be %d", int(resultPrimary.PeerScrub), int(inputPrimary.PeerScrub))
}

if inputPrimary.peerLinkAttrs.Name != "" {
var resultPeer *Netkit
Expand All @@ -102,6 +108,15 @@ func testLinkAddDel(t *testing.T, link Link) {
if resultPrimary.IsPrimary() == resultPeer.IsPrimary() {
t.Fatalf("Both primary and peer device has the same value in IsPrimary() %t", resultPrimary.IsPrimary())
}
if resultPrimary.SupportsScrub() != resultPeer.SupportsScrub() {
t.Fatalf("Peer SupportsScrub() should return %v", resultPrimary.SupportsScrub())
}
if resultPrimary.PeerScrub != resultPeer.Scrub {
t.Fatalf("Scrub from peer is %d, should be %d", int(resultPeer.Scrub), int(resultPrimary.PeerScrub))
}
if resultPrimary.Scrub != resultPeer.PeerScrub {
t.Fatalf("PeerScrub from peer is %d, should be %d", int(resultPeer.PeerScrub), int(resultPrimary.Scrub))
}
}
}

Expand Down Expand Up @@ -1051,6 +1066,8 @@ func TestLinkAddDelNetkit(t *testing.T) {
Mode: NETKIT_MODE_L2,
Policy: NETKIT_POLICY_FORWARD,
PeerPolicy: NETKIT_POLICY_BLACKHOLE,
Scrub: NETKIT_SCRUB_DEFAULT,
PeerScrub: NETKIT_SCRUB_NONE,
}
peerAttr := &LinkAttrs{
Name: "bar",
Expand Down
2 changes: 2 additions & 0 deletions nl/link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
IFLA_NETKIT_POLICY
IFLA_NETKIT_PEER_POLICY
IFLA_NETKIT_MODE
IFLA_NETKIT_SCRUB
IFLA_NETKIT_PEER_SCRUB
IFLA_NETKIT_MAX = IFLA_NETKIT_MODE
)

Expand Down

0 comments on commit 976bd8d

Please sign in to comment.