Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainer: add error return code
Browse files Browse the repository at this point in the history
Add error return code to append functions.

Fixes: #2035

Signed-off-by: Alice Frosi <[email protected]>
  • Loading branch information
Alice Frosi committed Sep 5, 2019
1 parent 94c47dc commit e3f92fe
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 89 deletions.
18 changes: 12 additions & 6 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ func (q *qemu) buildDevices(initrdPath string) ([]govmmQemu.Device, *govmmQemu.I
// bridge gets the first available PCI address i.e bridgePCIStartAddr
devices = q.arch.appendBridges(devices)

devices = q.arch.appendConsole(devices, console)
devices, err = q.arch.appendConsole(devices, console)
if err != nil {
return nil, nil, err
}

if initrdPath == "" {
devices, err = q.appendImage(devices)
Expand All @@ -418,7 +421,7 @@ func (q *qemu) buildDevices(initrdPath string) ([]govmmQemu.Device, *govmmQemu.I

var ioThread *govmmQemu.IOThread
if q.config.BlockDeviceDriver == config.VirtioSCSI {
devices, ioThread = q.arch.appendSCSIController(devices, q.config.EnableIOThreads)
return q.arch.appendSCSIController(devices, q.config.EnableIOThreads)
}

return devices, ioThread, nil
Expand Down Expand Up @@ -590,7 +593,10 @@ func (q *qemu) createSandbox(ctx context.Context, id string, networkNS NetworkNa
ID: rngID,
Filename: q.config.EntropySource,
}
qemuConfig.Devices = q.arch.appendRNGDevice(qemuConfig.Devices, rngDev)
qemuConfig.Devices, err = q.arch.appendRNGDevice(qemuConfig.Devices, rngDev)
if err != nil {
return err
}

q.qemuConfig = qemuConfig

Expand Down Expand Up @@ -1578,17 +1584,17 @@ func (q *qemu) addDevice(devInfo interface{}, devType deviceType) error {
q.qemuConfig.Devices, err = q.arch.appendVhostUserDevice(q.qemuConfig.Devices, vhostDev)
} else {
q.Logger().WithField("volume-type", "virtio-9p").Info("adding volume")
q.qemuConfig.Devices = q.arch.append9PVolume(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.append9PVolume(q.qemuConfig.Devices, v)
}
case types.Socket:
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:
q.qemuConfig.Devices = q.arch.appendNetwork(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.appendNetwork(q.qemuConfig.Devices, v)
case config.BlockDrive:
q.qemuConfig.Devices = q.arch.appendBlockDevice(q.qemuConfig.Devices, v)
q.qemuConfig.Devices, err = q.arch.appendBlockDevice(q.qemuConfig.Devices, v)
case config.VhostUserDeviceAttrs:
q.qemuConfig.Devices, err = q.arch.appendVhostUserDevice(q.qemuConfig.Devices, v)
case config.VFIODev:
Expand Down
50 changes: 26 additions & 24 deletions virtcontainers/qemu_arch_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ type qemuArch interface {
memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8) govmmQemu.Memory

// appendConsole appends a console to devices
appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device
appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error)

// appendImage appends an image to devices
appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error)

// appendSCSIController appens a SCSI controller to devices
appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread)
appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error)

// appendBridges appends bridges to devices
appendBridges(devices []govmmQemu.Device) []govmmQemu.Device

// append9PVolume appends a 9P volume to devices
append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device
append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error)

// appendSocket appends a socket to devices
appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device
Expand All @@ -83,10 +83,10 @@ type qemuArch interface {
appendVSockPCI(devices []govmmQemu.Device, vsock kataVSOCK) []govmmQemu.Device

// appendNetwork appends a endpoint device to devices
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device
appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error)

// appendBlockDevice appends a block drive to devices
appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device
appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error)

// appendVhostUserDevice appends a vhost user device to devices
appendVhostUserDevice(devices []govmmQemu.Device, drive config.VhostUserDeviceAttrs) ([]govmmQemu.Device, error)
Expand All @@ -95,7 +95,7 @@ type qemuArch interface {
appendVFIODevice(devices []govmmQemu.Device, vfioDevice config.VFIODev) []govmmQemu.Device

// appendRNGDevice appends a RNG device to devices
appendRNGDevice(devices []govmmQemu.Device, rngDevice config.RNGDev) []govmmQemu.Device
appendRNGDevice(devices []govmmQemu.Device, rngDevice config.RNGDev) ([]govmmQemu.Device, error)

// addDeviceToBridge adds devices to the bus
addDeviceToBridge(ID string, t types.Type) (string, types.Bridge, error)
Expand Down Expand Up @@ -293,7 +293,7 @@ func (q *qemuArchBase) memoryTopology(memoryMb, hostMemoryMb uint64, slots uint8
return memory
}

func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device {
func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
serial := govmmQemu.SerialDevice{
Driver: govmmQemu.VirtioSerial,
ID: "serial0",
Expand All @@ -312,7 +312,7 @@ func (q *qemuArchBase) appendConsole(devices []govmmQemu.Device, path string) []

devices = append(devices, console)

return devices
return devices, nil
}

func genericImage(path string) (config.BlockDrive, error) {
Expand Down Expand Up @@ -341,7 +341,11 @@ func (q *qemuArchBase) appendImage(devices []govmmQemu.Device, path string) ([]g
if err != nil {
return nil, err
}
return q.appendBlockDevice(devices, drive), nil
devices, err = q.appendBlockDevice(devices, drive)
if err != nil {
return nil, err
}
return devices, nil
}

func genericSCSIController(enableIOThreads, nestedRun bool) (govmmQemu.SCSIController, *govmmQemu.IOThread) {
Expand All @@ -365,10 +369,10 @@ func genericSCSIController(enableIOThreads, nestedRun bool) (govmmQemu.SCSIContr
return scsiController, t
}

func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) {
func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error) {
d, t := genericSCSIController(enableIOThreads, q.nestedRun)
devices = append(devices, d)
return devices, t
return devices, t, nil
}

// appendBridges appends to devices the given bridges
Expand Down Expand Up @@ -417,14 +421,14 @@ func generic9PVolume(volume types.Volume, nestedRun bool) govmmQemu.FSDevice {
}
}

func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device {
func (q *qemuArchBase) append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error) {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
return devices, nil
}

d := generic9PVolume(volume, q.nestedRun)
devices = append(devices, d)
return devices
return devices, nil
}

func (q *qemuArchBase) appendSocket(devices []govmmQemu.Device, socket types.Socket) []govmmQemu.Device {
Expand Down Expand Up @@ -517,15 +521,14 @@ func genericNetwork(endpoint Endpoint, vhost, nestedRun bool, index int) (govmmQ
return d, nil
}

func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device {
func (q *qemuArchBase) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) {
d, err := genericNetwork(endpoint, q.vhost, q.nestedRun, q.networkIndex)
if err != nil {
virtLog.WithField("subsystem", "qemuArch").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
q.networkIndex++
devices = append(devices, d)
return devices
return devices, nil
}

func genericBlockDevice(drive config.BlockDrive, nestedRun bool) (govmmQemu.BlockDevice, error) {
Expand All @@ -548,14 +551,13 @@ func genericBlockDevice(drive config.BlockDrive, nestedRun bool) (govmmQemu.Bloc
}, nil
}

func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device {
func (q *qemuArchBase) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error) {
d, err := genericBlockDevice(drive, q.nestedRun)
if err != nil {
virtLog.WithField("subsystem", "qemuArch").WithError(err).Error("Failed to append block device")
return devices
return devices, fmt.Errorf("Failed to append block device %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}

func (q *qemuArchBase) appendVhostUserDevice(devices []govmmQemu.Device, attr config.VhostUserDeviceAttrs) ([]govmmQemu.Device, error) {
Expand Down Expand Up @@ -599,15 +601,15 @@ func (q *qemuArchBase) appendVFIODevice(devices []govmmQemu.Device, vfioDev conf
return devices
}

func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) []govmmQemu.Device {
func (q *qemuArchBase) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) ([]govmmQemu.Device, error) {
devices = append(devices,
govmmQemu.RngDevice{
ID: rngDev.ID,
Filename: rngDev.Filename,
},
)

return devices
return devices, nil
}

func (q *qemuArchBase) handleImagePath(config HypervisorConfig) {
Expand Down
21 changes: 14 additions & 7 deletions virtcontainers/qemu_arch_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ func testQemuArchBaseAppend(t *testing.T, structure interface{}, expected []govm

switch s := structure.(type) {
case types.Volume:
devices = qemuArchBase.append9PVolume(devices, s)
devices, err = qemuArchBase.append9PVolume(devices, s)
case types.Socket:
devices = qemuArchBase.appendSocket(devices, s)
case config.BlockDrive:
devices = qemuArchBase.appendBlockDevice(devices, s)
devices, err = qemuArchBase.appendBlockDevice(devices, s)
case config.VFIODev:
devices = qemuArchBase.appendVFIODevice(devices, s)
case config.VhostUserDeviceAttrs:
Expand All @@ -255,6 +255,7 @@ func testQemuArchBaseAppend(t *testing.T, structure interface{}, expected []govm

func TestQemuArchBaseAppendConsoles(t *testing.T) {
var devices []govmmQemu.Device
var err error
assert := assert.New(t)
qemuArchBase := newQemuArchBase()

Expand All @@ -274,7 +275,8 @@ func TestQemuArchBaseAppendConsoles(t *testing.T) {
},
}

devices = qemuArchBase.appendConsole(devices, path)
devices, err = qemuArchBase.appendConsole(devices, path)
assert.NoError(err)
assert.Equal(expectedOut, devices)
}

Expand Down Expand Up @@ -484,16 +486,19 @@ func TestQemuArchBaseAppendSCSIController(t *testing.T) {
},
}

devices, ioThread := qemuArchBase.appendSCSIController(devices, false)
devices, ioThread, err := qemuArchBase.appendSCSIController(devices, false)
assert.Equal(expectedOut, devices)
assert.Nil(ioThread)
assert.NoError(err)

_, ioThread = qemuArchBase.appendSCSIController(devices, true)
_, ioThread, err = qemuArchBase.appendSCSIController(devices, true)
assert.NotNil(ioThread)
assert.NoError(err)
}

func TestQemuArchBaseAppendNetwork(t *testing.T) {
var devices []govmmQemu.Device
var err error
assert := assert.New(t)
qemuArchBase := newQemuArchBase()

Expand Down Expand Up @@ -551,7 +556,9 @@ func TestQemuArchBaseAppendNetwork(t *testing.T) {
},
}

devices = qemuArchBase.appendNetwork(devices, macvlanEp)
devices = qemuArchBase.appendNetwork(devices, macvtapEp)
devices, err = qemuArchBase.appendNetwork(devices, macvlanEp)
assert.NoError(err)
devices, err = qemuArchBase.appendNetwork(devices, macvtapEp)
assert.NoError(err)
assert.Equal(expectedOut, devices)
}
6 changes: 5 additions & 1 deletion virtcontainers/qemu_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ func (q *qemuPPC64le) appendImage(devices []govmmQemu.Device, path string) ([]go
ID: id,
}

return q.appendBlockDevice(devices, drive), nil
devices, err = q.appendBlockDevice(devices, drive)
if err != nil {
return nil, err
}
return devices, nil
}

// appendBridges appends to devices the given bridges
Expand Down
Loading

0 comments on commit e3f92fe

Please sign in to comment.