qemu_domain.h 18.9 KB
Newer Older
1 2 3
/*
 * qemu_domain.h: QEMU domain private state
 *
4
 * Copyright (C) 2006-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 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#ifndef __QEMU_DOMAIN_H__
# define __QEMU_DOMAIN_H__

27
# include "virthread.h"
28
# include "vircgroup.h"
29
# include "domain_addr.h"
30
# include "domain_conf.h"
31
# include "snapshot_conf.h"
32
# include "qemu_monitor.h"
D
Daniel P. Berrange 已提交
33
# include "qemu_agent.h"
34
# include "qemu_conf.h"
35
# include "qemu_capabilities.h"
36
# include "virchrdev.h"
37
# include "virobject.h"
38

39 40 41 42
# define QEMU_DOMAIN_FORMAT_LIVE_FLAGS      \
    (VIR_DOMAIN_XML_SECURE |                \
     VIR_DOMAIN_XML_UPDATE_CPU)

43 44 45
# if ULONG_MAX == 4294967295
/* Qemu has a 64-bit limit, but we are limited by our historical choice of
 * representing bandwidth in a long instead of a 64-bit int.  */
46
#  define QEMU_DOMAIN_MIG_BANDWIDTH_MAX ULONG_MAX
47
# else
48
#  define QEMU_DOMAIN_MIG_BANDWIDTH_MAX (INT64_MAX / (1024 * 1024))
49
# endif
50

51
# define JOB_MASK(job)                  (1 << (job - 1))
52
# define QEMU_JOB_DEFAULT_MASK          \
53 54 55
    (JOB_MASK(QEMU_JOB_QUERY) |         \
     JOB_MASK(QEMU_JOB_DESTROY) |       \
     JOB_MASK(QEMU_JOB_ABORT))
56

57 58 59 60 61
/* Jobs which have to be tracked in domain state XML. */
# define QEMU_DOMAIN_TRACK_JOBS         \
    (JOB_MASK(QEMU_JOB_DESTROY) |       \
     JOB_MASK(QEMU_JOB_ASYNC))

62 63 64
/* Only 1 job is allowed at any time
 * A job includes *all* monitor commands, even those just querying
 * information, not merely actions */
65
typedef enum {
66
    QEMU_JOB_NONE = 0,  /* Always set to 0 for easy if (jobActive) conditions */
67 68 69 70
    QEMU_JOB_QUERY,         /* Doesn't change any state */
    QEMU_JOB_DESTROY,       /* Destroys the domain (cannot be masked out) */
    QEMU_JOB_SUSPEND,       /* Suspends (stops vCPUs) the domain */
    QEMU_JOB_MODIFY,        /* May change state */
71
    QEMU_JOB_ABORT,         /* Abort current async job */
72
    QEMU_JOB_MIGRATION_OP,  /* Operation influencing outgoing migration */
73

74
    /* The following two items must always be the last items before JOB_LAST */
75 76
    QEMU_JOB_ASYNC,         /* Asynchronous job */
    QEMU_JOB_ASYNC_NESTED,  /* Normal job within an async job */
77 78

    QEMU_JOB_LAST
79
} qemuDomainJob;
80
VIR_ENUM_DECL(qemuDomainJob)
81 82 83 84 85

/* Async job consists of a series of jobs that may change state. Independent
 * jobs that do not change state (and possibly others if explicitly allowed by
 * current async job) are allowed to be run even if async job is active.
 */
86
typedef enum {
87 88 89 90 91
    QEMU_ASYNC_JOB_NONE = 0,
    QEMU_ASYNC_JOB_MIGRATION_OUT,
    QEMU_ASYNC_JOB_MIGRATION_IN,
    QEMU_ASYNC_JOB_SAVE,
    QEMU_ASYNC_JOB_DUMP,
92
    QEMU_ASYNC_JOB_SNAPSHOT,
93
    QEMU_ASYNC_JOB_START,
94 95

    QEMU_ASYNC_JOB_LAST
96
} qemuDomainAsyncJob;
97
VIR_ENUM_DECL(qemuDomainAsyncJob)
98

J
Jiri Denemark 已提交
99 100 101 102 103
typedef struct _qemuDomainJobInfo qemuDomainJobInfo;
typedef qemuDomainJobInfo *qemuDomainJobInfoPtr;
struct _qemuDomainJobInfo {
    virDomainJobType type;
    unsigned long long started; /* When the async job started */
104
    unsigned long long stopped; /* When the domain's CPUs were stopped */
105 106 107 108
    unsigned long long sent; /* When the source sent status info to the
                                destination (only for migrations). */
    unsigned long long received; /* When the destination host received status
                                    info from the source (migrations only). */
J
Jiri Denemark 已提交
109 110 111
    /* Computed values */
    unsigned long long timeElapsed;
    unsigned long long timeRemaining;
J
Jiri Denemark 已提交
112
    long long timeDelta; /* delta = received - sent, i.e., the difference
113 114 115 116 117
                            between the source and the destination time plus
                            the time between the end of Perform phase on the
                            source and the beginning of Finish phase on the
                            destination. */
    bool timeDeltaSet;
J
Jiri Denemark 已提交
118 119 120 121
    /* Raw values from QEMU */
    qemuMonitorMigrationStatus status;
};

122
struct qemuDomainJobObj {
123
    virCond cond;                       /* Use to coordinate jobs */
124
    qemuDomainJob active;               /* Currently running job */
125
    unsigned long long owner;           /* Thread id which set current job */
126
    const char *ownerAPI;               /* The API which owns the job */
J
Jiri Denemark 已提交
127
    unsigned long long started;         /* When the current job started */
128

129
    virCond asyncCond;                  /* Use to coordinate with async jobs */
130
    qemuDomainAsyncJob asyncJob;        /* Currently active async job */
131
    unsigned long long asyncOwner;      /* Thread which set current async job */
132
    const char *asyncOwnerAPI;          /* The API which owns the async job */
J
Jiri Denemark 已提交
133
    unsigned long long asyncStarted;    /* When the current async job started */
J
Jiri Denemark 已提交
134
    int phase;                          /* Job phase (mainly for migrations) */
135
    unsigned long long mask;            /* Jobs allowed during async job */
136
    bool dump_memory_only;              /* use dump-guest-memory to do dump */
J
Jiri Denemark 已提交
137
    qemuDomainJobInfoPtr current;       /* async job progress data */
138
    qemuDomainJobInfoPtr completed;     /* statistics data of a recently completed job */
139
    bool abortJob;                      /* abort of the job requested */
140
    bool spiceMigrated;                 /* spice migration completed */
141 142
};

143
typedef void (*qemuDomainCleanupCallback)(virQEMUDriverPtr driver,
144 145
                                          virDomainObjPtr vm);

146 147 148
typedef struct _qemuDomainObjPrivate qemuDomainObjPrivate;
typedef qemuDomainObjPrivate *qemuDomainObjPrivatePtr;
struct _qemuDomainObjPrivate {
149
    struct qemuDomainJobObj job;
150 151

    qemuMonitorPtr mon;
152
    virDomainChrSourceDefPtr monConfig;
E
Eric Blake 已提交
153
    bool monJSON;
154 155
    bool monError;
    unsigned long long monStart;
D
Daniel P. Berrange 已提交
156 157 158 159 160

    qemuAgentPtr agent;
    bool agentError;
    unsigned long long agentStart;

161
    bool gotShutdown;
162
    bool beingDestroyed;
163
    char *pidfile;
164 165 166 167

    int nvcpupids;
    int *vcpupids;

168
    virDomainPCIAddressSetPtr pciaddrs;
J
Ján Tomko 已提交
169
    virDomainCCWAddressSetPtr ccwaddrs;
170
    virDomainVirtioSerialAddrSetPtr vioserialaddrs;
171
    int persistentAddrs;
172

173
    virQEMUCapsPtr qemuCaps;
174
    char *lockState;
175 176

    bool fakeReboot;
177 178

    int jobs_queued;
179 180

    unsigned long migMaxBandwidth;
J
Jiri Denemark 已提交
181
    char *origname;
182
    int nbdPort; /* Port used for migration with NBD */
183
    unsigned short migrationPort;
184
    int preMigrationState;
185

186
    virChrdevsPtr devs;
187 188 189 190

    qemuDomainCleanupCallback *cleanupCallbacks;
    size_t ncleanupCallbacks;
    size_t ncleanupCallbacks_max;
191 192

    virCgroupPtr cgroup;
193 194 195

    virCond unplugFinished; /* signals that unpluggingDevice was unplugged */
    const char *unpluggingDevice; /* alias of the device that is being unplugged */
196
    char **qemuDevices; /* NULL-terminated list of devices aliases known to QEMU */
197 198

    bool hookRun;  /* true if there was a hook run over this domain */
199 200

    /* Bitmaps below hold data from the auto NUMA feature */
201
    virBitmapPtr autoNodeset;
202
    virBitmapPtr autoCpuset;
203 204 205

    bool signalIOError; /* true if the domain condition should be signalled on
                           I/O error */
206 207
};

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
# define QEMU_DOMAIN_DISK_PRIVATE(disk)	\
    ((qemuDomainDiskPrivatePtr) (disk)->privateData)

typedef struct _qemuDomainDiskPrivate qemuDomainDiskPrivate;
typedef qemuDomainDiskPrivate *qemuDomainDiskPrivatePtr;
struct _qemuDomainDiskPrivate {
    virObject parent;

    /* ideally we want a smarter way to interlock block jobs on single qemu disk
     * in the future, but for now we just disallow any concurrent job on a
     * single disk */
    bool blockjob;

    /* for some synchronous block jobs, we need to notify the owner */
    int blockJobType;   /* type of the block job from the event */
    int blockJobStatus; /* status of the finished block job */
    bool blockJobSync; /* the block job needs synchronized termination */
225 226

    bool migrating; /* the disk is being migrated */
227 228
};

C
Chen Fan 已提交
229 230
typedef enum {
    QEMU_PROCESS_EVENT_WATCHDOG = 0,
231
    QEMU_PROCESS_EVENT_GUESTPANIC,
232
    QEMU_PROCESS_EVENT_DEVICE_DELETED,
233
    QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED,
234
    QEMU_PROCESS_EVENT_SERIAL_CHANGED,
235
    QEMU_PROCESS_EVENT_BLOCK_JOB,
C
Chen Fan 已提交
236 237 238 239 240

    QEMU_PROCESS_EVENT_LAST
} qemuProcessEventType;

struct qemuProcessEvent {
241
    virDomainObjPtr vm;
C
Chen Fan 已提交
242
    qemuProcessEventType eventType;
243
    int action;
244
    int status;
245
    void *data;
246 247
};

248
const char *qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
249
                                            int phase);
250
int qemuDomainAsyncJobPhaseFromString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
251 252
                                      const char *phase);

253
void qemuDomainEventFlush(int timer, void *opaque);
254

255
void qemuDomainEventQueue(virQEMUDriverPtr driver,
256
                          virObjectEventPtr event);
257

258
int qemuDomainObjBeginJob(virQEMUDriverPtr driver,
259
                          virDomainObjPtr obj,
260
                          qemuDomainJob job)
261
    ATTRIBUTE_RETURN_CHECK;
262
int qemuDomainObjBeginAsyncJob(virQEMUDriverPtr driver,
263
                               virDomainObjPtr obj,
264
                               qemuDomainAsyncJob asyncJob)
265 266
    ATTRIBUTE_RETURN_CHECK;

267 268 269 270
void qemuDomainObjEndJob(virQEMUDriverPtr driver,
                         virDomainObjPtr obj);
void qemuDomainObjEndAsyncJob(virQEMUDriverPtr driver,
                              virDomainObjPtr obj);
271
void qemuDomainObjAbortAsyncJob(virDomainObjPtr obj);
272
void qemuDomainObjSetJobPhase(virQEMUDriverPtr driver,
J
Jiri Denemark 已提交
273 274
                              virDomainObjPtr obj,
                              int phase);
275 276
void qemuDomainObjSetAsyncJobMask(virDomainObjPtr obj,
                                  unsigned long long allowedJobs);
277 278
void qemuDomainObjRestoreJob(virDomainObjPtr obj,
                             struct qemuDomainJobObj *job);
279
void qemuDomainObjDiscardAsyncJob(virQEMUDriverPtr driver,
280
                                  virDomainObjPtr obj);
281
void qemuDomainObjReleaseAsyncJob(virDomainObjPtr obj);
282

283 284
qemuMonitorPtr qemuDomainGetMonitor(virDomainObjPtr vm)
    ATTRIBUTE_NONNULL(1);
285
void qemuDomainObjEnterMonitor(virQEMUDriverPtr driver,
286 287
                               virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
288 289
int qemuDomainObjExitMonitor(virQEMUDriverPtr driver,
                             virDomainObjPtr obj)
290 291
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
    ATTRIBUTE_RETURN_CHECK;
292
int qemuDomainObjEnterMonitorAsync(virQEMUDriverPtr driver,
293
                                   virDomainObjPtr obj,
294
                                   qemuDomainAsyncJob asyncJob)
295
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
D
Daniel P. Berrange 已提交
296 297


298 299 300 301
void qemuDomainObjEnterAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
D
Daniel P. Berrange 已提交
302 303


304 305 306 307
void qemuDomainObjEnterRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
308

309 310 311 312
virDomainDefPtr qemuDomainDefCopy(virQEMUDriverPtr driver,
                                  virDomainDefPtr src,
                                  unsigned int flags);

313
int qemuDomainDefFormatBuf(virQEMUDriverPtr driver,
314 315 316 317
                           virDomainDefPtr vm,
                           unsigned int flags,
                           virBuffer *buf);

318
char *qemuDomainDefFormatXML(virQEMUDriverPtr driver,
319
                             virDomainDefPtr vm,
320
                             unsigned int flags);
321

322
char *qemuDomainFormatXML(virQEMUDriverPtr driver,
323
                          virDomainObjPtr vm,
324
                          unsigned int flags);
325

326
char *qemuDomainDefFormatLive(virQEMUDriverPtr driver,
327
                              virDomainDefPtr def,
328 329
                              bool inactive,
                              bool compatible);
330

331
void qemuDomainObjTaint(virQEMUDriverPtr driver,
332
                        virDomainObjPtr obj,
333
                        virDomainTaintFlags taint,
334
                        int logFD);
335

336
void qemuDomainObjCheckTaint(virQEMUDriverPtr driver,
337 338
                             virDomainObjPtr obj,
                             int logFD);
339
void qemuDomainObjCheckDiskTaint(virQEMUDriverPtr driver,
340
                                 virDomainObjPtr obj,
341 342
                                 virDomainDiskDefPtr disk,
                                 int logFD);
343 344 345 346
void qemuDomainObjCheckHostdevTaint(virQEMUDriverPtr driver,
                                    virDomainObjPtr obj,
                                    virDomainHostdevDefPtr disk,
                                    int logFD);
347
void qemuDomainObjCheckNetTaint(virQEMUDriverPtr driver,
348
                                virDomainObjPtr obj,
349 350
                                virDomainNetDefPtr net,
                                int logFD);
351

352

353 354 355
int qemuDomainCreateLog(virQEMUDriverPtr driver, virDomainObjPtr vm, bool append);
int qemuDomainOpenLog(virQEMUDriverPtr driver, virDomainObjPtr vm, off_t pos);
int qemuDomainAppendLog(virQEMUDriverPtr driver,
356 357 358
                        virDomainObjPtr vm,
                        int logFD,
                        const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(4, 5);
359

360
const char *qemuFindQemuImgBinary(virQEMUDriverPtr driver);
361 362 363 364 365

int qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
                                    virDomainSnapshotObjPtr snapshot,
                                    char *snapshotDir);

366
int qemuDomainSnapshotForEachQcow2(virQEMUDriverPtr driver,
367 368 369 370 371
                                   virDomainObjPtr vm,
                                   virDomainSnapshotObjPtr snap,
                                   const char *op,
                                   bool try_all);

372
int qemuDomainSnapshotDiscard(virQEMUDriverPtr driver,
373 374 375 376 377
                              virDomainObjPtr vm,
                              virDomainSnapshotObjPtr snap,
                              bool update_current,
                              bool metadata_only);

378 379 380
typedef struct _virQEMUSnapRemove virQEMUSnapRemove;
typedef virQEMUSnapRemove *virQEMUSnapRemovePtr;
struct _virQEMUSnapRemove {
381
    virQEMUDriverPtr driver;
382 383 384 385 386 387 388 389 390 391
    virDomainObjPtr vm;
    int err;
    bool metadata_only;
    bool current;
};

void qemuDomainSnapshotDiscardAll(void *payload,
                                  const void *name,
                                  void *data);

392
int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver,
393 394
                                         virDomainObjPtr vm);

395
void qemuDomainRemoveInactive(virQEMUDriverPtr driver,
396 397
                              virDomainObjPtr vm);

398
void qemuDomainSetFakeReboot(virQEMUDriverPtr driver,
399 400 401
                             virDomainObjPtr vm,
                             bool value);

402
bool qemuDomainJobAllowed(qemuDomainObjPrivatePtr priv,
403
                          qemuDomainJob job);
M
Michal Privoznik 已提交
404

405
int qemuDomainCheckDiskPresence(virQEMUDriverPtr driver,
M
Michal Privoznik 已提交
406 407
                                virDomainObjPtr vm,
                                bool start_with_state);
408

409
int qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
410
                                 virDomainObjPtr vm,
411
                                 virDomainDiskDefPtr disk,
412 413
                                 bool force_probe,
                                 bool report_broken);
414

415 416 417 418
bool qemuDomainDiskSourceDiffers(virConnectPtr conn,
                                 virDomainDiskDefPtr disk,
                                 virDomainDiskDefPtr origDisk);

419 420 421
bool qemuDomainDiskChangeSupported(virDomainDiskDefPtr disk,
                                   virDomainDiskDefPtr orig_disk);

422 423 424
int qemuDomainStorageFileInit(virQEMUDriverPtr driver,
                              virDomainObjPtr vm,
                              virStorageSourcePtr src);
425
char *qemuDomainStorageAlias(const char *device, int depth);
426

427 428 429 430
int qemuDomainCleanupAdd(virDomainObjPtr vm,
                         qemuDomainCleanupCallback cb);
void qemuDomainCleanupRemove(virDomainObjPtr vm,
                             qemuDomainCleanupCallback cb);
431
void qemuDomainCleanupRun(virQEMUDriverPtr driver,
432 433
                          virDomainObjPtr vm);

434 435
extern virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks;
extern virDomainXMLNamespace virQEMUDriverDomainXMLNamespace;
436
extern virDomainDefParserConfig virQEMUDriverDomainDefParserConfig;
437

438
int qemuDomainUpdateDeviceList(virQEMUDriverPtr driver,
439
                               virDomainObjPtr vm, int asyncJob);
440

441 442 443 444
int qemuDomainUpdateMemoryDeviceInfo(virQEMUDriverPtr driver,
                                     virDomainObjPtr vm,
                                     int asyncJob);

445 446 447
bool qemuDomainDefCheckABIStability(virQEMUDriverPtr driver,
                                    virDomainDefPtr src,
                                    virDomainDefPtr dst);
448

449
bool qemuDomainAgentAvailable(virDomainObjPtr vm,
450 451
                              bool reportError);

J
Jiri Denemark 已提交
452 453
int qemuDomainJobInfoUpdateTime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
454 455
int qemuDomainJobInfoUpdateDowntime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
J
Jiri Denemark 已提交
456 457 458 459 460 461 462 463 464 465
int qemuDomainJobInfoToInfo(qemuDomainJobInfoPtr jobInfo,
                            virDomainJobInfoPtr info)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int qemuDomainJobInfoToParams(qemuDomainJobInfoPtr jobInfo,
                              int *type,
                              virTypedParameterPtr *params,
                              int *nparams)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);

466 467
int qemuDomainSupportsBlockJobs(virDomainObjPtr vm, bool *modern)
    ATTRIBUTE_NONNULL(1);
468
bool qemuDomainDiskBlockJobIsActive(virDomainDiskDefPtr disk);
469 470
bool qemuDomainHasBlockjob(virDomainObjPtr vm, bool copy_only)
    ATTRIBUTE_NONNULL(1);
471

472
int qemuDomainAlignMemorySizes(virDomainDefPtr def);
473 474
void qemuDomainMemoryDeviceAlignSize(virDomainDefPtr def,
                                     virDomainMemoryDefPtr mem);
475

476 477
virDomainChrSourceDefPtr qemuFindAgentConfig(virDomainDefPtr def);

478 479
bool qemuDomainMachineIsQ35(const virDomainDef *def);
bool qemuDomainMachineIsI440FX(const virDomainDef *def);
480
bool qemuDomainMachineNeedsFDC(const virDomainDef *def);
481
bool qemuDomainMachineIsS390CCW(const virDomainDef *def);
482
bool qemuDomainMachineHasBuiltinIDE(const virDomainDef *def);
483

484 485 486
int qemuDomainUpdateCurrentMemorySize(virQEMUDriverPtr driver,
                                      virDomainObjPtr vm);

487
unsigned long long qemuDomainGetMlockLimitBytes(virDomainDefPtr def);
488
bool qemuDomainRequiresMlock(virDomainDefPtr def);
489

490 491 492 493
int qemuDomainDefValidateMemoryHotplug(const virDomainDef *def,
                                       virQEMUCapsPtr qemuCaps,
                                       const virDomainMemoryDef *mem);

494
#endif /* __QEMU_DOMAIN_H__ */