qemu_domain.h 16.7 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

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

42 43 44
# 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.  */
45
#  define QEMU_DOMAIN_MIG_BANDWIDTH_MAX ULONG_MAX
46
# else
47
#  define QEMU_DOMAIN_MIG_BANDWIDTH_MAX (INT64_MAX / (1024 * 1024))
48
# endif
49

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

56 57 58 59 60
/* 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))

61 62 63
/* Only 1 job is allowed at any time
 * A job includes *all* monitor commands, even those just querying
 * information, not merely actions */
64
typedef enum {
65
    QEMU_JOB_NONE = 0,  /* Always set to 0 for easy if (jobActive) conditions */
66 67 68 69
    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 */
70
    QEMU_JOB_ABORT,         /* Abort current async job */
71
    QEMU_JOB_MIGRATION_OP,  /* Operation influencing outgoing migration */
72

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

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

/* 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.
 */
85
typedef enum {
86 87 88 89 90
    QEMU_ASYNC_JOB_NONE = 0,
    QEMU_ASYNC_JOB_MIGRATION_OUT,
    QEMU_ASYNC_JOB_MIGRATION_IN,
    QEMU_ASYNC_JOB_SAVE,
    QEMU_ASYNC_JOB_DUMP,
91
    QEMU_ASYNC_JOB_SNAPSHOT,
92 93

    QEMU_ASYNC_JOB_LAST
94
} qemuDomainAsyncJob;
95
VIR_ENUM_DECL(qemuDomainAsyncJob)
96

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

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

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

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

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

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

    qemuAgentPtr agent;
    bool agentError;
    unsigned long long agentStart;

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

    int nvcpupids;
    int *vcpupids;

165 166 167
    int niothreadpids;
    int *iothreadpids;

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
};

C
Chen Fan 已提交
205 206
typedef enum {
    QEMU_PROCESS_EVENT_WATCHDOG = 0,
207
    QEMU_PROCESS_EVENT_GUESTPANIC,
208
    QEMU_PROCESS_EVENT_DEVICE_DELETED,
209
    QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED,
210
    QEMU_PROCESS_EVENT_SERIAL_CHANGED,
211
    QEMU_PROCESS_EVENT_BLOCK_JOB,
C
Chen Fan 已提交
212 213 214 215 216

    QEMU_PROCESS_EVENT_LAST
} qemuProcessEventType;

struct qemuProcessEvent {
217
    virDomainObjPtr vm;
C
Chen Fan 已提交
218
    qemuProcessEventType eventType;
219
    int action;
220
    int status;
221
    void *data;
222 223
};

224
const char *qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
225
                                            int phase);
226
int qemuDomainAsyncJobPhaseFromString(qemuDomainAsyncJob job,
J
Jiri Denemark 已提交
227 228
                                      const char *phase);

229
void qemuDomainEventFlush(int timer, void *opaque);
230

231
void qemuDomainEventQueue(virQEMUDriverPtr driver,
232
                          virObjectEventPtr event);
233

234
int qemuDomainObjBeginJob(virQEMUDriverPtr driver,
235
                          virDomainObjPtr obj,
236
                          qemuDomainJob job)
237
    ATTRIBUTE_RETURN_CHECK;
238
int qemuDomainObjBeginAsyncJob(virQEMUDriverPtr driver,
239
                               virDomainObjPtr obj,
240
                               qemuDomainAsyncJob asyncJob)
241 242
    ATTRIBUTE_RETURN_CHECK;

243 244 245 246
void qemuDomainObjEndJob(virQEMUDriverPtr driver,
                         virDomainObjPtr obj);
void qemuDomainObjEndAsyncJob(virQEMUDriverPtr driver,
                              virDomainObjPtr obj);
247
void qemuDomainObjAbortAsyncJob(virDomainObjPtr obj);
248
void qemuDomainObjSetJobPhase(virQEMUDriverPtr driver,
J
Jiri Denemark 已提交
249 250
                              virDomainObjPtr obj,
                              int phase);
251 252
void qemuDomainObjSetAsyncJobMask(virDomainObjPtr obj,
                                  unsigned long long allowedJobs);
253 254
void qemuDomainObjRestoreJob(virDomainObjPtr obj,
                             struct qemuDomainJobObj *job);
255
void qemuDomainObjDiscardAsyncJob(virQEMUDriverPtr driver,
256
                                  virDomainObjPtr obj);
257
void qemuDomainObjReleaseAsyncJob(virDomainObjPtr obj);
258

259 260
qemuMonitorPtr qemuDomainGetMonitor(virDomainObjPtr vm)
    ATTRIBUTE_NONNULL(1);
261
void qemuDomainObjEnterMonitor(virQEMUDriverPtr driver,
262 263
                               virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
264 265
int qemuDomainObjExitMonitor(virQEMUDriverPtr driver,
                             virDomainObjPtr obj)
266 267
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
    ATTRIBUTE_RETURN_CHECK;
268
int qemuDomainObjEnterMonitorAsync(virQEMUDriverPtr driver,
269
                                   virDomainObjPtr obj,
270
                                   qemuDomainAsyncJob asyncJob)
271
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
D
Daniel P. Berrange 已提交
272 273


274 275 276 277
void qemuDomainObjEnterAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitAgent(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
D
Daniel P. Berrange 已提交
278 279


280 281 282 283
void qemuDomainObjEnterRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
void qemuDomainObjExitRemote(virDomainObjPtr obj)
    ATTRIBUTE_NONNULL(1);
284

285 286 287 288
virDomainDefPtr qemuDomainDefCopy(virQEMUDriverPtr driver,
                                  virDomainDefPtr src,
                                  unsigned int flags);

289
int qemuDomainDefFormatBuf(virQEMUDriverPtr driver,
290 291 292 293
                           virDomainDefPtr vm,
                           unsigned int flags,
                           virBuffer *buf);

294
char *qemuDomainDefFormatXML(virQEMUDriverPtr driver,
295
                             virDomainDefPtr vm,
296
                             unsigned int flags);
297

298
char *qemuDomainFormatXML(virQEMUDriverPtr driver,
299
                          virDomainObjPtr vm,
300
                          unsigned int flags);
301

302
char *qemuDomainDefFormatLive(virQEMUDriverPtr driver,
303
                              virDomainDefPtr def,
304 305
                              bool inactive,
                              bool compatible);
306

307
void qemuDomainObjTaint(virQEMUDriverPtr driver,
308
                        virDomainObjPtr obj,
309
                        virDomainTaintFlags taint,
310
                        int logFD);
311

312
void qemuDomainObjCheckTaint(virQEMUDriverPtr driver,
313 314
                             virDomainObjPtr obj,
                             int logFD);
315
void qemuDomainObjCheckDiskTaint(virQEMUDriverPtr driver,
316
                                 virDomainObjPtr obj,
317 318
                                 virDomainDiskDefPtr disk,
                                 int logFD);
319 320 321 322
void qemuDomainObjCheckHostdevTaint(virQEMUDriverPtr driver,
                                    virDomainObjPtr obj,
                                    virDomainHostdevDefPtr disk,
                                    int logFD);
323
void qemuDomainObjCheckNetTaint(virQEMUDriverPtr driver,
324
                                virDomainObjPtr obj,
325 326
                                virDomainNetDefPtr net,
                                int logFD);
327

328

329 330 331
int qemuDomainCreateLog(virQEMUDriverPtr driver, virDomainObjPtr vm, bool append);
int qemuDomainOpenLog(virQEMUDriverPtr driver, virDomainObjPtr vm, off_t pos);
int qemuDomainAppendLog(virQEMUDriverPtr driver,
332 333 334
                        virDomainObjPtr vm,
                        int logFD,
                        const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(4, 5);
335

336
const char *qemuFindQemuImgBinary(virQEMUDriverPtr driver);
337 338 339 340 341

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

342
int qemuDomainSnapshotForEachQcow2(virQEMUDriverPtr driver,
343 344 345 346 347
                                   virDomainObjPtr vm,
                                   virDomainSnapshotObjPtr snap,
                                   const char *op,
                                   bool try_all);

348
int qemuDomainSnapshotDiscard(virQEMUDriverPtr driver,
349 350 351 352 353
                              virDomainObjPtr vm,
                              virDomainSnapshotObjPtr snap,
                              bool update_current,
                              bool metadata_only);

354 355 356
typedef struct _virQEMUSnapRemove virQEMUSnapRemove;
typedef virQEMUSnapRemove *virQEMUSnapRemovePtr;
struct _virQEMUSnapRemove {
357
    virQEMUDriverPtr driver;
358 359 360 361 362 363 364 365 366 367
    virDomainObjPtr vm;
    int err;
    bool metadata_only;
    bool current;
};

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

368
int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver,
369 370
                                         virDomainObjPtr vm);

371
void qemuDomainRemoveInactive(virQEMUDriverPtr driver,
372 373
                              virDomainObjPtr vm);

374
void qemuDomainSetFakeReboot(virQEMUDriverPtr driver,
375 376 377
                             virDomainObjPtr vm,
                             bool value);

378
bool qemuDomainJobAllowed(qemuDomainObjPrivatePtr priv,
379
                          qemuDomainJob job);
M
Michal Privoznik 已提交
380

381
int qemuDomainCheckDiskPresence(virQEMUDriverPtr driver,
M
Michal Privoznik 已提交
382 383
                                virDomainObjPtr vm,
                                bool start_with_state);
384

385
int qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
386
                                 virDomainObjPtr vm,
387
                                 virDomainDiskDefPtr disk,
388 389
                                 bool force_probe,
                                 bool report_broken);
390

391 392 393
int qemuDomainStorageFileInit(virQEMUDriverPtr driver,
                              virDomainObjPtr vm,
                              virStorageSourcePtr src);
394
char *qemuDomainStorageAlias(const char *device, int depth);
395

396 397 398 399
int qemuDomainCleanupAdd(virDomainObjPtr vm,
                         qemuDomainCleanupCallback cb);
void qemuDomainCleanupRemove(virDomainObjPtr vm,
                             qemuDomainCleanupCallback cb);
400
void qemuDomainCleanupRun(virQEMUDriverPtr driver,
401 402
                          virDomainObjPtr vm);

403 404
extern virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks;
extern virDomainXMLNamespace virQEMUDriverDomainXMLNamespace;
405
extern virDomainDefParserConfig virQEMUDriverDomainDefParserConfig;
406

407
int qemuDomainUpdateDeviceList(virQEMUDriverPtr driver,
408
                               virDomainObjPtr vm, int asyncJob);
409

410 411 412 413
int qemuDomainUpdateMemoryDeviceInfo(virQEMUDriverPtr driver,
                                     virDomainObjPtr vm,
                                     int asyncJob);

414 415 416
bool qemuDomainDefCheckABIStability(virQEMUDriverPtr driver,
                                    virDomainDefPtr src,
                                    virDomainDefPtr dst);
417

418
bool qemuDomainAgentAvailable(virDomainObjPtr vm,
419 420
                              bool reportError);

J
Jiri Denemark 已提交
421 422
int qemuDomainJobInfoUpdateTime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
423 424
int qemuDomainJobInfoUpdateDowntime(qemuDomainJobInfoPtr jobInfo)
    ATTRIBUTE_NONNULL(1);
J
Jiri Denemark 已提交
425 426 427 428 429 430 431 432 433 434
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);

435 436
int qemuDomainSupportsBlockJobs(virDomainObjPtr vm, bool *modern)
    ATTRIBUTE_NONNULL(1);
437 438
bool qemuDomainDiskBlockJobIsActive(virDomainDiskDefPtr disk);

439
int qemuDomainAlignMemorySizes(virDomainDefPtr def);
440
void qemuDomainMemoryDeviceAlignSize(virDomainMemoryDefPtr mem);
441

442 443
virDomainChrSourceDefPtr qemuFindAgentConfig(virDomainDefPtr def);

444
#endif /* __QEMU_DOMAIN_H__ */