From f2f09230eed656a9cd06904e0b07d90d2b62e625 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Wed, 11 Sep 2019 16:30:58 +0000 Subject: [PATCH] virtcontainers: rename kataVSOCK type and move it into the types package Rename kataVSOCK to VSock and move it into the types package, this way it can be accessible by other subpackages. This change is required because in next commits the socket address and type (socket, vsock, hybrid vsock) will be hypervisor specific. Signed-off-by: Julio Montes --- virtcontainers/acrn.go | 2 +- virtcontainers/kata_agent.go | 18 ++++-------------- virtcontainers/kata_agent_test.go | 2 +- virtcontainers/qemu.go | 4 ++-- virtcontainers/qemu_arch_base.go | 10 +++++----- virtcontainers/qemu_test.go | 8 ++++---- virtcontainers/types/sandbox.go | 20 +++++++++++++++++++- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/virtcontainers/acrn.go b/virtcontainers/acrn.go index a4a431c0d0..4ecef48e5e 100644 --- a/virtcontainers/acrn.go +++ b/virtcontainers/acrn.go @@ -547,7 +547,7 @@ func (a *acrn) addDevice(devInfo interface{}, devType deviceType) error { err = nil case types.Socket: a.acrnConfig.Devices = a.arch.appendSocket(a.acrnConfig.Devices, v) - case kataVSOCK: + case types.VSock: // Not supported. return success err = nil case Endpoint: diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go index fa6297930b..a210e0a19c 100644 --- a/virtcontainers/kata_agent.go +++ b/virtcontainers/kata_agent.go @@ -145,16 +145,6 @@ type KataAgentConfig struct { KernelModules []string } -type kataVSOCK struct { - contextID uint64 - port uint32 - vhostFd *os.File -} - -func (s *kataVSOCK) String() string { - return fmt.Sprintf("%s://%d:%d", vsockSocketScheme, s.contextID, s.port) -} - // KataAgentState is the structure describing the data stored from this // agent implementation. type KataAgentState struct { @@ -340,7 +330,7 @@ func (k *kataAgent) agentURL() (string, error) { switch s := k.vmSocket.(type) { case types.Socket: return s.HostPath, nil - case kataVSOCK: + case types.VSock: return s.String(), nil case types.HybridVSock: return s.String(), nil @@ -390,12 +380,12 @@ func (k *kataAgent) configure(h hypervisor, id, sharePath string, builtin bool, if err != nil { return err } - case kataVSOCK: - s.vhostFd, s.contextID, err = utils.FindContextID() + case types.VSock: + s.VhostFd, s.ContextID, err = utils.FindContextID() if err != nil { return err } - s.port = uint32(vSockPort) + s.Port = uint32(vSockPort) if err = h.addDevice(s, vSockPCIDev); err != nil { return err } diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index cba9b01437..d5ba3c92d0 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -644,7 +644,7 @@ func TestAgentPathAPI(t *testing.T) { c.UseVSock = true err = k2.generateVMSocket(id, c) assert.Nil(err) - _, ok = k2.vmSocket.(kataVSOCK) + _, ok = k2.vmSocket.(types.VSock) assert.True(ok) } diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index ae33f846f4..7cab6e34c3 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -1591,8 +1591,8 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error { } case types.Socket: q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v) - case kataVSOCK: - q.fds = append(q.fds, v.vhostFd) + case types.VSock: + q.fds = append(q.fds, v.VhostFd) q.qemuConfig.Devices, err = q.arch.appendVSock(q.qemuConfig.Devices, v) case Endpoint: q.qemuConfig.Devices, err = q.arch.appendNetwork(q.qemuConfig.Devices, v) diff --git a/virtcontainers/qemu_arch_base.go b/virtcontainers/qemu_arch_base.go index 64792ba613..54dd2aeeb7 100644 --- a/virtcontainers/qemu_arch_base.go +++ b/virtcontainers/qemu_arch_base.go @@ -80,7 +80,7 @@ type qemuArch interface { appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device // appendVSock appends a vsock PCI to devices - appendVSock(devices []govmmQemu.Device, vsock kataVSOCK) ([]govmmQemu.Device, error) + appendVSock(devices []govmmQemu.Device, vsock types.VSock) ([]govmmQemu.Device, error) // appendNetwork appends a endpoint device to devices appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) @@ -451,12 +451,12 @@ func (q *qemuArchBase) appendSocket(devices []govmmQemu.Device, socket types.Soc return devices } -func (q *qemuArchBase) appendVSock(devices []govmmQemu.Device, vsock kataVSOCK) ([]govmmQemu.Device, error) { +func (q *qemuArchBase) appendVSock(devices []govmmQemu.Device, vsock types.VSock) ([]govmmQemu.Device, error) { devices = append(devices, govmmQemu.VSOCKDevice{ - ID: fmt.Sprintf("vsock-%d", vsock.contextID), - ContextID: vsock.contextID, - VHostFD: vsock.vhostFd, + ID: fmt.Sprintf("vsock-%d", vsock.ContextID), + ContextID: vsock.ContextID, + VHostFD: vsock.VhostFd, DisableModern: q.nestedRun, }, ) diff --git a/virtcontainers/qemu_test.go b/virtcontainers/qemu_test.go index 62799b9b2f..c64af10af1 100644 --- a/virtcontainers/qemu_test.go +++ b/virtcontainers/qemu_test.go @@ -274,10 +274,10 @@ func TestQemuAddDeviceKataVSOCK(t *testing.T) { }, } - vsock := kataVSOCK{ - contextID: contextID, - port: port, - vhostFd: vsockFile, + vsock := types.VSock{ + ContextID: contextID, + Port: port, + VhostFd: vsockFile, } testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut) diff --git a/virtcontainers/types/sandbox.go b/virtcontainers/types/sandbox.go index d2b930fc2a..4e3a623789 100644 --- a/virtcontainers/types/sandbox.go +++ b/virtcontainers/types/sandbox.go @@ -7,6 +7,7 @@ package types import ( "fmt" + "os" "strings" "github.com/opencontainers/runtime-spec/specs-go" @@ -29,7 +30,10 @@ const ( StateStopped StateString = "stopped" ) -const HybridVSockScheme = "hvsock" +const ( + HybridVSockScheme = "hvsock" + VSockScheme = "vsock" +) // SandboxState is a sandbox state structure type SandboxState struct { @@ -163,6 +167,20 @@ func (v *Volumes) String() string { return strings.Join(volSlice, " ") } +// VSock defines a virtio-socket to communicate between +// the host and any process inside the VM. +// This kind of socket is not supported in all hypervisors. +// QEMU and NEMU support it. +type VSock struct { + ContextID uint64 + Port uint32 + VhostFd *os.File +} + +func (s *VSock) String() string { + return fmt.Sprintf("%s://%d:%d", VSockScheme, s.ContextID, s.Port) +} + // HybridVSock defines a hybrid vsocket to communicate between // the host and any process inside the VM. // This is a virtio-vsock implementation based on AF_VSOCK on the