virtcontainers: implement function to cold plug vsocks

`appendVSockPCI` function can be used to cold plug vocks, vhost file descriptor
holds the context ID and it's inherit by QEMU process, ID must be unique and
disable-modern prevents qemu from relying on fast MMIO.

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2018-07-25 09:04:29 -05:00
parent 1515bd07a1
commit 052769196d
3 changed files with 54 additions and 0 deletions

View File

@ -62,6 +62,10 @@ type qemu struct {
state QemuState state QemuState
arch qemuArch arch qemuArch
// fds is a list of file descriptors inherited by QEMU process
// they'll be closed once QEMU process is running
fds []*os.File
} }
const ( const (
@ -497,6 +501,14 @@ func (q *qemu) startSandbox() error {
q.Logger().WithField("default-kernel-parameters", formatted).Debug() q.Logger().WithField("default-kernel-parameters", formatted).Debug()
} }
defer func() {
for _, fd := range q.fds {
if err := fd.Close(); err != nil {
q.Logger().WithError(err).Error("After launching Qemu")
}
}
}()
strErr, err := govmmQemu.LaunchQemu(q.qemuConfig, newQMPLogger()) strErr, err := govmmQemu.LaunchQemu(q.qemuConfig, newQMPLogger())
if err != nil { if err != nil {
return fmt.Errorf("%s", strErr) return fmt.Errorf("%s", strErr)
@ -956,6 +968,9 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error {
q.qemuConfig.Devices = q.arch.append9PVolume(q.qemuConfig.Devices, v) q.qemuConfig.Devices = q.arch.append9PVolume(q.qemuConfig.Devices, v)
case Socket: case Socket:
q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v) q.qemuConfig.Devices = q.arch.appendSocket(q.qemuConfig.Devices, v)
case kataVSOCK:
q.fds = append(q.fds, v.vhostFd)
q.qemuConfig.Devices = q.arch.appendVSockPCI(q.qemuConfig.Devices, v)
case Endpoint: case Endpoint:
q.qemuConfig.Devices = q.arch.appendNetwork(q.qemuConfig.Devices, v) q.qemuConfig.Devices = q.arch.appendNetwork(q.qemuConfig.Devices, v)
case deviceDrivers.Drive: case deviceDrivers.Drive:

View File

@ -68,6 +68,9 @@ type qemuArch interface {
// appendSocket appends a socket to devices // appendSocket appends a socket to devices
appendSocket(devices []govmmQemu.Device, socket Socket) []govmmQemu.Device appendSocket(devices []govmmQemu.Device, socket Socket) []govmmQemu.Device
// appendVSockPCI appends a vsock PCI to devices
appendVSockPCI(devices []govmmQemu.Device, vsock kataVSOCK) []govmmQemu.Device
// appendNetwork appends a endpoint device to devices // appendNetwork appends a endpoint device to devices
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device
@ -389,6 +392,20 @@ func (q *qemuArchBase) appendSocket(devices []govmmQemu.Device, socket Socket) [
return devices return devices
} }
func (q *qemuArchBase) appendVSockPCI(devices []govmmQemu.Device, vsock kataVSOCK) []govmmQemu.Device {
devices = append(devices,
govmmQemu.VSOCKDevice{
ID: fmt.Sprintf("vsock-%d", vsock.contextID),
ContextID: vsock.contextID,
VHostFD: vsock.vhostFd,
DisableModern: q.nestedRun,
},
)
return devices
}
func networkModelToQemuType(model NetInterworkingModel) govmmQemu.NetDeviceType { func networkModelToQemuType(model NetInterworkingModel) govmmQemu.NetDeviceType {
switch model { switch model {
case NetXConnectBridgedModel: case NetXConnectBridgedModel:

View File

@ -246,6 +246,28 @@ func TestQemuAddDeviceSerialPortDev(t *testing.T) {
testQemuAddDevice(t, socket, serialPortDev, expectedOut) testQemuAddDevice(t, socket, serialPortDev, expectedOut)
} }
func TestQemuAddDeviceKataVSOCK(t *testing.T) {
contextID := uint32(3)
port := uint32(1024)
vHostFD := os.NewFile(1, "vsock")
expectedOut := []govmmQemu.Device{
govmmQemu.VSOCKDevice{
ID: fmt.Sprintf("vsock-%d", contextID),
ContextID: contextID,
VHostFD: vHostFD,
},
}
vsock := kataVSOCK{
contextID: contextID,
port: port,
vhostFd: vHostFD,
}
testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut)
}
func TestQemuGetSandboxConsole(t *testing.T) { func TestQemuGetSandboxConsole(t *testing.T) {
q := &qemu{} q := &qemu{}
sandboxID := "testSandboxID" sandboxID := "testSandboxID"