Skip to content

Commit

Permalink
Remove unexpected AltName after rename interface (antrea-io#6321)
Browse files Browse the repository at this point in the history
Fix antrea-io#6301

Signed-off-by: gran <[email protected]>
Co-authored-by: Lan <[email protected]>
  • Loading branch information
gran-vmv and luolanzone committed Jun 5, 2024
1 parent c175f51 commit 21fa5c3
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 @@ -291,6 +291,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 the only valid altname of the interface is the same as the
// interface name, it would be left empty when the name is occupied by the interface name; after we rename the
// interface name to another value, the altname of the interface would be set to the original interface name by the
// system.
// This altname must be removed as we need to reserve the name for an OVS internal port.
if err := removeInterfaceAltName(to, from); err != nil {
return fmt.Errorf("failed to remove AltName %s on interface %s: %w", from, to, err)
}
return nil
}

Expand Down Expand Up @@ -392,6 +401,20 @@ func interfaceExists(name string) (bool, error) {
return false, 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: %w", testInvalidErr)
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 21fa5c3

Please sign in to comment.