Skip to content

Commit

Permalink
hypervisor: move determining hypervisor
Browse files Browse the repository at this point in the history
into hypervisor package, so that usbmanager can access it

Signed-off-by: Christoph Ostarek <[email protected]>
  • Loading branch information
christoph-zededa authored and eriknordmark committed Oct 31, 2023
1 parent a77ca11 commit 8b1f5ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
76 changes: 31 additions & 45 deletions pkg/pillar/cmd/domainmgr/domainmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,9 @@ func (ctx *domainContext) publishAssignableAdapters() {
ctx.pubAssignableAdapters.Publish("global", *ctx.assignableAdapters)
}

var currentHypervisor hypervisor.Hypervisor
var currentHypervisorMutex sync.Mutex
var logger *logrus.Logger
var log *base.LogObject

// CurrentHypervisor returns the current hypervisor
func CurrentHypervisor() hypervisor.Hypervisor {
currentHypervisorMutex.Lock()
hv := currentHypervisor
currentHypervisorMutex.Unlock()

return hv
}

func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, arguments []string) int {
logger = loggerArg
log = logArg
Expand Down Expand Up @@ -176,17 +165,14 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
fmt.Printf("%s: %s\n", agentName, Version)
return 0
}
currentHypervisorMutex.Lock()
currentHypervisor, err = hypervisor.GetHypervisor(*domainCtx.hypervisorPtr)
currentHypervisorMutex.Unlock()
if err != nil {
log.Fatal(err)
}

if err := pidfile.CheckAndCreatePidfile(log, agentName); err != nil {
log.Fatal(err)
}
log.Functionf("Starting %s with %s hypervisor backend", agentName, CurrentHypervisor().Name())
log.Functionf("Starting %s with %s hypervisor backend", agentName, hypervisor.CurrentHypervisor().Name())

// Run a periodic timer so we always update StillRunning
stillRunning := time.NewTicker(25 * time.Second)
Expand Down Expand Up @@ -483,15 +469,15 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar

capabilitiesSent := false
capabilitiesTicker := time.NewTicker(5 * time.Second)
if err := getAndPublishCapabilities(&domainCtx, CurrentHypervisor()); err != nil {
if err := getAndPublishCapabilities(&domainCtx, hypervisor.CurrentHypervisor()); err != nil {
log.Warnf("getAndPublishCapabilities: %v", err)
} else {
capabilitiesSent = true
capabilitiesTicker.Stop()
}

log.Functionf("Creating %s at %s", "metricsTimerTask", agentlog.GetMyStack())
go metricsTimerTask(&domainCtx, CurrentHypervisor())
go metricsTimerTask(&domainCtx, hypervisor.CurrentHypervisor())

// Before starting to process DomainConfig, domainmgr should (in this order):
// 1. wait for NIM to publish DNS to learn which ports are used for management
Expand Down Expand Up @@ -527,7 +513,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
publishProcessesHandler(&domainCtx)

case <-capabilitiesTicker.C:
if err := getAndPublishCapabilities(&domainCtx, CurrentHypervisor()); err != nil {
if err := getAndPublishCapabilities(&domainCtx, hypervisor.CurrentHypervisor()); err != nil {
log.Warnf("getAndPublishCapabilities: %v", err)
} else {
capabilitiesSent = true
Expand All @@ -553,7 +539,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
var resources types.HostMemory
for i := 0; true; i++ {
delay := 10
resources, err = CurrentHypervisor().GetHostCPUMem()
resources, err = hypervisor.CurrentHypervisor().GetHostCPUMem()
if err == nil {
break
}
Expand Down Expand Up @@ -941,7 +927,7 @@ func verifyStatus(ctx *domainContext, status *types.DomainStatus) {
configActivate = true
}

domainID, domainStatus, err := CurrentHypervisor().Task(status).Info(status.DomainName)
domainID, domainStatus, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName)
if err != nil || domainStatus == types.HALTED {
if status.Activated && configActivate {
if err == nil {
Expand Down Expand Up @@ -974,10 +960,10 @@ func verifyStatus(ctx *domainContext, status *types.DomainStatus) {
}

//cleanup app instance tasks
if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err)
}
if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err)
}
}
Expand Down Expand Up @@ -1115,7 +1101,7 @@ func maybeRetryBoot(ctx *domainContext, status *types.DomainStatus) {
}
defer file.Close()

if err := CurrentHypervisor().Task(status).Setup(*status, *config, ctx.assignableAdapters, nil, file); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Setup(*status, *config, ctx.assignableAdapters, nil, file); err != nil {
//it is retry, so omit error
log.Errorf("Failed to create DomainStatus from %v: %s",
config, err)
Expand Down Expand Up @@ -1394,14 +1380,14 @@ func doAssignIoAdaptersToDomain(ctx *domainContext, config types.DomainConfig,

}
for i, long := range assignmentsPci {
err := CurrentHypervisor().PCIReserve(long)
err := hypervisor.CurrentHypervisor().PCIReserve(long)
if err != nil {
// Undo what we assigned
for j, long := range assignmentsPci {
if j >= i {
break
}
CurrentHypervisor().PCIRelease(long)
hypervisor.CurrentHypervisor().PCIRelease(long)
}
if publishAssignableAdapters {
ctx.publishAssignableAdapters()
Expand Down Expand Up @@ -1506,7 +1492,7 @@ func doActivate(ctx *domainContext, config types.DomainConfig,
defer file.Close()

globalConfig := agentlog.GetGlobalConfig(log, ctx.subGlobalConfig)
if err := CurrentHypervisor().Task(status).Setup(*status, config, ctx.assignableAdapters, globalConfig, file); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Setup(*status, config, ctx.assignableAdapters, globalConfig, file); err != nil {
log.Errorf("Failed to create DomainStatus from %v: %s",
config, err)
status.SetErrorNow(err.Error())
Expand Down Expand Up @@ -1565,17 +1551,17 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus,
status.State = types.BOOTING
publishDomainStatus(ctx, status)

err := CurrentHypervisor().Task(status).Start(status.DomainName)
err := hypervisor.CurrentHypervisor().Task(status).Start(status.DomainName)
if err != nil {
log.Errorf("domain start for %s: %s", status.DomainName, err)
status.SetErrorNow(err.Error())

// Delete
if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err)
}
// Cleanup
if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err)
}

Expand All @@ -1589,7 +1575,7 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus,
status.VifList = checkIfEmu(status.VifList)

status.State = types.RUNNING
domainID, state, err := CurrentHypervisor().Task(status).Info(status.DomainName)
domainID, state, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName)

if err != nil {
// Immediate failure treat as above
Expand All @@ -1600,11 +1586,11 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus,
log.Errorf("doActivateTail(%v) failed for %s: %s",
status.UUIDandVersion, status.DisplayName, err)
// Delete
if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err)
}
// Cleanup
if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err)
}
return
Expand Down Expand Up @@ -1649,7 +1635,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool

log.Functionf("doInactivate(%v) for %s domainId %d",
status.UUIDandVersion, status.DisplayName, status.DomainId)
domainID, _, err := CurrentHypervisor().Task(status).Info(status.DomainName)
domainID, _, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName)
if err == nil && domainID != status.DomainId {
status.DomainId = domainID
status.BootTime = time.Now()
Expand Down Expand Up @@ -1723,7 +1709,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool
}

if status.DomainId != 0 {
if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil {
log.Errorf("Failed to delete domain %s (%v)", status.DomainName, err)
} else {
log.Functionf("doInactivate(%v) for %s: Delete succeeded",
Expand All @@ -1741,7 +1727,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool
}

func doCleanup(ctx *domainContext, status *types.DomainStatus) {
if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil {
log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err)
}

Expand Down Expand Up @@ -1845,7 +1831,7 @@ func releaseAdapters(ctx *domainContext, ioAdapterList []types.IoAdapter,
checkIoBundleAll(ctx)
}
for _, long := range assignments {
err := CurrentHypervisor().PCIRelease(long)
err := hypervisor.CurrentHypervisor().PCIRelease(long)
if err != nil && !ignoreErrors {
status.SetErrorNow(err.Error())
}
Expand Down Expand Up @@ -2196,7 +2182,7 @@ func waitForDomainGone(status types.DomainStatus, maxDelay time.Duration) bool {
time.Sleep(delay)
waited += delay
}
_, state, err := CurrentHypervisor().Task(&status).Info(status.DomainName)
_, state, err := hypervisor.CurrentHypervisor().Task(&status).Info(status.DomainName)
if err != nil {
log.Errorf("waitForDomainGone(%v) for %s error %s state %s",
status.UUIDandVersion, status.DisplayName,
Expand Down Expand Up @@ -2284,7 +2270,7 @@ func DomainCreate(ctx *domainContext, status types.DomainStatus) (int, error) {
log.Errorf("DomainCreate(%s) no DomainConfig", status.Key())
return 0, fmt.Errorf("DomainCreate(%s) no DomainConfig", status.Key())
}
domainID, err = CurrentHypervisor().Task(&status).Create(status.DomainName, filename, config)
domainID, err = hypervisor.CurrentHypervisor().Task(&status).Create(status.DomainName, filename, config)

return domainID, err
}
Expand All @@ -2297,7 +2283,7 @@ func DomainShutdown(status types.DomainStatus, force bool) error {

// Stop the domain
log.Functionf("Stopping domain - %s", status.DomainName)
err = CurrentHypervisor().Task(&status).Stop(status.DomainName, force)
err = hypervisor.CurrentHypervisor().Task(&status).Stop(status.DomainName, force)

return err
}
Expand Down Expand Up @@ -2755,7 +2741,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string,
len(aa.IoBundleList))

// check for mismatched PCI-ids and assignment groups and mark as errors
aa.CheckBadAssignmentGroups(log, CurrentHypervisor().PCISameController)
aa.CheckBadAssignmentGroups(log, hypervisor.CurrentHypervisor().PCISameController)
for i := range aa.IoBundleList {
ib := &aa.IoBundleList[i]
log.Functionf("handlePhysicalIOAdapterListImpl: new Adapter: %+v",
Expand Down Expand Up @@ -2800,7 +2786,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string,
aa.AddOrUpdateIoBundle(log, *ib)

// check for mismatched PCI-ids and assignment groups and mark as errors
aa.CheckBadAssignmentGroups(log, CurrentHypervisor().PCISameController)
aa.CheckBadAssignmentGroups(log, hypervisor.CurrentHypervisor().PCISameController)
// Lookup since it could have changed
ib = aa.LookupIoBundlePhylabel(ib.Phylabel)
updatePortAndPciBackIoBundle(ctx, ib)
Expand Down Expand Up @@ -2901,7 +2887,7 @@ func updatePortAndPciBackIoBundle(ctx *domainContext, ib *types.IoBundle) (chang
// expand list to include other PCI functions on the same PCI controller
// since they need to be treated as part of the same bundle even if the
// EVE controller doesn't know it
list = aa.ExpandControllers(log, list, CurrentHypervisor().PCISameController)
list = aa.ExpandControllers(log, list, hypervisor.CurrentHypervisor().PCISameController)
for _, ib := range list {
if types.IsPort(ctx.deviceNetworkStatus, ib.Ifname) {
isPort = true
Expand Down Expand Up @@ -2987,7 +2973,7 @@ func updatePortAndPciBackIoMember(ctx *domainContext, ib *types.IoBundle, isPort
if ib.PciLong != "" {
log.Functionf("updatePortAndPciBackIoMember: Removing %s (%s) from pciback",
ib.Phylabel, ib.PciLong)
err = CurrentHypervisor().PCIRelease(ib.PciLong)
err = hypervisor.CurrentHypervisor().PCIRelease(ib.PciLong)
if err != nil {
err = fmt.Errorf("adapter %s (group %s, type %d) PCI ID %s; not released by hypervisor: %v",
ib.Phylabel, ib.AssignmentGroup, ib.Type,
Expand Down Expand Up @@ -3034,7 +3020,7 @@ func updatePortAndPciBackIoMember(ctx *domainContext, ib *types.IoBundle, isPort
} else if ib.PciLong != "" && ib.UsbAddr == "" {
log.Noticef("Assigning %s (%s) to pciback",
ib.Phylabel, ib.PciLong)
err := CurrentHypervisor().PCIReserve(ib.PciLong)
err := hypervisor.CurrentHypervisor().PCIReserve(ib.PciLong)
if err != nil {
return changed, err
}
Expand Down Expand Up @@ -3275,7 +3261,7 @@ func handleIBDelete(ctx *domainContext, phylabel string) {
log.Functionf("handleIBDelete: Assigning %s (%s) back",
ib.Phylabel, ib.PciLong)
if ib.PciLong != "" {
err := CurrentHypervisor().PCIRelease(ib.PciLong)
err := hypervisor.CurrentHypervisor().PCIRelease(ib.PciLong)
if err != nil {
log.Errorf("handleIBDelete(%d %s %s) PCIRelease %s failed %v",
ib.Type, ib.Phylabel, ib.AssignmentGroup, ib.PciLong, err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/pillar/cmd/usbmanager/usbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/lf-edge/eve/pkg/pillar/agentbase"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/cmd/domainmgr"
"github.com/lf-edge/eve/pkg/pillar/hypervisor"
"github.com/lf-edge/eve/pkg/pillar/pubsub"
"github.com/lf-edge/eve/pkg/pillar/utils"
Expand Down Expand Up @@ -82,7 +81,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
}
log.Functionf("processed Vault Status")

currentHypervisor := domainmgr.CurrentHypervisor()
currentHypervisor := hypervisor.CurrentHypervisor()
_, ok := currentHypervisor.(hypervisor.KvmContext)
if ok {
usbCtx.subscribe(ps)
Expand Down
25 changes: 24 additions & 1 deletion pkg/pillar/hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,37 @@
package hypervisor

import (
"flag"
"fmt"
"log"
"os"

"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
"os"
)

var currentHypervisor Hypervisor

func init() {
var err error

flagSet := flag.NewFlagSet("", flag.ExitOnError)
allHypervisors, enabledHypervisors := GetAvailableHypervisors()
hypervisorPtr := flagSet.String("h", enabledHypervisors[0], fmt.Sprintf("Current hypervisor %+q", allHypervisors))

currentHypervisor, err = GetHypervisor(*hypervisorPtr)
if err != nil {
log.Fatal(err)
}
}

// CurrentHypervisor returns the current hypervisor
func CurrentHypervisor() Hypervisor {
return currentHypervisor
}

// Hypervisor provides methods for manipulating domains on the host
type Hypervisor interface {
Name() string
Expand Down

0 comments on commit 8b1f5ba

Please sign in to comment.