Skip to content

Commit

Permalink
Remove unexpected AltName after rename interface (#6321)
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 Jun 5, 2024
1 parent 91c25d6 commit 3f880c1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pkg/agent/util/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,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 @@ -379,3 +388,18 @@ 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 {

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Unit test (ubuntu-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Integration test

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Go benchmark test

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Build Antrea and antctl binaries

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)) (typecheck)

Check failure on line 398 in pkg/agent/util/net_linux.go

View workflow job for this annotation

GitHub Actions / golicense

link.Attrs().AltNames undefined (type *"github.com/vishvananda/netlink".LinkAttrs has no field or method AltNames)
if existAltName == altName {
return netlinkUtil.LinkDelAltName(link, altName)
}
}
return nil
}

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 3f880c1

Please sign in to comment.