Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove interface name from alt name if exist #292

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/static-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"
go-version: "1.21"
- uses: actions/checkout@v3
- name: run make lint
run: make lint
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.5
github.com/stretchr/testify v1.6.1
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230905152006-63484bbf69f8
github.com/vishvananda/netlink v1.2.1-beta.2.0.20240221172127-ec7bcb248e94
golang.org/x/net v0.17.0
)

Expand All @@ -35,4 +35,5 @@ require (
replace (
github.com/onsi/ginkgo/v2 => github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega => github.com/onsi/gomega v1.27.5
github.com/vishvananda/netlink => github.com/vishvananda/netlink v1.2.1-beta.2.0.20240221172127-ec7bcb248e94
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand All @@ -12,6 +13,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
Expand All @@ -29,7 +31,9 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/k8snetworkplumbingwg/cni-log v0.0.0-20230801160229-b6e062c9e0f2 h1:KB8UPZQwLge4Abuk9tNmvzffdCJgqXSN341BX98QTHg=
github.com/k8snetworkplumbingwg/cni-log v0.0.0-20230801160229-b6e062c9e0f2/go.mod h1:/x45AlZDoJVSSV4ECDb5TcHLzrVRDllsCMDzMrtHKwk=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ=
Expand All @@ -42,8 +46,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230905152006-63484bbf69f8 h1:xxfANIR2aBrzBufGZZkbIg0N4V1AEmw0hKWlWv8eMYM=
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230905152006-63484bbf69f8/go.mod h1:whJevzBpTrid75eZy99s3DqCmy05NfibNaF2Ol5Ox5A=
github.com/vishvananda/netlink v1.2.1-beta.2.0.20240221172127-ec7bcb248e94 h1:EO+D8nUD4bAhHHcJUSvH+8puZrDrcjc50tePSevGQrw=
github.com/vishvananda/netlink v1.2.1-beta.2.0.20240221172127-ec7bcb248e94/go.mod h1:whJevzBpTrid75eZy99s3DqCmy05NfibNaF2Ol5Ox5A=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
Expand Down Expand Up @@ -104,6 +108,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
26 changes: 17 additions & 9 deletions pkg/sriov/mocks/pci_utils_mock.go

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

38 changes: 28 additions & 10 deletions pkg/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,26 @@ func (s *sriovManager) SetupVF(conf *sriovtypes.NetConf, podifName string, netns
return fmt.Errorf("error setting temp IF name %s for %s", tempName, linkName)
}

// 3. Change netns
logging.Debug("3. Change netns",
// 3. Remove alt name from the nic
logging.Debug("3. Remove interface original name from alt names",
"func", "SetupVF",
"linkObj", linkObj,
"OriginalLinkName", linkName,
"tempName", tempName)
linkObj, err = s.nLink.LinkByName(tempName)
if err != nil {
return fmt.Errorf("error getting VF netdevice with name %s: %v", tempName, err)
}
for _, altName := range linkObj.Attrs().AltNames {
if altName == linkName {
if err := s.nLink.LinkDelAltName(linkObj, linkName); err != nil {
return fmt.Errorf("error removing VF altname %s: %v", linkName, err)
}
}
}

// 4. Change netns
logging.Debug("4. Change netns",
"func", "SetupVF",
"linkObj", linkObj,
"netns.Fd()", int(netns.Fd()))
Expand All @@ -100,25 +118,25 @@ func (s *sriovManager) SetupVF(conf *sriovtypes.NetConf, podifName string, netns
}

if err := netns.Do(func(_ ns.NetNS) error {
// 4. Set Pod IF name
logging.Debug("4. Set Pod IF name",
// 5. Set Pod IF name
logging.Debug("5. Set Pod IF name",
"func", "SetupVF",
"linkObj", linkObj,
"podifName", podifName)
if err := s.nLink.LinkSetName(linkObj, podifName); err != nil {
return fmt.Errorf("error setting container interface name %s for %s", linkName, tempName)
}

// 5. Enable IPv4 ARP notify and IPv6 Network Discovery notify
// 6. Enable IPv4 ARP notify and IPv6 Network Discovery notify
// Error is ignored here because enabling this feature is only a performance enhancement.
logging.Debug("5. Enable IPv4 ARP notify and IPv6 Network Discovery notify",
logging.Debug("6. Enable IPv4 ARP notify and IPv6 Network Discovery notify",
"func", "SetupVF",
"podifName", podifName)
_ = s.utils.EnableArpAndNdiscNotify(podifName)

// 6. Set MAC address
// 7. Set MAC address
if conf.MAC != "" {
logging.Debug("6. Set MAC address",
logging.Debug("7. Set MAC address",
"func", "SetupVF",
"s.nLink", s.nLink,
"podifName", podifName,
Expand All @@ -129,8 +147,8 @@ func (s *sriovManager) SetupVF(conf *sriovtypes.NetConf, podifName string, netns
}
}

// 7. Bring IF up in Pod netns
logging.Debug("7. Bring IF up in Pod netns",
// 8. Bring IF up in Pod netns
logging.Debug("8. Bring IF up in Pod netns",
"func", "SetupVF",
"linkObj", linkObj)
if err := s.nLink.LinkSetUp(linkObj); err != nil {
Expand Down
66 changes: 64 additions & 2 deletions pkg/sriov/sriov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,74 @@ var _ = Describe("Sriov", func() {
HardwareAddr: expMac,
}}

net2Link := &utils.FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "temp_1000",
HardwareAddr: expMac,
}}

mocked.On("LinkByName", "enp175s6").Return(fakeLink, nil)
mocked.On("LinkByName", "temp_1000").Return(net2Link, nil)
mocked.On("LinkByName", "net1").Return(net1Link, nil)
mocked.On("LinkSetDown", fakeLink).Return(nil)
mocked.On("LinkSetName", fakeLink, mock.Anything).Return(nil)
mocked.On("LinkSetName", net2Link, mock.Anything).Return(nil)
mocked.On("LinkSetHardwareAddr", net1Link, expMac).Return(nil)
mocked.On("LinkSetNsFd", fakeLink, mock.AnythingOfType("int")).Return(nil)
mocked.On("LinkSetUp", fakeLink).Return(nil)
mocked.On("LinkSetNsFd", net2Link, mock.AnythingOfType("int")).Return(nil)
mocked.On("LinkSetUp", net2Link).Return(nil)
mockedPciUtils.On("EnableArpAndNdiscNotify", mock.AnythingOfType("string")).Return(nil)
sm := sriovManager{nLink: mocked, utils: mockedPciUtils}
err = sm.SetupVF(netconf, podifName, targetNetNS)
Expect(err).NotTo(HaveOccurred())
mocked.AssertExpectations(t)
})
It("Remove altName", func() {
var targetNetNS ns.NetNS
targetNetNS, err := testutils.NewNS()
defer func() {
if targetNetNS != nil {
targetNetNS.Close()
}
}()
Expect(err).NotTo(HaveOccurred())
mocked := &mocks_utils.NetlinkManager{}
mockedPciUtils := &mocks.PciUtils{}
fakeMac, err := net.ParseMAC("6e:16:06:0e:b7:e9")
Expect(err).NotTo(HaveOccurred())

netconf.MAC = "e4:11:22:33:44:55"
expMac, err := net.ParseMAC(netconf.MAC)
Expect(err).NotTo(HaveOccurred())

fakeLink := &utils.FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "dummylink",
HardwareAddr: fakeMac,
}}

net1Link := &utils.FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "net1",
HardwareAddr: expMac,
}}

net2Link := &utils.FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "temp_1000",
HardwareAddr: expMac,
AltNames: []string{"enp175s6"},
}}

mocked.On("LinkByName", "enp175s6").Return(fakeLink, nil)
mocked.On("LinkByName", "temp_1000").Return(net2Link, nil)
mocked.On("LinkByName", "net1").Return(net1Link, nil)
mocked.On("LinkSetDown", fakeLink).Return(nil)
mocked.On("LinkSetName", fakeLink, mock.Anything).Return(nil)
mocked.On("LinkSetName", net2Link, mock.Anything).Return(nil)
mocked.On("LinkDelAltName", net2Link, "enp175s6").Return(nil)
mocked.On("LinkSetHardwareAddr", net1Link, expMac).Return(nil)
mocked.On("LinkSetNsFd", net2Link, mock.AnythingOfType("int")).Return(nil)
mocked.On("LinkSetUp", net2Link).Return(nil)
mockedPciUtils.On("EnableArpAndNdiscNotify", mock.AnythingOfType("string")).Return(nil)
sm := sriovManager{nLink: mocked, utils: mockedPciUtils}
err = sm.SetupVF(netconf, podifName, targetNetNS)
Expand Down Expand Up @@ -362,4 +423,5 @@ var _ = Describe("Sriov", func() {
mocked.AssertExpectations(t)
})
})

})
25 changes: 19 additions & 6 deletions pkg/utils/mocks/netlink_manager_mock.go

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

6 changes: 6 additions & 0 deletions pkg/utils/netlink_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type NetlinkManager interface {
LinkSetVfSpoofchk(netlink.Link, int, bool) error
LinkSetVfTrust(netlink.Link, int, bool) error
LinkSetVfState(netlink.Link, int, uint32) error
LinkDelAltName(netlink.Link, string) error
}

// MyNetlink NetlinkManager
Expand Down Expand Up @@ -88,3 +89,8 @@ func (n *MyNetlink) LinkSetVfTrust(link netlink.Link, vf int, state bool) error
func (n *MyNetlink) LinkSetVfState(link netlink.Link, vf int, state uint32) error {
return netlink.LinkSetVfState(link, vf, state)
}

// LinkDelAltName using NetlinkManager
func (n *MyNetlink) LinkDelAltName(link netlink.Link, altName string) error {
return netlink.LinkDelAltName(link, altName)
}
Loading