Skip to content

Commit

Permalink
Remove unexpected AltName after rename interface
Browse files Browse the repository at this point in the history
Fix #6301

Signed-off-by: gran <gran@vmware.com>
Co-authored-by: Lan <luola@vmware.com>
  • Loading branch information
gran-vmv and luolanzone committed May 29, 2024
1 parent 5b48493 commit 4d49aa3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
23 changes: 23 additions & 0 deletions pkg/agent/util/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ func RenameInterface(from, to string) error {
if pollErr != nil {
return fmt.Errorf("failed to rename host interface name %s to %s", from, to)
}
// Fix for the issue https://github.com/antrea-io/antrea/issues/6301.
// In some new Linux versions which support AltName, if there is only one valid value in AlternativeNamesPolicy
// which is also occupied by an interface name, after the interface name changed from generated value to another one,
// the interface altname will be set to previous name immediately.
// This altname must be removed to avoid unexpected conflict.
if err := removeInterfaceAltName(to, from); err != nil {
klog.ErrorS(err, "Failed to remove AltName after interface renamed")
return fmt.Errorf("failed to remove AltName %s on interface %s", from, to)
}
return nil
}

Expand Down Expand Up @@ -378,6 +387,20 @@ func renameHostInterface(oriName string, newName string) error {
return nil
}

// removeInterfaceAltName removes altName on interface with provided name. altName not found will return nil.
func removeInterfaceAltName(name string, altName string) error {
link, err := netlinkUtil.LinkByName(name)
if err != nil {
return err
}
for _, existAltName := range link.Attrs().AltNames {
if existAltName == altName {
return netlinkUtil.LinkDelAltName(link, altName)
}
}
return nil
}

// PrepareHostInterfaceConnection prepares host interface connection to the OVS bridge client by:
// 1. Renaming the host interface (a bridged suffix will be added to it).
// 2. Creating an internal port (original name of the host interface will be used here).
Expand Down
27 changes: 27 additions & 0 deletions pkg/agent/util/net_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type mockLink struct {
name string
masterIndex int
hardwareAddr net.HardwareAddr
altNames []string
}

func (l mockLink) Attrs() *netlink.LinkAttrs {
Expand All @@ -44,6 +45,7 @@ func (l mockLink) Attrs() *netlink.LinkAttrs {
Name: l.name,
MasterIndex: l.masterIndex,
HardwareAddr: l.hardwareAddr,
AltNames: l.altNames,
}
}

Expand Down Expand Up @@ -557,6 +559,7 @@ func TestGetInterfaceConfig(t *testing.T) {

func TestRenameInterface(t *testing.T) {
renameFailErr := fmt.Errorf("failed to rename host interface name test1 to test2")
removeAltNameFailErr := fmt.Errorf("failed to remove AltName test1 on interface test2")
tests := []struct {
name string
expectedCalls func(mockNetlink *netlinktest.MockInterfaceMockRecorder)
Expand All @@ -569,6 +572,18 @@ func TestRenameInterface(t *testing.T) {
mockNetlink.LinkSetDown(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkSetName(mockLink{name: "test1"}, "test2").Return(nil)
mockNetlink.LinkSetUp(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkByName("test2").Return(mockLink{name: "test2", altNames: []string{}}, nil)
},
},
{
name: "Rename Interface Success with AltName",
expectedCalls: func(mockNetlink *netlinktest.MockInterfaceMockRecorder) {
mockNetlink.LinkByName("test1").Return(mockLink{name: "test1"}, nil)
mockNetlink.LinkSetDown(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkSetName(mockLink{name: "test1"}, "test2").Return(nil)
mockNetlink.LinkSetUp(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkByName("test2").Return(mockLink{name: "test2", altNames: []string{"test1"}}, nil)
mockNetlink.LinkDelAltName(mockLink{name: "test2", altNames: []string{"test1"}}, "test1").Return(nil)
},
},
{
Expand Down Expand Up @@ -605,6 +620,18 @@ func TestRenameInterface(t *testing.T) {
},
wantErr: renameFailErr,
},
{
name: "Remove AltName Err",
expectedCalls: func(mockNetlink *netlinktest.MockInterfaceMockRecorder) {
mockNetlink.LinkByName("test1").Return(mockLink{name: "test1"}, nil)
mockNetlink.LinkSetDown(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkSetName(mockLink{name: "test1"}, "test2").Return(nil)
mockNetlink.LinkSetUp(mockLink{name: "test1"}).Return(nil)
mockNetlink.LinkByName("test2").Return(mockLink{name: "test2", altNames: []string{"test1"}}, nil)
mockNetlink.LinkDelAltName(mockLink{name: "test2", altNames: []string{"test1"}}, "test1").Return(testInvalidErr)
},
wantErr: removeAltNameFailErr,
},
}

for _, tc := range tests {
Expand Down
4 changes: 4 additions & 0 deletions pkg/agent/util/netlink/netlink_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type Interface interface {

LinkSetName(link netlink.Link, name string) error

LinkAddAltName(link netlink.Link, name string) error

LinkDelAltName(link netlink.Link, name string) error

LinkSetUp(link netlink.Link) error

ConntrackDeleteFilter(table netlink.ConntrackTableType, family netlink.InetFamily, filter netlink.CustomConntrackFilter) (uint, error)
Expand Down
30 changes: 29 additions & 1 deletion pkg/agent/util/netlink/testing/mock_netlink_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d49aa3

Please sign in to comment.