2017-04-04 16:22:31 +00:00
|
|
|
/*
|
|
|
|
* qemu_extdevice.c: QEMU external devices support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014, 2018 IBM Corporation
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2019-11-01 11:34:52 +00:00
|
|
|
#include "qemu_command.h"
|
2017-04-04 16:22:31 +00:00
|
|
|
#include "qemu_extdevice.h"
|
2019-09-23 10:44:40 +00:00
|
|
|
#include "qemu_vhost_user_gpu.h"
|
2020-04-08 17:23:50 +00:00
|
|
|
#include "qemu_dbus.h"
|
2017-04-04 16:22:31 +00:00
|
|
|
#include "qemu_domain.h"
|
|
|
|
#include "qemu_tpm.h"
|
2019-08-08 14:55:10 +00:00
|
|
|
#include "qemu_slirp.h"
|
2019-11-01 11:34:52 +00:00
|
|
|
#include "qemu_virtiofs.h"
|
2017-04-04 16:22:31 +00:00
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
#include "virtime.h"
|
2018-04-05 19:06:55 +00:00
|
|
|
#include "virtpm.h"
|
|
|
|
#include "virpidfile.h"
|
2017-04-04 16:22:31 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
|
|
|
|
2019-01-20 16:32:42 +00:00
|
|
|
VIR_LOG_INIT("qemu.qemu_extdevice");
|
2017-04-04 16:22:31 +00:00
|
|
|
|
|
|
|
int
|
2019-08-08 14:54:55 +00:00
|
|
|
qemuExtDeviceLogCommand(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm,
|
2017-04-04 16:22:31 +00:00
|
|
|
virCommandPtr cmd,
|
|
|
|
const char *info)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *timestamp = virTimeStringNow();
|
|
|
|
g_autofree char *cmds = virCommandToString(cmd, false);
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2019-08-08 14:54:55 +00:00
|
|
|
if (!timestamp || !cmds)
|
|
|
|
return -1;
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2019-08-08 14:54:55 +00:00
|
|
|
return qemuDomainLogAppendMessage(driver, vm,
|
|
|
|
_("%s: Starting external device: %s\n%s\n"),
|
|
|
|
timestamp, info, cmds);
|
2017-04-04 16:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-08 14:54:55 +00:00
|
|
|
|
2017-04-04 16:22:31 +00:00
|
|
|
/*
|
|
|
|
* qemuExtDevicesInitPaths:
|
|
|
|
*
|
|
|
|
* @driver: QEMU driver
|
|
|
|
* @def: domain definition
|
|
|
|
*
|
|
|
|
* Initialize paths of external devices so that it is known where state is
|
|
|
|
* stored and we can remove directories and files in case of domain XML
|
|
|
|
* changes.
|
|
|
|
*/
|
2019-08-09 08:48:35 +00:00
|
|
|
static int
|
2017-04-04 16:22:31 +00:00
|
|
|
qemuExtDevicesInitPaths(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0)
|
2020-06-10 18:11:45 +00:00
|
|
|
return qemuExtTPMInitPaths(driver, def);
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2020-06-10 18:11:45 +00:00
|
|
|
return 0;
|
2017-04-04 16:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-23 10:44:40 +00:00
|
|
|
/*
|
|
|
|
* qemuExtDevicesPrepareDomain:
|
|
|
|
*
|
|
|
|
* @driver: QEMU driver
|
|
|
|
* @vm: domain
|
|
|
|
*
|
|
|
|
* Code that modifies live XML of a domain which is about to start.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuExtDevicesPrepareDomain(virQEMUDriverPtr driver,
|
|
|
|
virDomainObjPtr vm)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < vm->def->nvideos; i++) {
|
|
|
|
virDomainVideoDefPtr video = vm->def->videos[i];
|
|
|
|
|
|
|
|
if (video->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER) {
|
|
|
|
if ((ret = qemuExtVhostUserGPUPrepareDomain(driver, video)) < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:38:52 +00:00
|
|
|
for (i = 0; i < vm->def->nfss; i++) {
|
|
|
|
virDomainFSDefPtr fs = vm->def->fss[i];
|
|
|
|
|
|
|
|
if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS) {
|
|
|
|
if (qemuVirtioFSPrepareDomain(driver, fs) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 10:44:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-04 16:22:31 +00:00
|
|
|
/*
|
|
|
|
* qemuExtDevicesPrepareHost:
|
|
|
|
*
|
|
|
|
* @driver: QEMU driver
|
|
|
|
* @def: domain definition
|
|
|
|
*
|
|
|
|
* Prepare host storage paths for external devices.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuExtDevicesPrepareHost(virQEMUDriverPtr driver,
|
2019-08-08 14:55:10 +00:00
|
|
|
virDomainObjPtr vm)
|
2017-04-04 16:22:31 +00:00
|
|
|
{
|
2019-08-08 14:55:10 +00:00
|
|
|
virDomainDefPtr def = vm->def;
|
|
|
|
size_t i;
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0 &&
|
2019-08-08 14:55:10 +00:00
|
|
|
qemuExtTPMPrepareHost(driver, def) < 0)
|
|
|
|
return -1;
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2019-08-08 14:55:10 +00:00
|
|
|
for (i = 0; i < def->nnets; i++) {
|
|
|
|
virDomainNetDefPtr net = def->nets[i];
|
|
|
|
qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
|
|
|
|
|
|
|
|
if (slirp && qemuSlirpOpen(slirp, driver, def) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-04-04 16:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
qemuExtDevicesCleanupHost(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
if (qemuExtDevicesInitPaths(driver, def) < 0)
|
|
|
|
return;
|
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0)
|
2017-04-04 16:22:31 +00:00
|
|
|
qemuExtTPMCleanupHost(def);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuExtDevicesStart(virQEMUDriverPtr driver,
|
2018-11-13 14:14:43 +00:00
|
|
|
virDomainObjPtr vm,
|
2019-11-01 11:34:52 +00:00
|
|
|
virLogManagerPtr logManager,
|
2019-07-26 20:41:10 +00:00
|
|
|
bool incomingMigration)
|
2017-04-04 16:22:31 +00:00
|
|
|
{
|
2019-08-08 14:55:10 +00:00
|
|
|
virDomainDefPtr def = vm->def;
|
|
|
|
size_t i;
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2020-01-24 18:41:45 +00:00
|
|
|
if (qemuExtDevicesInitPaths(driver, def) < 0)
|
2017-04-04 16:22:31 +00:00
|
|
|
return -1;
|
|
|
|
|
2020-01-24 18:41:45 +00:00
|
|
|
for (i = 0; i < def->nvideos; i++) {
|
|
|
|
virDomainVideoDefPtr video = def->videos[i];
|
2019-09-23 10:44:41 +00:00
|
|
|
|
|
|
|
if (video->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER) {
|
2020-01-24 18:43:18 +00:00
|
|
|
if (qemuExtVhostUserGPUStart(driver, vm, video) < 0)
|
|
|
|
return -1;
|
2019-09-23 10:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0 && qemuExtTPMStart(driver, vm, incomingMigration) < 0)
|
2020-01-24 18:43:18 +00:00
|
|
|
return -1;
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2019-08-08 14:55:10 +00:00
|
|
|
for (i = 0; i < def->nnets; i++) {
|
|
|
|
virDomainNetDefPtr net = def->nets[i];
|
|
|
|
qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
|
|
|
|
|
|
|
|
if (slirp &&
|
2020-02-25 09:55:05 +00:00
|
|
|
qemuSlirpStart(slirp, vm, driver, net, incomingMigration) < 0)
|
2019-08-08 14:55:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:34:52 +00:00
|
|
|
for (i = 0; i < def->nfss; i++) {
|
|
|
|
virDomainFSDefPtr fs = def->fss[i];
|
|
|
|
|
|
|
|
if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS) {
|
|
|
|
if (qemuVirtioFSStart(logManager, driver, vm, fs) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:18 +00:00
|
|
|
return 0;
|
2017-04-04 16:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
qemuExtDevicesStop(virQEMUDriverPtr driver,
|
2018-11-13 14:14:43 +00:00
|
|
|
virDomainObjPtr vm)
|
2017-04-04 16:22:31 +00:00
|
|
|
{
|
2019-08-08 14:55:10 +00:00
|
|
|
virDomainDefPtr def = vm->def;
|
|
|
|
size_t i;
|
|
|
|
|
2019-09-23 10:44:41 +00:00
|
|
|
if (qemuExtDevicesInitPaths(driver, def) < 0)
|
2017-04-04 16:22:31 +00:00
|
|
|
return;
|
|
|
|
|
2019-09-23 10:44:41 +00:00
|
|
|
for (i = 0; i < def->nvideos; i++) {
|
|
|
|
virDomainVideoDefPtr video = def->videos[i];
|
|
|
|
|
|
|
|
if (video->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER)
|
|
|
|
qemuExtVhostUserGPUStop(driver, vm, video);
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0)
|
2018-11-13 14:14:43 +00:00
|
|
|
qemuExtTPMStop(driver, vm);
|
2019-08-08 14:55:10 +00:00
|
|
|
|
|
|
|
for (i = 0; i < def->nnets; i++) {
|
|
|
|
virDomainNetDefPtr net = def->nets[i];
|
2020-05-26 00:41:00 +00:00
|
|
|
virDomainNetType actualType = virDomainNetGetActualType(net);
|
2019-08-08 14:55:10 +00:00
|
|
|
qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
|
|
|
|
|
|
|
|
if (slirp)
|
2020-02-25 09:55:05 +00:00
|
|
|
qemuSlirpStop(slirp, vm, driver, net);
|
2020-05-26 00:41:00 +00:00
|
|
|
if (actualType == VIR_DOMAIN_NET_TYPE_ETHERNET && net->downscript)
|
|
|
|
virNetDevRunEthernetScript(net->ifname, net->downscript);
|
2019-08-08 14:55:10 +00:00
|
|
|
}
|
2019-11-01 11:34:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i < def->nfss; i++) {
|
|
|
|
virDomainFSDefPtr fs = def->fss[i];
|
|
|
|
|
|
|
|
if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS)
|
|
|
|
qemuVirtioFSStop(driver, vm, fs);
|
|
|
|
}
|
2017-04-04 16:22:31 +00:00
|
|
|
}
|
2018-04-05 19:06:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
qemuExtDevicesHasDevice(virDomainDefPtr def)
|
|
|
|
{
|
2019-09-23 10:44:41 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < def->nvideos; i++) {
|
|
|
|
if (def->videos[i]->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
for (i = 0; i < def->ntpms; i++) {
|
|
|
|
if (def->tpms[i]->type == VIR_DOMAIN_TPM_TYPE_EMULATOR)
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-05 19:06:55 +00:00
|
|
|
|
2020-02-18 15:12:29 +00:00
|
|
|
for (i = 0; i < def->nfss; i++) {
|
|
|
|
virDomainFSDefPtr fs = def->fss[i];
|
|
|
|
|
|
|
|
if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-05 19:06:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuExtDevicesSetupCgroup(virQEMUDriverPtr driver,
|
2020-02-26 11:45:02 +00:00
|
|
|
virDomainObjPtr vm,
|
2018-04-05 19:06:55 +00:00
|
|
|
virCgroupPtr cgroup)
|
|
|
|
{
|
2020-02-26 11:45:02 +00:00
|
|
|
virDomainDefPtr def = vm->def;
|
2019-09-23 10:44:41 +00:00
|
|
|
size_t i;
|
2018-04-05 19:06:55 +00:00
|
|
|
|
2020-10-28 02:20:37 +00:00
|
|
|
if (qemuDBusSetupCgroup(driver, vm, cgroup) < 0)
|
2020-04-08 17:23:50 +00:00
|
|
|
return -1;
|
|
|
|
|
2019-09-23 10:44:41 +00:00
|
|
|
for (i = 0; i < def->nvideos; i++) {
|
|
|
|
virDomainVideoDefPtr video = def->videos[i];
|
2018-04-05 19:06:55 +00:00
|
|
|
|
2019-09-23 10:44:41 +00:00
|
|
|
if (video->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER &&
|
|
|
|
qemuExtVhostUserGPUSetupCgroup(driver, def, video, cgroup) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-04-08 17:23:47 +00:00
|
|
|
for (i = 0; i < def->nnets; i++) {
|
|
|
|
virDomainNetDefPtr net = def->nets[i];
|
|
|
|
qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
|
|
|
|
|
|
|
|
if (slirp && qemuSlirpSetupCgroup(slirp, cgroup) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:11:47 +00:00
|
|
|
if (def->ntpms > 0 &&
|
2019-09-23 10:44:41 +00:00
|
|
|
qemuExtTPMSetupCgroup(driver, def, cgroup) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2020-02-18 15:12:29 +00:00
|
|
|
for (i = 0; i < def->nfss; i++) {
|
|
|
|
virDomainFSDefPtr fs = def->fss[i];
|
|
|
|
|
|
|
|
if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS &&
|
|
|
|
qemuVirtioFSSetupCgroup(vm, fs, cgroup) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-23 10:44:41 +00:00
|
|
|
return 0;
|
2018-04-05 19:06:55 +00:00
|
|
|
}
|