This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The abstraction of the bridge type to add additional types. Fixes: #1153 Signed-off-by: Alice Frosi <[email protected]> Co-authored-by: Jan Schintag <[email protected]>
- Loading branch information
Showing
11 changed files
with
132 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) 2017 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package types | ||
|
||
import "fmt" | ||
|
||
// Type represents a type of bus and bridge. | ||
type Type string | ||
|
||
const pciBridgeMaxCapacity = 30 | ||
|
||
const ( | ||
// PCI represents a PCI bus and bridge | ||
PCI Type = "pci" | ||
|
||
// PCIE represents a PCIe bus and bridge | ||
PCIE Type = "pcie" | ||
) | ||
|
||
type Bridge struct { | ||
// Devices contains information about devices plugged and its address in the bridge | ||
Devices map[uint32]string | ||
|
||
// ID is used to identify the bridge in the hypervisor | ||
ID string | ||
|
||
// Addr is the slot of the bridge | ||
Addr int | ||
|
||
// Type is the type of the bridge (pci, pcie, etc) | ||
Type Type | ||
|
||
// MaxCapacity is the max capacity of the bridge | ||
MaxCapacity uint32 | ||
} | ||
|
||
func NewBridge(bt Type, id string, devices map[uint32]string, addr int) Bridge { | ||
var maxCapacity uint32 | ||
switch bt { | ||
case PCI: | ||
fallthrough | ||
case PCIE: | ||
maxCapacity = pciBridgeMaxCapacity | ||
default: | ||
maxCapacity = 0 | ||
} | ||
return Bridge{ | ||
Devices: devices, | ||
ID: id, | ||
Addr: addr, | ||
Type: bt, | ||
MaxCapacity: maxCapacity, | ||
} | ||
} | ||
|
||
func (b *Bridge) AddDevice(ID string) (uint32, error) { | ||
var addr uint32 | ||
|
||
// looking for the first available address | ||
for i := uint32(1); i <= b.MaxCapacity; i++ { | ||
if _, ok := b.Devices[i]; !ok { | ||
addr = i | ||
break | ||
} | ||
} | ||
|
||
if addr == 0 { | ||
return 0, fmt.Errorf("Unable to hot plug device on bridge: there are no empty slots") | ||
} | ||
|
||
// save address and device | ||
b.Devices[addr] = ID | ||
return addr, nil | ||
} | ||
|
||
func (b *Bridge) RemoveDevice(ID string) error { | ||
// check if the device was hot plugged in the bridge | ||
for addr, devID := range b.Devices { | ||
if devID == ID { | ||
// free address to re-use the same slot with other devices | ||
delete(b.Devices, addr) | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("Unable to hot unplug device %s: not present on bridge", ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.