Skip to content

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: kata-containers#2035

Signed-off-by: Alice Frosi <[email protected]>
  • Loading branch information
Alice Frosi committed Sep 5, 2019
1 parent 94c47dc commit ac965b9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 79 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
48 changes: 26 additions & 22 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,15 @@ 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 +552,14 @@ 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 +603,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
78 changes: 27 additions & 51 deletions virtcontainers/qemu_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,17 @@ func (q *qemuS390x) bridges(number uint32) {

// appendConsole appends a console to devices.
// The function has been overwriten to correctly set the driver to the CCW device
func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) []govmmQemu.Device {
func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
id := "serial0"
addr, b, err := q.addDeviceToBridge(id, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append console")
return devices
return devices, fmt.Errorf("Failed to append console %v", err)
}

var devno string
devno, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append console")
return devices
return devices, fmt.Errorf("Failed to append console %v", err)
}

serial := govmmQemu.SerialDevice{
Expand All @@ -126,37 +124,24 @@ func (q *qemuS390x) appendConsole(devices []govmmQemu.Device, path string) []gov

devices = append(devices, console)

return devices
return devices, nil
}

func (q *qemuS390x) appendImage(devices []govmmQemu.Device, path string) ([]govmmQemu.Device, error) {
drive, err := genericImage(path)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append image")
return nil, err
}

return q.appendBlockDevice(devices, drive), nil
}

func (q *qemuS390x) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) []govmmQemu.Device {
func (q *qemuS390x) appendBlockDevice(devices []govmmQemu.Device, drive config.BlockDrive) ([]govmmQemu.Device, error) {
d, err := genericBlockDevice(drive, false)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
addr, b, err := q.addDeviceToBridge(drive.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append blk-dev")
return devices
return devices, fmt.Errorf("Failed to append blk-dev %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}

// appendVhostUserDevice throws an error if vhost devices are tried to be used.
Expand All @@ -171,39 +156,34 @@ func (q *qemuS390x) supportGuestMemoryHotplug() bool {
return false
}

func (q *qemuS390x) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) []govmmQemu.Device {
func (q *qemuS390x) appendNetwork(devices []govmmQemu.Device, endpoint Endpoint) ([]govmmQemu.Device, error) {
d, err := genericNetwork(endpoint, false, false, q.networkIndex)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
q.networkIndex++
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append network")
return devices
return devices, fmt.Errorf("Failed to append network %v", err)
}

devices = append(devices, d)
return devices
return devices, nil
}

func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) []govmmQemu.Device {
func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RNGDev) ([]govmmQemu.Device, error) {
addr, b, err := q.addDeviceToBridge(rngDev.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append RNG-Device")
return devices
return devices, fmt.Errorf("Failed to append RNG-Device %v", err)
}
var devno string
devno, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append RNG-Device")
return devices
return devices, fmt.Errorf("Failed to append RNG-Device %v", err)
}

devices = append(devices,
Expand All @@ -214,46 +194,42 @@ func (q *qemuS390x) appendRNGDevice(devices []govmmQemu.Device, rngDev config.RN
},
)

return devices
return devices, nil
}

func (q *qemuS390x) append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device {
func (q *qemuS390x) append9PVolume(devices []govmmQemu.Device, volume types.Volume) ([]govmmQemu.Device, error) {
if volume.MountTag == "" || volume.HostPath == "" {
return devices
return devices, nil
}
d := generic9PVolume(volume, false)
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append 9p-Volume")
return devices
return devices, fmt.Errorf("Failed to append 9p-Volume %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append 9p-Volume")
return devices
return devices, fmt.Errorf("Failed to append 9p-Volume %v", err)
}
devices = append(devices, d)
return devices
return devices, nil
}

// appendBridges appends to devices the given bridges
func (q *qemuS390x) appendBridges(devices []govmmQemu.Device) []govmmQemu.Device {
return genericAppendBridges(devices, q.Bridges, q.machineType)
}

func (q *qemuS390x) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread) {
func (q *qemuS390x) appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread, error) {
d, t := genericSCSIController(enableIOThreads, q.nestedRun)
addr, b, err := q.addDeviceToBridge(d.ID, types.CCW)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append scsi-controller")
return devices, nil
return devices, nil, fmt.Errorf("Failed to append scsi-controller %v", err)
}
d.DevNo, err = b.AddressFormatCCW(addr)
if err != nil {
virtLog.WithField("subsystem", "qemus390x").WithError(err).Error("Failed to append scsi-controller")
return devices, nil
return devices, nil, fmt.Errorf("Failed to append scsi-controller %v", err)
}

devices = append(devices, d)
return devices, t
return devices, t, nil
}

0 comments on commit ac965b9

Please sign in to comment.