qemu_domain.h 17.8 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 94

    QEMU_ASYNC_JOB_LAST
95
} qemuDomainAsyncJob;
96
VIR_ENUM_DECL(qemuDomainAsyncJob)
97

J
Jiri Denemark 已提交
98 99 100 101 102
typedef struct _qemuDomainJobInfo qemuDomainJobInfo;
typedef qemuDomainJobInfo *qemuDomainJobInfoPtr;
struct _qemuDomainJobInfo {
    virDomainJobType type;
    unsigned long long started; /* When the async job started */
103
    unsigned long long stopped; /* When the domain's CPUs were stopped */
104 105 106 107
    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 已提交
108 109 110
    /* Computed values */
    unsigned long long timeElapsed;
    unsigned long long timeRemaining;
J
Jiri Denemark 已提交
111
    long long timeDelta; /* delta = received - sent, i.e., the difference
112 113 114 115 116
                            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 已提交
117 118 119 120
    /* Raw values from QEMU */
    qemuMonitorMigrationStatus status;
};

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

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

141
typedef void (*qemuDomainCleanupCallback)(virQEMUDriverPtr driver,
142 143
                                          virDomainObjPtr vm);

144 145 146
typedef struct _qemuDomainObjPrivate qemuDomainObjPrivate;
typedef qemuDomainObjPrivate *qemuDomainObjPrivatePtr;
struct _qemuDomainObjPrivate {
147
    struct qemuDomainJobObj job;
148 149

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

    qemuAgentPtr agent;
    bool agentError;
    unsigned long long agentStart;

159
    bool gotShutdown;
160
    bool beingDestroyed;
161
    char *pidfile;
162 163 164 165

    int nvcpupids;
    int *vcpupids;

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

171
    virQEMUCapsPtr qemuCaps;
172
    char *lockState;
173 174

    bool fakeReboot;
175 176

    int jobs_queued;
177 178

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

184
    virChrdevsPtr devs;
185 186 187 188

    qemuDomainCleanupCallback *cleanupCallbacks;
    size_t ncleanupCallbacks;
    size_t ncleanupCallbacks_max;
189 190

    virCgroupPtr cgroup;
191 192 193

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

    bool hookRun;  /* true if there was a hook run over this domain */
197 198

    /* Bitmaps below hold data from the auto NUMA feature */
199
    virBitmapPtr autoNodeset;
200
    virBitmapPtr autoCpuset;
201 202
};

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
# 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 */
    virCond blockJobSyncCond;
    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 */
221 222

    bool migrating; /* the disk is being migrated */
223 224
};

C
Chen Fan 已提交
225 226
typedef enum {
    QEMU_PROCESS_EVENT_WATCHDOG = 0,
227
    QEMU_PROCESS_EVENT_GUESTPANIC,
228
    QEMU_PROCESS_EVENT_DEVICE_DELETED,
229
    QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED,
230
    QEMU_PROCESS_EVENT_SERIAL_CHANGED,
231
    QEMU_PROCESS_EVENT_BLOCK_JOB,
C
Chen Fan 已提交
232 233 234 235 236

    QEMU_PROCESS_EVENT_LAST
} qemuProcessEventType;

struct qemuProcessEvent {
237
    virDomainObjPtr vm;
C
Chen Fan 已提交
238
    qemuProcessEventType eventType;
239
    int action;
240
    int status;
241
    void *data;
242 243
};

244
const char *qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
245
                                            int phase);
246
int qemuDomainAsyncJobPhaseFromString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
247 248
                                      const char *phase);

249
void qemuDomainEventFlush(int timer, void *opaque);
250

251
void qemuDomainEventQueue(virQEMUDriverPtr driver,
252
                          virObjectEventPtr event);
253

254
int qemuDomainObjBeginJob(virQEMUDriverPtr driver,
255
                          virDomainObjPtr obj,
256
                          qemuDomainJob job)
257
    ATTRIBUTE_RETURN_CHECK;
258
int qemuDomainObjBeginAsyncJob(virQEMUDriverPtr driver,
259
                               virDomainObjPtr obj,
260
                               qemuDomainAsyncJob asyncJob)
261 262
    ATTRIBUTE_RETURN_CHECK;

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

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


294 295 296 297
void qemuDomainObjEnterAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
D
Daniel P. Berrange 已提交
298 299


300 301 302 303
void qemuDomainObjEnterRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
304

305 306 307 308
virDomainDefPtr qemuDomainDefCopy(virQEMUDriverPtr driver,
                                  virDomainDefPtr src,
                                  unsigned int flags);

309
int qemuDomainDefFormatBuf(virQEMUDriverPtr driver,
310 311 312 313
                           virDomainDefPtr vm,
                           unsigned int flags,
                           virBuffer *buf);

314
char *qemuDomainDefFormatXML(virQEMUDriverPtr driver,
315
                             virDomainDefPtr vm,
316
                             unsigned int flags);
317

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

322
char *qemuDomainDefFormatLive(virQEMUDriverPtr driver,
323
                              virDomainDefPtr def,
324 325
                              bool inactive,
                              bool compatible);
326

327
void qemuDomainObjTaint(virQEMUDriverPtr driver,
328
                        virDomainObjPtr obj,
329
                        virDomainTaintFlags taint,
330
                        int logFD);
331

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

348

349 350 351
int qemuDomainCreateLog(virQEMUDriverPtr driver, virDomainObjPtr vm, bool append);
int qemuDomainOpenLog(virQEMUDriverPtr driver, virDomainObjPtr vm, off_t pos);
int qemuDomainAppendLog(virQEMUDriverPtr driver,
352 353 354
                        virDomainObjPtr vm,
                        int logFD,
                        const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(4, 5);
355

356
const char *qemuFindQemuImgBinary(virQEMUDriverPtr driver);
357 358 359 360 361

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

362
int qemuDomainSnapshotForEachQcow2(virQEMUDriverPtr driver,
363 364 365 366 367
                                   virDomainObjPtr vm,
                                   virDomainSnapshotObjPtr snap,
                                   const char *op,
                                   bool try_all);

368
int qemuDomainSnapshotDiscard(virQEMUDriverPtr driver,
369 370 371 372 373
                              virDomainObjPtr vm,
                              virDomainSnapshotObjPtr snap,
                              bool update_current,
                              bool metadata_only);

374 375 376
typedef struct _virQEMUSnapRemove virQEMUSnapRemove;
typedef virQEMUSnapRemove *virQEMUSnapRemovePtr;
struct _virQEMUSnapRemove {
377
    virQEMUDriverPtr driver;
378 379 380 381 382 383 384 385 386 387
    virDomainObjPtr vm;
    int err;
    bool metadata_only;
    bool current;
};

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

388
int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver,
389 390
                                         virDomainObjPtr vm);

391
void qemuDomainRemoveInactive(virQEMUDriverPtr driver,
392 393
                              virDomainObjPtr vm);

394
void qemuDomainSetFakeReboot(virQEMUDriverPtr driver,
395 396 397
                             virDomainObjPtr vm,
                             bool value);

398
bool qemuDomainJobAllowed(qemuDomainObjPrivatePtr priv,
399
                          qemuDomainJob job);
M
Michal Privoznik 已提交
400

401
int qemuDomainCheckDiskPresence(virQEMUDriverPtr driver,
M
Michal Privoznik 已提交
402 403
                                virDomainObjPtr vm,
                                bool start_with_state);
404

405
int qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
406
                                 virDomainObjPtr vm,
407
                                 virDomainDiskDefPtr disk,
408 409
                                 bool force_probe,
                                 bool report_broken);
410

411 412 413
int qemuDomainStorageFileInit(virQEMUDriverPtr driver,
                              virDomainObjPtr vm,
                              virStorageSourcePtr src);
414
char *qemuDomainStorageAlias(const char *device, int depth);
415

416 417 418 419
int qemuDomainCleanupAdd(virDomainObjPtr vm,
                         qemuDomainCleanupCallback cb);
void qemuDomainCleanupRemove(virDomainObjPtr vm,
                             qemuDomainCleanupCallback cb);
420
void qemuDomainCleanupRun(virQEMUDriverPtr driver,
421 422
                          virDomainObjPtr vm);

423 424
extern virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks;
extern virDomainXMLNamespace virQEMUDriverDomainXMLNamespace;
425
extern virDomainDefParserConfig virQEMUDriverDomainDefParserConfig;
426

427
int qemuDomainUpdateDeviceList(virQEMUDriverPtr driver,
428
                               virDomainObjPtr vm, int asyncJob);
429

430 431 432 433
int qemuDomainUpdateMemoryDeviceInfo(virQEMUDriverPtr driver,
                                     virDomainObjPtr vm,
                                     int asyncJob);

434 435 436
bool qemuDomainDefCheckABIStability(virQEMUDriverPtr driver,
                                    virDomainDefPtr src,
                                    virDomainDefPtr dst);
437

438
bool qemuDomainAgentAvailable(virDomainObjPtr vm,
439 440
                              bool reportError);

J
Jiri Denemark 已提交
441 442
int qemuDomainJobInfoUpdateTime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
443 444
int qemuDomainJobInfoUpdateDowntime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
J
Jiri Denemark 已提交
445 446 447 448 449 450 451 452 453 454
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);

455 456
int qemuDomainSupportsBlockJobs(virDomainObjPtr vm, bool *modern)
    ATTRIBUTE_NONNULL(1);
457
bool qemuDomainDiskBlockJobIsActive(virDomainDiskDefPtr disk);
458 459
bool qemuDomainHasBlockjob(virDomainObjPtr vm, bool copy_only)
    ATTRIBUTE_NONNULL(1);
460

461
int qemuDomainAlignMemorySizes(virDomainDefPtr def);
462
void qemuDomainMemoryDeviceAlignSize(virDomainMemoryDefPtr mem);
463

464 465
virDomainChrSourceDefPtr qemuFindAgentConfig(virDomainDefPtr def);

466 467 468
bool qemuDomainMachineIsQ35(const virDomainDef *def);
bool qemuDomainMachineIsI440FX(const virDomainDef *def);

469 470 471
int qemuDomainUpdateCurrentMemorySize(virQEMUDriverPtr driver,
                                      virDomainObjPtr vm);

472
#endif /* __QEMU_DOMAIN_H__ */