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

Commit

Permalink
virtcontainers: Add Bridge to the types package
Browse files Browse the repository at this point in the history
Bridge is representing a PCI/E bridge, so we're moving the bridge*.go
to types/pci*.go.

Fixes: #1119

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
  • Loading branch information
Samuel Ortiz committed Jan 16, 2019
1 parent b25f43e commit 2e1ddbc
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 55 deletions.
30 changes: 15 additions & 15 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type CPUDevice struct {

// QemuState keeps Qemu's state
type QemuState struct {
Bridges []Bridge
Bridges []types.PCIBridge
// HotpluggedCPUs is the list of CPUs that were hot-added
HotpluggedVCPUs []CPUDevice
HotpluggedMemory int
Expand Down Expand Up @@ -722,25 +722,25 @@ func (q *qemu) qmpShutdown() {
}
}

func (q *qemu) addDeviceToBridge(ID string) (string, Bridge, error) {
func (q *qemu) addDeviceToBridge(ID string) (string, types.PCIBridge, error) {
var err error
var addr uint32

// looking for an empty address in the bridges
for _, b := range q.state.Bridges {
addr, err = b.addDevice(ID)
addr, err = b.AddDevice(ID)
if err == nil {
return fmt.Sprintf("%02x", addr), b, nil
}
}

return "", Bridge{}, err
return "", types.PCIBridge{}, err
}

func (q *qemu) removeDeviceFromBridge(ID string) error {
var err error
for _, b := range q.state.Bridges {
err = b.removeDevice(ID)
err = b.RemoveDevice(ID)
if err == nil {
// device was removed correctly
return nil
Expand Down Expand Up @@ -1377,7 +1377,7 @@ func (q *qemu) resizeMemory(reqMemMB uint32, memoryBlockSizeMB uint32) (uint32,
}

// genericAppendBridges appends to devices the given bridges
func genericAppendBridges(devices []govmmQemu.Device, bridges []Bridge, machineType string) []govmmQemu.Device {
func genericAppendBridges(devices []govmmQemu.Device, bridges []types.PCIBridge, machineType string) []govmmQemu.Device {
bus := defaultPCBridgeBus
switch machineType {
case QemuQ35, QemuVirt:
Expand All @@ -1386,7 +1386,7 @@ func genericAppendBridges(devices []govmmQemu.Device, bridges []Bridge, machineT

for idx, b := range bridges {
t := govmmQemu.PCIBridge
if b.Type == pcieBridge {
if b.Type == types.PCIE {
t = govmmQemu.PCIEBridge
}

Expand All @@ -1408,29 +1408,29 @@ func genericAppendBridges(devices []govmmQemu.Device, bridges []Bridge, machineT
return devices
}

func genericBridges(number uint32, machineType string) []Bridge {
var bridges []Bridge
var bt bridgeType
func genericBridges(number uint32, machineType string) []types.PCIBridge {
var bridges []types.PCIBridge
var bt types.PCIType

switch machineType {
case QemuQ35:
// currently only pci bridges are supported
// qemu-2.10 will introduce pcie bridges
fallthrough
case QemuPC:
bt = pciBridge
bt = types.PCI
case QemuVirt:
bt = pcieBridge
bt = types.PCIE
case QemuPseries:
bt = pciBridge
bt = types.PCI
case QemuCCWVirtio:
bt = pciBridge
bt = types.PCI
default:
return nil
}

for i := uint32(0); i < number; i++ {
bridges = append(bridges, Bridge{
bridges = append(bridges, types.PCIBridge{
Type: bt,
ID: fmt.Sprintf("%s-bridge-%d", bt, i),
Address: make(map[uint32]string),
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/qemu_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (q *qemuAmd64) capabilities() types.Capabilities {
return caps
}

func (q *qemuAmd64) bridges(number uint32) []Bridge {
func (q *qemuAmd64) bridges(number uint32) []types.PCIBridge {
return genericBridges(number, q.machineType)
}

Expand Down Expand Up @@ -160,6 +160,6 @@ func (q *qemuAmd64) appendImage(devices []govmmQemu.Device, path string) ([]govm
}

// appendBridges appends to devices the given bridges
func (q *qemuAmd64) appendBridges(devices []govmmQemu.Device, bridges []Bridge) []govmmQemu.Device {
func (q *qemuAmd64) appendBridges(devices []govmmQemu.Device, bridges []types.PCIBridge) []govmmQemu.Device {
return genericAppendBridges(devices, bridges, q.machineType)
}
9 changes: 5 additions & 4 deletions virtcontainers/qemu_amd64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

govmmQemu "github.com/intel/govmm/qemu"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -43,8 +44,8 @@ func TestQemuAmd64Bridges(t *testing.T) {
assert.Len(bridges, len)

for i, b := range bridges {
id := fmt.Sprintf("%s-bridge-%d", pciBridge, i)
assert.Equal(pciBridge, b.Type)
id := fmt.Sprintf("%s-bridge-%d", types.PCI, i)
assert.Equal(types.PCI, b.Type)
assert.Equal(id, b.ID)
assert.NotNil(b.Address)
}
Expand All @@ -54,8 +55,8 @@ func TestQemuAmd64Bridges(t *testing.T) {
assert.Len(bridges, len)

for i, b := range bridges {
id := fmt.Sprintf("%s-bridge-%d", pciBridge, i)
assert.Equal(pciBridge, b.Type)
id := fmt.Sprintf("%s-bridge-%d", types.PCI, i)
assert.Equal(types.PCI, b.Type)
assert.Equal(id, b.ID)
assert.NotNil(b.Address)
}
Expand Down
18 changes: 9 additions & 9 deletions virtcontainers/qemu_arch_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type qemuArch interface {
capabilities() types.Capabilities

// bridges returns the number bridges for the machine type
bridges(number uint32) []Bridge
bridges(number uint32) []types.PCIBridge

// cpuTopology returns the CPU topology for the given amount of vcpus
cpuTopology(vcpus, maxvcpus uint32) govmmQemu.SMP
Expand All @@ -69,7 +69,7 @@ type qemuArch interface {
appendSCSIController(devices []govmmQemu.Device, enableIOThreads bool) ([]govmmQemu.Device, *govmmQemu.IOThread)

// appendBridges appends bridges to devices
appendBridges(devices []govmmQemu.Device, bridges []Bridge) []govmmQemu.Device
appendBridges(devices []govmmQemu.Device, bridges []types.PCIBridge) []govmmQemu.Device

// append9PVolume appends a 9P volume to devices
append9PVolume(devices []govmmQemu.Device, volume types.Volume) []govmmQemu.Device
Expand Down Expand Up @@ -235,13 +235,13 @@ func (q *qemuArchBase) capabilities() types.Capabilities {
return caps
}

func (q *qemuArchBase) bridges(number uint32) []Bridge {
var bridges []Bridge
func (q *qemuArchBase) bridges(number uint32) []types.PCIBridge {
var bridges []types.PCIBridge

for i := uint32(0); i < number; i++ {
bridges = append(bridges, Bridge{
Type: pciBridge,
ID: fmt.Sprintf("%s-bridge-%d", pciBridge, i),
bridges = append(bridges, types.PCIBridge{
Type: types.PCI,
ID: fmt.Sprintf("%s-bridge-%d", types.PCI, i),
Address: make(map[uint32]string),
})
}
Expand Down Expand Up @@ -346,10 +346,10 @@ func (q *qemuArchBase) appendSCSIController(devices []govmmQemu.Device, enableIO
}

// appendBridges appends to devices the given bridges
func (q *qemuArchBase) appendBridges(devices []govmmQemu.Device, bridges []Bridge) []govmmQemu.Device {
func (q *qemuArchBase) appendBridges(devices []govmmQemu.Device, bridges []types.PCIBridge) []govmmQemu.Device {
for idx, b := range bridges {
t := govmmQemu.PCIBridge
if b.Type == pcieBridge {
if b.Type == types.PCIE {
t = govmmQemu.PCIEBridge
}

Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/qemu_arch_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func TestQemuArchBaseBridges(t *testing.T) {
assert.Len(bridges, len)

for i, b := range bridges {
id := fmt.Sprintf("%s-bridge-%d", pciBridge, i)
assert.Equal(pciBridge, b.Type)
id := fmt.Sprintf("%s-bridge-%d", types.PCI, i)
assert.Equal(types.PCI, b.Type)
assert.Equal(id, b.ID)
assert.NotNil(b.Address)
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/qemu_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (q *qemuPPC64le) capabilities() types.Capabilities {
return caps
}

func (q *qemuPPC64le) bridges(number uint32) []Bridge {
func (q *qemuPPC64le) bridges(number uint32) []types.PCIBridge {
return genericBridges(number, q.machineType)
}

Expand Down Expand Up @@ -151,6 +151,6 @@ func (q *qemuPPC64le) appendImage(devices []govmmQemu.Device, path string) ([]go
}

// appendBridges appends to devices the given bridges
func (q *qemuPPC64le) appendBridges(devices []govmmQemu.Device, bridges []Bridge) []govmmQemu.Device {
func (q *qemuPPC64le) appendBridges(devices []govmmQemu.Device, bridges []types.PCIBridge) []govmmQemu.Device {
return genericAppendBridges(devices, bridges, q.machineType)
}
33 changes: 18 additions & 15 deletions virtcontainers/bridge.go → virtcontainers/types/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,41 @@
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers
package types

import "fmt"

type bridgeType string
// PCIType represents a type of PCI bus and bridge.
type PCIType string

const (
pciBridge bridgeType = "pci"
pcieBridge bridgeType = "pcie"
// PCI represents a PCI bus and bridge
PCI PCIType = "pci"

// PCIE represents a PCIe bus and bridge
PCIE PCIType = "pcie"
)

const pciBridgeMaxCapacity = 30

// Bridge is a bridge where devices can be hot plugged
type Bridge struct {
// PCIBridge is a PCI or PCIe bridge where devices can be hot plugged
type PCIBridge struct {
// Address contains information about devices plugged and its address in the bridge
Address map[uint32]string

// Type is the type of the bridge (pci, pcie, etc)
Type bridgeType
// Type is the PCI type of the bridge (pci, pcie, etc)
Type PCIType

//ID is used to identify the bridge in the hypervisor
// ID is used to identify the bridge in the hypervisor
ID string

// Addr is the PCI/e slot of the bridge
Addr int
}

// addDevice on success adds the device ID to the bridge and return the address
// where the device was added, otherwise an error is returned
func (b *Bridge) addDevice(ID string) (uint32, error) {
// AddDevice on success adds the device ID to the PCI bridge and returns
// the address where the device was added.
func (b *PCIBridge) AddDevice(ID string) (uint32, error) {
var addr uint32

// looking for the first available address
Expand All @@ -53,9 +57,8 @@ func (b *Bridge) addDevice(ID string) (uint32, error) {
return addr, nil
}

// removeDevice on success removes the device ID from the bridge and return nil,
// otherwise an error is returned
func (b *Bridge) removeDevice(ID string) error {
// RemoveDevice removes the device ID from the PCI bridge.
func (b *PCIBridge) RemoveDevice(ID string) error {
// check if the device was hot plugged in the bridge
for addr, devID := range b.Address {
if devID == ID {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers
package types

import (
"fmt"
Expand All @@ -16,30 +16,30 @@ func TestAddRemoveDevice(t *testing.T) {
assert := assert.New(t)

// create a bridge
bridges := []*Bridge{{make(map[uint32]string), pciBridge, "rgb123", 5}}
bridges := []*PCIBridge{{make(map[uint32]string), PCI, "rgb123", 5}}

// add device
devID := "abc123"
b := bridges[0]
addr, err := b.addDevice(devID)
addr, err := b.AddDevice(devID)
assert.NoError(err)
if addr < 1 {
assert.Fail("address cannot be less than 1")
}

// remove device
err = b.removeDevice("")
err = b.RemoveDevice("")
assert.Error(err)

err = b.removeDevice(devID)
err = b.RemoveDevice(devID)
assert.NoError(err)

// add device when the bridge is full
bridges[0].Address = make(map[uint32]string)
for i := uint32(1); i <= pciBridgeMaxCapacity; i++ {
bridges[0].Address[i] = fmt.Sprintf("%d", i)
}
addr, err = b.addDevice(devID)
addr, err = b.AddDevice(devID)
assert.Error(err)
if addr != 0 {
assert.Fail("address should be 0")
Expand Down

0 comments on commit 2e1ddbc

Please sign in to comment.