Skip to content
Open
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
4 changes: 4 additions & 0 deletions pkg/unikontainers/hypervisors/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
type Qemu struct {
binaryPath string
binary string
vhost bool
}

func (q *Qemu) Stop(pid int) error {
Expand Down Expand Up @@ -94,6 +95,9 @@ func (q *Qemu) BuildExecCmd(args types.ExecArgs, ukernel types.Unikernel) ([]str
netcli += args.Net.MAC
netcli += " -net tap,script=no,downscript=no,ifname="
netcli += args.Net.TapDev
if q.vhost {
netcli += ",vhost=on"
}
}
cmdString += netcli
} else {
Expand Down
28 changes: 18 additions & 10 deletions pkg/unikontainers/hypervisors/vmm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,33 @@ var vmmLog = logrus.WithField("subsystem", "monitors")

type VMMFactory struct {
binary string
createFunc func(binary, binaryPath string) types.VMM
createFunc func(binary, binaryPath string, vhost bool) types.VMM
}

var vmmFactories = map[VmmType]VMMFactory{
SptVmm: {
binary: SptBinary,
createFunc: func(binary, binaryPath string) types.VMM { return &SPT{binary: binary, binaryPath: binaryPath} },
binary: SptBinary,
createFunc: func(binary, binaryPath string, _ bool) types.VMM {
return &SPT{binary: binary, binaryPath: binaryPath}
},
},
HvtVmm: {
binary: HvtBinary,
createFunc: func(binary, binaryPath string) types.VMM { return &HVT{binary: binary, binaryPath: binaryPath} },
binary: HvtBinary,
createFunc: func(binary, binaryPath string, _ bool) types.VMM {
return &HVT{binary: binary, binaryPath: binaryPath}
},
},
QemuVmm: {
binary: QemuBinary,
createFunc: func(binary, binaryPath string) types.VMM { return &Qemu{binary: binary, binaryPath: binaryPath} },
binary: QemuBinary,
createFunc: func(binary, binaryPath string, vhost bool) types.VMM {
return &Qemu{binary: binary, binaryPath: binaryPath, vhost: vhost}
},
},
FirecrackerVmm: {
binary: FirecrackerBinary,
createFunc: func(binary, binaryPath string) types.VMM { return &Firecracker{binary: binary, binaryPath: binaryPath} },
binary: FirecrackerBinary,
createFunc: func(binary, binaryPath string, _ bool) types.VMM {
return &Firecracker{binary: binary, binaryPath: binaryPath}
},
},
}

Expand Down Expand Up @@ -80,7 +88,7 @@ func NewVMM(vmmType VmmType, monitors map[string]types.MonitorConfig) (vmm types
return nil, err
}

return factory.createFunc(factory.binary, vmmPath), nil
return factory.createFunc(factory.binary, vmmPath, monitors[string(vmmType)].Vhost), nil
}

func getVMMPath(vmmType VmmType, binary string, monitors map[string]types.MonitorConfig) (string, error) {
Expand Down
71 changes: 71 additions & 0 deletions pkg/unikontainers/hypervisors/vmm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hypervisors

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVMMFactoryQemuVhostFalse(t *testing.T) {
t.Parallel()
factory, exists := vmmFactories[QemuVmm]
assert.True(t, exists, "qemu factory should exist")

vmm := factory.createFunc(QemuBinary, "/usr/bin/qemu-system-x86_64", false)
qemu, ok := vmm.(*Qemu)
assert.True(t, ok, "factory should return *Qemu")
assert.False(t, qemu.vhost, "vhost should be false when passed as false")
}

func TestVMMFactoryQemuVhostTrue(t *testing.T) {
t.Parallel()
factory, exists := vmmFactories[QemuVmm]
assert.True(t, exists, "qemu factory should exist")

vmm := factory.createFunc(QemuBinary, "/usr/bin/qemu-system-x86_64", true)
qemu, ok := vmm.(*Qemu)
assert.True(t, ok, "factory should return *Qemu")
assert.True(t, qemu.vhost, "vhost should be true when passed as true")
}

func TestVMMFactoryNonQemuIgnoresVhost(t *testing.T) {
t.Parallel()

// SPT factory should ignore vhost parameter
factory, exists := vmmFactories[SptVmm]
assert.True(t, exists, "spt factory should exist")

vmm := factory.createFunc(SptBinary, "/usr/bin/solo5-spt", true)
_, ok := vmm.(*SPT)
assert.True(t, ok, "factory should return *SPT")

// HVT factory should ignore vhost parameter
factory, exists = vmmFactories[HvtVmm]
assert.True(t, exists, "hvt factory should exist")

vmm = factory.createFunc(HvtBinary, "/usr/bin/solo5-hvt", true)
_, ok = vmm.(*HVT)
assert.True(t, ok, "factory should return *HVT")

// Firecracker factory should ignore vhost parameter
factory, exists = vmmFactories[FirecrackerVmm]
assert.True(t, exists, "firecracker factory should exist")

vmm = factory.createFunc(FirecrackerBinary, "/usr/bin/firecracker", true)
_, ok = vmm.(*Firecracker)
assert.True(t, ok, "factory should return *Firecracker")
}
1 change: 1 addition & 0 deletions pkg/unikontainers/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ type MonitorConfig struct {
DefaultVCPUs uint `toml:"default_vcpus"`
BinaryPath string `toml:"path,omitempty"` // Optional path to the hypervisor binary
DataPath string `toml:"data_path,omitempty"` // Optional path to the hypervisor data files (e.g. qemu bios stuff)
Vhost bool `toml:"vhost,omitempty"` // Optional: enable vhost for network performance optimization
}
8 changes: 8 additions & 0 deletions pkg/unikontainers/urunc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (p *UruncConfig) Map() map[string]string {
cfgMap[prefix+"default_vcpus"] = strconv.FormatUint(uint64(hvCfg.DefaultVCPUs), 10)
cfgMap[prefix+"binary_path"] = hvCfg.BinaryPath
cfgMap[prefix+"data_path"] = hvCfg.DataPath
cfgMap[prefix+"vhost"] = strconv.FormatBool(hvCfg.Vhost)
}
for eb, ebCfg := range p.ExtraBins {
prefix := "urunc_config.extra_binaries." + eb + "."
Expand Down Expand Up @@ -171,6 +172,13 @@ func UruncConfigFromMap(cfgMap map[string]string) *UruncConfig {
hvCfg.BinaryPath = val
case "data_path":
hvCfg.DataPath = val
case "vhost":
boolVal, err := strconv.ParseBool(val)
if err != nil {
uniklog.Warnf("Invalid vhost value '%s' for monitor '%s': %v. Using default (false).", val, hv, err)
} else {
hvCfg.Vhost = boolVal
}
}
cfg.Monitors[hv] = hvCfg
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/unikontainers/urunc_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
testQemuVCPUsKey = "urunc_config.monitors.qemu.default_vcpus"
testQemuBinaryKey = "urunc_config.monitors.qemu.binary_path"
testQemuDataKey = "urunc_config.monitors.qemu.data_path"
testQemuVhostKey = "urunc_config.monitors.qemu.vhost"
testHvtMemoryKey = "urunc_config.monitors.hvt.default_memory_mb"
testVirtiofsdPathKey = "urunc_config.extra_binaries.virtiofsd.path"
testVirtiofsdOptsKey = "urunc_config.extra_binaries.virtiofsd.options"
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestUruncConfigFromMap(t *testing.T) {
testQemuVCPUsKey: "2",
testQemuBinaryKey: testQemuBinaryPath,
testQemuDataKey: testQemuDataPath,
testQemuVhostKey: "true",
}

config := UruncConfigFromMap(cfgMap)
Expand All @@ -67,6 +69,7 @@ func TestUruncConfigFromMap(t *testing.T) {
assert.Equal(t, uint(2), qemuConfig.DefaultVCPUs)
assert.Equal(t, testQemuBinaryPath, qemuConfig.BinaryPath)
assert.Equal(t, testQemuDataPath, qemuConfig.DataPath)
assert.True(t, qemuConfig.Vhost)
})

t.Run("multiple monitors", func(t *testing.T) {
Expand Down Expand Up @@ -323,6 +326,34 @@ func TestUruncConfigFromMap(t *testing.T) {
assert.Equal(t, testVirtiofsdDefOpts, vfsConfig.Options)
})

t.Run("vhost false is parsed correctly", func(t *testing.T) {
t.Parallel()
cfgMap := map[string]string{
testQemuMemoryKey: "512",
testQemuVhostKey: "false",
}

config := UruncConfigFromMap(cfgMap)

assert.NotNil(t, config)
qemuConfig := config.Monitors["qemu"]
assert.False(t, qemuConfig.Vhost)
})

t.Run("invalid vhost value defaults to false", func(t *testing.T) {
t.Parallel()
cfgMap := map[string]string{
testQemuMemoryKey: "512",
testQemuVhostKey: "invalid",
}

config := UruncConfigFromMap(cfgMap)

assert.NotNil(t, config)
qemuConfig := config.Monitors["qemu"]
assert.False(t, qemuConfig.Vhost, "invalid vhost value should default to false")
})

}

func TestUruncConfigMap(t *testing.T) {
Expand Down Expand Up @@ -415,6 +446,24 @@ func TestUruncConfigMap(t *testing.T) {
assert.NotNil(t, cfgMap)
assert.Empty(t, cfgMap)
})

t.Run("vhost true is serialized correctly", func(t *testing.T) {
t.Parallel()
config := &UruncConfig{
Monitors: map[string]types.MonitorConfig{
"qemu": {
DefaultMemoryMB: 512,
DefaultVCPUs: 2,
Vhost: true,
},
},
ExtraBins: map[string]types.ExtraBinConfig{},
}

cfgMap := config.Map()

assert.Equal(t, "true", cfgMap[testQemuVhostKey])
})
}

func TestDefaultConfigs(t *testing.T) {
Expand Down Expand Up @@ -449,6 +498,7 @@ func TestDefaultConfigs(t *testing.T) {
assert.Equal(t, uint(256), hvConfig.DefaultMemoryMB)
assert.Equal(t, uint(1), hvConfig.DefaultVCPUs)
assert.Equal(t, "", hvConfig.BinaryPath)
assert.False(t, hvConfig.Vhost, "vhost should be false by default")
}
})

Expand Down
Loading