diff --git a/virtcontainers/acrn.go b/virtcontainers/acrn.go index a4a431c0d..4ecef48e5 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 fa6297930..a210e0a19 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 cba9b0143..d5ba3c92d 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 ae33f846f..7cab6e34c 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 64792ba61..54dd2aeeb 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 62799b9b2..c64af10af 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 d2b930fc2..4e3a62378 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