qemu_vhost_user_gpu.c 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * qemu_vhost_user_gpu.c: QEMU vhost-user GPU support
 *
 * Copyright (C) 2019 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
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "qemu_vhost_user_gpu.h"
#include "qemu_vhost_user.h"
#include "qemu_extdevice.h"

#include "conf/domain_conf.h"
#include "configmake.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"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("qemu.vhost_user_gpu");


static char *
qemuVhostUserGPUCreatePidFilename(const char *stateDir,
                                  const char *shortName,
                                  const char *alias)
{
48
    g_autofree char *devicename = NULL;
49

50
    devicename = g_strdup_printf("%s-%s-vhost-user-gpu", shortName, alias);
51 52 53 54 55 56 57 58 59 60 61 62 63

    return virPidFileBuildPath(stateDir, devicename);
}


/*
 * qemuVhostUserGPUGetPid:
 * @binpath: path of executable associated with the pidfile
 * @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
 *
64
 * Return -1 upon error, or zero on successful reading of the pidfile.
65 66 67 68 69 70 71 72 73 74
 * If the PID was not still alive, zero will be returned, and @pid will be
 * set to -1;
 */
static int
qemuVhostUserGPUGetPid(const char *binPath,
                       const char *stateDir,
                       const char *shortName,
                       const char *alias,
                       pid_t *pid)
{
75
    g_autofree char *pidfile = NULL;
76

77 78
    if (!(pidfile = qemuVhostUserGPUCreatePidFilename(stateDir, shortName, alias)))
        return -1;
79

80 81 82 83
    if (virPidFileReadPathIfAlive(pidfile, pid, binPath) < 0)
        return -1;

    return 0;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}


int qemuExtVhostUserGPUPrepareDomain(virQEMUDriverPtr driver,
                                     virDomainVideoDefPtr video)
{
    return qemuVhostUserFillDomainGPU(driver, video);
}


/*
 * qemuExtVhostUserGPUStart:
 * @driver: QEMU driver
 * @vm: the VM domain
 * @video: the video device
 *
 * 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,
                             virDomainObjPtr vm,
                             virDomainVideoDefPtr video)
{
109
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
110 111
    g_autofree char *shortname = NULL;
    g_autofree char *pidfile = NULL;
J
Ján Tomko 已提交
112
    g_autoptr(virCommand) cmd = NULL;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    int pair[2] = { -1, -1 };
    int cmdret = 0, rc;
    int exitstatus = 0;
    pid_t pid;
    int ret = -1;

    shortname = virDomainDefGetShortName(vm->def);
    if (!shortname)
        goto error;

    /* stop any left-over for this VM */
    qemuExtVhostUserGPUStop(driver, vm, video);

    if (!(pidfile = qemuVhostUserGPUCreatePidFilename(
              cfg->stateDir, shortname, video->info.alias)))
        goto error;

    if (qemuSecuritySetSocketLabel(driver->securityManager, vm->def) < 0)
        goto error;

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
        virReportSystemError(errno, "%s", _("failed to create socket"));
        goto error;
    }

    if (qemuSecurityClearSocketLabel(driver->securityManager, vm->def) < 0)
        goto error;

    cmd = virCommandNew(video->driver->vhost_user_binary);
    if (!cmd)
        goto error;

    virCommandClearCaps(cmd);
    virCommandSetPidFile(cmd, pidfile);
    virCommandDaemonize(cmd);

    if (qemuExtDeviceLogCommand(driver, vm, cmd, "vhost-user-gpu") < 0)
        goto error;

    virCommandAddArgFormat(cmd, "--fd=%d", pair[0]);
    virCommandPassFD(cmd, pair[0], VIR_COMMAND_PASS_FD_CLOSE_PARENT);
    pair[0] = -1;

    if (video->accel) {
        if (video->accel->accel3d)
            virCommandAddArg(cmd, "--virgl");

        if (video->accel->rendernode)
            virCommandAddArgFormat(cmd, "--render-node=%s", video->accel->rendernode);
    }

    if (qemuSecurityStartVhostUserGPU(driver, vm, cmd,
                                      &exitstatus, &cmdret) < 0)
        goto error;

    if (cmdret < 0 || exitstatus != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not start 'vhost-user-gpu'. exitstatus: %d"), exitstatus);
        goto cleanup;
    }

    rc = virPidFileReadPath(pidfile, &pid);
    if (rc < 0) {
        virReportSystemError(-rc,
                             _("Unable to read vhost-user-gpu pidfile '%s'"),
                             pidfile);
        goto cleanup;
    }

    ret = 0;
    QEMU_DOMAIN_VIDEO_PRIVATE(video)->vhost_user_fd = pair[1];
    pair[1] = -1;

 cleanup:
    VIR_FORCE_CLOSE(pair[0]);
    VIR_FORCE_CLOSE(pair[1]);

    return ret;

 error:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("vhost-user-gpu failed to start"));
    goto cleanup;
}


/*
 * qemuExtVhostUserGPUStop:
 *
 * @driver: QEMU driver
 * @vm: the VM domain
 * @video: the video device
 *
 * Check if vhost-user process pidfile is around, kill the process,
 * and remove the pidfile.
 */
void qemuExtVhostUserGPUStop(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             virDomainVideoDefPtr video)
{
213
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
214 215
    g_autofree char *pidfile = NULL;
    g_autofree char *shortname = NULL;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    virErrorPtr orig_err;

    shortname = virDomainDefGetShortName(vm->def);
    if (!(pidfile = 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 != ENOENT) {
            virReportSystemError(errno,
                                 _("Unable to remove stale pidfile %s"),
                                 pidfile);
        }
    }
    virErrorRestore(&orig_err);
}


/*
 * 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)
{
256
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
257
    g_autofree char *shortname = NULL;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    int rc;
    pid_t pid;

    shortname = virDomainDefGetShortName(def);
    if (!shortname)
        return -1;

    rc = qemuVhostUserGPUGetPid(video->driver->vhost_user_binary,
                                cfg->stateDir, shortname, video->info.alias, &pid);
    if (rc < 0 || (rc == 0 && pid == (pid_t)-1)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not get process id of vhost-user-gpu"));
        return -1;
    }
    if (virCgroupAddProcess(cgroup, pid) < 0)
        return -1;

    return 0;
}