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 <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2019-09-11 16:30:58 +00:00
parent f42dd7d115
commit f2f09230ee
7 changed files with 36 additions and 28 deletions

View File

@ -547,7 +547,7 @@ func (a *acrn) addDevice(devInfo interface{}, devType deviceType) error {
err = nil err = nil
case types.Socket: case types.Socket:
a.acrnConfig.Devices = a.arch.appendSocket(a.acrnConfig.Devices, v) a.acrnConfig.Devices = a.arch.appendSocket(a.acrnConfig.Devices, v)
case kataVSOCK: case types.VSock:
// Not supported. return success // Not supported. return success
err = nil err = nil
case Endpoint: case Endpoint:

View File

@ -145,16 +145,6 @@ type KataAgentConfig struct {
KernelModules []string 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 // KataAgentState is the structure describing the data stored from this
// agent implementation. // agent implementation.
type KataAgentState struct { type KataAgentState struct {
@ -340,7 +330,7 @@ func (k *kataAgent) agentURL() (string, error) {
switch s := k.vmSocket.(type) { switch s := k.vmSocket.(type) {
case types.Socket: case types.Socket:
return s.HostPath, nil return s.HostPath, nil
case kataVSOCK: case types.VSock:
return s.String(), nil return s.String(), nil
case types.HybridVSock: case types.HybridVSock:
return s.String(), nil return s.String(), nil
@ -390,12 +380,12 @@ func (k *kataAgent) configure(h hypervisor, id, sharePath string, builtin bool,
if err != nil { if err != nil {
return err return err
} }
case kataVSOCK: case types.VSock:
s.vhostFd, s.contextID, err = utils.FindContextID() s.VhostFd, s.ContextID, err = utils.FindContextID()
if err != nil { if err != nil {
return err return err
} }
s.port = uint32(vSockPort) s.Port = uint32(vSockPort)
if err = h.addDevice(s, vSockPCIDev); err != nil { if err = h.addDevice(s, vSockPCIDev); err != nil {
return err return err
} }

View File

@ -644,7 +644,7 @@ func TestAgentPathAPI(t *testing.T) {
c.UseVSock = true c.UseVSock = true
err = k2.generateVMSocket(id, c) err = k2.generateVMSocket(id, c)
assert.Nil(err) assert.Nil(err)
_, ok = k2.vmSocket.(kataVSOCK) _, ok = k2.vmSocket.(types.VSock)
assert.True(ok) assert.True(ok)
} }

View File

@ -1591,8 +1591,8 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error {
} }
case types.Socket: case types.Socket:
q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v) q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v)
case kataVSOCK: case types.VSock:
q.fds = append(q.fds, v.vhostFd) q.fds = append(q.fds, v.VhostFd)
q.qemuConfig.Devices, err = q.arch.appendVSock(q.qemuConfig.Devices, v) q.qemuConfig.Devices, err = q.arch.appendVSock(q.qemuConfig.Devices, v)
case Endpoint: case Endpoint:
q.qemuConfig.Devices, err = q.arch.appendNetwork(q.qemuConfig.Devices, v) q.qemuConfig.Devices, err = q.arch.appendNetwork(q.qemuConfig.Devices, v)

View File

@ -80,7 +80,7 @@ type qemuArch interface {
appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device
// appendVSock appends a vsock PCI to devices // 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 appends a endpoint device to devices
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) 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 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, devices = append(devices,
govmmQemu.VSOCKDevice{ govmmQemu.VSOCKDevice{
ID: fmt.Sprintf("vsock-%d", vsock.contextID), ID: fmt.Sprintf("vsock-%d", vsock.ContextID),
ContextID: vsock.contextID, ContextID: vsock.ContextID,
VHostFD: vsock.vhostFd, VHostFD: vsock.VhostFd,
DisableModern: q.nestedRun, DisableModern: q.nestedRun,
}, },
) )

View File

@ -274,10 +274,10 @@ func TestQemuAddDeviceKataVSOCK(t *testing.T) {
}, },
} }
vsock := kataVSOCK{ vsock := types.VSock{
contextID: contextID, ContextID: contextID,
port: port, Port: port,
vhostFd: vsockFile, VhostFd: vsockFile,
} }
testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut) testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut)

View File

@ -7,6 +7,7 @@ package types
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
@ -29,7 +30,10 @@ const (
StateStopped StateString = "stopped" StateStopped StateString = "stopped"
) )
const HybridVSockScheme = "hvsock" const (
HybridVSockScheme = "hvsock"
VSockScheme = "vsock"
)
// SandboxState is a sandbox state structure // SandboxState is a sandbox state structure
type SandboxState struct { type SandboxState struct {
@ -163,6 +167,20 @@ func (v *Volumes) String() string {
return strings.Join(volSlice, " ") 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 // HybridVSock defines a hybrid vsocket to communicate between
// the host and any process inside the VM. // the host and any process inside the VM.
// This is a virtio-vsock implementation based on AF_VSOCK on the // This is a virtio-vsock implementation based on AF_VSOCK on the