qemu_hostdev.c 14.5 KB
Newer Older
1 2 3
/*
 * qemu_hostdev.c: QEMU hostdev management
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2007, 2009-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

26 27 28 29 30
#include <dirent.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>

31
#include "qemu_hostdev.h"
32
#include "virlog.h"
33
#include "virerror.h"
34
#include "viralloc.h"
35
#include "virpci.h"
36
#include "virusb.h"
37
#include "virscsi.h"
38
#include "virnetdev.h"
39
#include "virfile.h"
40
#include "virhostdev.h"
41

42 43
#define VIR_FROM_THIS VIR_FROM_QEMU

44 45 46
VIR_LOG_INIT("qemu.qemu_hostdev");


47
int
48 49
qemuHostdevUpdateActivePCIDevices(virQEMUDriverPtr driver,
                                  virDomainDefPtr def)
50 51 52 53 54 55
{
    virHostdevManagerPtr mgr = driver->hostdevMgr;

    if (!def->nhostdevs)
        return 0;

56 57
    return virHostdevUpdateActivePCIDevices(mgr, def->hostdevs, def->nhostdevs,
                                            QEMU_DRIVER_NAME, def->name);
58 59
}

60
int
61 62
qemuHostdevUpdateActiveUSBDevices(virQEMUDriverPtr driver,
                                  virDomainDefPtr def)
63 64 65 66 67 68
{
    virHostdevManagerPtr mgr = driver->hostdevMgr;

    if (!def->nhostdevs)
        return 0;

69 70
    return virHostdevUpdateActiveUSBDevices(mgr, def->hostdevs, def->nhostdevs,
                                            QEMU_DRIVER_NAME, def->name);
71 72
}

73
int
74 75
qemuHostdevUpdateActiveSCSIDevices(virQEMUDriverPtr driver,
                                   virDomainDefPtr def)
76 77 78 79 80 81
{
    virHostdevManagerPtr mgr = driver->hostdevMgr;

    if (!def->nhostdevs)
        return 0;

82 83
    return virHostdevUpdateActiveSCSIDevices(mgr, def->hostdevs, def->nhostdevs,
                                             QEMU_DRIVER_NAME, def->name);
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

int
qemuHostdevUpdateActiveMediatedDevices(virQEMUDriverPtr driver,
                                       virDomainDefPtr def)
{
    virHostdevManagerPtr mgr = driver->hostdevMgr;

    if (!def->nhostdevs)
        return 0;

    return virHostdevUpdateActiveMediatedDevices(mgr, def->hostdevs,
                                                 def->nhostdevs,
                                                 QEMU_DRIVER_NAME, def->name);
}


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
int
qemuHostdevUpdateActiveDomainDevices(virQEMUDriverPtr driver,
                                     virDomainDefPtr def)
{
    if (!def->nhostdevs)
        return 0;

    if (qemuHostdevUpdateActivePCIDevices(driver, def) < 0)
        return -1;

    if (qemuHostdevUpdateActiveUSBDevices(driver, def) < 0)
        return -1;

    if (qemuHostdevUpdateActiveSCSIDevices(driver, def) < 0)
        return -1;

118 119 120
    if (qemuHostdevUpdateActiveMediatedDevices(driver, def) < 0)
        return -1;

121 122
    return 0;
}
123

124
bool
125 126 127 128 129
qemuHostdevHostSupportsPassthroughVFIO(void)
{
    DIR *iommuDir = NULL;
    struct dirent *iommuGroup = NULL;
    bool ret = false;
E
Eric Blake 已提交
130
    int direrr;
131 132

    /* condition 1 - /sys/kernel/iommu_groups/ contains entries */
J
Ján Tomko 已提交
133
    if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0)
134 135
        goto cleanup;

E
Eric Blake 已提交
136
    while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) {
137 138 139 140
        /* assume we found a group */
        break;
    }

E
Eric Blake 已提交
141
    if (direrr < 0 || !iommuGroup)
142 143 144 145 146 147 148 149 150
        goto cleanup;
    /* okay, iommu is on and recognizes groups */

    /* condition 2 - /dev/vfio/vfio exists */
    if (!virFileExists("/dev/vfio/vfio"))
        goto cleanup;

    ret = true;

151
 cleanup:
J
Ján Tomko 已提交
152
    VIR_DIR_CLOSE(iommuDir);
153 154 155 156 157 158
    return ret;
}


#if HAVE_LINUX_KVM_H
# include <linux/kvm.h>
159
bool
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
qemuHostdevHostSupportsPassthroughLegacy(void)
{
    int kvmfd = -1;
    bool ret = false;

    if ((kvmfd = open("/dev/kvm", O_RDONLY)) < 0)
        goto cleanup;

# ifdef KVM_CAP_IOMMU
    if ((ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU)) <= 0)
        goto cleanup;

    ret = true;
# endif

175
 cleanup:
176 177 178 179 180
    VIR_FORCE_CLOSE(kvmfd);

    return ret;
}
#else
181
bool
182 183 184 185 186 187 188 189
qemuHostdevHostSupportsPassthroughLegacy(void)
{
    return false;
}
#endif


static bool
190 191 192
qemuHostdevPreparePCIDevicesCheckSupport(virDomainHostdevDefPtr *hostdevs,
                                         size_t nhostdevs,
                                         virQEMUCapsPtr qemuCaps)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
{
    bool supportsPassthroughKVM = qemuHostdevHostSupportsPassthroughLegacy();
    bool supportsPassthroughVFIO = qemuHostdevHostSupportsPassthroughVFIO();
    size_t i;

    /* assign defaults for hostdev passthrough */
    for (i = 0; i < nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = hostdevs[i];
        int *backend = &hostdev->source.subsys.u.pci.backend;

        if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
            continue;
        if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
            continue;

208
        switch ((virDomainHostdevSubsysPCIBackendType) *backend) {
209 210 211 212
        case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
            if (supportsPassthroughVFIO &&
                virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
                *backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
213
            } else if (supportsPassthroughKVM) {
214 215 216 217 218 219 220 221 222 223
                *backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM;
            } else {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("host doesn't support passthrough of "
                                 "host PCI devices"));
                return false;
            }

            break;

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
            if (!supportsPassthroughVFIO) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("host doesn't support VFIO PCI passthrough"));
                return false;
            }
            break;

        case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
            if (!supportsPassthroughKVM) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("host doesn't support legacy PCI passthrough"));
                return false;
            }

            break;

241
        case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_XEN:
242 243 244 245 246 247 248 249
        case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
            break;
        }
    }

    return true;
}

250
int
251
qemuHostdevPreparePCIDevices(virQEMUDriverPtr driver,
252 253 254 255 256 257 258 259 260 261
                             const char *name,
                             const unsigned char *uuid,
                             virDomainHostdevDefPtr *hostdevs,
                             int nhostdevs,
                             virQEMUCapsPtr qemuCaps,
                             unsigned int flags)
{
    int ret = -1;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

262
    if (!qemuHostdevPreparePCIDevicesCheckSupport(hostdevs, nhostdevs, qemuCaps))
263 264
        goto out;

265 266
    ret = virHostdevPreparePCIDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                      name, uuid, hostdevs,
267
                                      nhostdevs, flags);
268
 out:
269 270 271
    return ret;
}

272
int
273 274 275 276 277
qemuHostdevPrepareUSBDevices(virQEMUDriverPtr driver,
                             const char *name,
                             virDomainHostdevDefPtr *hostdevs,
                             int nhostdevs,
                             unsigned int flags)
278 279 280
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

281
    return virHostdevPrepareUSBDevices(hostdev_mgr, QEMU_DRIVER_NAME, name,
282 283 284
                                       hostdevs, nhostdevs, flags);
}

285
int
286
qemuHostdevPrepareSCSIDevices(virQEMUDriverPtr driver,
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                              const char *name,
                              virDomainHostdevDefPtr *hostdevs,
                              int nhostdevs)
{
    size_t i;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    /* Loop 1: Add the shared scsi host device to shared device
     * table.
     */
    for (i = 0; i < nhostdevs; i++) {
        virDomainDeviceDef dev;

        dev.type = VIR_DOMAIN_DEVICE_HOSTDEV;
        dev.data.hostdev = hostdevs[i];

        if (qemuAddSharedDevice(driver, &dev, name) < 0)
            return -1;

        if (qemuSetUnprivSGIO(&dev) < 0)
            return -1;
    }

310 311
    return virHostdevPrepareSCSIDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                        name, hostdevs, nhostdevs);
312 313
}

314 315 316 317 318 319 320 321 322 323 324
int
qemuHostdevPrepareSCSIVHostDevices(virQEMUDriverPtr driver,
                                   const char *name,
                                   virDomainHostdevDefPtr *hostdevs,
                                   int nhostdevs)
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    return virHostdevPrepareSCSIVHostDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                             name, hostdevs, nhostdevs);
}
325

326 327 328 329 330 331 332
int
qemuHostdevPrepareMediatedDevices(virQEMUDriverPtr driver,
                                  const char *name,
                                  virDomainHostdevDefPtr *hostdevs,
                                  int nhostdevs)
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
333 334
    bool supportsVFIO = qemuHostdevHostSupportsPassthroughVFIO();
    size_t i;
335

336 337 338 339 340 341 342 343 344 345 346
    for (i = 0; i < nhostdevs; i++) {
        if (hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV) {
            if (!supportsVFIO) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("Mediated host device assignment requires "
                                 "VFIO support"));
                return -1;
            }
            break;
        }
347 348 349 350 351 352
    }

    return virHostdevPrepareMediatedDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                            name, hostdevs, nhostdevs);
}

353
int
354 355 356 357
qemuHostdevPrepareDomainDevices(virQEMUDriverPtr driver,
                                virDomainDefPtr def,
                                virQEMUCapsPtr qemuCaps,
                                unsigned int flags)
358 359 360 361
{
    if (!def->nhostdevs)
        return 0;

362
    if (qemuHostdevPreparePCIDevices(driver, def->name, def->uuid,
363
                                     def->hostdevs, def->nhostdevs,
364
                                     qemuCaps, flags) < 0)
365 366
        return -1;

367 368
    if (qemuHostdevPrepareUSBDevices(driver, def->name,
                                     def->hostdevs, def->nhostdevs, flags) < 0)
369 370
        return -1;

371
    if (qemuHostdevPrepareSCSIDevices(driver, def->name,
372 373 374
                                      def->hostdevs, def->nhostdevs) < 0)
        return -1;

375 376 377 378
    if (qemuHostdevPrepareSCSIVHostDevices(driver, def->name,
                                           def->hostdevs, def->nhostdevs) < 0)
        return -1;

379 380 381 382
    if (qemuHostdevPrepareMediatedDevices(driver, def->name,
                                          def->hostdevs, def->nhostdevs) < 0)
        return -1;

383 384 385
    return 0;
}

386
void
387 388 389 390
qemuHostdevReAttachPCIDevices(virQEMUDriverPtr driver,
                              const char *name,
                              virDomainHostdevDefPtr *hostdevs,
                              int nhostdevs)
391 392
{
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
393
    const char *oldStateDir = cfg->stateDir;
394 395
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

396
    virHostdevReAttachPCIDevices(hostdev_mgr, QEMU_DRIVER_NAME, name,
397 398
                                 hostdevs, nhostdevs, oldStateDir);

399
    virObjectUnref(cfg);
400 401
}

402
void
403 404 405 406
qemuHostdevReAttachUSBDevices(virQEMUDriverPtr driver,
                              const char *name,
                              virDomainHostdevDefPtr *hostdevs,
                              int nhostdevs)
407 408 409
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

410
    virHostdevReAttachUSBDevices(hostdev_mgr, QEMU_DRIVER_NAME,
411
                                  name, hostdevs, nhostdevs);
412 413
}

414
void
415 416 417 418
qemuHostdevReAttachSCSIDevices(virQEMUDriverPtr driver,
                               const char *name,
                               virDomainHostdevDefPtr *hostdevs,
                               int nhostdevs)
419 420 421 422 423 424 425 426 427 428 429 430 431 432
{
    size_t i;
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    for (i = 0; i < nhostdevs; i++) {
        virDomainHostdevDefPtr hostdev = hostdevs[i];
        virDomainDeviceDef dev;

        dev.type = VIR_DOMAIN_DEVICE_HOSTDEV;
        dev.data.hostdev = hostdev;

        ignore_value(qemuRemoveSharedDevice(driver, &dev, name));
    }

433 434
    virHostdevReAttachSCSIDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                  name, hostdevs, nhostdevs);
435 436
}

437 438 439 440 441 442 443 444 445 446 447 448
void
qemuHostdevReAttachSCSIVHostDevices(virQEMUDriverPtr driver,
                                    const char *name,
                                    virDomainHostdevDefPtr *hostdevs,
                                    int nhostdevs)
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    virHostdevReAttachSCSIVHostDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                       name, hostdevs, nhostdevs);
}

449 450 451 452 453 454 455 456 457 458 459 460
void
qemuHostdevReAttachMediatedDevices(virQEMUDriverPtr driver,
                                   const char *name,
                                   virDomainHostdevDefPtr *hostdevs,
                                   int nhostdevs)
{
    virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;

    virHostdevReAttachMediatedDevices(hostdev_mgr, QEMU_DRIVER_NAME,
                                      name, hostdevs, nhostdevs);
}

461
void
462 463
qemuHostdevReAttachDomainDevices(virQEMUDriverPtr driver,
                                 virDomainDefPtr def)
464 465 466 467
{
    if (!def->nhostdevs)
        return;

468 469
    qemuHostdevReAttachPCIDevices(driver, def->name, def->hostdevs,
                                  def->nhostdevs);
470

471 472
    qemuHostdevReAttachUSBDevices(driver, def->name, def->hostdevs,
                                  def->nhostdevs);
473

474 475
    qemuHostdevReAttachSCSIDevices(driver, def->name, def->hostdevs,
                                   def->nhostdevs);
476 477 478

    qemuHostdevReAttachSCSIVHostDevices(driver, def->name, def->hostdevs,
                                        def->nhostdevs);
479 480 481

    qemuHostdevReAttachMediatedDevices(driver, def->name, def->hostdevs,
                                       def->nhostdevs);
482
}