Skip to content

Commit

Permalink
macvtap: calcluate libvirt domain interface in phase2
Browse files Browse the repository at this point in the history
Signed-off-by: Alona Kaplan <alkaplan@redhat.com>
  • Loading branch information
AlonaKaplan committed Jul 12, 2021
1 parent aa96d31 commit 6d4a8f6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 95 deletions.
1 change: 0 additions & 1 deletion pkg/network/infraconfigurators/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go_library(
"bridge.go",
"common.go",
"generated_mock_common.go",
"macvtap.go",
"masquerade.go",
],
importpath = "kubevirt.io/kubevirt/pkg/network/infraconfigurators",
Expand Down
70 changes: 0 additions & 70 deletions pkg/network/infraconfigurators/macvtap.go

This file was deleted.

38 changes: 30 additions & 8 deletions pkg/virt-launcher/virtwrap/network/podinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ type LibvirtSpecGenerator interface {
generate() error
}

func newMacvtapLibvirtSpecGenerator(iface *v1.Interface, domain *api.Domain, cachedDomainInterface api.Interface) *MacvtapLibvirtSpecGenerator {
func newMacvtapLibvirtSpecGenerator(iface *v1.Interface, domain *api.Domain, podInterfaceName string, handler netdriver.NetworkHandler) *MacvtapLibvirtSpecGenerator {
return &MacvtapLibvirtSpecGenerator{
vmiSpecIface: iface,
domain: domain,
cachedDomainInterface: cachedDomainInterface,
vmiSpecIface: iface,
domain: domain,
podInterfaceName: podInterfaceName,
handler: handler,
}
}

Expand Down Expand Up @@ -178,9 +179,10 @@ func (b *SlirpLibvirtSpecGenerator) generate() error {
}

type MacvtapLibvirtSpecGenerator struct {
vmiSpecIface *v1.Interface
domain *api.Domain
cachedDomainInterface api.Interface
vmiSpecIface *v1.Interface
domain *api.Domain
podInterfaceName string
handler netdriver.NetworkHandler
}

func (b *MacvtapLibvirtSpecGenerator) generate() error {
Expand All @@ -201,5 +203,25 @@ func (b *MacvtapLibvirtSpecGenerator) generate() error {
}

func (b *MacvtapLibvirtSpecGenerator) discoverDomainIfaceSpec() (*api.Interface, error) {
return &b.cachedDomainInterface, nil
podNicLink, err := b.handler.LinkByName(b.podInterfaceName)
if err != nil {
log.Log.Reason(err).Errorf("failed to get a link for interface: %s", b.podInterfaceName)
return nil, err
}
mac, err := virtnetlink.RetrieveMacAddressFromVMISpecIface(b.vmiSpecIface)
if err != nil {
return nil, err
}
if mac == nil {
mac = &podNicLink.Attrs().HardwareAddr
}

return &api.Interface{
MAC: &api.MAC{MAC: mac.String()},
MTU: &api.MTU{Size: strconv.Itoa(podNicLink.Attrs().MTU)},
Target: &api.InterfaceTarget{
Device: b.podInterfaceName,
Managed: "no",
},
}, nil
}
9 changes: 3 additions & 6 deletions pkg/virt-launcher/virtwrap/network/podinterface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,17 +655,14 @@ var _ = Describe("Pod Network", func() {
mockNetwork.EXPECT().LinkByName(podnic.podInterfaceName).Return(macvtapInterface, nil)
})

XIt("Should pass a non-privileged macvtap interface to qemu", func() {
// TODO the test fails in this commit since the domainCache is not stored anywhere, the next commit is removing the domain cache usgae so it will fix the test
It("Should pass a non-privileged macvtap interface to qemu", func() {
domain := NewDomainWithMacvtapInterface("default")

api.NewDefaulter(runtime.GOARCH).SetObjectDefaults_Domain(domain)
driver, err := podnic.newLibvirtSpecGenerator(domain)
specGenerator, err := podnic.newLibvirtSpecGenerator(domain)
Expect(err).ToNot(HaveOccurred(), "should have identified the correct binding mechanism")

Expect(podnic.infraConfigurator.DiscoverPodNetworkInterface(podnic.podInterfaceName)).To(Succeed())
Expect(podnic.infraConfigurator.PreparePodNetworkInterface()).To(Succeed())
Expect(driver.generate()).To(Succeed())
Expect(specGenerator.generate()).To(Succeed())

Expect(len(domain.Spec.Devices.Interfaces)).To(Equal(1), "should have a single interface")
Expect(domain.Spec.Devices.Interfaces[0].Target).To(Equal(&api.InterfaceTarget{Device: podnic.podInterfaceName, Managed: "no"}), "should have an unmanaged interface")
Expand Down
11 changes: 1 addition & 10 deletions pkg/virt-launcher/virtwrap/network/podnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func newPhase1PodNIC(vmi *v1.VirtualMachineInstance, network *v1.Network, handle
podnic.vmiSpecNetwork,
*podnic.launcherPID,
podnic.handler)
} else if podnic.vmiSpecIface.Macvtap != nil {
podnic.infraConfigurator = infraconfigurators.NewMacvtapPodNetworkConfigurator(
podnic.podInterfaceName,
podnic.vmiSpecIface,
podnic.handler)
}
return podnic, nil
}
Expand Down Expand Up @@ -287,11 +282,7 @@ func (l *podNIC) newLibvirtSpecGenerator(domain *api.Domain) (LibvirtSpecGenerat
return newSlirpLibvirtSpecGenerator(l.vmiSpecIface, domain), nil
}
if l.vmiSpecIface.Macvtap != nil {
cachedDomainIface, err := l.cachedDomainInterface()
if err != nil {
return nil, err
}
return newMacvtapLibvirtSpecGenerator(l.vmiSpecIface, domain, *cachedDomainIface), nil
return newMacvtapLibvirtSpecGenerator(l.vmiSpecIface, domain, l.podInterfaceName, l.handler), nil
}
return nil, fmt.Errorf("Not implemented")
}
Expand Down

0 comments on commit 6d4a8f6

Please sign in to comment.