bhyve_capabilities.c 8.4 KB
Newer Older
1 2 3 4
/*
 * bhyve_capabilities.c: bhyve capabilities module
 *
 * Copyright (C) 2014 Roman Bogorodskiy
5
 * Copyright (C) 2014 Semihalf
6
 * Copyright (C) 2016 Fabian Freyer
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 <sys/utsname.h>
F
Fabian Freyer 已提交
25 26 27
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
28 29

#include "viralloc.h"
30
#include "virfile.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "virlog.h"
#include "virstring.h"
#include "cpu/cpu.h"
#include "nodeinfo.h"
#include "bhyve_utils.h"
#include "domain_conf.h"
#include "vircommand.h"
#include "bhyve_capabilities.h"

#define VIR_FROM_THIS   VIR_FROM_BHYVE

VIR_LOG_INIT("bhyve.bhyve_capabilities");

static int
virBhyveCapsInitCPU(virCapsPtr caps,
46
                    virArch arch)
47 48 49
{
    virNodeInfo nodeinfo;

50
    if (nodeGetInfo(&nodeinfo))
51
        return -1;
52

53
    if (!(caps->host.cpu = virCPUGetHost(arch, VIR_CPU_TYPE_HOST, &nodeinfo)))
54
        return -1;
55

56
    return 0;
57 58 59 60 61 62 63 64 65
}

virCapsPtr
virBhyveCapsBuild(void)
{
    virCapsPtr caps;
    virCapsGuestPtr guest;

    if ((caps = virCapabilitiesNew(virArchFromHost(),
66
                                   false, false)) == NULL)
67 68
        return NULL;

69
    if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
70 71 72 73 74
                                         VIR_ARCH_X86_64,
                                         "bhyve",
                                         NULL, 0, NULL)) == NULL)
        goto error;

75 76
    if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_BHYVE,
                                      NULL, NULL, 0, NULL) == NULL)
77 78 79
        goto error;

    if (virBhyveCapsInitCPU(caps, virArchFromHost()) < 0)
80
        VIR_WARN("Failed to get host CPU: %s", virGetLastErrorMessage());
81 82 83 84 85 86 87

    return caps;

 error:
    virObjectUnref(caps);
    return NULL;
}
88

89 90 91 92 93 94 95
virDomainCapsPtr
virBhyveDomainCapsBuild(const char *emulatorbin,
                        const char *machine,
                        virArch arch,
                        virDomainVirtType virttype)
{
    virDomainCapsPtr caps = NULL;
F
Fabian Freyer 已提交
96
    unsigned int bhyve_caps = 0;
F
Fabian Freyer 已提交
97 98 99 100
    DIR *dir;
    struct dirent *entry;
    const char *firmware_dir = "/usr/local/share/uefi-firmware";
    size_t firmwares_alloc = 0;
101 102 103 104

    if (!(caps = virDomainCapsNew(emulatorbin, machine, arch, virttype)))
        goto cleanup;

F
Fabian Freyer 已提交
105 106 107 108 109 110
    if (virBhyveProbeCaps(&bhyve_caps)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed probing capabilities"));
        goto cleanup;
    }

111
    caps->os.supported = true;
F
Fabian Freyer 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    caps->os.loader.supported = true;
    VIR_DOMAIN_CAPS_ENUM_SET(caps->os.loader.type,
                             VIR_DOMAIN_LOADER_TYPE_PFLASH);
    VIR_DOMAIN_CAPS_ENUM_SET(caps->os.loader.readonly,
                             VIR_TRISTATE_BOOL_YES);

    if (virDirOpenIfExists(&dir, firmware_dir) > 0) {
        while ((virDirRead(dir, &entry, firmware_dir)) > 0) {
            if (VIR_RESIZE_N(caps->os.loader.values.values,
                firmwares_alloc, caps->os.loader.values.nvalues, 2) < 0)
                goto cleanup;

            if (virAsprintf(
                    &caps->os.loader.values.values[caps->os.loader.values.nvalues],
                    "%s/%s", firmware_dir, entry->d_name) < 0)
                goto cleanup;

           caps->os.loader.values.nvalues++;
        }
    }
132 133 134 135 136 137 138 139 140
    caps->disk.supported = true;
    VIR_DOMAIN_CAPS_ENUM_SET(caps->disk.diskDevice,
                             VIR_DOMAIN_DISK_DEVICE_DISK,
                             VIR_DOMAIN_DISK_DEVICE_CDROM);

    VIR_DOMAIN_CAPS_ENUM_SET(caps->disk.bus,
                             VIR_DOMAIN_DISK_BUS_SATA,
                             VIR_DOMAIN_DISK_BUS_VIRTIO);

F
Fabian Freyer 已提交
141 142 143 144 145 146
    if (bhyve_caps & BHYVE_CAP_FBUF) {
        caps->graphics.supported = true;
        caps->video.supported = true;
        VIR_DOMAIN_CAPS_ENUM_SET(caps->graphics.type, VIR_DOMAIN_GRAPHICS_TYPE_VNC);
        VIR_DOMAIN_CAPS_ENUM_SET(caps->video.modelType, VIR_DOMAIN_VIDEO_TYPE_GOP);
    }
147
 cleanup:
F
Fabian Freyer 已提交
148
    VIR_DIR_CLOSE(dir);
149 150 151
    return caps;
}

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
int
virBhyveProbeGrubCaps(virBhyveGrubCapsFlags *caps)
{
    char *binary, *help;
    virCommandPtr cmd;
    int ret, exit;

    ret = 0;
    *caps = 0;
    cmd = NULL;
    help = NULL;

    binary = virFindFileInPath("grub-bhyve");
    if (binary == NULL)
        goto out;
    if (!virFileIsExecutable(binary))
        goto out;

    cmd = virCommandNew(binary);
    virCommandAddArg(cmd, "--help");
    virCommandSetOutputBuffer(cmd, &help);
    if (virCommandRun(cmd, &exit) < 0) {
        ret = -1;
        goto out;
    }

    if (strstr(help, "--cons-dev") != NULL)
        *caps |= BHYVE_GRUB_CAP_CONSDEV;

 out:
    VIR_FREE(help);
    virCommandFree(cmd);
    VIR_FREE(binary);
    return ret;
}
R
Roman Bogorodskiy 已提交
187

188 189
static int
bhyveProbeCapsRTC_UTC(unsigned int *caps, char *binary)
R
Roman Bogorodskiy 已提交
190
{
191
    char *help;
R
Roman Bogorodskiy 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    virCommandPtr cmd = NULL;
    int ret = 0, exit;

    cmd = virCommandNew(binary);
    virCommandAddArg(cmd, "-h");
    virCommandSetErrorBuffer(cmd, &help);
    if (virCommandRun(cmd, &exit) < 0) {
        ret = -1;
        goto out;
    }

    if (strstr(help, "-u:") != NULL)
        *caps |= BHYVE_CAP_RTC_UTC;

 out:
    VIR_FREE(help);
    virCommandFree(cmd);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    return ret;
}

static int
bhyveProbeCapsAHCI32Slot(unsigned int *caps, char *binary)
{
    char *error;
    virCommandPtr cmd = NULL;
    int ret = 0, exit;

    cmd = virCommandNew(binary);
    virCommandAddArgList(cmd, "-s", "0,ahci", NULL);
    virCommandSetErrorBuffer(cmd, &error);
    if (virCommandRun(cmd, &exit) < 0) {
        ret = -1;
        goto out;
    }

    if (strstr(error, "pci slot 0:0: unknown device \"ahci\"") == NULL)
        *caps |= BHYVE_CAP_AHCI32SLOT;

 out:
    VIR_FREE(error);
    virCommandFree(cmd);
    return ret;
}

R
Roman Bogorodskiy 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static int
bhyveProbeCapsNetE1000(unsigned int *caps, char *binary)
{
    char *error;
    virCommandPtr cmd = NULL;
    int ret = -1, exit;

    cmd = virCommandNew(binary);
    virCommandAddArgList(cmd, "-s", "0,e1000", NULL);
    virCommandSetErrorBuffer(cmd, &error);
    if (virCommandRun(cmd, &exit) < 0)
        goto cleanup;

    if (strstr(error, "pci slot 0:0: unknown device \"e1000\"") == NULL)
        *caps |= BHYVE_CAP_NET_E1000;

    ret = 0;
 cleanup:
    VIR_FREE(error);
    virCommandFree(cmd);
    return ret;
}
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int
bhyveProbeCapsLPC_Bootrom(unsigned int *caps, char *binary)
{
    char *error;
    virCommandPtr cmd = NULL;
    int ret = -1, exit;

    cmd = virCommandNew(binary);
    virCommandAddArgList(cmd, "-l", "bootrom", NULL);
    virCommandSetErrorBuffer(cmd, &error);
    if (virCommandRun(cmd, &exit) < 0)
        goto cleanup;

    if (strstr(error, "bhyve: invalid lpc device configuration 'bootrom'") == NULL)
        *caps |= BHYVE_CAP_LPC_BOOTROM;

    ret = 0;
 cleanup:
    VIR_FREE(error);
    virCommandFree(cmd);
    return ret;
}

F
Fabian Freyer 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

static int
bhyveProbeCapsFramebuffer(unsigned int *caps, char *binary)
{
    char *error;
    virCommandPtr cmd = NULL;
    int ret = -1, exit;

    cmd = virCommandNew(binary);
    virCommandAddArgList(cmd, "-s", "0,fbuf", NULL);
    virCommandSetErrorBuffer(cmd, &error);
    if (virCommandRun(cmd, &exit) < 0)
        goto cleanup;

    if (strstr(error, "pci slot 0:0: unknown device \"fbuf\"") == NULL)
        *caps |= BHYVE_CAP_FBUF;

    ret = 0;
 cleanup:
    VIR_FREE(error);
    virCommandFree(cmd);
    return ret;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
int
virBhyveProbeCaps(unsigned int *caps)
{
    char *binary;
    int ret = 0;

    binary = virFindFileInPath("bhyve");
    if (binary == NULL)
        goto out;
    if (!virFileIsExecutable(binary))
        goto out;

    if ((ret = bhyveProbeCapsRTC_UTC(caps, binary)))
        goto out;

    if ((ret = bhyveProbeCapsAHCI32Slot(caps, binary)))
        goto out;

R
Roman Bogorodskiy 已提交
324 325 326
    if ((ret = bhyveProbeCapsNetE1000(caps, binary)))
        goto out;

327 328 329
    if ((ret = bhyveProbeCapsLPC_Bootrom(caps, binary)))
        goto out;

F
Fabian Freyer 已提交
330 331 332
    if ((ret = bhyveProbeCapsFramebuffer(caps, binary)))
        goto out;

333
 out:
R
Roman Bogorodskiy 已提交
334 335 336
    VIR_FREE(binary);
    return ret;
}