Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
cli: add configuration option to enable/disable vhost_net
Browse files Browse the repository at this point in the history
Add `disable_vhost_net` option to enable or disable the use of
vhost_net. Vhost_net can improve network performance.

Signed-off-by: Ruidong Cao <[email protected]>
  • Loading branch information
caoruidong committed Sep 13, 2018
1 parent 5ebb7cf commit 225e10c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
2 changes: 2 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type hypervisor struct {
EnableIOThreads bool `toml:"enable_iothreads"`
UseVSock bool `toml:"use_vsock"`
HotplugVFIOOnRootBus bool `toml:"hotplug_vfio_on_root_bus"`
DisableVhostNet bool `toml:"disable_vhost_net"`
}

type proxy struct {
Expand Down Expand Up @@ -375,6 +376,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
Msize9p: h.msize9p(),
UseVSock: useVSock,
HotplugVFIOOnRootBus: h.HotplugVFIOOnRootBus,
DisableVhostNet: h.DisableVhostNet,
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions cli/config/configuration.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ enable_iothreads = @DEFENABLEIOTHREADS@
# Default false
#hotplug_vfio_on_root_bus = true

# If host doesn't support vhost_net, set to true. Thus we won't create vhost fds for nics.
# Default false
#disable_vhost_net = true

[factory]
# VM templating support. Once enabled, new VMs are created from template
# using vm cloning. They will share the same initial kernel, initramfs and
Expand Down
3 changes: 3 additions & 0 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ type HypervisorConfig struct {
// DevicesStatePath is the VM device state file path. Used when either BootToBeTemplate or
// BootFromTemplate is true.
DevicesStatePath string

// DisableVhostNet is used to indicate if host supports vhost_net
DisableVhostNet bool
}

func (conf *HypervisorConfig) checkTemplateConfig() error {
Expand Down
38 changes: 21 additions & 17 deletions virtcontainers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func networkLogger() *logrus.Entry {
func (endpoint *VirtualEndpoint) Attach(h hypervisor) error {
networkLogger().WithField("endpoint-type", "virtual").Info("Attaching endpoint")

if err := xconnectVMNetwork(&(endpoint.NetPair), true, h.hypervisorConfig().NumVCPUs); err != nil {
if err := xconnectVMNetwork(&(endpoint.NetPair), true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual endpoint")
return err
}
Expand All @@ -245,14 +245,14 @@ func (endpoint *VirtualEndpoint) Detach(netNsCreated bool, netNsPath string) err
networkLogger().WithField("endpoint-type", "virtual").Info("Detaching endpoint")

return doNetNS(netNsPath, func(_ ns.NetNS) error {
return xconnectVMNetwork(&(endpoint.NetPair), false, 0)
return xconnectVMNetwork(&(endpoint.NetPair), false, 0, false)
})
}

// HotAttach for the virtual endpoint uses hot plug device
func (endpoint *VirtualEndpoint) HotAttach(h hypervisor) error {
networkLogger().Info("Hot attaching virtual endpoint")
if err := xconnectVMNetwork(&(endpoint.NetPair), true, h.hypervisorConfig().NumVCPUs); err != nil {
if err := xconnectVMNetwork(&(endpoint.NetPair), true, h.hypervisorConfig().NumVCPUs, h.hypervisorConfig().DisableVhostNet); err != nil {
networkLogger().WithError(err).Error("Error bridging virtual ep")
return err
}
Expand All @@ -271,7 +271,7 @@ func (endpoint *VirtualEndpoint) HotDetach(h hypervisor, netNsCreated bool, netN
}
networkLogger().Info("Hot detaching virtual endpoint")
if err := doNetNS(netNsPath, func(_ ns.NetNS) error {
return xconnectVMNetwork(&(endpoint.NetPair), false, 0)
return xconnectVMNetwork(&(endpoint.NetPair), false, 0, h.hypervisorConfig().DisableVhostNet)
}); err != nil {
networkLogger().WithError(err).Error("Error abridging virtual ep")
return err
Expand Down Expand Up @@ -721,21 +721,21 @@ func getLinkByName(netHandle *netlink.Handle, name string, expectedLink netlink.
}

// The endpoint type should dictate how the connection needs to be made
func xconnectVMNetwork(netPair *NetworkInterfacePair, connect bool, numCPUs uint32) error {
func xconnectVMNetwork(netPair *NetworkInterfacePair, connect bool, numCPUs uint32, disableVhostNet bool) error {
if netPair.NetInterworkingModel == NetXConnectDefaultModel {
netPair.NetInterworkingModel = DefaultNetInterworkingModel
}
switch netPair.NetInterworkingModel {
case NetXConnectBridgedModel:
netPair.NetInterworkingModel = NetXConnectBridgedModel
if connect {
return bridgeNetworkPair(netPair, numCPUs)
return bridgeNetworkPair(netPair, numCPUs, disableVhostNet)
}
return unBridgeNetworkPair(*netPair)
case NetXConnectMacVtapModel:
netPair.NetInterworkingModel = NetXConnectMacVtapModel
if connect {
return tapNetworkPair(netPair, numCPUs)
return tapNetworkPair(netPair, numCPUs, disableVhostNet)
}
return untapNetworkPair(*netPair)
case NetXConnectEnlightenedModel:
Expand Down Expand Up @@ -824,7 +824,7 @@ func setIPs(link netlink.Link, addrs []netlink.Addr) error {
return nil
}

func tapNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32) error {
func tapNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32, disableVhostNet bool) error {
netHandle, err := netlink.NewHandle()
if err != nil {
return err
Expand Down Expand Up @@ -904,16 +904,18 @@ func tapNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32) error {
return fmt.Errorf("Could not setup macvtap fds %s: %s", netPair.TAPIface, err)
}

vhostFds, err := createVhostFds(int(numCPUs))
if err != nil {
return fmt.Errorf("Could not setup vhost fds %s : %s", netPair.VirtIface.Name, err)
if !disableVhostNet {
vhostFds, err := createVhostFds(int(numCPUs))
if err != nil {
return fmt.Errorf("Could not setup vhost fds %s : %s", netPair.VirtIface.Name, err)
}
netPair.VhostFds = vhostFds
}
netPair.VhostFds = vhostFds

return nil
}

func bridgeNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32) error {
func bridgeNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32, disableVhostNet bool) error {
netHandle, err := netlink.NewHandle()
if err != nil {
return err
Expand All @@ -926,11 +928,13 @@ func bridgeNetworkPair(netPair *NetworkInterfacePair, numCPUs uint32) error {
}
netPair.VMFds = fds

vhostFds, err := createVhostFds(int(numCPUs))
if err != nil {
return fmt.Errorf("Could not setup vhost fds %s : %s", netPair.VirtIface.Name, err)
if !disableVhostNet {
vhostFds, err := createVhostFds(int(numCPUs))
if err != nil {
return fmt.Errorf("Could not setup vhost fds %s : %s", netPair.VirtIface.Name, err)
}
netPair.VhostFds = vhostFds
}
netPair.VhostFds = vhostFds

vethLink, err := getLinkByName(netHandle, netPair.VirtIface.Name, &netlink.Veth{})
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func (q *qemu) init(ctx context.Context, id string, hypervisorConfig *Hypervisor
q.arch.disableNestingChecks()
}

if !q.config.DisableVhostNet {
q.arch.enableVhostNet()
} else {
q.Logger().Debug("Disable vhost_net")
q.arch.disableVhostNet()
}

return nil
}

Expand Down
17 changes: 16 additions & 1 deletion virtcontainers/qemu_arch_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type qemuArch interface {
// disableNestingChecks nesting checks will be ignored
disableNestingChecks()

// enableVhostNet vhost will be enabled
enableVhostNet()

// disableVhostNet vhost will be disabled
disableVhostNet()

// machine returns the machine type
machine() (govmmQemu.Machine, error)

Expand Down Expand Up @@ -92,6 +98,7 @@ type qemuArch interface {
type qemuArchBase struct {
machineType string
nestedRun bool
vhost bool
networkIndex int
qemuPaths map[string]string
supportedQemuMachines []govmmQemu.Machine
Expand Down Expand Up @@ -176,6 +183,14 @@ func (q *qemuArchBase) disableNestingChecks() {
q.nestedRun = false
}

func (q *qemuArchBase) enableVhostNet() {
q.vhost = true
}

func (q *qemuArchBase) disableVhostNet() {
q.vhost = false
}

func (q *qemuArchBase) machine() (govmmQemu.Machine, error) {
for _, m := range q.supportedQemuMachines {
if m.Type == q.machineType {
Expand Down Expand Up @@ -437,7 +452,7 @@ func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoi
MACAddress: ep.NetPair.TAPIface.HardAddr,
DownScript: "no",
Script: "no",
VHost: true,
VHost: q.vhost,
DisableModern: q.nestedRun,
FDs: ep.NetPair.VMFds,
VhostFDs: ep.NetPair.VhostFds,
Expand Down

0 comments on commit 225e10c

Please sign in to comment.