From 4680e58e08d0b667fa1f006216bb14040b30eae4 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Mon, 23 Jul 2018 18:02:38 -0500 Subject: [PATCH] cli: add configuration option to enable/disable vsocks Add `use_vsock` option to enable or disable the use of vsocks for communication between host and guest. Signed-off-by: Jose Carlos Venegas Munoz Signed-off-by: Julio Montes --- cli/config.go | 24 ++++++++++++ cli/config/configuration.toml.in | 6 +++ cli/config_test.go | 67 +++++++++++++++++++++++++++++++- cli/kata-env.go | 7 +++- virtcontainers/hypervisor.go | 3 ++ 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/cli/config.go b/cli/config.go index a800f19f7..c859f2cbd 100644 --- a/cli/config.go +++ b/cli/config.go @@ -15,6 +15,7 @@ import ( "github.com/BurntSushi/toml" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" + "github.com/kata-containers/runtime/virtcontainers/utils" "github.com/sirupsen/logrus" ) @@ -91,6 +92,7 @@ type hypervisor struct { Debug bool `toml:"enable_debug"` DisableNestingChecks bool `toml:"disable_nesting_checks"` EnableIOThreads bool `toml:"enable_iothreads"` + UseVSock bool `toml:"use_vsock"` } type proxy struct { @@ -267,6 +269,10 @@ func (h hypervisor) msize9p() uint32 { return h.Msize9p } +func (h hypervisor) useVSock() bool { + return h.UseVSock +} + func (p proxy) path() string { if p.Path == "" { return defaultProxyPath @@ -333,6 +339,16 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { return vc.HypervisorConfig{}, err } + useVSock := false + if h.useVSock() { + if utils.SupportsVsocks() { + kataLog.Info("vsock supported") + useVSock = true + } else { + kataLog.Warn("No vsock support, falling back to legacy serial port") + } + } + return vc.HypervisorConfig{ HypervisorPath: hypervisor, KernelPath: kernel, @@ -355,6 +371,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { BlockDeviceDriver: blockDriver, EnableIOThreads: h.EnableIOThreads, Msize9p: h.msize9p(), + UseVSock: useVSock, }, nil } @@ -544,6 +561,13 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat return "", config, err } + // use no proxy if HypervisorConfig.UseVSock is true + if config.HypervisorConfig.UseVSock { + kataLog.Info("VSOCK supported, configure to not use proxy") + config.ProxyType = vc.NoProxyType + config.ProxyConfig = vc.ProxyConfig{} + } + return resolved, config, nil } diff --git a/cli/config/configuration.toml.in b/cli/config/configuration.toml.in index b7517284e..f98ae536d 100644 --- a/cli/config/configuration.toml.in +++ b/cli/config/configuration.toml.in @@ -134,6 +134,12 @@ enable_iothreads = @DEFENABLEIOTHREADS@ # used for 9p packet payload. #msize_9p = @DEFMSIZE9P@ +# If true and vsocks are supported, use vsocks to communicate directly +# with the agent and no proxy is started, otherwise use unix +# sockets and start a proxy to communicate with the agent. +# Default false +#use_vsock = 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 diff --git a/cli/config_test.go b/cli/config_test.go index 9b79d5bab..5a29377d2 100644 --- a/cli/config_test.go +++ b/cli/config_test.go @@ -20,6 +20,7 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" + "github.com/kata-containers/runtime/virtcontainers/utils" "github.com/stretchr/testify/assert" ) @@ -552,6 +553,52 @@ func TestMinimalRuntimeConfig(t *testing.T) { t.Fatalf("Got %+v\n expecting %+v", config, expectedConfig) } + // minimal config with vsock enabled + runtimeMinimalConfig = ` + # Runtime configuration file + [hypervisor.qemu] + use_vsock = true + + [proxy.kata] + path = "` + proxyPath + `" + + [shim.kata] + path = "` + shimPath + `" + + [agent.kata] +` + orgVHostVSockDevicePath := utils.VHostVSockDevicePath + orgVSockDevicePath := utils.VSockDevicePath + defer func() { + utils.VHostVSockDevicePath = orgVHostVSockDevicePath + utils.VSockDevicePath = orgVSockDevicePath + }() + utils.VHostVSockDevicePath = "/dev/null" + utils.VSockDevicePath = "/dev/null" + + configPath = path.Join(dir, "runtime.toml") + err = createConfig(configPath, runtimeMinimalConfig) + if err != nil { + t.Fatal(err) + } + + _, config, err = loadConfiguration(configPath, false) + if err != nil { + t.Fatal(err) + } + + if config.ProxyType != vc.NoProxyType { + t.Fatalf("Proxy type must be NoProxy, got %+v", config.ProxyType) + } + + if !reflect.DeepEqual(config.ProxyConfig, vc.ProxyConfig{}) { + t.Fatalf("Got %+v\n expecting %+v", config.ProxyConfig, vc.ProxyConfig{}) + } + + if config.HypervisorConfig.UseVSock != true { + t.Fatalf("use_vsock must be true, got %v", config.HypervisorConfig.UseVSock) + } + if err := os.Remove(configPath); err != nil { t.Fatal(err) } @@ -570,6 +617,14 @@ func TestNewQemuHypervisorConfig(t *testing.T) { machineType := "machineType" disableBlock := true enableIOThreads := true + orgVSockDevicePath := utils.VSockDevicePath + orgVHostVSockDevicePath := utils.VHostVSockDevicePath + defer func() { + utils.VSockDevicePath = orgVSockDevicePath + utils.VHostVSockDevicePath = orgVHostVSockDevicePath + }() + utils.VSockDevicePath = "/dev/abc/xyz" + utils.VHostVSockDevicePath = "/dev/abc/xyz" hypervisor := hypervisor{ Path: hypervisorPath, @@ -578,6 +633,7 @@ func TestNewQemuHypervisorConfig(t *testing.T) { MachineType: machineType, DisableBlockDeviceUse: disableBlock, EnableIOThreads: enableIOThreads, + UseVSock: true, } files := []string{hypervisorPath, kernelPath, imagePath} @@ -597,12 +653,21 @@ func TestNewQemuHypervisorConfig(t *testing.T) { } } - // all paths exist now + // falling back to legacy serial port config, err := newQemuHypervisorConfig(hypervisor) if err != nil { t.Fatal(err) } + utils.VSockDevicePath = "/dev/null" + utils.VHostVSockDevicePath = "/dev/null" + + // all paths exist now + config, err = newQemuHypervisorConfig(hypervisor) + if err != nil { + t.Fatal(err) + } + if config.HypervisorPath != hypervisor.Path { t.Errorf("Expected hypervisor path %v, got %v", hypervisor.Path, config.HypervisorPath) } diff --git a/cli/kata-env.go b/cli/kata-env.go index 150b51ca7..6f9a864cb 100644 --- a/cli/kata-env.go +++ b/cli/kata-env.go @@ -11,19 +11,20 @@ import ( "os" "strings" + runtim "runtime" + "github.com/BurntSushi/toml" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/urfave/cli" - runtim "runtime" ) // Semantic version for the output of the command. // // XXX: Increment for every change to the output format // (meaning any change to the EnvInfo type). -const formatVersion = "1.0.12" +const formatVersion = "1.0.13" // MetaInfo stores information on the format of the output itself type MetaInfo struct { @@ -80,6 +81,7 @@ type HypervisorInfo struct { BlockDeviceDriver string Msize9p uint32 Debug bool + UseVSock bool } // ProxyInfo stores proxy details @@ -276,6 +278,7 @@ func getHypervisorInfo(config oci.RuntimeConfig) HypervisorInfo { Path: hypervisorPath, BlockDeviceDriver: config.HypervisorConfig.BlockDeviceDriver, Msize9p: config.HypervisorConfig.Msize9p, + UseVSock: config.HypervisorConfig.UseVSock, } } diff --git a/virtcontainers/hypervisor.go b/virtcontainers/hypervisor.go index 1e8a04e12..334658471 100644 --- a/virtcontainers/hypervisor.go +++ b/virtcontainers/hypervisor.go @@ -217,6 +217,9 @@ type HypervisorConfig struct { // Msize9p is used as the msize for 9p shares Msize9p uint32 + // UseVSock use a vsock for agent communication + UseVSock bool + // BootToBeTemplate used to indicate if the VM is created to be a template VM BootToBeTemplate bool