qemu_conf.h 14.3 KB
Newer Older
D
Daniel P. Berrange 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * config.h: VM configuration management
 *
 * Copyright (C) 2006, 2007 Red Hat, Inc.
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24 25
#ifndef __QEMUD_CONF_H
#define __QEMUD_CONF_H
D
Daniel P. Berrange 已提交
26

27
#include <config.h>
28 29 30

#ifdef WITH_QEMU

31
#include "internal.h"
32 33
#include "bridge.h"
#include "iptables.h"
34
#include "capabilities.h"
35 36 37
#include <netinet/in.h>

#define qemudDebug(fmt, ...) do {} while(0)
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* Different types of QEMU acceleration possible */
enum qemud_vm_virt_type {
    QEMUD_VIRT_QEMU,
    QEMUD_VIRT_KQEMU,
    QEMUD_VIRT_KVM,
};

/* Two types of disk backends */
enum qemud_vm_disk_type {
    QEMUD_DISK_BLOCK,
    QEMUD_DISK_FILE
};

/* Three types of disk frontend */
enum qemud_vm_disk_device {
    QEMUD_DISK_DISK,
    QEMUD_DISK_CDROM,
    QEMUD_DISK_FLOPPY,
};

59 60 61 62 63
enum qemud_vm_disk_bus {
    QEMUD_DISK_BUS_IDE,
    QEMUD_DISK_BUS_FDC,
    QEMUD_DISK_BUS_SCSI,
    QEMUD_DISK_BUS_VIRTIO,
64
    QEMUD_DISK_BUS_XEN,
65 66 67 68

    QEMUD_DISK_BUS_LAST
};

69 70 71 72
/* Stores the virtual disk configuration */
struct qemud_vm_disk_def {
    int type;
    int device;
73
    int bus;
74 75 76 77 78 79 80 81
    char src[PATH_MAX];
    char dst[NAME_MAX];
    int readonly;

    struct qemud_vm_disk_def *next;
};

#define QEMUD_MAC_ADDRESS_LEN 6
82
#define QEMUD_MODEL_MAX_LEN 10
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define QEMUD_OS_TYPE_MAX_LEN 10
#define QEMUD_OS_ARCH_MAX_LEN 10
#define QEMUD_OS_MACHINE_MAX_LEN 10

/* 5 different types of networking config */
enum qemud_vm_net_type {
    QEMUD_NET_USER,
    QEMUD_NET_ETHERNET,
    QEMUD_NET_SERVER,
    QEMUD_NET_CLIENT,
    QEMUD_NET_MCAST,
    QEMUD_NET_NETWORK,
    QEMUD_NET_BRIDGE,
};

98 99 100 101 102 103
/* 2 possible types of forwarding */
enum qemud_vm_net_forward_type {
    QEMUD_NET_FORWARD_NAT,
    QEMUD_NET_FORWARD_ROUTE,
};

104 105 106 107
#define QEMUD_MAX_NAME_LEN 50
#define QEMUD_MAX_XML_LEN 4096
#define QEMUD_MAX_ERROR_LEN 1024

108 109 110 111
/* Stores the virtual network interface configuration */
struct qemud_vm_net_def {
    int type;
    unsigned char mac[QEMUD_MAC_ADDRESS_LEN];
112
    char model[QEMUD_MODEL_MAX_LEN];
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    union {
        struct {
            char ifname[BR_IFNAME_MAXLEN];
            char script[PATH_MAX];
        } ethernet;
        struct {
            char address[BR_INET_ADDR_MAXLEN];
            int port;
        } socket; /* any of NET_CLIENT or NET_SERVER or NET_MCAST */
        struct {
            char name[QEMUD_MAX_NAME_LEN];
            char ifname[BR_IFNAME_MAXLEN];
        } network;
        struct {
            char brname[BR_IFNAME_MAXLEN];
            char ifname[BR_IFNAME_MAXLEN];
        } bridge;
    } dst;

    struct qemud_vm_net_def *next;
};

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
enum qemu_vm_chr_dst_type {
    QEMUD_CHR_SRC_TYPE_NULL,
    QEMUD_CHR_SRC_TYPE_VC,
    QEMUD_CHR_SRC_TYPE_PTY,
    QEMUD_CHR_SRC_TYPE_DEV,
    QEMUD_CHR_SRC_TYPE_FILE,
    QEMUD_CHR_SRC_TYPE_PIPE,
    QEMUD_CHR_SRC_TYPE_STDIO,
    QEMUD_CHR_SRC_TYPE_UDP,
    QEMUD_CHR_SRC_TYPE_TCP,
    QEMUD_CHR_SRC_TYPE_UNIX,

    QEMUD_CHR_SRC_TYPE_LAST,
};

enum {
    QEMUD_CHR_SRC_TCP_PROTOCOL_RAW,
    QEMUD_CHR_SRC_TCP_PROTOCOL_TELNET,
};

struct qemud_vm_chr_def {
    int dstPort;

    int srcType;
    union {
        struct {
            char path[PATH_MAX];
        } file; /* pty, file, pipe, or device */
        struct {
            char host[BR_INET_ADDR_MAXLEN];
            char service[BR_INET_ADDR_MAXLEN];
            int listen;
            int protocol;
        } tcp;
        struct {
            char bindHost[BR_INET_ADDR_MAXLEN];
            char bindService[BR_INET_ADDR_MAXLEN];
            char connectHost[BR_INET_ADDR_MAXLEN];
            char connectService[BR_INET_ADDR_MAXLEN];
        } udp;
        struct {
            char path[PATH_MAX];
            int listen;
        } nix;
    } srcData;

    struct qemud_vm_chr_def *next;
};
183 184 185 186 187 188 189 190 191

enum qemu_vm_input_type {
    QEMU_INPUT_TYPE_MOUSE,
    QEMU_INPUT_TYPE_TABLET,
};

enum qemu_vm_input_bus {
    QEMU_INPUT_BUS_PS2,
    QEMU_INPUT_BUS_USB,
192
    QEMU_INPUT_BUS_XEN,
193 194 195 196 197 198 199 200
};

struct qemud_vm_input_def {
    int type;
    int bus;
    struct qemud_vm_input_def *next;
};

D
Daniel Veillard 已提交
201 202 203 204 205 206 207 208 209 210 211 212
enum qemu_vm_sound_model {
    QEMU_SOUND_NONE   = 0,
    QEMU_SOUND_SB16,
    QEMU_SOUND_ES1370,
    QEMU_SOUND_PCSPK,
};

struct qemud_vm_sound_def {
    int model;
    struct qemud_vm_sound_def *next;
};

D
Daniel P. Berrange 已提交
213 214 215 216 217
/* Flags for the 'type' field in next struct */
enum qemud_vm_device_type {
    QEMUD_DEVICE_DISK,
    QEMUD_DEVICE_NET,
    QEMUD_DEVICE_INPUT,
D
Daniel Veillard 已提交
218
    QEMUD_DEVICE_SOUND,
D
Daniel P. Berrange 已提交
219 220 221 222 223 224 225 226
};

struct qemud_vm_device_def {
    int type;
    union {
        struct qemud_vm_disk_def disk;
        struct qemud_vm_net_def net;
        struct qemud_vm_input_def input;
D
Daniel Veillard 已提交
227
        struct qemud_vm_sound_def sound;
D
Daniel P. Berrange 已提交
228 229 230
    } data;
};

231 232 233 234 235 236 237 238 239 240
#define QEMUD_MAX_BOOT_DEVS 4

/* 3 possible boot devices */
enum qemud_vm_boot_order {
    QEMUD_BOOT_FLOPPY,
    QEMUD_BOOT_CDROM,
    QEMUD_BOOT_DISK,
    QEMUD_BOOT_NET,
};
/* 3 possible graphics console modes */
241
enum qemud_vm_graphics_type {
242 243 244 245 246 247 248
    QEMUD_GRAPHICS_NONE,
    QEMUD_GRAPHICS_SDL,
    QEMUD_GRAPHICS_VNC,
};

/* Internal flags to keep track of qemu command line capabilities */
enum qemud_cmd_flags {
249 250 251
    QEMUD_CMD_FLAG_KQEMU          = (1 << 0),
    QEMUD_CMD_FLAG_VNC_COLON      = (1 << 1),
    QEMUD_CMD_FLAG_NO_REBOOT      = (1 << 2),
252 253 254
    QEMUD_CMD_FLAG_DRIVE          = (1 << 3),
    QEMUD_CMD_FLAG_DRIVE_BOOT     = (1 << 4),
    QEMUD_CMD_FLAG_NAME           = (1 << 5),
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
};


enum qemud_vm_features {
    QEMUD_FEATURE_ACPI = 1,
};

/* Operating system configuration data & machine / arch */
struct qemud_vm_os_def {
    char type[QEMUD_OS_TYPE_MAX_LEN];
    char arch[QEMUD_OS_ARCH_MAX_LEN];
    char machine[QEMUD_OS_MACHINE_MAX_LEN];
    int nBootDevs;
    int bootDevs[QEMUD_MAX_BOOT_DEVS];
    char kernel[PATH_MAX];
    char initrd[PATH_MAX];
    char cmdline[PATH_MAX];
    char binary[PATH_MAX];
};

/* Guest VM main configuration */
struct qemud_vm_def {
    int virtType;
278
    unsigned char uuid[VIR_UUID_BUFLEN];
279 280
    char name[QEMUD_MAX_NAME_LEN];

281 282
    unsigned long memory;
    unsigned long maxmem;
283 284 285 286 287 288
    int vcpus;

    int noReboot;

    struct qemud_vm_os_def os;

289
    int localtime;
290 291 292 293
    int features;
    int graphicsType;
    int vncPort;
    int vncActivePort;
294
    char vncListen[BR_INET_ADDR_MAXLEN];
295
    char *keymap;
296

297
    unsigned int ndisks;
298 299
    struct qemud_vm_disk_def *disks;

300
    unsigned int nnets;
301
    struct qemud_vm_net_def *nets;
302

303
    unsigned int ninputs;
304
    struct qemud_vm_input_def *inputs;
305

D
Daniel Veillard 已提交
306 307 308
    unsigned int nsounds;
    struct qemud_vm_sound_def *sounds;

309 310 311 312 313
    unsigned int nserials;
    struct qemud_vm_chr_def *serials;

    unsigned int nparallels;
    struct qemud_vm_chr_def *parallels;
314 315 316 317
};

/* Guest VM runtime state */
struct qemud_vm {
318
    int stdin;
319 320 321 322 323 324 325 326 327 328 329
    int stdout;
    int stderr;
    int monitor;
    int logfile;
    int pid;
    int id;
    int state;

    int *tapfds;
    int ntapfds;

330 331 332
    int qemuVersion;
    int qemuCmdFlags; /* values from enum qemud_cmd_flags */

333 334
    char configFile[PATH_MAX];
    char autostartLink[PATH_MAX];
335
    char migrateFrom[PATH_MAX];
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

    struct qemud_vm_def *def; /* The current definition */
    struct qemud_vm_def *newDef; /* New definition to activate at shutdown */

    unsigned int autostart : 1;

    struct qemud_vm *next;
};

/* Store start and end addresses of a dhcp range */
struct qemud_dhcp_range_def {
    char start[BR_INET_ADDR_MAXLEN];
    char end[BR_INET_ADDR_MAXLEN];

    struct qemud_dhcp_range_def *next;
};

/* Virtual Network main configuration */
struct qemud_network_def {
355
    unsigned char uuid[VIR_UUID_BUFLEN];
356 357 358 359 360 361 362
    char name[QEMUD_MAX_NAME_LEN];

    char bridge[BR_IFNAME_MAXLEN];
    int disableSTP;
    int forwardDelay;

    int forward;
363
    int forwardMode; /* From qemud_vm_net_forward_type */
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    char forwardDev[BR_IFNAME_MAXLEN];

    char ipAddress[BR_INET_ADDR_MAXLEN];
    char netmask[BR_INET_ADDR_MAXLEN];
    char network[BR_INET_ADDR_MAXLEN+BR_INET_ADDR_MAXLEN+1];

    int nranges;
    struct qemud_dhcp_range_def *ranges;
};

/* Virtual Network runtime state */
struct qemud_network {
    char configFile[PATH_MAX];
    char autostartLink[PATH_MAX];

    struct qemud_network_def *def; /* The current definition */
    struct qemud_network_def *newDef; /* New definition to activate at shutdown */

    char bridge[BR_IFNAME_MAXLEN];
    int dnsmasqPid;

    unsigned int active : 1;
    unsigned int autostart : 1;

    struct qemud_network *next;
};


/* Main driver state */
struct qemud_driver {
    int qemuVersion;
    int nactivevms;
    int ninactivevms;
    struct qemud_vm *vms;
    int nextvmid;
    int nactivenetworks;
    int ninactivenetworks;
    struct qemud_network *networks;
    brControl *brctl;
    iptablesContext *iptables;
    char *configDir;
    char *autostartDir;
    char *networkConfigDir;
    char *networkAutostartDir;
    char logDir[PATH_MAX];
409 410
    unsigned int vncTLS : 1;
    unsigned int vncTLSx509verify : 1;
D
Daniel P. Berrange 已提交
411 412
    char *vncTLSx509certdir;
    char vncListen[BR_INET_ADDR_MAXLEN];
413 414

    virCapsPtr caps;
415 416 417 418
};


static inline int
419
qemudIsActiveVM(const struct qemud_vm *vm)
420 421 422 423 424
{
    return vm->id != -1;
}

static inline int
425
qemudIsActiveNetwork(const struct qemud_network *network)
426 427 428 429 430 431 432 433 434 435 436
{
    return network->active;
}

void qemudReportError(virConnectPtr conn,
                      virDomainPtr dom,
                      virNetworkPtr net,
                      int code, const char *fmt, ...)
    ATTRIBUTE_FORMAT(printf,5,6);


D
Daniel P. Berrange 已提交
437 438
int qemudLoadDriverConfig(struct qemud_driver *driver,
                          const char *filename);
439 440 441 442 443 444 445 446 447 448 449 450

struct qemud_vm *qemudFindVMByID(const struct qemud_driver *driver,
                                 int id);
struct qemud_vm *qemudFindVMByUUID(const struct qemud_driver *driver,
                                   const unsigned char *uuid);
struct qemud_vm *qemudFindVMByName(const struct qemud_driver *driver,
                                   const char *name);

struct qemud_network *qemudFindNetworkByUUID(const struct qemud_driver *driver,
                                             const unsigned char *uuid);
struct qemud_network *qemudFindNetworkByName(const struct qemud_driver *driver,
                                             const char *name);
D
Daniel P. Berrange 已提交
451

452 453
virCapsPtr  qemudCapsInit               (void);

454 455 456 457
int         qemudExtractVersion         (virConnectPtr conn,
                                         struct qemud_driver *driver);
int         qemudBuildCommandLine       (virConnectPtr conn,
                                         struct qemud_driver *driver,
458 459
                                         struct qemud_vm *vm,
                                         char ***argv);
460

461
int         qemudScanConfigs            (struct qemud_driver *driver);
462 463
int         qemudDeleteConfig           (virConnectPtr conn,
                                         struct qemud_driver *driver,
464 465
                                         const char *configFile,
                                         const char *name);
466

467 468
void        qemudFreeVMDef              (struct qemud_vm_def *vm);
void        qemudFreeVM                 (struct qemud_vm *vm);
469

470
struct qemud_vm *
471 472
            qemudAssignVMDef            (virConnectPtr conn,
                                         struct qemud_driver *driver,
473
                                         struct qemud_vm_def *def);
474
void        qemudRemoveInactiveVM       (struct qemud_driver *driver,
475 476
                                         struct qemud_vm *vm);

D
Daniel P. Berrange 已提交
477 478
struct qemud_vm_device_def *
            qemudParseVMDeviceDef       (virConnectPtr conn,
479
                                         const struct qemud_vm_def *def,
D
Daniel P. Berrange 已提交
480 481
                                         const char *xmlStr);

482
struct qemud_vm_def *
483 484
            qemudParseVMDef             (virConnectPtr conn,
                                         struct qemud_driver *driver,
485 486
                                         const char *xmlStr,
                                         const char *displayName);
487 488
int         qemudSaveVMDef              (virConnectPtr conn,
                                         struct qemud_driver *driver,
489 490
                                         struct qemud_vm *vm,
                                         struct qemud_vm_def *def);
491 492
char *      qemudGenerateXML            (virConnectPtr conn,
                                         struct qemud_driver *driver,
493 494 495 496 497 498 499 500
                                         struct qemud_vm *vm,
                                         struct qemud_vm_def *def,
                                         int live);

void        qemudFreeNetworkDef         (struct qemud_network_def *def);
void        qemudFreeNetwork            (struct qemud_network *network);

struct qemud_network *
501 502
            qemudAssignNetworkDef       (virConnectPtr conn,
                                         struct qemud_driver *driver,
503
                                         struct qemud_network_def *def);
504
void        qemudRemoveInactiveNetwork  (struct qemud_driver *driver,
505 506 507
                                         struct qemud_network *network);

struct qemud_network_def *
508 509
            qemudParseNetworkDef        (virConnectPtr conn,
                                         struct qemud_driver *driver,
510 511
                                         const char *xmlStr,
                                         const char *displayName);
512 513
int         qemudSaveNetworkDef         (virConnectPtr conn,
                                         struct qemud_driver *driver,
514 515
                                         struct qemud_network *network,
                                         struct qemud_network_def *def);
516 517
char *      qemudGenerateNetworkXML     (virConnectPtr conn,
                                         struct qemud_driver *driver,
518 519
                                         struct qemud_network *network,
                                         struct qemud_network_def *def);
D
Daniel P. Berrange 已提交
520

521
const char *qemudVirtTypeToString       (int type);
522

523 524 525
#endif /* WITH_QEMU */

#endif /* __QEMUD_CONF_H */