From nobody Mon Feb 9 22:02:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153148854804013.695620601881501; Fri, 13 Jul 2018 06:29:08 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BE86D308212A; Fri, 13 Jul 2018 13:29:06 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8224BC6A34; Fri, 13 Jul 2018 13:29:06 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 35D0D1841C6E; Fri, 13 Jul 2018 13:29:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6DDSuvo032569 for ; Fri, 13 Jul 2018 09:28:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF49F21565E2; Fri, 13 Jul 2018 13:28:56 +0000 (UTC) Received: from localhost (unknown [10.36.112.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2857321565E1; Fri, 13 Jul 2018 13:28:56 +0000 (UTC) From: marcandre.lureau@redhat.com To: libvir-list@redhat.com Date: Fri, 13 Jul 2018 15:28:18 +0200 Message-Id: <20180713132824.8655-12-marcandre.lureau@redhat.com> In-Reply-To: <20180713132824.8655-1-marcandre.lureau@redhat.com> References: <20180713132824.8655-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [libvirt] [RFC PATCH 11/17] qemu: add vhost-user-gpu helper unit X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Fri, 13 Jul 2018 13:29:07 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 From: Marc-Andr=C3=A9 Lureau Similar to the qemu_tpm.c, add a unit with a few functions to start/stop and setup the cgroup of the external vhost-user-gpu process. See function documentation. Signed-off-by: Marc-Andr=C3=A9 Lureau --- src/conf/device_conf.h | 1 + src/qemu/Makefile.inc.am | 2 + src/qemu/qemu_vhost_user_gpu.c | 318 +++++++++++++++++++++++++++++++++ src/qemu/qemu_vhost_user_gpu.h | 48 +++++ 4 files changed, 369 insertions(+) create mode 100644 src/qemu/qemu_vhost_user_gpu.c create mode 100644 src/qemu/qemu_vhost_user_gpu.h diff --git a/src/conf/device_conf.h b/src/conf/device_conf.h index a31ce9c376..544fd33a33 100644 --- a/src/conf/device_conf.h +++ b/src/conf/device_conf.h @@ -175,6 +175,7 @@ struct _virDomainDeviceInfo { * cases we might want to prevent that from happening by * locking the isolation group */ bool isolationGroupLocked; + int vhost_user_fd; }; =20 int virDomainDeviceInfoCopy(virDomainDeviceInfoPtr dst, diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am index 2afa67f195..28daf9d426 100644 --- a/src/qemu/Makefile.inc.am +++ b/src/qemu/Makefile.inc.am @@ -56,6 +56,8 @@ QEMU_DRIVER_SOURCES =3D \ qemu/qemu_qapi.h \ qemu/qemu_tpm.c \ qemu/qemu_tpm.h \ + qemu/qemu_vhost_user_gpu.c \ + qemu/qemu_vhost_user_gpu.h \ $(NULL) =20 =20 diff --git a/src/qemu/qemu_vhost_user_gpu.c b/src/qemu/qemu_vhost_user_gpu.c new file mode 100644 index 0000000000..9007614020 --- /dev/null +++ b/src/qemu/qemu_vhost_user_gpu.c @@ -0,0 +1,318 @@ +/* + * qemu_vhost_user_gpu.c: QEMU vhost-user GPU support + * + * Copyright (C) 2018 Red Hat, Inc. + * + * 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 + * . + * + * Author: Marc-Andr=C3=A9 Lureau + */ + +#include + +#include "qemu_extdevice.h" +#include "qemu_domain.h" +#include "qemu_security.h" + +#include "conf/domain_conf.h" +#include "vircommand.h" +#include "viralloc.h" +#include "virlog.h" +#include "virutil.h" +#include "virfile.h" +#include "virstring.h" +#include "virtime.h" +#include "virpidfile.h" +#include "qemu_vhost_user_gpu.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +VIR_LOG_INIT("qemu.vhost-user-gpu") + +/* + * Look up the vhost-user-gpu executable; to be found on the host + */ +static char *vhost_user_gpu_path; + +static int +qemuVhostUserGPUInit(void) +{ + if (!vhost_user_gpu_path) { + vhost_user_gpu_path =3D virFindFileInPath("vhost-user-gpu"); + if (!vhost_user_gpu_path) { + virReportSystemError(ENOENT, "%s", + _("Unable to find 'vhost-user-gpu' binary= in $PATH")); + return -1; + } + if (!virFileIsExecutable(vhost_user_gpu_path)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("vhost-user-gpu %s is not an executable"), + vhost_user_gpu_path); + VIR_FREE(vhost_user_gpu_path); + return -1; + } + } + + return 0; +} + + +static char * +qemuVhostUserGPUCreatePidFilename(const char *stateDir, + const char *shortName, + const char *alias) +{ + char *pidfile =3D NULL; + char *devicename =3D NULL; + + if (virAsprintf(&devicename, "%s-%s-vhost-user-gpu", shortName, alias)= < 0) + return NULL; + + pidfile =3D virPidFileBuildPath(stateDir, devicename); + + VIR_FREE(devicename); + + return pidfile; +} + + +/* + * qemuVhostUserGPUGetPid + * + * @stateDir: the directory where vhost-user-gpu writes the pidfile into + * @shortName: short name of the domain + * @alias: video device alias + * @pid: pointer to pid + * + * Return -errno upon error, or zero on successful reading of the pidfile. + * If the PID was not still alive, zero will be returned, and @pid will be + * set to -1; + */ +static int +qemuVhostUserGPUGetPid(const char *stateDir, + const char *shortName, + const char *alias, + pid_t *pid) +{ + int ret; + char *pidfile =3D qemuVhostUserGPUCreatePidFilename(stateDir, shortNam= e, alias); + if (!pidfile) + return -ENOMEM; + + ret =3D virPidFileReadPathIfAlive(pidfile, pid, vhost_user_gpu_path); + + VIR_FREE(pidfile); + + return ret; +} + + +/* + * qemuExtVhostUserGPUStart: + * + * @driver: QEMU driver + * @def: domain definition + * @video: the video device + * @logCtxt: log context + * + * Start the external vhost-user-gpu process: + * - open a socketpair for vhost-user communication + * - have the command line built + * - start the external process and sync with it before QEMU start + */ +int qemuExtVhostUserGPUStart(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video, + qemuDomainLogContextPtr logCtxt) +{ + int ret =3D -1; + virCommandPtr cmd =3D NULL; + int exitstatus =3D 0; + char *errbuf =3D NULL; + virQEMUDriverConfigPtr cfg; + char *pidfile, *shortName =3D virDomainDefGetShortName(def); + virTimeBackOffVar timebackoff; + const unsigned long long timeout =3D 500000; /* ms */ + int cmdret =3D 0, rc; + int pair[2] =3D { -1, -1 }; + + pid_t pid; + + if (!shortName) + return -1; + + cfg =3D virQEMUDriverGetConfig(driver); + + /* stop any left-over for this VM */ + qemuExtVhostUserGPUStop(driver, def, video); + + if (!(pidfile =3D qemuVhostUserGPUCreatePidFilename( + cfg->stateDir, shortName, video->info.alias))) + goto error; + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) { + virReportSystemError(errno, "%s", _("failed to create socket")); + goto error; + } + + cmd =3D virCommandNew(vhost_user_gpu_path); + if (!cmd) + goto error; + + virCommandClearCaps(cmd); + virCommandDaemonize(cmd); + + if (qemuExtDeviceLogCommand(logCtxt, cmd, "vhost-user-gpu") < 0) + goto error; + + virCommandAddArgList(cmd, "--pid", pidfile, NULL); + virCommandAddArgFormat(cmd, "--fd=3D%d", pair[0]); + virCommandPassFD(cmd, pair[0], VIR_COMMAND_PASS_FD_CLOSE_PARENT); + pair[0] =3D -1; + + if (video->accel && video->accel->accel3d) { + virCommandAddArg(cmd, "--virgl"); + } + if (qemuSecurityStartVhostUserGPU(driver, def, cmd, + &exitstatus, &cmdret) < 0) + goto error; + + if (cmdret < 0 || exitstatus !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not start 'vhost-user-gpu'. exitstatus: %d= , " + "error: %s"), exitstatus, errbuf); + goto cleanup; + } + + /* check that the helper has written its pid into the file */ + if (virTimeBackOffStart(&timebackoff, 1, timeout) < 0) + goto error; + while (virTimeBackOffWait(&timebackoff)) { + rc =3D qemuVhostUserGPUGetPid(cfg->stateDir, shortName, video->inf= o.alias, &pid); + if (rc < 0) + continue; + if (rc =3D=3D 0 && pid =3D=3D (pid_t)-1) + goto error; + break; + } + + ret =3D 0; + video->info.vhost_user_fd =3D pair[1]; + pair[1] =3D -1; + +cleanup: + VIR_FORCE_CLOSE(pair[0]); + VIR_FORCE_CLOSE(pair[1]); + virObjectUnref(cfg); + VIR_FREE(pidfile); + virCommandFree(cmd); + + return ret; + +error: + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("vhost-user-gpu failed to start")); + goto cleanup; +} + + +/* + * qemuExtVhostUserGPUStop: + * + * @driver: QEMU driver + * @def: domain definition + * @video: the video device + * + * Check if vhost-user process pidfile is around, kill the process, + * and remove the pidfile. + */ +void qemuExtVhostUserGPUStop(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video) +{ + virErrorPtr orig_err; + char *pidfile, *shortName =3D virDomainDefGetShortName(def); + virQEMUDriverConfigPtr cfg =3D virQEMUDriverGetConfig(driver); + + if (qemuVhostUserGPUInit() < 0) + return; + + if (!(pidfile =3D qemuVhostUserGPUCreatePidFilename( + cfg->stateDir, shortName, video->info.alias))) { + VIR_WARN("Unable to construct vhost-user-gpu pidfile path"); + return; + } + + virErrorPreserveLast(&orig_err); + if (virPidFileForceCleanupPath(pidfile) < 0) { + VIR_WARN("Unable to kill vhost-user-gpu process"); + } else { + if (unlink(pidfile) < 0 && + errno !=3D ENOENT) { + virReportSystemError(errno, + _("Unable to remove stale pidfile %s"), + pidfile); + } + } + virErrorRestore(&orig_err); + + VIR_FREE(pidfile); +} + + +/* + * qemuExtVhostUserGPUSetupCgroup: + * + * @driver: QEMU driver + * @def: domain definition + * @video: the video device + * @cgroupe: a cgroup + * + * Add the vhost-user-gpu PID to the given cgroup. + */ +int +qemuExtVhostUserGPUSetupCgroup(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video, + virCgroupPtr cgroup) +{ + virQEMUDriverConfigPtr cfg =3D virQEMUDriverGetConfig(driver); + char *pidfile =3D NULL; + char *shortName =3D NULL; + int ret =3D -1, rc; + pid_t pid; + + shortName =3D virDomainDefGetShortName(def); + if (!shortName) + goto cleanup; + + rc =3D qemuVhostUserGPUGetPid(cfg->stateDir, shortName, video->info.al= ias, &pid); + if (rc < 0 || (rc =3D=3D 0 && pid =3D=3D (pid_t)-1)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not get process id of vhost-user-gpu")); + goto cleanup; + } + if (virCgroupAddTask(cgroup, pid) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FREE(pidfile); + VIR_FREE(shortName); + virObjectUnref(cfg); + + return ret; +} diff --git a/src/qemu/qemu_vhost_user_gpu.h b/src/qemu/qemu_vhost_user_gpu.h new file mode 100644 index 0000000000..0f5331cce8 --- /dev/null +++ b/src/qemu/qemu_vhost_user_gpu.h @@ -0,0 +1,48 @@ +/* + * qemu_vhost_user_gpu.h: QEMU vhost-user GPU support + * + * Copyright (C) 2018 Red Hat, Inc. + * + * 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 + * . + * + * Author: Marc-Andr=C3=A9 Lureau + */ +#ifndef __QEMU_VHOST_USER_GPU_H__ +# define __QEMU_VHOST_USER_GPU_H__ + +# include "qemu_conf.h" +# include "vircommand.h" + +int qemuExtVhostUserGPUStart(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video, + qemuDomainLogContextPtr logCtxt) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) + ATTRIBUTE_RETURN_CHECK; + +void qemuExtVhostUserGPUStop(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +int +qemuExtVhostUserGPUSetupCgroup(virQEMUDriverPtr driver, + virDomainDefPtr def, + virDomainVideoDefPtr video, + virCgroupPtr cgroup) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) + ATTRIBUTE_RETURN_CHECK; + +#endif /* __QEMU_VHOST_USER_GPU_H__ */ --=20 2.18.0.129.ge3331758f1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list